How to get texture coordinate to GLSL in version 150

glslshadertextures

In GLSL version 110 i can get coordinate in gl_TexCoord[] but it's deprecated in 150.

OpenGL code:

shader.setupShaderFromFile(GL_VERTEX_SHADER, "t.vert");
shader.setupShaderFromFile(GL_FRAGMENT_SHADER, "t.frag");
shader.linkProgram();

glGetFloatv(GL_MODELVIEW_MATRIX, modelview_mat_);
glGetFloatv(GL_PROJECTION_MATRIX, projectionview_mat_);

shader.begin();

glUniformMatrix4fv( glGetUniformLocation(shader.getProgram(), "MVMatrix"), 1, GL_FALSE, modelview_mat_);
glUniformMatrix4fv( glGetUniformLocation(shader.getProgram(), "MPMatrix"), 1, GL_FALSE, projectionview_mat_);

glBegin(GL_QUADS);
    glTexCoord2f(0, 0);glVertex2f(0, 0);
    glTexCoord2f(1, 0);glVertex2f(100, 0);
    glTexCoord2f(1, 1);glVertex2f(100, 100);
    glTexCoord2f(0, 1);glVertex2f(0, 100);
glEnd();

shader.end();

Shader Code:

VERTEX-

#version 150
in vec3 in_vertex;
in vec2 in_coord;
uniform mat4 MVMatrix;
uniform mat4 MPMatrix;

out vec2 tex_coord;

void main()
{
    vec4 v = vec4( in_vertex, 1.0 );

    tex_coord = in_coord;
    gl_Position = MPMatrix * MVMatrix * v;
}

FRAGMENT:

#version 150

in vec2 tex_coord;

out vec4 frgcolor;

void main()
{
    frgcolor = vec4( tex_coord, 0.0, 1.0);
}

final screen is a red quad.

How to pass texture coordinate to shader? or my code is wrong?

Best Answer

If you don't want to use the deprecated methods, you'll have to stop using glTexCoord2f, and use a custom attribute.

If you want to stay with immediate mode, you'll use glVertexAttrib2f.

You pair this up with your new variable 'in_coord' by quering the index after shader linking:

int program = glCreateProgram();
//create shader as normal
int texcoord_index = glGetAttribLocation(program, "in_coord");

glBegin(GL_QUADS);
glVertexAttrib2f(texcoord_index, 0, 0); ...

If you want to use VA/VBO instead, the functions you use instead is glEnableVertexAttribArray/glVertexAttribPointer

Related Topic