8
votes

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?

2

2 Answers

6
votes

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

2
votes

in your case just look into gl_TexCoord[0] in your vertex shader. Definitely works with shader model 1.5

in C++:

glActiveTexture(GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, id);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

glMatrixMode(GL_TEXTURE);
glLoadIdentity();

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();

in vertex shader:

gl_TexCoord[0]=gl_MultiTexCoord0; 

in fragment shader:

vec4 c = texture2D(texSampler, gl_TexCoord[0].st );