Search code examples
javaopenglrenderdrawcube

Java OpenGL Cube Not Drawing


I am trying to create a cube, because that is the thing to do in OpenGl 2.9. However, I had a cube rendering originally, but I am trying to make my design a little better, but I am unable to see my cube now. Help me see my cube. The code will be below. I am not getting any errors, just unable to see my cube.

import java.awt.*;
import org.lwjgl.*;
import javax.swing.*;

public class Scene
{
  private static boolean run = false;
  private static int width = 640;
  private static int height = 480;
  long lastFrame; //time at last frame
  int fps; 
  long lastFPS;
  /** position of quad */
  float x = 1, y = 1, z = 1; //400,300,400
  /** angle of quad rotation */
  float rotation = 0;

  public void start()
  {
    try
    {
      Display.setDisplayMode(new DisplayMode(width, height));
      Display.create();
    } catch (LWJGLException e)
    {
      e.printStackTrace();
      System.exit(0);
    }

    initGL(); // init OpenGL
    getDelta(); // call once before loop to initialise lastFrame
    lastFPS = getTime(); // call before loop to initialise fps timer

    while (run == true)
    { 
      int delta = getDelta();

      update(delta);
      renderGL();

      Display.update();
      Display.sync(60); // cap fps to 60fps

      if (Keyboard.isKeyDown(Keyboard.KEY_Q))
      {
        break;
      }
    }
    Display.destroy();
    System.exit(0);
  }

  public void update(int delta) {
    // rotate quad
    rotation += 0.15f * delta;

    if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;
    if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;

    if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta;
    if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y += 0.35f * delta;

    // keep quad on the screen
    if (x < 0) x = 0;
    if (x > 800) x = 800;
    if (y < 0) y = 0;
    if (y > 600) y = 600;

    updateFPS(); // update FPS Counter
  }

  public void renderGL() {
    // Clear The Screen And The Depth Buffer
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);

    GL11.glColor3f(1, 0, 0);

    // draw quad
    GL11.glPushMatrix();
      GL11.glTranslatef(x, y, z);
      GL11.glRotatef(rotation, 0f, 0f, 1f);
      GL11.glTranslatef(-x, -y, z);

      GL11.glBegin(GL11.GL_QUADS);
      GL11.glColor3f(0.0f,1.0f,0.0f);     // Set The Color To Green
      GL11.glVertex3f( x, y,-z);          // Top Right Of The Quad (Top)
      GL11.glVertex3f(-x, y,-z);          // Top Left Of The Quad (Top)
      GL11.glVertex3f(-x, y, z);          // Bottom Left Of The Quad (Top)
      GL11.glVertex3f( x, y, z);          // Bottom Right Of The Quad (Top)

      GL11.glColor3f(1.0f,0.5f,0.0f);     // Set The Color To Orange
      GL11.glVertex3f( x, -y, z);         // Top Right Of The Quad (Bottom)
      GL11.glVertex3f(-x, -y, z);         // Top Left Of The Quad (Bottom)
      GL11.glVertex3f(-x, -y,-z);         // Bottom Left Of The Quad (Bottom)
      GL11.glVertex3f( x,-y,-z);          // Bottom Right Of The Quad (Bottom)

      GL11.glColor3f(1.0f,0.0f,0.0f);     // Set The Color To Red
      GL11.glVertex3f( x, y, z); // Top Right Of The Quad (Front)
      GL11.glVertex3f(-x, y, z); // Top Left Of The Quad (Front)
      GL11.glVertex3f(-x,-y, z); // Bottom Left Of The Quad (Front)
      GL11.glVertex3f( x,-y, z); // Bottom Right Of The Quad (Front)

      GL11.glColor3f(1.0f,1.0f,0.0f);     // Set The Color To Yellow
      GL11.glVertex3f( x,-y,-z); // Bottom Left Of The Quad (Back)
      GL11.glVertex3f(-x,-y,-z); // Bottom Right Of The Quad (Back)
      GL11.glVertex3f(-x, y,-z); // Top Right Of The Quad (Back)
      GL11.glVertex3f( x, y,-z); // Top Left Of The Quad (Back)

