opengl texture problem

Started by
12 comments, last by swiftcoder 9 years, 10 months ago

Hello everybody,

I have an annoying problem while trying to make my cube have a texture,

my vertex shader just gives the texture coords to the fragment shader

Here is the code

Texture Loading Code:


int width, height;
 
FIBITMAP* bitmap = FreeImage_Load(FreeImage_GetFileType("assets/textures/basic_tile.png", 0), "assets/textures/basic_tile.png");
FIBITMAP *img = FreeImage_ConvertTo32Bits(bitmap);
width = FreeImage_GetWidth(img);
height = FreeImage_GetHeight(img);
 
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 
width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(img));
 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
glGenerateMipmap(GL_TEXTURE_2D);
 
FreeImage_Unload(bitmap);
FreeImage_Unload(img);
 
Fragment Shader:

 
in vec2 TexCoord;
 
out vec4 fragColor;
 
uniform sampler2D tex;
 
void main()
{
fragColor = texture(tex, TexCoord);
}
 
P.S. If I need to give some more code, just ask.
Advertisement

Is the problem that the texture isn't showing at all? Your code seems ok to me. How are you passing the uniform? Have you used debugger(CodeXL/gDebugger) to check if the texture is actually shown right?

Derp

well, I'm following some tutorial like open.gl and I just searched how to use freeimage, but I don't really know about how the textures is passed through.

like you probably have noticed from the line above, I'm relatively new to opengl.

The texture is not showing.

Your setup looks ok, but can you show us the code used to bind the set uniform values, bind the shader and draw?

Most likely issue off the top of my head is that you haven't set the uniform for the sampler texture unit.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

well, I haven't set up any uniform for the texture unit, so that is my problem.

can you tell me how.

here is my setup code:

aspect = (GLfloat)screen_width / (GLfloat)screen_height;
 
glClearColor(0.1f, 0.1f, 0.2f, 1.0f);
 
glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);
 
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
 
float vertices[] = {
//X      Y      Z     R     G     B    UV X  UV Y
-0.5f,  0.5f, -0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f,  0.5f, -0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f,
 
-0.5f,  0.5f,  0.5f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
0.5f,  0.5f,  0.5f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f,  0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
-0.5f, -0.5f,  0.5f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f
};
 
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
 
GLuint elements[] = {
3, 2, 1,
1, 0, 3,
 
7, 6, 5,
5, 4, 7,
 
4, 0, 3,
3, 7, 4,
 
5, 1, 2,
2, 6, 5,
 
3, 2, 6,
6, 7, 3,
 
0, 1, 5, 
5, 4, 0
};
 
glGenBuffers(1, &ebo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);
 
vertexShader = new Shader("defaultVert.glsl", GL_VERTEX_SHADER);
fragmentShader = new Shader("defaultFrag.glsl", GL_FRAGMENT_SHADER);
 
shaderProg = new ShaderProgram();
shaderProg->attachShader(vertexShader);
shaderProg->attachShader(fragmentShader);
 
shaderProg->bindFragDataLocation(0, "fragColor");
 
shaderProg->link();
shaderProg->Use();
 
positionLoc = shaderProg->getAttribLoc("vPosition");
glEnableVertexAttribArray(positionLoc);
glVertexAttribPointer(positionLoc, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), 0);
 
colorLoc = shaderProg->getAttribLoc("color");
glEnableVertexAttribArray(colorLoc);
glVertexAttribPointer(colorLoc, 3, GL_FLOAT, GL_FALSE, 8*sizeof(float), (void*)(3*sizeof(float)));
 
texLoc = shaderProg->getAttribLoc("texCoord");
glEnableVertexAttribArray(texLoc);
glVertexAttribPointer(texLoc, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
 
int width, height;
 
FIBITMAP* bitmap = FreeImage_Load(FreeImage_GetFileType("assets/textures/basic_tile.png", 0), "assets/textures/basic_tile.png");
FIBITMAP *img = FreeImage_ConvertTo32Bits(bitmap);
width = FreeImage_GetWidth(img);
height = FreeImage_GetHeight(img);
 
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
 
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 
width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, (void*)FreeImage_GetBits(img));
 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
 
glGenerateMipmap(GL_TEXTURE_2D);
 
FreeImage_Unload(bitmap);
FreeImage_Unload(img);

You'll need something equivalent to glUniform1i(glGetUniformLocation("tex"), 0).

In effect, you need to tell the shader which texture unit is associated with the sampler 'tex', and the default texture unit is GL_TEXTURE0, so...

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

well, I've added this to my code

texLoc = shaderProg->GetUniformLoc("tex");
glUniform1i(texLoc, 0);

but it still doesn't show my texture

Can you make a zip of the whole thing? Would be much easier to find the problem that way.

Derp

well, here is my code in a zip, the camera class and inputmanager isn't really used

link: https://www.dropbox.com/s/d1yf5hwtjbj6v3z/TJGS%20Engine.zip

You're requesting the texture coordinate attribute with wrong name:


texLoc = shaderProg->getAttribLoc("texCoord");

It should be same as in the vertex shader, you have "texcoord" there. Also seems that the FreeImage loads the image as BGR instead of RGB, so the color channels are swapped.

Derp

This topic is closed to new replies.

Advertisement