Hello Guest

Need help with fragment shader

  • 7 Replies
  • 6783 Views
*

Offline Cornix

  • *****
  • 488
Need help with fragment shader
« on: June 29, 2013, 09:25:35 »
Hello,

I was trying to set up a very simple vertex and fragment shader.
Vertex Shader:
Code: [Select]
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:
Code: [Select]
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!

Re: Need help with fragment shader
« Reply #1 on: July 04, 2013, 13:50:42 »
Have you already take a look on The Quads? There is a example for using texture with fragment shaders and this may help you with.

Also, You can try to set position to:

Code: [Select]
gl_Position = gl_ModelViewProjectionMatrix * vec4(position, 1, 1);
« Last Edit: July 04, 2013, 13:53:08 by afonsolage »

Re: Need help with fragment shader
« Reply #2 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/
Code: [Select]
varying out vec2 pass_tex_coord;
Code: [Select]
varying in vec2 pass_tex_coord;
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

*

Offline Cornix

  • *****
  • 488
Re: Need help with fragment shader
« Reply #3 on: July 09, 2013, 17:05:46 »
I got the fragment shader working by using:
Code: [Select]
gl_FragColor = texture2D(texture, gl_TexCoord[0].st);
However, when I use the varying keyword I get the following error:
Code: [Select]
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)
Code: [Select]
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)
Code: [Select]
uniform sampler2D texture;

varying in vec2 pass_tex_coord;

void main(void) {
gl_FragColor = texture2D(texture, pass_tex_coord);
}

« Last Edit: July 09, 2013, 17:08:21 by Cornix »

Re: Need help with fragment shader
« Reply #4 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
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

*

Offline Cornix

  • *****
  • 488
Re: Need help with fragment shader
« Reply #5 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.

*

Offline Cornix

  • *****
  • 488
Re: Need help with fragment shader
« Reply #6 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:
Code: [Select]
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?

Re: Need help with fragment shader
« Reply #7 on: July 24, 2013, 12:13:34 »
make sure your pointer's are rewinded!