#1
|
|||
|
|||
Shader Trouble
I'm trying to send the model view projection matrix from the CPU side into my shader, but it's not working as expected. I have two objects loaded into my scene, without any transformations: 'box.wrl' and 'ground.osgb'.
1) Sending over the projection matrix seems to mess up the order of rendering. Code:
uniform vec4 S1, S2, S3, S4; void main() { gl_Position = mat4(S1, S2, S3, S4) * gl_ModelViewMatrix * gl_Vertex; //gl_Position = ftransform(); } 2) I can't get the view matrix to work, or I don't understand how to get it. Code:
uniform vec4 S1, S2, S3, S4; void main() { gl_Position = gl_ProjectionMatrix * mat4(S1, S2, S3, S4) * gl_Vertex; //gl_Position = ftransform(); } *Sorry for the double post in Vizard 3.0 forum. I realized I posted on the wrong forum, and also have more useful information this time. |
#2
|
|||
|
|||
By default, Vizard will automatically compute the near/far clipping planes based on what geometry is in view. The clip planes in the matrix returned by viz.MainWindow.getProjectionMatrix() won't match the clip planes that will be used to perform the rendering. This is why the ground shows up on top of the box.
You can use the viz.MainWindow.clip command to specify static clip planes. This will ensure the projection matrix is the same as the one being used to render the scene. Here is an updated version of the script: Code:
import viz, vizcam, vizshape, vizfx.postprocess, vizact vizcam.PivotNavigate(center=[0, 0.1, 0], distance=5) viz.go() box = viz.addChild('box.wrl') ground = viz.addChild('ground.osgb') vertCode = """ uniform vec4 S1, S2, S3, S4; void main() { gl_Position = mat4(S1, S2, S3, S4) * gl_ModelViewMatrix * gl_Vertex; } """ fragCode = """ void main() { gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); } """ shader = viz.addShader(vert=vertCode, frag=fragCode) box.apply(shader) # Use static clip plane so projection matrix doesn't change viz.MainWindow.clip(0.1,100) m = viz.MainWindow.getProjectionMatrix() S1 = viz.addUniformFloat('S1', m[0:4]) S2 = viz.addUniformFloat('S2', m[4:8]) S3 = viz.addUniformFloat('S3', m[8:12]) S4 = viz.addUniformFloat('S4', m[12:16]) box.apply(S1) box.apply(S2) box.apply(S3) box.apply(S4) |
#3
|
|||
|
|||
Thanks for the help!
Do you happen to know why viz.MainWindow.getMatrix() * box.getMatrix() is incorrect for the modelview matrix? The box shows up but is moving incorrectly when I rotate the viewpoint. |
#4
|
|||
|
|||
You need to pass the inverse of the view matrix:
Code:
m = viz.MainWindow.getMatrix().inverse() |
#5
|
|||
|
|||
Hi farshizzo,
That's what I thought at first when the box was moving the wrong direction. But I already tried passing the inverse and the box doesn't show up at all. Is there something else I'm doing wrong? |
#6
|
|||
|
|||
Sorry, forgot to mention that you need to convert the matrix to the OpenGL reference frame:
Code:
m = viz.MainView.getMatrix().inverse() m[2] = -m[2]; m[6] = -m[6]; m[8] = -m[8]; m[9] = -m[9]; m[14] = -m[14]; |
#7
|
|||
|
|||
Thanks for that, farshizzo. One last question:
How would I tie everything together and add the model matrix to the equation? Do I have to perform any operations/conversions on this matrix as well? Also, what is the multiplication order of model, view, and projection matrices? <node3d>.getMatrix() on objects doesn't seem to work properly without either an inverse() or the same conversion you used for the view matrix. This seems really weird to me because I manually checked that the transformation matrix was correct as is. I'm simply trying to send the MVP to the shader, but everything seems to be flipped from what I'm used to. For example, P * V * M seems to be the wrong multiplication order. I also don't understand why you needed to do the conversion on the view matrix. Thanks again! |
#8
|
|||
|
|||
Yikes! I've been debugging for some time and it seems like there's still something wrong with the view matrix.
I made a simple plane and tried rendering it with: Code:
m = viz.MainView.getMatrix().inverse() m[2] = -m[2]; m[6] = -m[6]; m[8] = -m[8]; m[9] = -m[9]; m[14] = -m[14]; |
#9
|
|||
|
|||
Vizard will rotate certain model formats by 90 degrees along the X-axis in order to bring it into the Vizard coordinate frame. This is probably why you see a rotation.
|
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Vizard Headlight in Shader | shivanangel | Vizard | 1 | 06-27-2012 02:19 PM |
SSAO Shader Implementation | FranciscoPeters | Vizard | 2 | 10-08-2011 05:19 AM |
How to apply shader and render texture to an object | whj | Vizard | 0 | 04-23-2010 01:23 PM |
Multiple Textures for Diffuse and Specularity Shader Issue | shivanangel | Vizard | 1 | 05-11-2009 11:44 AM |
Specular Shader Issue | shivanangel | Vizard | 2 | 05-06-2009 12:08 PM |