Jump to content

  • Log In with Google      Sign In   
  • Create Account


opengl texture problem

  • You cannot reply to this topic
13 replies to this topic

#1 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 06:37 AM

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.


Sponsor:

#2 Sponji   Members   -  Reputation: 1137

Like
0Likes
Like

Posted 29 May 2014 - 06:57 AM

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

#3 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 07:24 AM

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, 29 May 2014 - 07:25 AM.


#4 swiftcoder   Senior Moderators   -  Reputation: 9584

Like
0Likes
Like

Posted 29 May 2014 - 08:21 AM

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 - Software Engineer @Amazon - [swiftcoding]


#5 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 08:24 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);

Edited by thejelmega, 29 May 2014 - 08:28 AM.


#6 swiftcoder   Senior Moderators   -  Reputation: 9584

Like
0Likes
Like

Posted 29 May 2014 - 08:52 AM

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 - Software Engineer @Amazon - [swiftcoding]


#7 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 09:03 AM

well, I've added this to my code

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

 

but it still doesn't show my texture



#8 Sponji   Members   -  Reputation: 1137

Like
0Likes
Like

Posted 29 May 2014 - 09:54 AM

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


Derp

#9 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 10:03 AM

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



#10 Sponji   Members   -  Reputation: 1137

Like
0Likes
Like

Posted 29 May 2014 - 10:44 AM

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, 29 May 2014 - 10:48 AM.

Derp

#11 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 11:02 AM

thank you, that is it

also, does anyone know how to do my texture coords seperate from my cube, because the sides are just lines that correspond to a pixel on the side


Edited by thejelmega, 29 May 2014 - 11:05 AM.


#12 swiftcoder   Senior Moderators   -  Reputation: 9584

Like
0Likes
Like

Posted 29 May 2014 - 12:21 PM


also, does anyone know how to do my texture coords seperate from my cube, because the sides are just lines that correspond to a pixel on the side

You will need to do separate sets of texture coordinates for each face (and hence, separate vertices).

 

In general, it is very hard to draw a textured cube using less than 24 unique vertices, because each corner vertex will need to be a member of three distinct faces, each with its own UV coordinates.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]


#13 thejelmega   Members   -  Reputation: 122

Like
0Likes
Like

Posted 29 May 2014 - 01:15 PM

Oh, so you can't give the vertices to draw using elements and the texture coords separately.

and If I use separate vertices, isn't it going to be laggy? 


Edited by thejelmega, 29 May 2014 - 02:04 PM.


#14 swiftcoder   Senior Moderators   -  Reputation: 9584

Like
0Likes
Like

Posted 29 May 2014 - 02:04 PM

No, need to have matching arrays of each.


Tristam MacDonald - Software Engineer @Amazon - [swiftcoding]






PARTNERS