View Full Version : how to treat vertex shader in world-space?

TexFetch

12-17-2004, 07:47 AM

Hi there.

First of all, sorry about my bad English...

Okay so, I have a simple question.

How to treat vertex shader in world-space?

I know how to treat it in eye-space but in world-space, I'm at a loss...

vec4 VertexPos = gl_ModelViewMatrix * gl_Vertex;

LightVec = LightPos.xyz - VertexPos.xyz;

ViewVec = -VertexPos.xyz;

NormalVec = gl_NormalMatrix * gl_Normal;

gl_Position = ftransform();I have to use gl_ModelViewMatrixInverse or something?

Thanks.

jwatte

12-18-2004, 10:18 AM

It seems that your problem is that you want to break MODEL and VIEW into separate matrices.

You can do that: just declare two matrix uniforms named mxModel and mxView (or whatever), and in your application, set them to the appropriate matrices. In fact, you may want mxModel, and mxViewProjection instead.

If you still want to use ftransform(), then you use that for position, but then when you need the world-space position, you multiply mxModel with gl_Vertex to get it.

mogumbo

12-20-2004, 01:19 PM

When I do this I always like to send the inverse view matrix to the vertex program and not bother with the individual model matrices. The reason is because the model matrices might all be different, but the view matrix is the same for everything model, so you don't have as many matrices to deal with. Since you always multiply your vertices by the modelview matrix, it's easy to multiply again by the inverse view matrix to go back to world space.

TexFetch

12-20-2004, 08:43 PM

jwatte and mogumbo,

thank you for your replies.

Okay, as you people advised me,

I calculated the inverse of view matrix in main program and send it to vertex shader as unifrom.

And finally, I got a fine result!

vec4 VertexPos = gl_ModelViewMatrix * gl_Vertex;

VertexPos = InvViewMatrix * VertexPos;

VertColor = gl_Color;

LightVec = LightPos.xyz - VertexPos.xyz;

ViewVec = CameraPos.xyz - VertexPos.xyz;

NormalVec = vec3(InvViewMatrix * vec4(gl_NormalMatrix * gl_Normal, 0.0)).xyz;At first, I was again at a loss because

I didn't know the inverse of view matrix has to be multiplied to gl_NormalMatrix but now it's okay.

And yes, I feel that multiplying the inverse of view matrix is the best way

to get the world-space coordinates as mogumbo pointed out.

Well, thank you again for helping such a three-year-old kid's question like this.

And sorry for my bad English, too.

Thanks.

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.