0

I'm writing an application using OpenGL 4.3 and GLSL and I need the shader to do basic UV mapping. The problem is that GLSL compiler seems to be optimising-out the UV coordinates. I cannot access them from the application side of things.

Vertex shader:

#version 330 core

uniform mat4 projection;

layout (location = 0) in vec4 position;
layout (location = 1) in vec2 uvCoord;

out vec2 texCoord;

void main(void)
{
    texCoord = uvCoord;
    gl_Position = position;
}

Vertex shader:

#version 330 core

in vec2 texCoord;

out vec4 color;

uniform sampler2D tex;

void main(void)
{
    color = texture2D(tex, texCoord);
}

Both the vertex and fragment shader compile and link without errors, but when I call the attributes using the following code:

GLint effectPositionLocation = glGetAttribLocation(effect->getEffect(), "position");
GLint effectUVLocation = glGetAttribLocation(effect->getEffect(), "uvCoord");

I get the 0 for the position and -1 for the uvCoord, so I can only assume that the uvCoord has been optimised out even though I am using it to pass it from the vertex shader to the fragment shader.

The result is that the geometry is displayed but only in black, no texture mapping.

I have Written similar applications in Direct3D and HLSL with no problem of attributes being optimised out. I'm thinking that it is something simple that I am forgetting or not doing but have not found out what.

Kromster
  • 7,181
  • 7
  • 63
  • 111
Dave
  • 9
  • 2
  • Why do you have two vertex shaders? Post a [MCVE](http://stackoverflow.com/help/mcve). – genpfault Mar 16 '15 at 13:52
  • @genpfault Looks like a typo; the second shader looks like a fragment shader. – Colonel Thirty Two Mar 16 '15 at 14:03
  • the source looks right, and the variable should not have been optimized out. Are you shure getEffect() gives you the correct program object? – Arne Mar 16 '15 at 14:07
  • 2
    And you're completely sure that the fragment shader compiles? It shouldn't. `texture2D()` is not a built-in function in the core profile. It's just `texture()`. – Reto Koradi Mar 16 '15 at 14:11

2 Answers2

0

Replace the 'texture2D' with 'texture', and your attribute will be used.

Bad GLSL compiler: it should not compile your shader since texture2D is not available in core profile.

Luca
  • 11,646
  • 11
  • 70
  • 125
-1

EDIT: You may have forgotten to call glEnableVertexAttribArray(1); after setting your glVertexAttribPointers.

Dimo Markov
  • 422
  • 2
  • 9