Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (113)
games submitted by our members
Games in WIP (562)
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  
  Issues Drawing a Cube  (Read 4351 times)
0 Members and 1 Guest are viewing this topic.
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Posted 2005-09-27 03:18:40 »

I'll always give you guys a question to answer.

Generally most of my questions seem to stem from Nehe's tutorials because, although they are nice, I think they are rather poor examples for converting into game code, simply because he never makes it easy to put them in with other stuff. Anyway.

I used tutorial number 6 to see how to draw a textured cube, then created a glCube method for accessing it. Nothing is drawn.

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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
public static void glCube(String tex, int x, int y, int z, int w, int h, int d)
    {
          //store the current model matrix
     GL11.glPushMatrix();
      GL11.glTranslatef(0, 0, 0);
          try
          {
             Texture texture = BestGameEver.textureLoader.getTexture(tex);
             GL11.glEnable(GL11.GL_DEPTH_TEST);  //Enable 3D depth testing
            GL11.glTranslatef(0.0f, 0.0f, -5.0f); // Move Into The Screen 5 Units
            texture.bind();
          } catch (Exception e) {e.printStackTrace();}
          ImageViewer.glColor(255,255,255,255);
          GL11.glBegin(GL11.GL_QUADS);
          // Front Face
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z); // Top Left Of The Texture and Quad
         // Back Face
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z-d); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z-d); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z-d); // Bottom Left Of The Texture and Quad
         // Top Face
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y,z); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z-d); // Top Right Of The Texture and Quad
         // Bottom Face
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y+h,z-d); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y+h,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
         // Right face
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z-d); // Bottom Left Of The Texture and Quad
         // Left Face
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z-d); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x,y,z); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
         GL11.glEnd();
         
          GL11.glPopMatrix();
    }


One thing to note what I am trying to do specifically. The entirety of the game is 2D, so depth testing is most of the time disabled. When you start a dialogue or open an item window, however, the window is a 3D one that gently bobbles left and right slightly. Therefore, as you can see, I need to enable depth testing and then pop it back to the old matrix. The BestGameEver.textureLoader references the game's TextureLoader, which is pretty much exactly Kev's class from Space Invaders, aside from a few additions.

Nothing is drawn, and the texture is definitely found because the exact same image path is drawn correctly somewhere else.

In Nehe's original source, he uses 0.0f or 1.0f or -1.0f in the coords, whereas I use actual points. This could be some problem. In any case, it woud be great if someone could point me in the right direction.

PS - Got text to work. Wink

See my work:
OTC Software
Offline anarchotron

Junior Member




...precious bodily fluids.


« Reply #1 - Posted 2005-09-27 04:24:56 »

Your second command moves the modelview matrix by 0,0,0, a NO-OP.  However below I see you moving the matrix by -5 z each frame, which will push your matrix right off into the distance over time (unless you are clearing the matrix somewhere else).

Perhaps this command was supposed to be GL11.glLoadIdentity()?

Other than that everything looks fine to me.  You might make sure that
1. you've cleared the depth buffer
2. lighting is off
3. your projection matrix and or viewport is not fubar



    //store the current model matrix
GL11.glPushMatrix();
GL11.glTranslatef(0, 0, 0);
    try
    {
   
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #2 - Posted 2005-09-27 05:31:40 »

Great that worked. Just needed to kill the "GL11.glTranslatef(0.0f, 0.0f, -5.0f); // Move Into The Screen 5 Units" and it worked, although I added in GL11.glLoadIdentity() for good measure.  Tongue

Thanks very much.

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #3 - Posted 2005-09-27 06:03:01 »

Hmm... or not. I can see something now, but there's still a couple things that don't work.

a) For some reason to texture on the front draws from the top left to about 50 pixels away from the bottom right, leaving black space there. Why won't it fill the entire thing with my texture?

b) I cannot move it past -1 or 1 on the z axis without it going MIA, how do I fix that?

c) Chaning the z doesn't seem to do anything I can notice.

