# OpenGL Orthographic matrix not working

This topic is 1994 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi all,

I am having trouble creating an orthographic projection in opengl (without using glOrtho).  And before anyone tells me to use glOrtho, lets just pretend I'm using opengl 3.0 as an exercise.  Here is the way I have set up my matrix:

projectionMatrix.m00 = 2/WIDTH;
projectionMatrix.m11 = 2/HEIGHT;
projectionMatrix.m22 = -2/(far_plane - near_plane);
projectionMatrix.m32 = -((far_plane + near_plane)/(far_plane - near_plane));
projectionMatrix.m33 = 1;


This is your typical ortho projection matrix, taken straight from the opengl spec def. for glOrtho.

When I set up a frustrum and use a perspective matrix, this works fine, but I can't use my regular screen coordinates properly.  But when I use the glOrtho matrix above, I see nothing but my background color. Anyone know what's going on? Is my matrix wrong?

#version 150 core
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

in vec4 in_Position;
in vec4 in_Color;
in vec2 in_TextureCoord;

out vec4 pass_Color;
out vec2 pass_TextureCoord;

void main(void){
gl_Position = in_Position;

//override gl position with new calculated position
gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;

pass_Color = in_Color;
pass_TextureCoord = in_TextureCoord;

}

Any help appreciated.

Thanks!

##### Share on other sites

Try this:

void glOrtho(float* out, float left, float right,float bottom, float top,float near, float far)
{

float a = 2.0f / (right - left);
float b = 2.0f / (top - bottom);
float c = -2.0f / (far - near);

float tx = - (right + left)/(right - left);
float ty = - (top + bottom)/(top - bottom);
float tz = - (far + near)/(far - near);

float ortho[16] = {
a, 0, 0, 0,
0, b, 0, 0,
0, 0, c, 0,
tx, ty, tz, 1
};

memcpy(out, ortho, sizeof(float)*16);
}


Your vertex program should be fine if it works with your perspective matrix.  Let me know if this fixes your problem, this has been confirmed to work for OpenGL ES 2.0.

Shogun.

##### Share on other sites

Thanks Shogun - but isn't that matrix the same as the one I have, if I have 0 as left and 680 as right for example (and same for top/bottom/height)?

##### Share on other sites

What geometry are you feeding it?

A mistake I see sometimes is that people will be drawing, say, a quad: (-1,-1,0) -> (1,1,0) or some other geometry on a very small scale. Which will work fine with a perspective matrix, since you can always move your camera to near enough the object that it appears as large in the viewport as you need it to be. But in a screen-size orthographic projection, that 2x2 size quad will only ever be 2x2 pixels on screen, regardless of where you put your camera. This makes switching from perspective to ortho tricky, in that you have to think about the scale of your objects. If you make your base unit to be a meter for example (ie, 1 unit=1 meter) then you might have a character model that is 2 units tall. In an orthographic projection, this object will only be 2 pixels tall.

So if your geometry is small, you either need to scale the geometry or include a scaling factor when constructing your matrix.

##### Share on other sites

Well right now all I am trying to do is get a 40 pixel by 40 sprite displayed on the screen.  I have tried messing around with the size of the sprite to no avail.  Once I get home I will take a look at the code and give you some more details about it, but I have a feeling you ar right about the scaling/size/etc.

##### Share on other sites

Ok, so I have a sprite with x,y,z (10,10,0)  with w/h 10fx10f

I have left as 0 and right as 680

top as as 480, bottom as 0

Still I see nothing.

Also, I am using lwjgl, in which if you have a matrix4f, I think m23 means 2nd column, 3rd row.

              Sprite sprite = new Sprite("assets/texture/redman.png", 50f, 50f, 10f, 10f );

projectionMatrix.m00 = 2/WIDTH;
projectionMatrix.m11 = 2/HEIGHT;
projectionMatrix.m22 = -2/(far_plane - near_plane);
projectionMatrix.m30 = -1;
projectionMatrix.m31 = -1;
projectionMatrix.m32 = -((far_plane + near_plane)/(far_plane - near_plane));
projectionMatrix.m33 = 1;

cameraPos = new Vector3f(0,0,-1); ...

//Translate camera
Matrix4f.translate(cameraPos, viewMatrix, viewMatrix);

//Scale, translate and rotate model
Matrix4f.scale(modelScale, modelMatrix, modelMatrix);
Matrix4f.translate(modelPos, modelMatrix, modelMatrix);


..

Edited by sufimaster

##### Share on other sites

Have you tried disabling back face culling?

If your sprite doesn't have the correct winding, and back face culling is enabled , then it will not be drawn.

Also, in OpenGL the Y coordinate goes from bottom to top, so make sure that you are positioning the sprite inside the view (unlike the projection matrix where point 0.0, 0.0, 0.0, is the center of the screen, on an orthographic projection, it is the left bottom corner).

Although you can change this when you build the matrix.

You can also try a bigger sprite, like with a size of (100, 100).

One last note. You should try the function Shogun posted to calculate your matrix. Somehow, and i might be wrong, but it doesn't seem like your matrix is being calculated like it should (again, it may be just me).

Don't know if this is your problem, but try it and see.

Edited by __SKYe

##### Share on other sites

Ok- so i used Shogun's method, I messed around with the camera position and near and far plane.

Now:

near plane is 1f, far plane is 100f (though I dont see why this matters in an ortho projection)

Camera is located at 0,0,-1

Sprite is at 0,0,0 dim: 10x10 pixels.

I can actually see it on the screen now, but it seems like everything is vanishing at the origin point, as attached:

This is supposed to be a quad, but the perspective is all wonky. Any idea what's going on now?

##### Share on other sites

Bump - anyone? This is super frustrating.  Is my ortho matrix still messed up?

##### Share on other sites

How are you sending this to your shader?  Are you multiplying it by your previous projection matrix before doing so?  These are two other areas where things can go wrong, and you haven't provided sufficient info to diagnose.

1. 1
2. 2
3. 3
4. 4
frob
13
5. 5

• 16
• 13
• 20
• 12
• 19
• ### Forum Statistics

• Total Topics
632170
• Total Posts
3004547

×