开发者

OpenGL left-handed coordinate system

I'm writing a 3D OpenGL app and I'm having problems with my vertex translation matrix.

Here's my vertex shader:

attribute vec4     vInPos;

uniform mat4    kWorld;
uniform mat4    kProj;

void main( void )
{
    vec4 world_pos = kWorld * vInPos;
    gl_Position = kProj * world_pos;
}

All my matrices are stored row major and I'm using a left handed perspective matrix as my projection matrix.

The problem is, when I translate a vertex by my matrix, it moves in the wrong direction on both the X and Y axes, and is flipped. So translating by (100.0f,100.0f,100.0f) moves my vertex the correct way on the Z axis (away from the camera), but to the left on the X axis (backwards) and down on the Y axis (backwards).

Here's what it currently looks like:

OpenGL left-handed coordinate system

The FPS counter should be up and to the right, instead of down to the left. It should also obviously not be mirrored. Based on what I've read, this is happening because OpenGL by default uses the right-handed coordinate system, but I'm not sure what I can do to fix this.

I found a few articles that try to explain it, but I couldn't find any solutions for what to do. This question mentions that the OpenGL spec shows what the coordinate system expected, but after looking, I didn't see how that helped me.

EDIT: For reference, my projection matrix code is based on the DirectX SDK.

EDIT: Per Nicol Bolas' suggestion, I'm now using the matrix from gluPerspective, but I'm getting the same results.


Here's the code I'm using to set my matrices:

/* Vertex data for reference
floa开发者_如何学运维t vertices[] =
{
    {0.0f,0.0f,0.0f},
    {5.0f,0.0f,0.0f},
    {0.0f,5.0f,0.0f},
    {5.0f,5.0f,0.0f},
}; */    
float worldf[16];
float projf[16];
float fov       = 60.0f;
float aspect    = 1280.0f/720.0f;
float near      = 1.0f;
float far       = 1000.0f;
float f         = 1.0f/tanf( (fov*6.2831f/360.0f)/2.0f );

/* Build world matrix */
memset(worldf, 0, sizeof(worldf));
worldf[0] = worldf[5] = worldf[10] = worldf[15] = 1.0f;
worldf[12] = 100.0f;
worldf[13] = 100.0f;
worldf[14] = -100.0f;

/* Build perspective matrix */
memset(projf, 0, sizeof(projf));
projf[0] = f/aspect;
projf[5] = f;
projf[10] = (far+near)/(near-far);
projf[14] = (2.0f*far*near)/(near-far);
projf[11] = -1.0f;

glUniformMatrix4fv(proj_uniform, 1, GL_FALSE, projf);
glUniformMatrix4fv(world_uniform, 1, GL_FALSE, worldf);

Using this code, my text doesn't show up at all.

EDIT: Not sure if it's relevant, but I'm using OpenGL 2.0 on OS X Snow Leopard.


I'm using a left-handed perspective matrix

Stop doing that.

Equally importantly, the output of the projection matrix (clip-space) is different for OpenGL and D3D. You can't just take a D3D projection matrix, transpose it, and expect that GLSL will accept the output just fine.

So again, look up gluPerspective and use that matrix.


Per Nicol Bolas' suggestion, I'm now using the matrix from gluPerspective, but I'm getting the same results.

You're likely also using a bunch of other left-handed matrices. Your model-to-camera matrix needs to be right-handed. Also, your vertex data needs to be right-handed as well. Alternatively, you can try to convert from left-handed space to right-handed, but I wouldn't advise it unless you had no other choice.


float f = 1.0f/tanf(fov/2.0f);

fov is an angle in degrees. tanf takes an angle in radians. The tanf of 30.0f is a negative number, which means your frustum scale in your perspective matrix is negative. That effectively inverts the X and Y axes of your view.

What you really need is this:

float f = 1.0f/tanf((fov * 6.2831f / 360.0f ) / 2.0f);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