LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Cornix on June 29, 2013, 09:25:35

Title: Need help with fragment shader
Post by: Cornix on June 29, 2013, 09:25:35
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!
Title: Re: Need help with fragment shader
Post by: afonsolage on July 04, 2013, 13:50:42
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);
Title: Re: Need help with fragment shader
Post by: Fool Running on July 08, 2013, 14:00:44
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;
Title: Re: Need help with fragment shader
Post by: Cornix on July 09, 2013, 17:05:46
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);
}

Title: Re: Need help with fragment shader
Post by: Fool Running on July 10, 2013, 12:50:24
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
Title: Re: Need help with fragment shader
Post by: Cornix on July 10, 2013, 19:03:19
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.
Title: Re: Need help with fragment shader
Post by: Cornix on July 14, 2013, 14:24:33
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?
Title: Re: Need help with fragment shader
Post by: laserlars on July 24, 2013, 12:13:34
make sure your pointer's are rewinded!