d) Using GL11.glRotatef(xDir, 1.0f, 0.0f, 0.0f); to rotate the cube does nothing unless I remove GL11.glLoadIdentity(), ending up with what appears to be the texture moving up and down along the cube face, leaving gaps in between.

Hmm.. that's a lot of issues. Any ideas what may be up, or what GL11 things I need to set before drawing this correctly? I'm pretty sure I've got them all...

See my work:
OTC Software
Offline hvor2

Junior Member




Beyond mind, there is an awareness...


« Reply #4 - Posted 2005-10-04 07:21:03 »

Hmm.. Don't have much time now, but...

b), c)  this sounds to me that you are in ortho mode, constrained in z from -1 to 1, so, that could be the problem...

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2005-10-04 17:55:44 »

Oh yeah, that's probably it. How do I jump out of ortho mode?

See my work:
OTC Software
Offline cborders

Junior Member





« Reply #6 - Posted 2005-10-04 19:07:27 »

you need to do this:
1  
2  
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GLU.gluPerspective(45.0f, (float)width/(float)height, 0.1f, 1000.0f);

or something similar.  You put it in ortho mode by doing
1  
GL11.glOrtho()

instead of
1  
GLU.gluPerspective()
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #7 - Posted 2005-10-04 20:41:04 »

Okay, now after I do that I get this:

Quote
Exception in thread "main" org.lwjgl.opengl.OpenGLException: Stack underflow (1284)
   at org.lwjgl.opengl.Util.checkGLError(Util.java:56)
   at org.lwjgl.opengl.Display.update(Display.java:549)
   at Panel.paint(Panel.java:208)
   at Panel.mainLoop(Panel.java:58)
   at Panel.<init>(Panel.java:35)
   at BestGameEver.run(BestGameEver.java:93)
   at BestGameEver.main(BestGameEver.java:31)

After commenting some stuff out I isolated it as the glMatrixMode that breaks it. If I have only gluPerspective then instead of seeing a weird messed up line skewing like I had I see nothing.

See my work:
OTC Software
Offline cborders

Junior Member





« Reply #8 - Posted 2005-10-04 20:56:28 »

Weird!  Here is what I use to init everything:
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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
private void initDisplay()
{
    try
    {
        //find displaymode
       DisplayMode[] modes = Display.getAvailableDisplayModes();
        for (int i = 0; i < modes.length; i++)
        {
            if (modes[i].getWidth() == 640 &&
                    modes[i].getHeight() == 480 &&
                    modes[i].getBitsPerPixel() >= 32 &&
                    modes[i].getFrequency() == 60)
            {
                mode = modes[i];
                break;
            }
        }
        Display.setDisplayMode(mode);
        Display.setTitle("DXF Reader");
        Display.create();
        return;
    }
    catch (LWJGLException e)
    {
        e.printStackTrace();
    }
}

private void initGL()
{
    GL11.glShadeModel(GL11.GL_SMOOTH);
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GL11.glClearDepth(1.0);
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glDepthFunc(GL11.GL_LEQUAL);
   
    // Go into orthographic projection mode.
   GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
   
    GLU.gluPerspective(45.0f, mode.getWidth()/mode.getHeight(), 0.1f, 1000.0f);
    GL11.glViewport(0, 0, mode.getWidth(), mode.getHeight());
   
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
    GL11.glLoadIdentity();
    GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);
    GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE);
}


... and in my constructor I call them in that order!  Then I have the main loop of the program.

Hope this helps!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #9 - Posted 2005-10-04 21:15:52 »

Well obviously something is going hunky-dory between me trying to have everything be 2D except this single cube. I must be forgetting to switch some stuff back to 2D.

After adding in
1  
2  
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();


before glPopMatrix() I don't get the exception anymore, which after reading through my Java book stems from this line, which makes sense because it says there is a stack problem. Anyway, after doing this, it continues to run, however, the entire screen turns white. I think I'm probably forgetting to set something back to the 2D settings, although I believe am setting ortho mode manually before every drawing... or not. I'll try re-enabling ortho mode after the cube is drawn...

See my work:
OTC Software
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #10 - Posted 2005-10-04 21:21:36 »

