Well, this is more of a question relating to C++ than to java, however it has to do with java and JOGL and I could think of no other place to post it, so I will post it here.
I have spent quite a long time trying to figure out why this strange behavior is exhibited, however I have not been able to come up with an answer, and perhaps the people here much more experienced with JOGL may be able to provide an explanation.
So basically, I created this small function that allows my app to easily switch to rendering between 2d and 3d objects by changing some values of the matrix stacks, namely in GL_PROJECTION and GL_MODELVIEW. I ported this code exactly as is, as well as my initialization code for opengl to C++ (of which I have more experience in than java), however in C++, this code does not produce the same effect as it’s exact counterpart in java does.
Anyways, enough talking, here’s the code.
public static void setDrawMode(int DrawMode)
{
if (CurrentDrawMode != DrawMode)
{
switch (DrawMode)
{
case DRAWMODE_2D:
{
//glDisable(GL.GL_LIGHTING);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrtho(0, WindowWidth, WindowHeight, 0, 0, 1);
gl.glDisable(GL.GL_DEPTH_TEST);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glLoadIdentity();
// Displacement trick for exact pixelization
gl.glTranslatef(0.375f, 0.375f, 0f);
break;
}
case DRAWMODE_3D:
{
gl.glEnable(GL.GL_DEPTH_TEST);
gl.glViewport(0, 0, WindowWidth, WindowHeight);
gl.glMatrixMode( GL.GL_PROJECTION );
gl.glLoadIdentity();
gluPerspective( 45.0f, (float)WindowWidth / (float)WindowHeight, 0.1f, 1000.0f);
gl.glMatrixMode( GL.GL_MODELVIEW );
gl.glLoadIdentity();
break;
}
default:
{
break;
}
}
CurrentDrawMode = DrawMode;
}
}
And here’s the version I ported to C++: (uses constants for window sizing for simplicity in tests)
void setDrawMode(int DrawMode)
{
if (CurrentDrawMode != DrawMode)
{
switch (DrawMode)
{
case DRAWMODE_2D:
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 800, 600, 0, 0, 1);
glDisable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Displacement trick for exact pixelization
glTranslatef(0.375f, 0.375f, 0.0f);
break;
}
case DRAWMODE_3D:
{
glEnable(GL_DEPTH_TEST);
glViewport(0, 0, 800, 600);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( 45.0f, (float)800 / (float)600, 0.1f, 1000.0f);
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
break;
}
default:
{
break;
}
}
CurrentDrawMode = DrawMode;
}
}
In the interest of saving people reading, I included only the function to show basically what I’m doing, however if anyone wants the full cpde or whatever, it’s not too long and doesn’t do much so I’d be willing to share it.
So basically, what I’m trying to ask is: why does this code function as expected in java but not in C++. I examined further strange behavior upon trying to do the same matrix transforms… when i tried to just push/pop matrixes from the matrix stack (in C++) instead of loading identities over and over (like the java code does), it works… this is driving me crazy as to my knowledge, this should result in exactly the same matrixes being formed, but it seems it doesn’t function as expected? I’m not sure anymore…
I now resorted to using the push/pop-ing of matrixes in C++ as a solution to achieve the effect I want (seeing is it’s probably faster as well), however just for my own knowledge I would really like to know why this doesn’t work although it (theoretically?) should… maybe JOGL does something strange here?