Hello Guest

Newbie problem with drawin 3d Cube

  • 9 Replies
  • 10443 Views
Newbie problem with drawin 3d Cube
« on: September 17, 2005, 15:59:59 »
Hello,

taking the following code:
Code: [Select]
public void draw()
  {
    GL11.glPushMatrix();
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
    GL11.glLoadIdentity();
    GL11.glTranslatef(x,y,-7.0f);
    //GL.glRotatef(angle, rotx, roty, rotz);
    GL11.glBegin(GL11.GL_QUADS);
    {
      //oben
      GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
      GL11.glVertex3f( 10.0f, 10.0f,-10.0f); // Top Right Of The Quad (Top)
      GL11.glVertex3f(-10.0f, 10.0f,-10.0f); // Top Left Of The Quad (Top)
      GL11.glVertex3f(-10.0f, 10.0f, 10.0f); // Bottom Left Of The Quad (Top)
      GL11.glVertex3f( 10.0f, 10.0f, 10.0f); // Bottom Right Of The Quad (Top)
     
      //vorne
      GL11.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
GL11.glVertex3f( 10.0f,-10.0f, 10.0f); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-10.0f,-10.0f, 10.0f); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-10.0f,-10.0f, -10.0f); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f( 10.0f,-10.0f, -10.0f); // Bottom Right Of The Quad (Bottom)
      //unten
      GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glVertex3f( 10.0f, 10.0f, 10.0f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-10.0f, 10.0f, 10.0f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-10.0f,-10.0f, 10.0f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( 10.0f,-10.0f, 10.0f); // Bottom Right Of The Quad (Front)
      //hinten
      GL11.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
GL11.glVertex3f( 10.0f,-10.0f,-10.0f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-10.0f,-10.0f,-10.0f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-10.0f, 10.0f,-10.0f); // Top Right Of The Quad (Back)
GL11.glVertex3f( 10.0f, 10.0f,-10.0f); // Top Left Of The Quad (Back)
     
      GL11.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
GL11.glVertex3f(-10.0f, 10.0f, 10.0f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-10.0f, 10.0f,-10.0f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-10.0f,-10.0f,-10.0f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-10.0f,-10.0f, 10.0f); // Bottom Right Of The Quad (Left)
     
      GL11.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
    GL11.glVertex3f( 10.0f, 10.0f,-10.0f); // Top Right Of The Quad (Right)
GL11.glVertex3f( 10.0f, 10.0f, 10.0f); // Top Left Of The Quad (Right)
GL11.glVertex3f( 10.0f,-10.0f, 10.0f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( 10.0f,-10.0f,-10.0f); // Bottom Right Of The Quad (Right)
         
    }
   
    GL11.glEnd();
    GL11.glPopMatrix();
  }
is there any mistake, because there is absolutely nothing drawn on the screen?

*

Offline hvor

  • *
  • 37
    • http://hvor.madpage.com
Newbie problem with drawin 3d Cube
« Reply #1 on: September 19, 2005, 08:52:16 »
Maybe your box is out of your sight? You translate -7 in z... Try draw box with smaller sides, not 10:

GL11.glVertex3f( 1.0f, 1.0f,-1.0f);

and see then.

Newbie problem with drawin 3d Cube
« Reply #2 on: September 20, 2005, 10:24:21 »
yea, side sizes of 10 at -7 depthview is really to big. The cubes curves are out of sight, as my preposter allready said.

Newbie problem with drawin 3d Cube
« Reply #3 on: September 20, 2005, 15:10:05 »
I changed the GL11.glVertex3f to GL11.glVertex3f(1.0f, 1.0f, 1.0f) but i still couldn't see any cube-like thing. Then i thought the moving just 1 to each side might create a cube that's simply hard to see so i multiplied all values by 5 but still nothing...

*

Offline CaseyB

  • ***
  • 118
Newbie problem with drawin 3d Cube
« Reply #4 on: September 20, 2005, 15:25:09 »
How are you changing the x and y values that you have in the draw()?  Are you using the Keyboard?  If so check for another key and have that adjust the z value so you can move the cube in and out.  If you leave x and y set at 0's for now you should be able to move the cube in and out and find the optimal viewing distance!

CaseyB

Newbie problem with drawin 3d Cube
« Reply #5 on: September 20, 2005, 19:03:54 »
I tried that too, i pressed the keys until the Z values was +/- around 1000 and i still couldn't see anything. I am going to paste the two classes here, amybe there is a mistake somewhere and i just can't see it:
Code: [Select]
package manager;

import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.opengl.*;

public class Main
{
 
  private boolean done = false;
  private Player player;
 
  public Main()
  {
  }

  public void render()
  {
    GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
   
    player.draw();
  }
 
  public void cleanUp()
  {
    Display.destroy();
    Keyboard.destroy();
  }

