View Single Post
  #1  
Old 05-16-2013, 06:14 AM
goro goro is offline
Member
 
Join Date: May 2012
Posts: 33
Texture Coordinate Generation (TexGen) using Shader

Hi, we are receiving particular result with fixed functionality but finding difficult to get that using shader. We have to use the shader to go beyond the fixed functionality.

We have to mimic viz.TEXGEN_PROJECT_EYE in our shader to use nearby Texture Coordinates.

First the successful implementation of what we are getting using fixed functionality:

Code:
import viz
import vizact

viz.go()

#Add Piazza
piazza = viz.addChild('piazza.osgb')

#Add, Position & Scale TexQuad
quad = viz.addTexQuad()
quad.setPosition(0,2,6)
quad.setScale(2,2)

#Render Node & Render Texture
RT = viz.addRenderTexture()
RT.wrap(viz.WRAP_T, viz.REPEAT)
RT.wrap(viz.WRAP_S, viz.REPEAT)

RN = viz.addRenderNode()
RN.attachTexture(RT)

#Apply Rendered Texture to quad
quad.texture(RT)

#Get the Invisible Effect from generating the Texture Coordanates
quad.texGen(viz.TEXGEN_PROJECT_EYE)

#Spin the quad to see invisible effect 
spin = vizact.spin(1,1,1,10)
quad.addAction(spin)
Bye using above code we are making quad invisible.

We have written a shader instead of TexGen. Check the code below:
Code:
import viz
import vizact

viz.go()

#Add Piazza
piazza = viz.addChild('piazza.osgb')

#Add, Position & Scale TexQuad
quad = viz.addTexQuad()
quad.setPosition(0,2,6)
quad.setScale(2,2)

#Render Node & Render Texture
RT = viz.addRenderTexture()
RT.wrap(viz.WRAP_T, viz.REPEAT)
RT.wrap(viz.WRAP_S, viz.REPEAT)

RN = viz.addRenderNode()
RN.attachTexture(RT)

#Apply Rendered Texture to quad
quad.texture(RT)

#Shader
vertCode = """

void main()
{
	gl_Position = ftransform();
	vec4 ecPosition = gl_ModelViewMatrix * gl_Vertex;
	
	gl_TexCoord[0].s = dot(ecPosition,gl_EyePlaneS[0]);
	gl_TexCoord[0].t = dot(ecPosition,gl_EyePlaneT[0]);
	gl_TexCoord[0].p = dot(ecPosition,gl_EyePlaneR[0]);
	gl_TexCoord[0].q = dot(ecPosition,gl_EyePlaneQ[0]);
}
"""


fragCode = """

uniform sampler2D tex;

void main()
{
	gl_FragColor = texture2D(tex,gl_TexCoord[0].st);
	
}
"""

shader = viz.addShader(vert = vertCode, frag = fragCode)

quad.apply(shader)
quad.apply(viz.addUniformInt('tex',0))

#Spin the quad to see invisible effect 
spin = vizact.spin(1,1,1,10)
quad.addAction(spin)
This code is giving us the parallel projection of Rendered Texture to screen but we don't know how to position & scale it to get as fine effect as the previous fixed functionality code.

Please help us. If any efficient alternate way then please suggest.

Thanks!
Reply With Quote