LWJGL
May 25, 2013, 14:05:04 *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: LWJGL 2.9.0 released!
 
   Home   Help Search Login Register  



Pages: [1]
  Print  
Author Topic: Newbie problem with drawin 3d Cube  (Read 5294 times)
abenstex
Newbie
*
Posts: 6


« on: September 17, 2005, 08:59:59 »

Hello,

taking the following code:
Code:
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?
Logged
hvor
Newbie
*
Posts: 37



WWW
« Reply #1 on: September 19, 2005, 01: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.
Logged

Evil-Devil
Prolific Timewaster
****
Posts: 298



WWW
« Reply #2 on: September 20, 2005, 03: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.
Logged
abenstex
Newbie
*
Posts: 6


« Reply #3 on: September 20, 2005, 08: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...
Logged
CaseyB
Talks Too Much
***
Posts: 118



« Reply #4 on: September 20, 2005, 08: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
Logged
abenstex
Newbie
*
Posts: 6


« Reply #5 on: September 20, 2005, 12: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:
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:
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();
  }
}
Logged
Fool Running
Nerdus Imperius
*****
Posts: 742


« Reply #6 on: September 21, 2005, 06:25:27 »

Code:
GL11.glOrtho(0, dm.getWidth(), dm.getHeight(), 0, -1, 1);

That code is at least part of your problem.
Try this:
Code:
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: )
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
abenstex
Newbie
*
Posts: 6


« Reply #7 on: September 21, 2005, 09: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...
Logged
Fool Running
Nerdus Imperius
*****
Posts: 742


« Reply #8 on: September 23, 2005, 06:13:52 »

I noticed something else... Try putting:
Code:
GL11.glMatrixMode(GL11.GL_MODELVIEW);

after the GLU.gluPerspective() call.
Logged

Programmers will, one day, rule the world... and the world won't notice until its too late.Just testing the marquee option Grin
CaptainJester
Talks Too Much
***
Posts: 175



« Reply #9 on: September 23, 2005, 11: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
Logged

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)
Cool
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
SMFAds for Free Forums
Valid XHTML 1.0! Valid CSS!