Code_TYMPAN  4.4.0
Industrial site acoustic simulation
OGLTexture2D.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) <2012-2024> <EDF-DTG> <FRANCE>
3  * This file is part of Code_TYMPAN (R).
4  * Code_TYMPAN (R) is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * Code_TYMPAN (R) is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  * You should have received a copy of the GNU General Public License along
13  * with Code_TYMPAN (R). If not, see <https://www.gnu.org/licenses/>.
14  */
15 
16 /*
17  *
18  */
19 
20 #include "OImage.h"
21 #include "OGLTexture2D.h"
22 #include "TYImageManager.h"
23 #include <cassert>
24 
26 
28 
29 bool OGLTexture2D::load(const char* filename)
30 {
31  if (id > 0)
32  {
33  free();
34  }
35 
36  genTexture();
37 
38  // Bind texture and activate bilinear filtering
39  glBindTexture(GL_TEXTURE_2D, id);
40  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
41  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
42  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
43  glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
44  glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
45 
46  // Get image
47  OImage* im = TYImageManager::get()->getImage(filename);
48 
49  if (!im)
50  {
51  return false;
52  }
53 
54  // Get image informations
55  GLsizei w = im->getSizeX();
56  GLsizei h = im->getSizeY();
57  GLenum format = 0;
58 
59  unsigned int bpp = im->getDepth() / 8;
60  switch (bpp)
61  {
62  case 1:
63  format = GL_LUMINANCE;
64  break;
65  case 2:
66  format = GL_LUMINANCE_ALPHA;
67  break;
68  case 3:
69  format = GL_RGB;
70  break;
71  case 4:
72  format = GL_RGBA;
73  break;
74  default:
75  return false;
76  }
77 
78  // Upload texture
79  glTexImage2D(GL_TEXTURE_2D, 0, bpp, w, h, 0, format, GL_UNSIGNED_BYTE, im->getData());
80 
81  // Unbind texture
82  glBindTexture(GL_TEXTURE_2D, 0);
83 
84  return true;
85 }
virtual ~OGLTexture2D()
bool load(const char *filename)
void genTexture()
Definition: OGLTexture.cpp:46
virtual void free()
Definition: OGLTexture.cpp:37
Definition: OImage.h:34
unsigned char * getData() const
Definition: OImage.h:45
unsigned int getDepth() const
Definition: OImage.h:69
unsigned int getSizeX() const
Definition: OImage.h:53
unsigned int getSizeY() const
Definition: OImage.h:61
static LPTYImageManager get()