Jump to content
  • Advertisement
Sign in to follow this  
thejelmega

OpenGL opengl texture problem

This topic is 1907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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.

Edited by thejelmega

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

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);
Edited by thejelmega

Share this post


Link to post
Share on other sites

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...

Share this post


Link to post
Share on other sites

well, I've added this to my code

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

 

but it still doesn't show my texture

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

Edited by Sponji

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!