Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get texture coordinate to GLSL in version 150?

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?

like image 861
g.Eis Avatar asked May 17 '12 06:05

g.Eis


People also ask

What is GLSL version?

GLSL is a C-style language. The language has undergone a number of version changes, and it shares the deprecation model of OpenGL. The current version of GLSL is 4.60.

What is UV in GLSL?

UV coordinates are also called Texture coordinates. Texture coordinates can be any 2d coordinate and are usually created/defined in your modelling software, or in the code you use to generate your 3d models. For each point (vertex) in the model there are between 0 and 2 sets of texture coordinates.

What is texture2D GLSL?

The texture2D function returns a texel, i.e. the (color) value of the texture for the given coordinates. The function has one input parameter of the type sampler2D and one input parameter of the type vec2 : sampler, the uniform the texture is bound to, and coord, the 2-dimensional coordinates of the texel to look up.

What is sampler2D?

A sampler2D is used to do lookup in a standard texture image; a samplerCube is used to do lookup in a cubemap texture (Subsection 5.3. 4). The value of a sampler variable is a reference to a texture unit. The value tells which texture unit is invoked when the sampler variable is used to do texture lookup.


2 Answers

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

like image 175
Tim Avatar answered Nov 20 '22 18:11

Tim


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 );
like image 26
ChatCloud Avatar answered Nov 20 '22 17:11

ChatCloud