Hello,
I was trying to set up a very simple vertex and fragment shader.
Vertex Shader:
in vec3 position;
in vec2 tex_coord;
out vec2 pass_tex_coord;
void main(void) {
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1);
pass_tex_coord = tex_coord;
}
Fragment Shader:
uniform sampler2D texture;
in vec2 pass_tex_coord;
void main(void) {
gl_FragColor = texture2D(texture, pass_tex_coord);
}
As simple as it could get i would say.
But when i try to draw a triangle, all vertices get the same texture coordinate.
I cant figure out why.
(By the way: Would it be better to declare "position" as a vec4 instead of a vec3?)
Thanks in advance!
Have you already take a look on The Quads (http://www.lwjgl.org/wiki/index.php?title=The_Quad_with_Projection,_View_and_Model_matrices)? There is a example for using texture with fragment shaders and this may help you with.
Also, You can try to set position to:
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1, 1);
If you are sure that you are passing in different texture coordinates for each vertex, then I would add the varying keyword before your out/in definition for pass_tex_coord: http://www.lighthouse3d.com/tutorials/glsl-tutorial/varying-variables/
varying out vec2 pass_tex_coord;
varying in vec2 pass_tex_coord;
I got the fragment shader working by using:
gl_FragColor = texture2D(texture, gl_TexCoord[0].st);
However, when I use the varying keyword I get the following error:
Fragment shader failed to compile with the following errors:
ERROR: 0:5: error(#138) "varying in" supported in geometry shaders only
ERROR: error(#273) 1 compilation errors. No code generated
I wrote it like this:
(Vertex shader)
in vec3 position;
in vec2 tex_coord;
varying out vec2 pass_tex_coord;
void main(void) {
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1);
pass_tex_coord = tex_coord;
}
(fragment shader)
uniform sampler2D texture;
varying in vec2 pass_tex_coord;
void main(void) {
gl_FragColor = texture2D(texture, pass_tex_coord);
}
Yes, sorry, you need to remove the "in" and "out" from the varying variables. If you got it working some other way, then you can just ignore my comments. :P
Still nothing. All tex coords are set to 0.
The "solution" i am using right now is not very "nice". I am not using an own vertex shader but simply the default vertex shader and gl_multitexcoord0.
I was playing around with this some more and I found out, that the tex_coords I pass to the shader are always (0,0) no matter what.
this is the way I am drawing the VBO's:
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, data_vbo_id);
GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, index_vbo_id);
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
GL11.glVertexPointer(3, GL11.GL_FLOAT, vertex_stride, vertex_pointer);
GL11.glTexCoordPointer(2, GL11.GL_FLOAT, tex_coord_stride, tex_coord_pointer);
GL11.glDrawElements(GL11.GL_TRIANGLES, number_of_indices, GL11.GL_UNSIGNED_BYTE, first_index_id);
Am I doing anything wrong with the drawing maybe?
make sure your pointer's are rewinded!