I want to do a simple CG task: draw a skymap cube with textures on the 6 sides. Read already a lot of tutorials, but there is some problem with the textures. All 6 sides of the cubes are completely black (but if I replace in the fragment shader the texture(..) call with a simple vec4(1.0f, ...), the walls will be colored, so it must be something with the textures).
So, here's an excerpt of what I'm doing:
void SkyCube::init() {
mShader = new Shader("./Shader/skybox.vs", "./Shader/skybox.fs");
// from: https://www.opengl.org/wiki/Common_Mistakes#Creating_a_Cubemap_Texture
glEnable(GL_TEXTURE_CUBE_MAP);
glGenTextures(1, &mTextureId);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureId);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAX_LEVEL, 0);
// now load the textures from TGA files and make them available to OpenGL
TgaLoader * tgaLoader = new TgaLoader();
string texturefilenames[] = {"back", "front", "bottom", "top", "left", "right"};
int texturedefs[] = {GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
GL_TEXTURE_CUBE_MAP_POSITIVE_Y, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z};
for (int i = 0; i < 6; i++) {
tgaLoader->loadTga(mTextures[i], "data/skycube/" + texturefilenames[i] + ".tga");
glTexImage2D(texturedefs[i], 0, GL_RGB, mTextures[i].width, mTextures[i].height,
0, GL_BGR, GL_UNSIGNED_BYTE, mTextures[i].imageData);
}
delete tgaLoader;
mVboSkyCube = this->generateSkyCubeVBO(vec3(-1.0f, -1.0f, -1.0f), vec3(1.0f, 1.0f, 1.0f));
glDisable(GL_TEXTURE_CUBE_MAP);
}
The generateSkyCubeVBO method basically generates all the vertices and texture normalized coords:
VertexBufferObjectAttribs* SkyCube::generateSkyCubeVBO(const vec3 &mi, const vec3 &ma) {
vector<vec3> vertices;
vector<vec3> normals;
vector<vec2> textureUV;
// tofo liste mit texture koords
// 00 01
// 10 11
// 2d vec
vertices.push_back(vec3(mi.x, ma.y, mi.z));
vertices.push_back(vec3(mi.x, ma.y, ma.z));
vertices.push_back(vec3(ma.x, ma.y, ma.z));
vertices.push_back(vec3(ma.x, ma.y, mi.z));
normals.push_back(vec3(0.0f, 1.0f, 0.0f));
normals.push_back(vec3(0.0f, 1.0f, 0.0f));
normals.push_back(vec3(0.0f, 1.0f, 0.0f));
normals.push_back(vec3(0.0f, 1.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 1.0f));
textureUV.push_back(vec2(1.0f, 0.0f));
textureUV.push_back(vec2(1.0f, 1.0f));
vertices.push_back(vec3(mi.x, mi.y, mi.z));
vertices.push_back(vec3(ma.x, mi.y, mi.z));
vertices.push_back(vec3(ma.x, mi.y, ma.z));
vertices.push_back(vec3(mi.x, mi.y, ma.z));
normals.push_back(vec3(0.0f, -1.0f, 0.0f));
normals.push_back(vec3(0.0f, -1.0f, 0.0f));
normals.push_back(vec3(0.0f, -1.0f, 0.0f));
normals.push_back(vec3(0.0f, -1.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 1.0f));
textureUV.push_back(vec2(1.0f, 0.0f));
textureUV.push_back(vec2(1.0f, 1.0f));
vertices.push_back(vec3(mi.x, mi.y, mi.z));
vertices.push_back(vec3(mi.x, ma.y, mi.z));
vertices.push_back(vec3(ma.x, ma.y, mi.z));
vertices.push_back(vec3(ma.x, mi.y, mi.z));
normals.push_back(vec3(0.0f, 0.0f, -1.0f));
normals.push_back(vec3(0.0f, 0.0f, -1.0f));
normals.push_back(vec3(0.0f, 0.0f, -1.0f));
normals.push_back(vec3(0.0f, 0.0f, -1.0f));
textureUV.push_back(vec2(0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 1.0f));
textureUV.push_back(vec2(1.0f, 0.0f));
textureUV.push_back(vec2(1.0f, 1.0f));
vertices.push_back(vec3(mi.x, mi.y, ma.z));
vertices.push_back(vec3(ma.x, mi.y, ma.z));
vertices.push_back(vec3(ma.x, ma.y, ma.z));
vertices.push_back(vec3(mi.x, ma.y, ma.z));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
normals.push_back(vec3(0.0f, 0.0f, 1.0f));
textureUV.push_back(vec2(0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 1.0f));
textureUV.push_back(vec2(1.0f, 0.0f));
textureUV.push_back(vec2(1.0f, 1.0f));
vertices.push_back(vec3(mi.x, mi.y, mi.z));
vertices.push_back(vec3(mi.x, mi.y, ma.z));
vertices.push_back(vec3(mi.x, ma.y, ma.z));
vertices.push_back(vec3(mi.x, ma.y, mi.z));
normals.push_back(vec3(-1.0f, 0.0f, 0.0f));
normals.push_back(vec3(-1.0f, 0.0f, 0.0f));
normals.push_back(vec3(-1.0f, 0.0f, 0.0f));
normals.push_back(vec3(-1.0f, 0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 1.0f));
textureUV.push_back(vec2(1.0f, 0.0f));
textureUV.push_back(vec2(1.0f, 1.0f));
vertices.push_back(vec3(ma.x, mi.y, mi.z));
vertices.push_back(vec3(ma.x, ma.y, mi.z));
vertices.push_back(vec3(ma.x, ma.y, ma.z));
vertices.push_back(vec3(ma.x, mi.y, ma.z));
normals.push_back(vec3(1.0f, 0.0f, 0.0f));
normals.push_back(vec3(1.0f, 0.0f, 0.0f));
normals.push_back(vec3(1.0f, 0.0f, 0.0f));
normals.push_back(vec3(1.0f, 0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 0.0f));
textureUV.push_back(vec2(0.0f, 1.0f));
textureUV.push_back(vec2(1.0f, 0.0f));
textureUV.push_back(vec2(1.0f, 1.0f));
GLuint nrVertices = vertices.size();
VertexBufferObjectAttribs::DATA *attrData = new VertexBufferObjectAttribs::DATA[nrVertices];
for (GLuint i = 0; i < nrVertices; ++i) {
vec3 v = vertices[i];
vec3 n = normals[i];
vec2 t = textureUV[i];
attrData[i].vx = v.x;
attrData[i].vy = v.y;
attrData[i].vz = v.z;
attrData[i].vw = 1.0f;
attrData[i].nx = n.x;
attrData[i].ny = n.y;
attrData[i].nz = n.z;
attrData[i].nw = 0.0f;
attrData[i].tx = t.x;
attrData[i].ty = t.y;
attrData[i].tz = 0.0f;
attrData[i].tw = 0.0f;
}
VertexBufferObjectAttribs *vbo = new VertexBufferObjectAttribs();
vbo->setData(attrData, GL_STATIC_DRAW, nrVertices, GL_QUADS);
vbo->addAttrib(VERTEX_POSITION);
vbo->addAttrib(VERTEX_NORMAL);
vbo->addAttrib(VERTEX_COLOR);
vbo->addAttrib(VERTEX_TEXTURE);
vbo->bindAttribs();
delete[] attrData;
return vbo;
}
That works fine, all textures are successfully loaded. The vertex shader:
// OpenGL 3.3
#version 130
// following is probably not really needed
#define VERT_POSITION 0
#define VERT_NORMAL 1
#define VERT_COLOR 2
#define VERT_TEXTURE 3
uniform mat4x4 matModel;
uniform mat4x4 matView;
uniform mat4x4 matProjection;
attribute vec4 Position;
attribute vec4 Normal;
attribute vec4 Color;
attribute vec4 Texture;
out vec4 VertPosition;
out vec4 VertNormal;
out vec4 VertColor;
out vec4 VertTexture;
void main()
{
VertPosition = Position;
VertNormal = Normal;
VertColor = Color;
VertTexture = Texture;
gl_Position = matProjection * matView * matModel * vec4(Position.xyz, 1);
}
The fragment shader:
// OpenGL 3.3
#version 130
in vec4 VertPosition;
in vec4 VertNormal;
in vec4 VertColor;
in vec4 VertTexture; // Interpolated values from the vertex shaders, similar to the tutorial :-)
uniform vec3 lightPos;
uniform sampler2D skyBoxTextureSampler; // to know which texture to access, from the tutorial :-)
void main()
{
vec4 color = VertColor;
gl_FragColor = texture( skyBoxTextureSampler, VertTexture.xy); //vec4( 1.0f, 1.0f, 0.9f, 1.0f); //
}
The render method:
void SkyCube::render(const Transform& trans) {
mat4 projection = trans.projection;
mat4 view = trans.view;
mat4 model = mat4::identitiy();
glPushAttrib(GL_ALL_ATTRIB_BITS);
glPushClientAttrib(GL_CLIENT_ALL_ATTRIB_BITS);
glEnable(GL_TEXTURE_CUBE_MAP);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_CUBE_MAP, mTextureId);
mShader->bind();
mShader->setMatrix("matProjection", projection, GL_TRUE);
mShader->setMatrix("matView", view, GL_TRUE);
// mShader->setMatrix("Texture")
model = mat4::scale(20.0f, 20.0f, 20.0f);
mShader->setMatrix("matModel", model, GL_TRUE);
// set the texture
mShader->seti("skyBoxTextureSampler", 0);
mVboSkyCube->render();
mShader->release();
glPopClientAttrib();
glPopAttrib();
}
The render method of the VBO object looks like this:
void VertexBufferObjectAttribs::render() {
GLint size = m_attribLocations.size();
glBindBuffer(GL_ARRAY_BUFFER, m_bufferId);
for (GLint i = 0; i < size; ++i) {
GLint attribLoc = m_attribLocations[i];
glVertexAttribPointer(attribLoc, 4, GL_FLOAT, GL_FALSE, sizeof (DATA), ((GLchar*) NULL + 4 * sizeof (GLfloat) * i));
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
for (GLint i = 0; i < size; ++i)
glEnableVertexAttribArray(m_attribLocations[i]);
if (m_useIndexBuffer) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_indexBufferId);
if (m_dynamicRendering) {
glDrawElements(m_primitiveMode, m_nrDynamicIndices, GL_UNSIGNED_INT, 0);
} else {
glDrawElements(m_primitiveMode, m_nrIndices, GL_UNSIGNED_INT, 0);
}
} else {
if (m_dynamicRendering) {
glDrawArrays(m_primitiveMode, 0, m_nrDynamicVertices);
} else {
glDrawArrays(m_primitiveMode, 0, m_nrVertices);
}
}
for (GLint i = 0; i < size; ++i)
glDisableVertexAttribArray(m_attribLocations[i]);
}
If I compare this code with the steps from the tutorials, I cannot see anything missing or being in the wrong order.
Note that I cannot use a newer GLSL version as my current Linux version only supports 1.3.
sampler2D
, you usesamplerCube
and 3D texture coordinates representing the direction to lookup. I am not sure what you are currently trying to do with 2D coordinates? – Andon M. Coleman#version 130 in vec4 VertPosition; in vec4 VertNormal; in vec4 VertColor; in vec4 VertTexture; // Interpolated values from the vertex shaders, similar to the tutorial :-) uniform vec3 lightPos; uniform samplerCube skyBoxTextureSampler; // to know which texture to access, from the tutorial :-) void main() { vec4 color = VertColor; gl_FragColor = texture( skyBoxTextureSampler, VertTexture.xyz); // looks like the texture itself is not loaded, because this also yields black vec2( 0.8f, 0.2f));// //vec4( 1.0f, 1.0f, 0.9f, 1.0f); // }
– pedjjj