Well, well. Setting it back to ortho mode, along with changing the matrix mode to modelview, "fixes" everything. No freezes, no crashes, everything is drawn. BUT. There is absolutely no cube drawn at all.  Roll Eyes

Here's what I have at this stage:
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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
public static void glCube(String tex, int x, int y, int z, int w, int h, int d)
    {
          //store the current model matrix
     GL11.glPushMatrix();
      //GL11.glTranslatef(0, 0, 0);
     //GL11.glLoadIdentity();
         try
          {
             Texture texture = BestGameEver.textureLoader.getTexture(tex);
             GL11.glEnable(GL11.GL_DEPTH_TEST);  //Enable 3D depth testing
            GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
            GL11.glLoadIdentity();
             GLU.gluPerspective(45.0f, (float)800/(float)600, 0.1f, 1000.0f);
             texture.bind();
          } catch (Exception e) {e.printStackTrace();}
          ImageViewer.glColor(255,255,255,255);
          GL11.glBegin(GL11.GL_QUADS);
          // Front Face
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z); // Top Left Of The Texture and Quad
         // Back Face
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z-d); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z-d); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z-d); // Bottom Left Of The Texture and Quad
         // Top Face
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y,z); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z-d); // Top Right Of The Texture and Quad
         // Bottom Face
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y+h,z-d); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y+h,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
         // Right face
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x+w,y,z-d); // Top Left Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x+w,y+h,z-d); // Bottom Left Of The Texture and Quad
         // Left Face
         GL11.glTexCoord2f(0.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z-d); // Bottom Left Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 0.0f);
          GL11.glVertex3f(x,y+h,z); // Bottom Right Of The Texture and Quad
         GL11.glTexCoord2f(1.0f, 1.0f);
          GL11.glVertex3f(x,y,z); // Top Right Of The Texture and Quad
         GL11.glTexCoord2f(0.0f, 1.0f);
          GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
         GL11.glEnd();
         
          GL11.glDisable(GL11.GL_DEPTH_TEST);  //Enable 3D depth testing
     GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
     GL11.glLoadIdentity();
      GL11.glOrtho(0, 800, 600, 0, -1, 1);
          GL11.glMatrixMode(GL11.GL_MODELVIEW);
          GL11.glLoadIdentity();
          GL11.glPopMatrix();
    }

See my work:
OTC Software
Offline cborders

Junior Member





« Reply #11 - Posted 2005-10-04 21:37:06 »

Ok, I am new to all of this too so I may be wrong, but try putting the glPopMatrix() right after the glEnd().  And this stuff:
1  
2  
3  
4  
5  
6  
GL11.glDisable(GL11.GL_DEPTH_TEST);  //Enable 3D depth testing
GL11.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 600, 0, -1, 1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glLoadIdentity();

... only needs to be done once when you initialize everything.  Also is the cube the only thing that you're drawing?  If it is then you shouldn't need the push pop matrix stuff at all.  Is there a place in your code where you do a glTranslate() into the screen?  If not then the box is being drawn around the camera and you would be looking at the "other" side of the quads that you're drawing and therefore see through them.
Offline cborders

Junior Member





« Reply #12 - Posted 2005-10-04 21:55:21 »

Here, this works.  The placement of the cube in your method is a little off so it doesn't rotate exacly around the center, but you can tweek that no problem!  Grin

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  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;

public class Cube
{
   private DisplayMode mode;
   private boolean done = false;
   private float z = -10f, rotX = 0, rotY = 0;
   
   public static void main(String[] args)
   {
      new Cube();
   }
   public Cube()
   {
      initDisplay();
        initGL();
        run();
   }
   
   private void initDisplay()
    {
        try
        {
            //find displaymode
           DisplayMode[] modes = Display.getAvailableDisplayModes();
            for (int i = 0; i < modes.length; i++)
            {
                if (modes[i].getWidth() == 640 &&
                        modes[i].getHeight() == 480 &&
                        modes[i].getBitsPerPixel() >= 32 &&
                        modes[i].getFrequency() == 60)
                {
                    mode = modes[i];
                    break;
                }
            }
            Display.setDisplayMode(mode);
            Display.setTitle("DXF Reader");
            Display.create();
            return;
        }
        catch (LWJGLException e)
        {
            e.printStackTrace();
        }
    }
   