      GL11.glColor3f(0.0f,0.0f,1.0f);     // Set The Color To Blue
      GL11.glVertex3f(-x, y, z); // Top Right Of The Quad (Left)
      GL11.glVertex3f(-x, y,-z); // Top Left Of The Quad (Left)
      GL11.glVertex3f(-x,-y,-z); // Bottom Left Of The Quad (Left)
      GL11.glVertex3f(-x,-y, z); // Bottom Right Of The Quad (Left)

      GL11.glColor3f(1.0f,0.0f,1.0f);     // Set The Color To Violet
      GL11.glVertex3f( x, y,-z); // Top Right Of The Quad (Right)
      GL11.glVertex3f( x, y, z); // Top Left Of The Quad (Right)
      GL11.glVertex3f( x,-y, z); // Bottom Left Of The Quad (Right)
      GL11.glVertex3f( x,-y,-z); // Bottom Right Of The Quad (Right)   

//        GL11.glVertex2f(x - 50, y - 50);
//        GL11.glVertex2f(x + 50, y - 50);
//        GL11.glVertex2f(x + 50, y + 50);
//        GL11.glVertex2f(x - 50, y + 50);

     GL11.glEnd();
    GL11.glPopMatrix();
  }
  /** 
   * Calculate how many milliseconds have passed 
   * since last frame.
   * 
   * @return milliseconds passed since last frame 
   */
  public int getDelta() {
      long time = getTime();
      int delta = (int) (time - lastFrame);
      lastFrame = time;

      return delta;
  }

  /**
   * Calculate the FPS and set it in the title bar
   */
  public void updateFPS() {
    if (getTime() - lastFPS > 1000) {
      Display.setTitle("FPS: " + fps);
      fps = 0;
      lastFPS += 1000;
    }
    fps++;
  }

  /**
   * Get the accurate system time
   * 
   * @return The system time in milliseconds
   */
  public long getTime() {
      return (Sys.getTime() * 1000) / Sys.getTimerResolution();
  }

  public void initGL() {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glLoadIdentity();
    GL11.glOrtho(0, 800, 0, 600, 1, -1);
    GL11.glMatrixMode(GL11.GL_MODELVIEW);
  }

  public static void main(String[] Args)
  {
    final JFrame info = new JFrame();
    JButton button = new JButton();
    JLabel infoText1 = new JLabel();
    JLabel infoText2 = new JLabel();
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();

    button.setText("Acknowledge");
    button.setSize(20, 10);
    infoText1.setText("Press Q to quit");
    infoText2.setText("Click Acknowledge to start simulation");
    info.setTitle("Operator's Manual");

    info.setLayout(new FlowLayout());
    info.add(infoText1);
    info.add(infoText2);
    info.setSize(240, 120);
    info.setLocation(dim.width/2-info.getSize().width/2, dim.height/2-info.getSize().height/2);
    info.add(button);
    info.setVisible(true);

    button.addActionListener(new java.awt.event.ActionListener()
      {
        @Override
        public void actionPerformed(java.awt.event.ActionEvent acknowledge)
        {
          run = true;
          Scene sceneStart = new Scene();
          sceneStart.start();
        }
      });
  }
}

Solution

  • EDIT: Your near and far values for glOrtho are backwards. Note that your later translate of 7 z-units will also move your object outside of the volume defined by glOrtho, which will cause it not to be drawn.

    You are undoing your own translate in your model view matrix.

        GL11.glTranslatef(1.5f, 0.0f, -7.0f);
        GL11.glLoadIdentity();   // << reloads identity matrix, overriding the translate.
    

    Correct is:

        // Ensure model view matrix is starting is nice fresh state.
        GL11.glLoadIdentity();
        // Put the quad where you want it.
        GL11.glTranslatef(1.5f, 0.0f, -7.0f);