Sampling from a texture in OpenGL is black
11,229
The line where the the texture is bound is:
glBindTexture(GL_TEXTURE, texture_id);
that should be:
glBindTexture(GL_TEXTURE_2D, texture_id);
Author by
nate
Updated on June 04, 2022Comments
-
nate almost 2 years
I'm trying out my hand at graphics, following a tutorial at http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_06
Problem: When I try to texture my cube, my sample is black.
Screenshot: http://puu.sh/2JP1H.jpg (note: I set blue = uv.x to test my UVs)
I looked at the threads OpenGL textures appear just black, and Texture is all black, but it seemed they had different problems.
First I load my texture using SOIL image loading library:
int w, h; unsigned char* img = SOIL_load_image("resources/images/4x4window.jpg",&w,&h,0,0); ErrorIf(!img, "%s", SOIL_last_result()); glGenTextures(1, &texture_id); glBindTexture(GL_TEXTURE, texture_id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,w,h,0,GL_RGB,GL_UNSIGNED_BYTE,img);
My render function where I pass my texture to the shader program:
void onDisplay() { glClearColor(1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glUseProgram(program); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture_id); glUniform1i(uniform_myTexture, /*GL_TEXTURE*/0); glEnableVertexAttribArray(attribute_coord3d); // open shader var glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_verts); // put stuff in buffer glVertexAttribPointer(attribute_coord3d, 3, GL_FLOAT, GL_FALSE, 0, 0); //send glEnableVertexAttribArray(attribute_texcoord); glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_texcoords); glVertexAttribPointer(attribute_texcoord, 2, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_cube_elements); int size; glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size); glDrawElements(GL_TRIANGLES, size / sizeof(GLushort), GL_UNSIGNED_SHORT, 0); glDisableVertexAttribArray(attribute_coord3d); glDisableVertexAttribArray(attribute_texcoord); glutSwapBuffers(); }
Vertex Shader:
attribute vec3 coord3d; attribute vec2 texcoord; varying vec2 f_texcoord; uniform mat4 mvp; void main(void) { gl_Position = mvp * vec4(coord3d, 1.0); f_texcoord = texcoord; }
Fragment Shader:
varying vec2 f_texcoord; uniform sampler2D mytexture; void main(void) { vec4 result = texture2D(mytexture, f_texcoord); result.z = f_texcoord.x; result.w = 1; gl_FragColor = result; }
-
nate about 11 yearsThanks, that worked! I should have diffed with the example. Follow up problem: 256x256 texture works fine puu.sh/2JRBa.jpg, but non power of 2 texture is wonky puu.sh/2JRDH.jpg.
-
nate about 11 yearsHmm... one of my pngs works fine (the box), but another one is weird: puu.sh/2JRRv.jpg
-
nate about 11 yearsOh I fixed that too! puu.sh/2JSw3.jpg The problem was that my box texture has no transparency, so when I loaded it, the format only used 3 channels (R, G, and B). My other texture has transparency, so when I loaded it, the format uses 4 channels (R, G, B, and A). So I just force 4 channels now.