AnsweredAssumed Answered

OpenGL Shader vertex attributes problem

Question asked by michal_k on Apr 22, 2012
Latest reply on Nov 13, 2012 by michal_k

I have a strange problem with vertex attributes in shader at OpenGL 4.2 (the same happens in 3.3). I have written simple program to learn about OpenGL shaders and VAO, it is quite simple because I draw only a couple of rectangles. My problem start with even two rectangles, so I will explain it at first using only this two rectangles. I have one rectangle that have color and texture and one that have only color, for each of this rectangles is separate shader. For rectangle with only color, shader takes vertex attributes for position and it's color, shader for rectangle that have texture take position, color and texture coordinates. Each of this rectangles have its own VAO that uses vertex buffer and index buffer. I know that drawing simple rectangle don't need index buffer, but it's test program. Here start my problem, when I draw only one of this rectangles every thing is fine, but when both of them is draw something is wrong. When I draw first rectangle witch color and then with texture, second rectangle (this with texture) don't have texture. When I draw first rectangle with texture it have texture but second rectangle doesn't draw at all. What is strange in this is that it happens from second swap buffers operation. I made a test and render everything only once, then everything is fine. Normally I rendering it in loop like for a game or some animation. I have found that when rectangle that should have texture don't have it, it's shader just gets texture coordinates that is (I think) default, that means (0, 0), and it looks like from some reason this attribute stops working.


I attached some files with source code to this question, to better explain what I doing:

rectColor.vs, rectColor.fs - vertex and fragment shader for rectangle with only color

rectTexutre.vs, rectTexture.fs - vertex and fragment shader for rectangle witch texture and color

VertexBuffer.cpp - here is how I made VAO for my rectangles, basically I made VAO, bind it, create buffer for vertex data and for indices, setup pointers and enable vertex attrib arrays. I don't know maybe here I made some mistakes. To draw this rectangles I use glDrawElements and off course before calling this function i call glUseProgram with program I want to use and glBindVertexArray.

As I see I can't attach more files but this should be enough to show how i doing VAO and what is in shaders. Important thing to mention here is that i testing it under windows 7 x64 and I created OpenGL context using windows api. First I creating dummy context then I using glew to get extensions, next i create real context core profile 4.2 and before making current 4.2 context I delete dummy context. But this should be ok since other things works fine.


That not the end of strange things that I have found. To explain other things I show how I set vertex attributes, I use layout(location=x) in shader files.


For rectangle with texture shader it looks like this:

layout(location=0) in vec4 in_Position;

layout(location=1) in vec4 in_Color;

layout(location=2) in vec4 in_TexCoord;


Rectangle with color only shader have this arguments:

layout(location=0) in vec4 in_Position;

layout(location=1) in vec4 in_Color;


And when it is used like above  it doesn't working. But when i set vertex attributes of shader for rectangle with only color to:

layout(location=1) in vec4 in_Position;

layout(location=2) in vec4 in_Color;

and of course call  glEnableVertexAttribArray with arguments 1 and 2 my test example start working as it should, rectangle have texture and second one is draw. This is really strange for me, I don't know why it works like that? It's normal behavior? I though that when one VAO have enabled 0,1,2 vertex attributes other VAO can have enabled 0,1 or only 0 attribute. I have tested this behavior using glBindAttribLocation and deleting layout(location=x) from shaders, it worked the same way, no changes.


And thats not the end I made another test, and I added triangle. But this time I don't used index array and I drawing this triangle with call to glDrawArrays. This triangle have separate shader with vertex attributes at locations 0, 1 (vertex position and color) not 1,2. Triangle is draw with my rectangles. This triangle draws like it should without any problems, and it draws with three other rectangles one with color only and two with textures, rectangles are draw fine, but this with color have attributes at locations 1,2.


I don't know if how it works is correct, and if I doing it right, so if anyone have some idea or explanation please for help.


I don't know if this is important but my hardware is Radeon HD 5670, driver is Catalyst 12.3. I have also made a test on my netbook that have AMD C-30 APU with Radeon 6250 (driver is also Catalyst 12.3), results are the same as at HD 5670.