  private void initialize(DisplayMode dm)
  {
    try {
      Display.setDisplayMode(dm);
      Display.setFullscreen(false);
      Display.setTitle("Tutorial1");
      Display.create();
     
      GL11.glClearColor(1.5f, 0.0f, 1.0f, 0.0f);

      GL11.glEnable(GL11.GL_BLEND);
      GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
      GL11.glDisable(GL11.GL_DEPTH_TEST);
      GL11.glMatrixMode(GL11.GL_PROJECTION);
      GL11.glLoadIdentity();
      GL11.glOrtho(0, dm.getWidth(), dm.getHeight(), 0, -1, 1);
    }
    catch(LWJGLException e)
    {
      e.printStackTrace();
    }
  }

 
  public DisplayMode getDisplayMode(int width, int height, int depth)
  {
    try {
      DisplayMode[] modes = Display.getAvailableDisplayModes();
      for(int i=0; i<modes.length; i++)
      {
        if((modes[i].getWidth() == width) && (modes[i].getHeight() == height)
          && (modes[i].getBitsPerPixel()>=depth))
          {
            return modes[i];
          }
      }
    }
    catch(LWJGLException e)
    {
      e.printStackTrace();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    return null;
  }

  public void poll()
  {
    Keyboard.poll();
    if(Keyboard.isKeyDown(Keyboard.KEY_ESCAPE) || Display.isCloseRequested())
    {
      done = true;
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_LEFT))
    {
      player.setX(player.getX() - 0.5f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
    {
      player.setX(player.getX() + 0.5f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_UP))
    {
      player.setY(player.getY() - 1.0f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_DOWN))
    {
      player.setY(player.getY() + 1.0f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD9))
    {
      player.setX(player.getX() + 0.4f);
      player.setY(player.getY() - 0.5f);
   
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD7))
    {
      player.setX(player.getX() - 0.4f);
      player.setY(player.getY() - 0.5f);
     
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD3))
    {
      player.setX(player.getX() + 0.4f);
      player.setY(player.getY() + 0.5f);
     
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD1))
    {
      player.setX(player.getX() - 0.4f);
      player.setY(player.getY() + 0.5f);
     
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD2))
    {
      player.setY(player.getY() + 1.0f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD4))
    {
      player.setX(player.getX() - 0.5f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD6))
    {
      player.setX(player.getX() + 0.5f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_NUMPAD8))
    {
      player.setY(player.getY() - 1.0f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_W))
    {
      player.setZ(player.getZ() + 1.0f);
    }
    else if(Keyboard.isKeyDown(Keyboard.KEY_S))
    {
      player.setZ(player.getZ() - 1.0f);
    }
    player.draw();
  }

  public void mainLoop() throws Exception
  {
    Display.makeCurrent();
    player.setPosition(15.0f, 1.0f, -7.0f);
   
    while(!done)
    {
      poll();
      render();
      Display.update();
    }
   
  }

  public static void main(String[] args)
  {
    Main main = new Main();
    try {
      DisplayMode dm = main.getDisplayMode(640, 480, 32);
      main.player = new Player(dm);
      main.initialize(dm);
      main.mainLoop();
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
    finally
    {
      main.cleanUp();
    }
  }
}

Code: [Select]
package manager;

import org.lwjgl.opengl.*;
import org.lwjgl.util.*;
import org.lwjgl.opengl.glu.Sphere;

public class Player
{
 
  private float x, y, z, rotx, roty, rotz, angle;
  private DisplayMode dm;
 
  public Player(DisplayMode dm)
  {
    this.dm = dm;
  }
 
  public void setX(float x)
  {
    this.x = x;
   
    if(x >= dm.getWidth())
    {
      this.x = 1.0f;
    }
    else if(x == 0)
    {
      this.x = dm.getWidth();
    }
  }
 
  public float getX()
  {
    return x;
  }
 
  public void setY(float y)
  {
    this.y = y;
   
    if(y >= dm.getHeight())
    {
      this.y = 1.0f;
    }
    else if(y == 0)
    {
      this.y = dm.getHeight();
    }
  }
 
  public float getY()
  {
    return y;
  }
 
  public void setPosition(float x, float y)
  {
    this.x = x;
    this.y = y;
  }
 
  public void setPosition(float x, float y, float z)
  {
    this.x = x;
    this.y = y;
    this.z = z;
  }
 
  public void setRotX(float x)
  {
    rotx = x;
  }
 
  public float getRotX()
  {
    return rotx;
  }
 
  public void setZ(float z)
  {
    this.z = z;
  }
 
  public float getZ()
  {
    return z;
  }
 
  public void setRotY(float y)
  {
    roty = y;
  }
 
  public float getRotY()
  {
    return roty;
  }
 
  public void setRotZ(float z)
  {
    rotz = z;
  }
 
  public float getRotZ()
  {
    return rotz;
  }
 
  public void setRotAngle(float angle)
  {
    this.angle = angle;
  }
 
  public float getRotAngle()
  {
    return angle;
  }
 
  public void setInitialRotation(float angle, float x, float y, float z)
  {
    this.angle = angle;
    rotx = x;
    roty = y;
    rotz = z;
  }
 
  public void draw()
  {
    GL11.glPushMatrix();
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);    
    GL11.glLoadIdentity();
    GL11.glTranslatef(x,y,z);
   
