Hello Guest

Beginner Q

  • 8 Replies
  • 12692 Views
Beginner Q
« on: May 02, 2005, 10:05:20 »
Please take a look at the code below, can u tell me why eventually after a couple loops of my program my quad dissapears (is no longer visible on the screen). When i say a couple loops im just estimating.

Code: [Select]


public void gameLoop(){

try{
while(!finnish){

update();
render();
display.update();

}

cleanup();

}catch(Exception e){System.out.println(e);}


}

                public void render() {

        gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
        gl.glLoadIdentity();                          // Reset The Current Modelview Matrix

gl.glTranslatef(0.0f,0.0f,-6.0f);  



for(int y1 = 0; y < 5; y++){

for(int x1 = 0; x < 5; x++){

        gl.glBegin(gl.GL_QUADS);        
            gl.glVertex3f( -1.0f, 1.0f, 0.0f);        
            gl.glVertex3f(-1.0f,-1.0f, 0.0f);        
            gl.glVertex3f( 1.0f,-1.0f, 0.0f);        
        gl.glVertex3f( 1.0f,1.0f, 0.0f);
gl.glEnd();

}


}


    }




Cheers, Nick

*

Offline princec

  • *****
  • 1933
    • Puppygames
Beginner Q
« Reply #1 on: May 02, 2005, 10:17:50 »
Because you are every loop translating the modelview matrix by a further 6.0f units out of the Z plane!

You need to do this:
Code: [Select]

glPushMatrix();
glTranslatef(x,y,z);
// Draw here
glPopMatrix();


Cas :)

Beginner Q
« Reply #2 on: May 02, 2005, 10:31:19 »
Sorry no luck there:

Here is the code again, my first one was formatted incorrectly.

Code: [Select]

public void gameLoop(){

try{
while(!finnish){

update();
render();
display.update();

Thread.sleep(1000);

}

cleanup();

}catch(Exception e){System.out.println(e);}


}

    public void render() {

        gl.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);          // Clear The Screen And The Depth Buffer
        gl.glLoadIdentity();                          // Reset The Current Modelview Matrix

gl.glTranslatef(0.0f, 0.0f, -6.0f);


for(int y1 = 0; y < 5; y++){

for(int x1 = 0; x < 5; x++){

        gl.glBegin(gl.GL_QUADS);         // Drawing Using Quads
            gl.glVertex3f( -1.0f, 1.0f, 0.0f);         // Top Left
            gl.glVertex3f(-1.0f,-1.0f, 0.0f);         // Bottom Left
            gl.glVertex3f( 1.0f,-1.0f, 0.0f);         // Bottom Right
        gl.glVertex3f( 1.0f,1.0f, 0.0f);  //Top right
gl.glEnd();


}


}




    }


If i get rid of the two for loops its works fine, ive tried to use glPushMatrix and PopMatrix but they didnt work.

Am i incorrect in saying that gl.glLoadIdentity(); sets it back to 0,0,0.

*

Offline princec

  • *****
  • 1933
    • Puppygames
Beginner Q
« Reply #3 on: May 02, 2005, 10:37:38 »
Try removing the translate and see if it works.

Cas :)

Beginner Q
« Reply #4 on: May 02, 2005, 10:44:21 »
Did, doesnt work. I cant see how the problem can relate to the glTranslatef. Because if i remove to two for loops its works fine. Its weird, lol i always get this kinda stuff happening when i start new things. Below ill paste my init code:

Code: [Select]


public void glInit(){

try{

gl.glEnable(GL11.GL_TEXTURE_2D); // Enable Texture Mapping
        gl.glShadeModel(GL11.GL_SMOOTH); // Enable Smooth Shading
        gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background
        gl.glClearDepth(1.0); // Depth Buffer Setup
        gl.glEnable(GL11.GL_DEPTH_TEST); // Enables Depth Testing
        gl.glDepthFunc(GL11.GL_LEQUAL); // The Type Of Depth Testing To Do

        gl.glMatrixMode(GL11.GL_PROJECTION); // Select The Projection Matrix
        gl.glLoadIdentity(); // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        glu.gluPerspective(
          45.0f,
          (float) display.getDisplayMode().getWidth() / (float) display.getDisplayMode().getHeight(),
          0.1f,
          100.0f);
        gl.glMatrixMode(GL11.GL_MODELVIEW); // Select The Modelview Matrix

        // Really Nice Perspective Calculations
        gl.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);




}catch(Exception e){System.out.println(e);}

}



Cheers, Nick

*

Offline princec

  • *****
  • 1933
    • Puppygames
Beginner Q
« Reply #5 on: May 02, 2005, 10:50:58 »
for(int y1 = 0; y < 5; y++){
       
         for(int x1 = 0; x < 5; x++){

very suspect looking loops there...


Cas :)

Beginner Q
« Reply #6 on: May 02, 2005, 10:52:20 »
Haha i dont quiet know what u mean there, and to thell you the truth there is no point of having the loops, i was just playing around and this happened.

Beginner Q
« Reply #7 on: May 02, 2005, 11:38:03 »
Haha jeez sorry just saw that lol thats shoking i my behalf sorry for wasting ur time lol cant believe i didnt see that, i looked at them so closely, must been looking to closely lol

*

Offline princec

  • *****
  • 1933
    • Puppygames
Beginner Q
« Reply #8 on: May 02, 2005, 12:20:24 »
No problem ;)

Cas :)