openGL texture/shader binding -
i'm new opengl , having trouble sorting out how binding textures , shaders vbos works.
i'm using cinder's texture , shader classes. here's part of draw method:
mshader.bind(); myimage.bind(); glpushmatrix(); gltranslated( scaledx, scaledy, scaledz); gl::draw(sphere.getvbo()); glpopmatrix(); glpushmatrix(); gltranslated( 0, 0, zshift - 200); mdisc.draw(); glpopmatrix(); in above code if comment out call mshader.bind(), sphere vbo display texture (myimage). shader works fine plain (untextured) shapes, when bind shader before drawing shapes wrapped textures blocks textures being displayed.
is problem shader i'm using, or else i'm not understanding? (...there still lot i'm not understanding)
thanks
edit:
here shaders i'm using:
(frag):
uniform sampler2dshadow depthtexture; varying vec3 n, v, l; varying vec4 q; void main(void) { vec3 normal = normalize( n ); vec3 r = -normalize( reflect( l, normal ) ); vec4 ambient = gl_frontlightproduct[0].ambient; vec4 diffuse = gl_frontlightproduct[0].diffuse * max(dot( normal, l), 0.0); vec4 specular = gl_frontlightproduct[0].specular * pow(max(dot(r, v), 0.0), gl_frontmaterial.shininess); vec3 coord = 0.5 * (q.xyz / q.w + 1.0); float shadow = shadow2d( depthtexture, coord ).r; gl_fragcolor = ((ambient + (0.2 + 0.8 * shadow) * diffuse) + specular * shadow); }
(vert):
varying vec3 n, v, l; varying vec4 q; uniform mat4 shadowtransmatrix; void main(void) { vec4 eyecoord = gl_modelviewmatrix * gl_vertex; v = normalize( -eyecoord.xyz ); l = normalize( gl_lightsource[0].position.xyz - eyecoord.xyz ); n = gl_normalmatrix * gl_normal; q = shadowtransmatrix * eyecoord; gl_position = gl_modelviewprojectionmatrix * gl_vertex; }
you must implement texture sampling in shader. see shader uses shadow texture sampler, texture want use? if so, have this:
- set active texture unit:
glactivetexture(gl_texture0); - activate texture:
myimage.bind(); - get location of uniform variable 'depthtexture':
glint loc = glgetuniformlocation(program, "depthtexture"); - bind texture texture sampler in shader:
gluniform1i(loc, 0);
notes: in step 3, must pass shader program id first parameter glgetuniformlocation. don't know cinder's texture , shader classes, suppose there either way query them directly uniform variable locations (or set variable values directly), or @ least should allow program id. in step 4, telling opengl set depthtexture sampler texture of first texture unit (which has index 0); need because have selected texture unit glactivetexture(gl_texture0);.
please see this more in-depth example of using textures in shaders.
Comments
Post a Comment