i am a complete newbie to opengl taking a computer graphics course this semester. we just did transformations so i am playing around with some code to make sure i understand them. however there seems to be some basic flaw in my understanding as i always get output different that what i predicted.

I have posted a sample code below and hoping someone would be kind enough to walk me through it to point what i am not getting right.

the code below is supposed to draw x,y and z axis with the viewpoint situated at (1,1,1) so that i can see all the axis.

gl.glBegin(GL.GL_LINES); //draw all three axis within -0.5 to +0.5 with viewer at (0,0,0). this when rendered will show agl.glVertex3f( -0.5f, 0.0f, 0.0f ); // a plus sign since z axis would be 'hidden'gl.glVertex3f( 0.5f, 0.0f, 0.0f );

gl.glVertex3f( 0.0f, -0.5f, 0.0f );gl.glVertex3f( 0.0f, 0.5f, 0.0f );

gl.glVertex3f( 0.0f, 0.0f, -0.5f );gl.glVertex3f( 0.0f, 0.0f, 0.5f );

gl.glEnd();gl.glTranslatef(1.0f, 1.0f, 1.0f); //finally move the viewer to (1,1,1) to get the desired view

when i run this all i see is half a part of of the plus sign drawn at the top right corner of the gl canvas.

First thing with opengl: it is a state machine, so you need to set up all colors, lights and transforms before rendering anything with that state (and then make any changes before rendering something else, rinse and repeat).

So to get a transform affecting the axis you're drawing, you need to call glTranslate() before glBegin().Also there is no need to call glClear() twice, just move glClearColor(white) to be above 1st clear and remove second.

Finally, translations and transforms in opengl are all matrices that get multiplied together, so the last translation used is the first actually applied. Also, the translations move what is being drawn (effectively), so to simulate the viewing position (which is always at the origin, looking down neg. z axis), you want to do the opposite transform for the viewer.

Gotcha, here are a couple of suggestions:1. Move the camera farther back. You're drawing axis each with a length of 3 (-1.5 to 1.5), but you've only moved back 1 unit.2. Use a longer draw range when you're setting the projection matrix (last argument in gluPerspective if that's what you're using).

If none of that works, can you post the entire code of your setup and GLEventListener and I can toy around with it.

so i am calling the display() and init() every time => redrawing the whole scene. i am wondering since opengl is a state machine there should be a way to just recompute the projection according to the new angle instead of drawing the whole thing again ... any ideas how would i do that ?

There are a couple of ways to do this, but first make sure that you never actually call display() from an event listener for AWT events (key, mouse, etc) because they are supposed to be fast executing. Also, to actually make opengl calls with JOGL, you need to have a current context (which is guaranteed when you use the GLEventListener pattern and let JOGL call those methods when you're updating the frame).

Now, it is best to only have to call init() once per startup of the application, and include only 1-time things in there. If you need to change the projection matrix possibly each frame, then the key listener could set a boolean saying to re-compute the projection matrix, and actually do it in the display() method. Unfortunately you have to re-draw the whole thing each frame. opengl doesn't actually remember what was rendered, the state only remembers how to draw something. So when changed, you need to clear the frame and re-render the geometry.

so i use init() for 1-time stuff. now you said that in the keylistener i should set a boolean to recompute the projection matrix .. assuming i do that , how would the display() function of opengl get called ? wont i need to explicitly call it from my keylistener ?

Here is the best way to render simple things with JOGL (read best as safest):1. Create a GLCanvas and add it to a JFrame or other window type2. Create your GLEventListener (that has display(GLDrawable), init(GLDrawable), etc. methods) and add to the GLCanvas3. Make the frame visible, and use an Animator or FPSAnimator that automatically calls the GLCanvas's display() methodrepeatedly.For more details, I'd check out the jogl-demos source code.

Too recompute the projection matrix, you'd do something like:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

publicvoiddisplay(GLAutoDrawableglad) {GLgl = glad.getGL();

gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);

if (this.redoProjection) {gl.glMatrixMode(GL.GL_PROJECTION);gl.glLoadIdentity();newGLU().gluPerspective(...); // don't actually create a new GLU each time.gl.glMatrixMode(GL.GL_MODELVIEW);this.redoProjection = false; }gl.glLoadIdentity();// set up view transform// set up model transform and others// render stuff}

Also, there is a forum category specifically for JOGL at java-gaming.org, so more specific questions could be asked (and then you'd get more advice than just mine).

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org