    GL11.glBegin(GL11.GL_QUADS);
    {
      //oben
      GL11.glColor3f(0.0f,1.0f,0.0f); // Set The Color To Green
      GL11.glVertex3f( 10.0f, 10.0f,-1.0f); // Top Right Of The Quad (Top)
      GL11.glVertex3f(-10.0f, 10.0f,-1.0f); // Top Left Of The Quad (Top)
      GL11.glVertex3f(-10.0f, 10.0f, 1.0f); // Bottom Left Of The Quad (Top)
      GL11.glVertex3f( 10.0f, 10.0f, 1.0f); // Bottom Right Of The Quad (Top)
     
      //vorne
      GL11.glColor3f(1.0f,0.5f,0.0f); // Set The Color To Orange
GL11.glVertex3f( 10.0f,-10.0f, 1.0f); // Top Right Of The Quad (Bottom)
GL11.glVertex3f(-10.0f,-10.0f, 1.0f); // Top Left Of The Quad (Bottom)
GL11.glVertex3f(-10.0f,-10.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
GL11.glVertex3f( 10.0f,-10.0f, -1.0f); // Bottom Right Of The Quad (Bottom)
      //unten

      GL11.glColor3f(1.0f,0.0f,0.0f); // Set The Color To Red
GL11.glVertex3f( 10.0f, 10.0f, 1.0f); // Top Right Of The Quad (Front)
GL11.glVertex3f(-10.0f, 10.0f, 1.0f); // Top Left Of The Quad (Front)
GL11.glVertex3f(-10.0f,-10.0f, 1.0f); // Bottom Left Of The Quad (Front)
GL11.glVertex3f( 10.0f,-10.0f, 1.0f); // Bottom Right Of The Quad (Front)
     
      //hinten
      GL11.glColor3f(1.0f,1.0f,0.0f); // Set The Color To Yellow
GL11.glVertex3f( 10.0f,-10.0f,-1.0f); // Bottom Left Of The Quad (Back)
GL11.glVertex3f(-10.0f,-10.0f,-1.0f); // Bottom Right Of The Quad (Back)
GL11.glVertex3f(-10.0f, 10.0f,-1.0f); // Top Right Of The Quad (Back)
GL11.glVertex3f( 10.0f, 10.0f,-1.0f); // Top Left Of The Quad (Back)
     
      GL11.glColor3f(0.0f,0.0f,1.0f); // Set The Color To Blue
GL11.glVertex3f(-10.0f, 10.0f, 1.0f); // Top Right Of The Quad (Left)
GL11.glVertex3f(-10.0f, 10.0f,-1.0f); // Top Left Of The Quad (Left)
GL11.glVertex3f(-10.0f,-10.0f,-1.0f); // Bottom Left Of The Quad (Left)
GL11.glVertex3f(-10.0f,-10.0f, 1.0f); // Bottom Right Of The Quad (Left)
     
      GL11.glColor3f(1.0f,0.0f,1.0f); // Set The Color To Violet
    GL11.glVertex3f( 10.0f, 10.0f,-1.0f); // Top Right Of The Quad (Right)
GL11.glVertex3f( 10.0f, 10.0f, 1.0f); // Top Left Of The Quad (Right)
GL11.glVertex3f( 10.0f,-10.0f, 1.0f); // Bottom Left Of The Quad (Right)
GL11.glVertex3f( 10.0f,-10.0f,-1.0f); // Bottom Right Of The Quad (Right)
         
    }
   
    GL11.glEnd();
    GL11.glPopMatrix();
  }
}

hmmmmmmm...
« Reply #6 on: September 21, 2005, 13:25:27 »
Code: [Select]
GL11.glOrtho(0, dm.getWidth(), dm.getHeight(), 0, -1, 1);
That code is at least part of your problem.
Try this:
Code: [Select]
GLU.gluPerspective(45.0f, (float)dm.getWidth() / (float)dm.getHeight(), 0.1f, 1000.0f);
You were setting up an Ortho view with a z value from -1 to 1 (probably not enough to see the cube  :lol: )
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Newbie problem with drawin 3d Cube
« Reply #7 on: September 21, 2005, 16:00:41 »
Thanks! Of course that makes sense what you said, so i changed it but i'm still not successful. I tried the old code with GL11.glOrtho as well with changed values, but the same...

hmmmmmm...
« Reply #8 on: September 23, 2005, 13:13:52 »
I noticed something else... Try putting:
Code: [Select]
GL11.glMatrixMode(GL11.GL_MODELVIEW);
after the GLU.gluPerspective() call.
Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option ;D

Newbie problem with drawin 3d Cube
« Reply #9 on: September 23, 2005, 18:45:12 »
Also change the cube sides to between 0.9f and -0.9f on the z coordinates.

ie.
GL11.glVertex3f( 10.0f,-10.0f,-0.9f);         // Bottom Right Of The Quad
The problems of this world cannot possibly be solved by skeptics or cynics whose horizons are limited by the obvious realities.  We need men and women who can dream of things that never were. - John Fitzgerald Kennedy(35th US President)
8)