opengl - GLSL Geometry Shader problems -


i have been pulling hair out 2 days on this.

first.. code.. of it.

//leaf vertex shader #version 330 compatibility   out vec4 vertexcolor; void main(void) {     gl_position = gl_modelviewmatrix *gl_vertex;     gl_texcoord[0].st = gl_multitexcoord0.st;     gl_texcoord[1].st  = gl_multitexcoord1.st;     gl_texcoord[2].st  = gl_multitexcoord2.st;     gl_texcoord[3].st  = gl_multitexcoord3.st;     gl_texcoord[4].st = gl_multitexcoord4.st; // out size in x.y     vertexcolor = gl_color; } 

geo shader

#version 330 compatibility  #extension gl_ext_geometry_shader4 : enable           //uniform float unormalslength;        out vec2 texcoord; out vec4 color; layout(points) in; layout(triangle_strip, max_vertices = 4) out; void main(void) {     vec4 vvertex = gl_in[0].gl_position;     vec2 uvs[4];     uvs[0] = gl_texcoord[0].st;     uvs[1] = gl_texcoord[1].st;     uvs[2] = gl_texcoord[2].st;     uvs[3] = gl_texcoord[3].st;     vec2 scale = gl_texcoord[4].st;     float vx[4];     vx[0] = -1.0;     vx[1] = -1.0;     vx[2] = 1.0;     vx[3] = 1.0;     float vy[4];     vy[0]= -1.0;     vy[1]= 1.0;     vy[2]= 1.0;     vy[3]= -1.0;     int indi[4];     indi[0] =  0;     indi[1] =  3;     indi[2] =  1;     indi[3] =  2;     texcoord = uvs[0];     vec4 overtex = gl_positionin[0];     color = vec4(0.5, 0.5, 0.5, 1.0);     for(int = 0; < 4; ++i)         {         overtex.xyz = vvertex.xyz;         color = vec4(0.0, 0.0, 0.5, 1.0);         overtex.x += (vx[ indi[i] ] * scale.x);         overtex.y += (vx[ indi[i] ] * scale.y);         texcoord.xy = uvs[ indi[i] ].xy;          gl_position = gl_modelviewprojectionmatrix * overtex;         emitvertex();     }            endprimitive(); } 

frag shader

// leaf fragment shader #version 330 compatibility  uniform sampler2d colormap; in vec2 texcoord; in vec4 color; out vec4 fragcolor; void main (void) {     vec2 uv = texcoord.xy;     uv.y *= -1.0;     vec4 t = texture2d(colormap,uv);     fragcolor = t;     fragcolor.rgb += color.rgb; } 

when send point, nothing out point. have tried sending triangle_stips.. makes no difference.

when check int32 returned gl_getuniformlocation colormap, -1.

it program not running when call gl_useprogram. has not effect.. can't effect color and.. getting -1 colormap makes me think fragment shader never gets input. have tried hard coding vertex in vertex shader , makes no difference.

i'm not sure transform can't test cant see point. said.. its never being loaded. if send vertices triangle_strip, see triangle_strip on screen. 1 takes time read this.

sorry code maybe can spot i'm missing. also.. there lot of deprecated things in there has never been problem in other shaders.

ok, i'll try point out of wrong things do. since there's so much that's wrong code, won't them all.

#extension gl_ext_geometry_shader4 : enable  

the interactions between core geometry shaders in gl 3.2 , ext_geometry_shader4 functionality not well-defined. therefore, line unknown.

this line, , code relates it, should removed. example, input variable gl_positionin defined ext_geometry_shader4, not core gs. oddly, use value, overwrite in first step of loop, reason.

uvs[0] = gl_texcoord[0].st; uvs[1] = gl_texcoord[1].st; uvs[2] = gl_texcoord[2].st; uvs[3] = gl_texcoord[3].st; vec2 scale = gl_texcoord[4].st; 

no matter shader stage put in, gl_texcoord always refers output of stage. never input. you're copying value of shader stage output variable never write to.

the correct input variable name gl_in[0].gl_texcoord[x].

the easiest way check see if you're reading input variable in gs this: have prefix/suffix array index? if don't, it's not input variable. geometry shader input variables arrayed.

gl_position = gl_modelviewprojectionmatrix * overtex; 

in vertex shader, transformed output position gl_modelviewmatrix. transforms positions model space camera/eye/view space (whatever want call it). positions camera-relative.

the gl_modelviewprojectionmatrix transforms positions model space projection space. if positions in camera space (which, established, are), transformation nonsensical.

the correct matrix use here gl_projectionmatrix, transforms camera space projection space.


this functioning code like. code uses opengl 3.3 core profile.

vertex shader:

#version 330 core  in vec4 position; in vec4 color; in vec2 texcoords[4]; //yes, that's legal. in vec2 scalefactor;  out vs {     out vec4 color;     out vec2 texcoords[4]; //yes, legal too.     out vec2 scalefactor; } dest;  //the transformation model space camera space. uniform mat4 modeltocamera;  void main(void) {     gl_position = modeltocamera * position;     dest.color = color;     for(int = 0; < texcoord.length(); ++i)         dest.texcoord[i] = texcoord[i];     dest.scalefactor = scalefactor; } 

geometry shader:

#version 330 core layout(points) in; layout(triangle_strip, points) out;  in vs {     vec4 color;     vec2 texcoords[4]; //still legal.     vec2 scalefactor; } source[]; //one each input vertex, since we're using points, it's one.  out fs {     vec4 color;     vec2 texcoord; } dest;  const vec2 offsets[4] = vec2[4](     vec2(-1.0, -1.0),     vec2(-1.0,  1.0),     vec2( 1.0,  1.0),     vec2( 1.0, -1.0));  //the transformation camera projection. uniform mat4 projectiontm;  void main(void) {     for(int = 0; < offsets.length(); ++i)     {         dest.color = source[0].color;         dest.texcoord = source[0].texcoord[i];          vec2 scale = offsets[i] * source[0].scalefactor;         vec4 pos = gl_in[0].gl_position;         pos.xy += scale;         gl_position = projectiontm * pos;         emitvertex();     }      endprimitive(); } 

fragment shader:

#version 330 core  in fs {     vec4 color;     vec2 texcoord; };  layout(location = 0) out vec4 fragcolor;  uniform sampler2d colormap;  void main (void) {     vec4 t = texture2d(colormap, texcoord.xy * vec2(1.0, -1.0));     fragcolor = t;     fragcolor.rgb += color.rgb; } 

Comments

Popular posts from this blog

routing - AngularJS State management ->load multiple states in one page -

python - GRASS parser() error -

Swift game error message -