PDA

View Full Version : projection in OpenGL =(



Vieinco
08-27-2010, 08:36 AM
Please help! =) I can't get the correct transformation in OpleGL.

I have point3D - P(X,Y,Z), and projection matrix M, which equal to K*(R|T) where

K - camera calibration matrix
http://i.imgur.com/alJOE.gif

(R|T) - point (object) coordinate system transformation (R - rotation matrix, T - translation vector)

As a result we have projected point as p = M*P

I know P,K,R,T, and I wont to calculate p in OpenGl terms.

In OpenCV terms it will be look as follows (little abstraction code):


CvMat* R = cvCreateMat(4,4, CV_32F, getRotationData());
CvMat* T = cvCreateMat(4,1, CV_32F, getTranslationData());
CvMat* K = cvCreateMat(4,4, CV_32F, getCameraCalibrationData());

// (R|T)
R->data.fl[3] = T->data.fl[0];
R->data.fl[7] = T->data.fl[1];
R->data.fl[11] = T->data.fl[2];
R->data.fl[15] = T->data.fl[3];

CvMat M = cvMat(4,4, CV_32F);
// M = R*(R|T)
cvMulMat(K, R, &M);

CvMat* P = cvCreateMat(4,1, CV_32F, getTestedPoint3D());
cvMar p = cvMat(4,1, CV_32F); // result transformation

// p = M*P
cvMulMat(&M, P, &p);

// project
float z = p.data.fl[2];
float x = p.data.fl[0] / z;
float y = p.data.fl[1] / z;

printf("Point projection [%f,%f]", x, y);
cvDrawPonit(img, cvPoint(x,y), CV_RGB(255,0,0)); /// <- !!!!
Sombody helps me to translate this logic to OpenGl! =(

How could I set GL_PROJECTION and what could i do in GL_MODELVIEW mode or some else? =(

Mukund
08-28-2010, 01:55 PM
>>How could I set GL_PROJECTION and what could i do in GL_MODELVIEW mode or some else? =(

i am assuming you need help regarding the projection and modelview matrices.

Basically, OpenGL supports 4 modes.
1)GL_PROJECTION
2)GLMODELVIEW
3)GL_TEXTURE
4)GL_COLOR

Now, this is set using the following code:


glMatrixMode(GL_PROJECTION); // One of the 4 matrices
glLoadIdentity(); //Make the matrix an identity matrix
glOrtho(left, right, bottom, top, near, far);

So glOrtho is basically used to set the viewing volume.

You can use the modelview matrix to do rotation, scaling operations.

The functions are:
glRotate[fd]
glScale[fd]
glTranslate[fd].

Hope this helps.

Vieinco
08-29-2010, 05:40 AM
Why glOrtho?... screenX = pointX/pointZ - this is a persperctive projection...

No one can't answer to my question =(((
I can read a manual, i know function like gluPerspective, but I can't understand how the basic transformation of geometry translate to via OpenGL. =(

Any attempts to solve this problem doesn't work =(

HEEEElP!

david_f_knight
08-29-2010, 10:17 AM
I'll just throw this idea out there, but I'm not sure if you will find it helpful.

In a sense, there are two very different OpenGLs: the old (dare I say, obsolete) fixed function pipeline that nearly everyone still seems to be writing programs for, and the new programmable shader pipeline that is the future and is great and seems to largely be ignored.

Using the old fixed function pipeline, you have to find a way to fit your square pegs into OpenGL's round holes. Using the new programmable shaders, you just program whatever you want however you want. You can put your square pegs into square holes, in other words.

The programmable shaders don't impose any particular projection scheme on you, because you define it yourself by how you program it. The only OpenGL terms you need to know are that, ultimately:

(1) whatever is within the cube from (-1, -1, -1) to (1, 1, 1) will not be clipped and everything outside will be clipped, and

(2) the XYZ coordinates of vertices will be divided by their W coordinate for the "Perspective Divide" transforming Clip Coordinates to Normalized Device Coordinates, and

(3) the resulting X and Y coordinates are multiplied by half the viewport width and height, and then viewport origin offsets are added, to obtain Window Coordinates.

All three of those operations occur in fixed hardware immediately prior to the execution of the fragment shader, which is the final programmable shader stage in the OpenGL pipeline.

Writing your own projection code is obviously far more versatile than using the fixed function pipeline (there are, after all, infinitely many ways to project points, but the fixed function pipeline only offers two of them), but also often easier to write your own rather than have to fit your stuff into OpenGL's fixed function methods.

In other words, you didn't state what version of OpenGL you are using, but it is apparently a fixed function version. Could you use the programmable shaders instead? I don't know the fixed function version of OpenGL well, so I can't give advice on how to use it to do what you want. But it seems to me that new development should be done with the new OpenGL rather than the essentially obsolete OpenGL. If you use the new OpenGL, then your problem pretty much disappears.