Reputation: 81
I have a problem with the gluProject
Function (OpenGL).
I would like to transform a simple 3D Point from the object space into the screen space.
My code is the following:
int main(){
GLdouble mvmatrix[16];
GLdouble projmatrix[16];
GLint viewport[4];
GLdouble winx;
GLdouble winy;
GLdouble winz;
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);
glGetIntegerv(GL_VIEWPORT, viewport);
gluProject(0.0, 0.0, 0.0, mvmatrix, projmatrix, viewport, &winx, &winy, &winz);
std::cout << winx << winy;
getchar();
getchar();
return 0;
}
The output is:
-1.71799e+009 -1.71799e+009
This is a weird result and does not make sense to me. Does anyone know what went wrong? I did not found anything online.
Upvotes: 1
Views: 144
Reputation: 52082
Does anyone know what went wrong?
You haven't created a GL context and made it current before using glGetDoublev()
and glGetIntegerv()
:
In order for any OpenGL commands to work, a context must be current; all OpenGL commands affect the state of whichever context is current. The current context is a thread-local variable, so a single process can have several threads, each of which has its own current context. However, a single context cannot be current in multiple threads at the same time.
You can use (Free)GLUT (among other things (not exhaustive)) to create a window & associated GL context:
#include <GL/glut.h>
#include <iostream>
int main(int argc, char **argv)
{
// create context & make it current
glutInit( &argc, argv );
glutInitWindowSize( 200, 200 );
glutInitDisplayMode( GLUT_RGBA );
glutCreateWindow( "GLUT" );
GLdouble mvmatrix[16];
GLdouble projmatrix[16];
GLint viewport[4];
GLdouble winx;
GLdouble winy;
GLdouble winz;
glGetDoublev( GL_MODELVIEW_MATRIX, mvmatrix );
glGetDoublev( GL_PROJECTION_MATRIX, projmatrix );
glGetIntegerv( GL_VIEWPORT, viewport );
gluProject
(
0.0, 0.0, 0.0,
mvmatrix, projmatrix, viewport,
&winx, &winy, &winz
);
std::cout << winx << " " << winy << std::endl;
return 0;
}
Output:
100 100
Upvotes: 1