WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-23-2012, 11:55 PM
leoaered leoaered is offline
Member
 
Join Date: Sep 2012
Posts: 5
Unhappy 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();
}
When I pass in viz.MainWindow.getProjectionMatrix(), the scene looks OK, just the ground is rendered on top of the box. I don't really understand why, since if I use ftransform() or the inbuilt gl_ProjectionMatrix/gl_ModelViewProjectionMatrix the problem goes away. I also don't think there is a problem with the way I'm sending over the matrix, because I tried hardcoding the projection matrix into the vertex shader and had the same issue.

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();
}
I tried passing viz.MainView.getMatrix() / viz.MainWindow.getMatrix() to no avail. The box doesn't show up properly. What am I missing? The model matrix should be identity because I didn't have any transforms on the objects.

*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.
Attached Files
File Type: txt test.txt (1.3 KB, 1573 views)
Reply With Quote
  #2  
Old 09-24-2012, 09:18 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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)
Reply With Quote
  #3  
Old 09-24-2012, 01:29 PM
leoaered leoaered is offline
Member
 
Join Date: Sep 2012
Posts: 5
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.
Reply With Quote
  #4  
Old 09-24-2012, 01:39 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
You need to pass the inverse of the view matrix:
Code:
m = viz.MainWindow.getMatrix().inverse()
Reply With Quote
  #5  
Old 09-24-2012, 02:12 PM
leoaered leoaered is offline
Member
 
Join Date: Sep 2012
Posts: 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?
Reply With Quote
  #6  
Old 09-25-2012, 08:12 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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];
Reply With Quote
  #7  
Old 09-25-2012, 01:17 PM
leoaered leoaered is offline
Member
 
Join Date: Sep 2012
Posts: 5
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!
Reply With Quote
  #8  
Old 10-01-2012, 02:30 AM
leoaered leoaered is offline
Member
 
Join Date: Sep 2012
Posts: 5
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];
The plane is rotated! Everything goes back to normal with ftransform().
Attached Files
File Type: txt test.py.txt (930 Bytes, 1497 views)
File Type: txt plane.obj.txt (242 Bytes, 1544 views)
Reply With Quote
  #9  
Old 10-03-2012, 12:42 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
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.
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Vizard Headlight in Shader shivanangel Vizard 1 06-27-2012 01:19 PM
SSAO Shader Implementation FranciscoPeters Vizard 2 10-08-2011 04:19 AM
How to apply shader and render texture to an object whj Vizard 0 04-23-2010 12:23 PM
Multiple Textures for Diffuse and Specularity Shader Issue shivanangel Vizard 1 05-11-2009 10:44 AM
Specular Shader Issue shivanangel Vizard 2 05-06-2009 11:08 AM


All times are GMT -7. The time now is 01:41 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC