Bubba-3D  0.9.0
Awesome game engine!
CubeMapTexture.cpp
1 /*
2  * This file is part of Bubba-3D.
3  *
4  * Bubba-3D is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser 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  *
9  * Bubba-3D is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with Bubba-3D. If not, see http://www.gnu.org/licenses/.
16  */
17 #include "CubeMapTexture.h"
18 #include "glutil/glutil.h"
19 #include <Texture.h>
20 #include <stb_image.h>
21 #include <Logger.h>
22 
23 
24 CubeMapTexture::CubeMapTexture(const std::string &posXFilename, const std::string &negXFilename,
25  const std::string &posYFilename, const std::string &negYFilename,
26  const std::string &posZFilename, const std::string &negZFilename) {
27 
28  //************************************************
29  // Creating a texture ID for the OpenGL texture
30  //************************************************
31  GLuint textureID;
32  glGenTextures(1, &textureID);
33 
34  //************************************************
35  // Load the faces into the cube map texture
36  //************************************************
37  glBindTexture(GL_TEXTURE_CUBE_MAP, textureID);
38 
39  loadCubeMapFace(posXFilename, GL_TEXTURE_CUBE_MAP_POSITIVE_X);
40  loadCubeMapFace(negXFilename, GL_TEXTURE_CUBE_MAP_NEGATIVE_X);
41  loadCubeMapFace(posYFilename, GL_TEXTURE_CUBE_MAP_POSITIVE_Y);
42  loadCubeMapFace(negYFilename, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y);
43  loadCubeMapFace(posZFilename, GL_TEXTURE_CUBE_MAP_POSITIVE_Z);
44  loadCubeMapFace(negZFilename, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z);
45 
46  //************************************************
47  // Set filtering parameters
48  //************************************************
49 
50  glGenerateMipmap(GL_TEXTURE_CUBE_MAP);
51 
52  // Sets the type of mipmap interpolation to be used on magnifying and
53  // minifying the active texture.
54  // For cube maps, filtering across faces causes artifacts - so disable filtering
55  //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56  //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
57 
58  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
59  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
60  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
61  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
62  glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_REPEAT);
63 
64  // In case you want filtering anyway, try this below instead
65  //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
66  //glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Use tri-linear mip map filtering
67  //glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_ANISOTROPY_EXT, 16); // or replace trilinear mipmap filtering with nicest anisotropic filtering
68 
69 
70  CHECK_GL_ERROR();
71  m_texture = textureID;
72 
73 }
74 
75 CubeMapTexture::~CubeMapTexture()
76 {
77 }
78 
79 void CubeMapTexture::loadCubeMapFace(std::string filename, GLenum face)
80 {
81  // TODO(Bubbad) LET TEXTURE HANDLE ALL THIS
82  stbi_set_flip_vertically_on_load(true);
83 
84  int width;
85  int height;
86  int comp;
87  unsigned char* image = stbi_load(filename.c_str(), &width, &height, &comp, 0);
88 
89  if (image == nullptr) {
90  Logger::logError("Couldnt load image "+ filename);
91  }
92 
93  GLenum format = comp == 3 ? GL_RGB : GL_RGBA;
94 
95  glTexImage2D(face, 0, GL_SRGB_ALPHA_EXT, width, height, 0, format, GL_UNSIGNED_BYTE, image);
96 
97  stbi_image_free(image);
98 }
99 
100 void CubeMapTexture::bind(GLenum textureUnit) {
101  glActiveTexture(textureUnit);
102 // glEnable(GL_TEXTURE_2D);
103  glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture);
104 }