Java OpenGL多维数据集不绘图

时间:2014-11-06 02:36:02

标签: java opengl render draw cube

我正在尝试创建一个多维数据集,因为这是在OpenGl 2.9中要做的事情。但是,我最初有一个立方体渲染,但我试图让我的设计好一点,但我现在无法看到我的立方体。帮我看看我的立方体。代码如下。我没有收到任何错误,只是无法看到我的立方体。

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();
        }
      });
  }
}

1 个答案:

答案 0 :(得分:0)

编辑:glOrtho的近似值和远值都是向后的。请注意,您之后的7个z单位的平移也会将您的对象移动到glOrtho定义的体积之外,这将导致它不被绘制。

您正在模型视图矩阵中撤消自己的翻译。

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

正确的是:

    // 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);