Java-Gaming.org Hi !
Featured games (81)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (119)
games submitted by our members
Games in WIP (576)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  OpenGl transformation question newbie  (Read 2022 times)
0 Members and 1 Guest are viewing this topic.
Offline ankitmcgill

Junior Newbie





« Posted 2008-11-15 22:13:05 »

Hi all,

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.

what i want to see is all 3 axes in something like

              *         *
              *       *
              *     *
              *   *
*******************
            **
         *   *
       *     *
    *        *


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
gl.glClear(GL.GL_COLOR_BUFFER_BIT|gl.GL_DEPTH_BUFFER_BIT);   //clear gl buffers
gl.glMatrixMode(GL.GL_MODELVIEW); //set the current matrix as modelview
gl.glLoadIdentity(); //load identity matrix => locate viewpoint at (0,0,0)

gl.glClearColor(1.0f,1.0f,1.0f,0); //clear window with white
gl.glClear(GL.GL_COLOR_BUFFER_BIT);
gl.glColor3f(0.0f, 0.0f, 0.0f); //set drawing color as black
       
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 a
     gl.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.

thank you for all the help
-ankit

Offline lhkbob

JGO Knight


Medals: 32



« Reply #1 - Posted 2008-11-16 05:17:50 »

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.

Offline ankitmcgill

Junior Newbie





« Reply #2 - Posted 2008-11-16 07:57:23 »

ok ..makes sense .. so i modified my code and now i can see the axis with the desired view however there is a small problem in the code

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
 final GL gl = drawable.getGL();
        final GLU glu = new GLU();
        gl.glClear(GL.GL_COLOR_BUFFER_BIT|GL.GL_DEPTH_BUFFER_BIT);
        gl.glMatrixMode(GL.GL_MODELVIEW);
        gl.glLoadIdentity();
        gl.glTranslatef(0.0f, 0.0f, -1.0f);
        gl.glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
        gl.glRotatef(45.0f, 0.0f, 1.0f, 0.0f);
        //gl.glRotatef(xy_angle0 , xz_angle, xy_angle, xz_angle);
       
 

        gl.glColor3f(0.0f, 0.0f, 0.0f);
       
        //gl.glRotatef(xz_angle, 0.0f, 0.0f, 1.0f);
        gl.glBegin(GL.GL_LINES);
       
           
            //x axis = green
            gl.glColor3f(0.0f, 1.0f, 0.0f);
            gl.glVertex3f( -1.5f, 0.0f, 0.0f );
            gl.glVertex3f( 1.5f, 0.0f, 0.0f );
           
            //y axis = red
            gl.glColor3f(1.0f, 0.0f, 0.0f);
            gl.glVertex3f( 0.0f, -1.5f, 0.0f );
            gl.glVertex3f( 0.0f, 1.5f, 0.0f );
           
            //z axis = blue
            gl.glColor3f(0.0f, 0.0f, 1.0f);
            gl.glVertex3f( 0.0f, 0.0f, -1.5f );
            gl.glVertex3f( 0.0f, 0.0f, 1.5f );
           
                     
           
        gl.glEnd();
        gl.glFlush();


the output that i get is :

Click to Play


my question .. why cant i see the -ve part of the axis ?

thanks again
-ankit
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline lhkbob

JGO Knight


Medals: 32



« Reply #3 - Posted 2008-11-16 23:32:27 »

Not exactly sure what you mean by -ve, can you elaborate?

Offline ankitmcgill

Junior Newbie





« Reply #4 - Posted 2008-11-17 00:22:05 »

Not exactly sure what you mean by -ve, can you elaborate?

the code is supposed to draw all the axes between the limits [-0.5,0.5] however if you look at the attached picture
of the output all i see is the positive half of the axis lines.

-ankit
Offline lhkbob

JGO Knight


Medals: 32



« Reply #5 - Posted 2008-11-17 17:31:36 »

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.

Offline ankitmcgill

Junior Newbie





« Reply #6 - Posted 2008-11-18 00:00:54 »

lhkbob,

thank you so much ! fixing the camera project did the trick  Grin

have another noob question.

i added code so that every time the user presses the right key i change the camera rotate angle to rotate the final projection. it works fine except that right now what i do is (pseudocode)

1  
2  
3  
4  
5  
6  
7  
8  
9  
void keypressed(....)
{
       if key(==right key)
       {
               angle+=2.0f;
               init();
               display();
        }
}


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 ?

thanks
-ankit
Offline lhkbob

JGO Knight


Medals: 32



!?
« Reply #7 - Posted 2008-11-18 04:07:42 »

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.

Offline ankitmcgill

Junior Newbie





« Reply #8 - Posted 2008-11-18 05:39:27 »

hmm .. .i am not sure if i follow ur advice completely.

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 ?

thanks
-ankit
Offline lhkbob

JGO Knight


Medals: 32



« Reply #9 - Posted 2008-11-18 18:58:05 »

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 type
2. Create your GLEventListener (that has display(GLDrawable), init(GLDrawable), etc. methods) and add to the GLCanvas
3. Make the frame visible, and use an Animator or FPSAnimator that automatically calls the GLCanvas's display() method
repeatedly.
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  
public void display(GLAutoDrawable glad) {
  GL gl = glad.getGL();

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

  if (this.redoProjection) {
    gl.glMatrixMode(GL.GL_PROJECTION);
    gl.glLoadIdentity();
    new GLU().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).

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ankitmcgill

Junior Newbie





« Reply #10 - Posted 2008-11-23 09:19:11 »

lhkbob,

thank you for the advice ... i think i understand it now Cheesy

-ankit
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (46 views)
2014-10-17 03:59:02

Norakomi (37 views)
2014-10-16 15:22:06

Norakomi (28 views)
2014-10-16 15:20:20

lcass (32 views)
2014-10-15 16:18:58

TehJavaDev (62 views)
2014-10-14 00:39:48

TehJavaDev (62 views)
2014-10-14 00:35:47

TehJavaDev (52 views)
2014-10-14 00:32:37

BurntPizza (70 views)
2014-10-11 23:24:42

BurntPizza (40 views)
2014-10-11 23:10:45

BurntPizza (82 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!