java - LWJGL - 黑屏

标签 java opengl lwjgl

出于某种原因,当我尝试运行此代码时,我得到的只是黑屏。我怎样才能真正让它显示我想要显示的内容?

我正在使用 Eclipse,并且已将 lwjgl.jar 和 lwjgl_utils.jar 添加到构建路径中。

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.glu.GLU;
import org.lwjgl.input.Keyboard;

public class game {
 private boolean done = false;
 private boolean fullscreen = false;
 private final String windowTitle = "Lesson 1";
 private boolean f1 = false;
 private DisplayMode displayMode;

  /**
     * Everything starts and ends here.  Takes 1 optional command line argument.
     * If fullscreen is specified on the command line then fullscreen is used,
     * otherwise windowed mode will be used.
     * @param args command line arguments
     */

 public static void main(String args[]) {
  boolean fullscreen = false;
  if (args.length > 0) {
   if (args[0].equalsIgnoreCase("fullscreen")) {
    fullscreen = true;
   }
  }
  game gamez = new game();
  gamez.run(fullscreen);
 }

    /**
     * Launch point
     * @param fullscreen boolean value, set to true to run in fullscreen mode
     */
 public void run(boolean fullscreen) {
  this.fullscreen = fullscreen;
  try {
   init();
   while (!done) {
    mainloop();
    render();
    Display.update();
   }
   cleanup();
  } catch (Exception e) {
   e.printStackTrace();
   System.exit(0);
  }
 }

    /**
     * All updating is done here.  Key and mouse polling as well as window closing and
     * custom updates, such as AI.
     */
 private void mainloop() {
  if (Keyboard.isKeyDown(Keyboard.KEY_ESCAPE))
   done = true;
  if (Display.isCloseRequested())
   done = true;
  if (Keyboard.isKeyDown(Keyboard.KEY_F1)) {
   f1 = true;
   switchmode();
  }
  if (!Keyboard.isKeyDown(Keyboard.KEY_F1))
   f1 = false;
 }

 private void switchmode() {
  fullscreen = !fullscreen;
  try {
   Display.setFullscreen(fullscreen);
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

   /**
     * For rendering all objects to the screen
     * @return boolean for success or not
     */
 private void render() {
  GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
  GL11.glLoadIdentity();
  GL11.glTranslatef(-1.5f, 0.0f, -6.0f);
  GL11.glBegin(GL11.GL_TRIANGLES);
   GL11.glVertex3f(0.0f, 1.0f, 0.0f);
   GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
   GL11.glVertex3f(1.0f, -1.0f, 0.0f);
  GL11.glEnd();
  GL11.glTranslatef(3.0f, 0.0f, 0.0f);
  GL11.glBegin(GL11.GL_QUADS);
   GL11.glVertex3f(-1.0f, 1.0f, 0.0f);
   GL11.glVertex3f(1.0f, 1.0f, 0.0f);
   GL11.glVertex3f(1.0f, -1.0f, 0.0f);
   GL11.glVertex3f(-1.0f, -1.0f, 0.0f);
  GL11.glEnd();
 }

    /**
     * Create a window depending on whether fullscreen is selected
     * @throws Exception Throws the window.create() exception up the stack.
     */
 private void createWindow() throws Exception {
  Display.setFullscreen(fullscreen);
  DisplayMode d[] = Display.getAvailableDisplayModes();
  for (int i = 0; i < d.length; i++) {
   if (d[i].getWidth() == 640 && d[i].getHeight() == 480 && d[i].getBitsPerPixel() == 32) {
    displayMode = d[i];
    break;
   }
  }
  Display.setDisplayMode(displayMode);
  Display.setTitle(windowTitle);
  Display.create();
 }

    /**
     * Do all initilization code here.  Including Keyboard and OpenGL
     * @throws Exception Passes any exceptions up to the main loop to be handled
     */
 private void init() throws Exception {
  createWindow();
  initGL();
 }

    /**
     * Initialize OpenGL
     *
     */
 private void initGL() {
  GL11.glEnable(GL11.GL_TEXTURE_2D);
  GL11.glShadeModel(GL11.GL_SMOOTH);
  GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  GL11.glClearDepth(1.0);
  GL11.glEnable(GL11.GL_DEPTH_TEST);
  GL11.glDepthFunc(GL11.GL_EQUAL);

  GL11.glMatrixMode(GL11.GL_PROJECTION);
  GL11.glLoadIdentity();

  // calculate the apsect ratio of the window
  GLU.gluPerspective(45.0f, 
               (float)displayMode.getWidth() / (float)displayMode.getHeight(), 
               0.1f, 
               100.0f);
  GL11.glHint(GL11.GL_PERSPECTIVE_CORRECTION_HINT, GL11.GL_NICEST);
 }

 /**
  * Cleanup all the resources 
  * 
  */
 private void cleanup() {
  Display.destroy();
 }
}

最佳答案

在渲染中执行此 glColor3f(1,1,1) 看起来你的三角形是黑色的。

关于java - LWJGL - 黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4702304/

相关文章:

c++ - 如何让 OpenGL 在 OSX 上运行

java - LWJGL Mouse.getDX 始终返回 0

java - 默认为空白的 JSP 下拉列表

java - html5检查器编译

java - 跨项目的继承和可见性

java - OpenGL - 使用 GL11 的 OS X

Java - LWJGL - OS X 视网膜支持

java - 如何从绑定(bind)结果验证失败返回错误响应?

c++ - 用 0x 作为十六进制值的前缀

c - glClear 函数 : Question about the parameters