views:

110

answers:

2

I'm trying to transform vertices with a shader program, but i can't understand the results. I prepared a projection, a view(trans), and model(trans) matrices:

First i have a vertex shader function of: viewproj * position. In a Objective-C program i start with identity multiplicated by a translation and projection matrices and then uniformed to viewproj. I can see geometry responding to a global translation, in perspective.

I would like to uniform matrices separately and operate all in shader. I uniform a proj, view(trans), model(trans), and try in shader: proj*view*model*position. But sems not well transformed (out representation?) Then i try to simply simulate my first transform. I uniform proj, and view, in shader i try with (view*proj)*position in order to do the same operation, but is'nt the view*proj done in C.

I'm using matrices described in columns 0-4 is first column. And with the structure defined in http://www.songho.ca/opengl/gl_transform.html#modelview

Now i don't know what i'm missing, wish that have a simple answer. If any one have a clue, thank you.

pd: Can i debug this shaders? ( XCode + iphoneos simulator )

A: 

i start with identity multiplicated by a translation and projection matrices and then uniformed to viewproj

It's identity->translation->viewproj->projection, just like you said :

proj*view*model*position

You're hard to read. Could you post some code instead please ?

Calvin1602
A: 

If any one have a clue, thank you.

Matrix multiplication in OpenGL shader goes like this:

    gl_Position = projectionMat*modelViewMat*vec4(inPos, 1.0);

inPos is vec3 vertex position.

If you want more matrices (DirectX style), then you'll have to use

    gl_Position = projectionMat*viewMat*worldMat*vec4(inPos, 1.0);

Ideally matrices should be extracted from OpenGL, and projection shouldn't be identity transform. Use gluOrtho2D if you need 2d graphics.

Also you could accidentally transpose or forget to transpose matrices while loading them into uniform.

Anyway, if you have trouble with shaders, first write app that will render things without shader, then write shader and load transformation matrices from OpenGL, then convert it (if you need that) to custom matrix computation routines.

Depending on OpenGL version, you may have ftransform() GLSL function available which will simplify things even further.

SigTerm