    private void initGL()
    {
        GL11.glShadeModel(GL11.GL_SMOOTH);
        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        GL11.glClearDepth(1.0);
        GL11.glEnable(GL11.GL_DEPTH_TEST);
        GL11.glDepthFunc(GL11.GL_LEQUAL);
       
        // Go into orthographic projection mode.
       GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glLoadIdentity();
       
        GLU.gluPerspective(45.0f, mode.getWidth()/mode.getHeight(), 0.1f, 1000.0f);
        GL11.glViewport(0, 0, mode.getWidth(), mode.getHeight());
       
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();
        GL11.glHint(GL11.GL_LINE_SMOOTH_HINT, GL11.GL_NICEST);
    }
   
    private void run()
    {
        while(!done)
        {
            mainLoop();
            render();
            Display.update();
           
            Thread.yield();
        }
        cleanup();
    }
   
    private void mainLoop()
    {
       if(Keyboard.isKeyDown(Keyboard.KEY_UP))
        {
            rotX -= 1.0f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
        {
           rotX += 1.0f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_LEFT))
        {
           rotY -= 5.0f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
        {
           rotY += 5.0f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_ADD))
        {
            z += 0.1f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_SUBTRACT))
        {
            z -= 0.1f;
        }
        if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
        {
            done = true;
        }
        if(Display.isCloseRequested())
        {
            done = true;
        }
    }
   
    private void render()
    {
        // clear background
       GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glLoadIdentity();
        GL11.glTranslatef(0.0f, 0.0f, z);
        GL11.glRotatef(rotX, 1.0f, 0.0f, 0.0f);
        GL11.glRotatef(rotY, 0.0f, 1.0f, 0.0f);

        glCube("", -1, -1, -1, 2, 2, 2);
    }
   
   public static void glCube(String tex, int x, int y, int z, int w, int h, int d)
    {
      GL11.glBegin(GL11.GL_QUADS);
      // Front Face
     GL11.glVertex3f(x,y+h,z); // Bottom Left Of The Texture and Quad
     GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
     GL11.glVertex3f(x+w,y,z); // Top Right Of The Texture and Quad
     GL11.glVertex3f(x,y,z); // Top Left Of The Texture and Quad
     // Back Face
     GL11.glVertex3f(x+w,y+h,z-d); // Bottom Right Of The Texture and Quad
     GL11.glVertex3f(x+w,y,z-d); // Top Right Of The Texture and Quad
     GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
     GL11.glVertex3f(x,y+h,z-d); // Bottom Left Of The Texture and Quad
     // Top Face
     GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
     GL11.glVertex3f(x,y,z); // Bottom Left Of The Texture and Quad
     GL11.glVertex3f(x+w,y,z); // Bottom Right Of The Texture and Quad
     GL11.glVertex3f(x+w,y,z-d); // Top Right Of The Texture and Quad
     // Bottom Face
     GL11.glVertex3f(x+w,y+h,z-d); // Top Right Of The Texture and Quad
     GL11.glVertex3f(x,y+h,z-d); // Top Left Of The Texture and Quad
     GL11.glVertex3f(x,y+h,z); // Bottom Left Of The Texture and Quad
     GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
     // Right face
     GL11.glVertex3f(x+w,y+h,z); // Bottom Right Of The Texture and Quad
     GL11.glVertex3f(x+w,y,z); // Top Right Of The Texture and Quad
     GL11.glVertex3f(x+w,y,z-d); // Top Left Of The Texture and Quad
     GL11.glVertex3f(x+w,y+h,z-d); // Bottom Left Of The Texture and Quad
     // Left Face
     GL11.glVertex3f(x,y+h,z-d); // Bottom Left Of The Texture and Quad
     GL11.glVertex3f(x,y+h,z); // Bottom Right Of The Texture and Quad
     GL11.glVertex3f(x,y,z); // Top Right Of The Texture and Quad
     GL11.glVertex3f(x,y,z-d); // Top Left Of The Texture and Quad
     GL11.glEnd();
    }
   
   private void cleanup()
    {
        System.exit(0);
    }
}
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #13 - Posted 2005-10-04 22:16:35 »

