PDA

View Full Version : Geometry shader, color and Texture coords



saski
02-19-2011, 05:39 AM
howdy folks,

very basic question: do I have to pass color values and texture coordinates through a geometry shader?
I wrote a simple passthough GS that renders the geometry fine but OpenGL fails to include color and texcoord values. If I remove the GS and use only the vertex shader and fragment shader, color and texcoords are fine.

Maybe someone can give me a hint?

Thx in advance :)

Here's my GS:
------------
layout(triangles) in;
layout(triangle_strip, max_vertices = 3) out;
void main() {
gl_Position = vec4(0,0,0,0);
for(int i = 0; i < gl_in.length(); i++) {
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}

And here my VS:
---------------
attribute vec4 li_vtx;
attribute vec3 li_normal;
attribute vec2 li_texcoord;
attribute vec4 li_ambient;

uniform mat4 u_pjmatrix; // projection matrix
uniform mat4 u_mvmatrix; // modelview matrix

varying vec2 g_texcoord;
varying vec4 g_gambient;
void main(void) {
g_gambient = li_ambient;
g_texcoord = li_texcoord;
gl_Position = u_pjmatrix * u_mvmatrix * li_vtx;
}

And finally my FS:
------------------
uniform sampler2D u_colormap;
varying vec2 g_texcoord;
varying vec4 g_gambient;

void main(void) {
vec4 texcolor = texture2D(u_colormap, g_texcoord.st);
gl_FragColor = g_gambient * texcolor;
}

kyle_
02-19-2011, 06:01 AM
very basic question: do I have to pass color values and texture coordinates through a geometry shader?
yes

Your program shouldnt even link, as interfaces betwean VS -> GS -> FS are inconsistent.

saski
02-19-2011, 06:11 AM
hi,

thanks for your fast reply! I did try to pass the color and texcoord through the shader but glDrawElements() didn't like that. And yes the nvidia compiler compiles and links without any warning :(.

could you please point me to a comprehensive tutorial or at least to a complete sample(maybe version 150+)? So far I didn't find any GS sample passing color and texture coords.

thanks a bunch :)

kyle_
02-19-2011, 07:01 AM
Have a look at gtruc samples:
http://ogl-samples.git.sourceforge.net/git/gitweb.cgi?p=ogl-samples/ogl-samples;a=blob;f=data/330/smooth-shading-geom.geom

bcthund
02-19-2011, 04:11 PM
Here is a shader that passes color values across all shaders the vertex shader. The color comes in as vColor and is assigned to vFragColorVs in the vertex shader. It then comes into the geometry shader as vFragColorVs and gets assigned to vFragColor. The fragment shader then has vFragColor come in and assigns it to gl_FragColor.

Vertex Shader


#version 330
uniform mat4 mvpMatrix;
uniform vec4 vColor;
out vec4 vFragColorVs;
in vec4 vVertex;
void main(void) {
vFragColorVs = vColor;
gl_Position = mvpMatrix * vVertex;
}



Geometry Shader


#version 330
precision highp float;
in vec4 vFragColorVs[];
out vec4 vFragColor;
layout (triangles) in;
layout (triangle_strip, max_vertices = 6) out;
void main(void) {
int i;
for ( i=0; i < gl_in.length(); i++) {
vFragColor = vFragColorVs[i];
gl_Position = gl_in[i].gl_Position;
EmitVertex();
}
EndPrimitive();
}


Fragment Shader


#version 330
in vec4 vFragColor;
void main(void) {
gl_FragColor = vFragColor;
}

saski
02-20-2011, 04:32 AM
thanks a lot to you both. now I see my mistakes - silly me ;)
btw. the opengl samples git has some really interesting demos, great resource!