Yeah, I run that on its own and it works, thanks for the code.

My big problem, however, is that I am not only drawing a cube. What I have right now is an entire 2D game drawn out. I simply trying to change the dialogue window that pops when you talk to a character from a 2D rectangle to a 3D cube that gently rotates back and forth. I am having some issues actively converting it between these drawing methods.

The 2D stuff is drawn in ortho mode with depth testing off, no z axis, etc. The cube obviously needs some perspective to be drawn correctly.

I'll try to use your code to change my initializers in here somewhere.

See my work:
OTC Software
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #14 - Posted 2005-10-04 22:46:47 »

It should also be known that I am not plugging in dumb values like -1, 1, etc. into the coords for drawing my cube, which could very well be the problem. I am trying to put in flat out x, y coordinates. Say the window is like this:

1  
2  
3  
4  
5  
6  
+---------------------+
|    *-----------*    |
|    | dialogue  |    |
|    *-----------*    |
|                     |
+---------------------+


The coordinates of the actual display are 0,0 as the top left corner and 800,600 as the bottom right. The window itself is drawn at about 100,10 as top left to 500,210 as the bottom right. These are the coordinates I am plugging into the cube method, whereas yours are very small values, and if I put in small values I can actually see something drawn even within my game. I like glOrtho because it turns it all to this display-like format, which is useful for drawing sprites and the like. How can I have cube drawing work the same way?

I felt like I should edit this and post that I do in fact have a spinning cube in there now, the only problem is trying to translate it into that sort of coordinate use. It may not be possible, it occurs to me, but that's fine. I'll figure it out.

Also, for some off reason whenever I map textures in this way they only fill up a part of the quad, leaving black space on the edges for no reason. Any ideas why? I can "stretch" textures to fit, but then I need to tailor it for every texture that crosses my path, something I don't want to do.

See my work:
OTC Software
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #15 - Posted 2005-10-05 01:40:22 »

Any idea how I can draw text onto the cube so that it rotates with the cube? Should I just draw text along with it?

See my work:
OTC Software
Offline cborders

Junior Member





« Reply #16 - Posted 2005-10-05 01:49:08 »

elias4444 has some code in his ScreenManager class that calculates the proper z-value to make each unit a pixel on the screen.  I have not looked too far into it, but you may appeal to him for help on that front.  I am a little at a loss for what you are trying to do in the big picture though.  You have a cube with text on it that oscillates back and forth?  Is the player seeing the edge of the cube or the side? (i.e. could it be a single quad?)  elias4444 also has a very handy text class that you may be able to get some good stuff from!
Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #17 - Posted 2005-10-05 03:40:18 »

I'll check him out, thanks for the help.

I do need a cube because of the perspective angle. You can see the sides of it, even though the front face is the really important part. On the left side of the cube a texture for the current talker's portrait is drawn, then on the right side the text will (theoretically) be drawn. I already have text working on a 2D level, I'm just going to extend points into 3D, that should work.

All in all, thanks for everyone's help, now I finally can figure (most of) this stuff out by myself. Smiley

See my work:
OTC Software
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.

Dwinin (15 views)
2014-09-12 09:08:26

Norakomi (45 views)
2014-09-10 13:57:51

TehJavaDev (57 views)
2014-09-10 06:39:09

Tekkerue (26 views)
2014-09-09 02:24:56

mitcheeb (49 views)
2014-09-08 06:06:29

BurntPizza (33 views)
2014-09-07 01:13:42

Longarmx (19 views)
2014-09-07 01:12:14

Longarmx (21 views)
2014-09-07 01:11:22

Longarmx (20 views)
2014-09-07 01:10:19

mitcheeb (30 views)
2014-09-04 23:08:59
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!