无法弄清楚如何使用LWJGL 3中的投影

时间:2015-08-03 21:04:03

标签: scala opengl lwjgl

这是我的代码(应该使用着色器,我知道:)。

import java.nio.ByteBuffer

import org.lwjgl.glfw.Callbacks._
import org.lwjgl.glfw.GLFW._
import org.lwjgl.glfw._
import org.lwjgl.opengl.GL11._
import org.lwjgl.opengl._
import org.lwjgl.system.MemoryUtil._

object Main extends App {
  SharedLibraryLoader.load()

  private val errorCallback = new GLFWErrorCallback() {
    override def invoke(error: Int, description: Long): Unit = {
      errorCallbackPrint(System.err)
    }
  }

  private val keyCallback = new GLFWKeyCallback() {
    override def invoke(window: Long, key: Int, scancode: Int, action: Int, mods: Int): Unit = {
      if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
        glfwSetWindowShouldClose(window, GL_TRUE)
    }
  }

  private val resizeCallback = new GLFWWindowSizeCallback {
    override def invoke(window: Long, width: Int, height: Int): Unit = {
      glViewport(0, 0, width, height)
    }
  }

  try {
    val windowId = init()
    setupGeometry()
    loop(windowId)
    glfwDestroyWindow(windowId)
  } finally {
    keyCallback.release()
    errorCallback.release()
    glfwTerminate()
  }

  private def init(): Long = {
    glfwSetErrorCallback(errorCallback)
    if (glfwInit() != GL11.GL_TRUE)
      throw new IllegalStateException("glfwInit returned false")

    glfwDefaultWindowHints()
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE)

    val windowId = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL)
    if (windowId == NULL)
      throw new RuntimeException("glfwCreateWindow returned 0")

    glfwSetKeyCallback(windowId, keyCallback)
    glfwSetWindowSizeCallback(windowId, resizeCallback)

    glfwMakeContextCurrent(windowId)
    glfwSwapInterval(1)
    glfwShowWindow(windowId)

    GLContext.createFromCurrent()

    glEnable(GL_DEPTH_TEST | GL_DOUBLEBUFFER)
    glDepthFunc(GL_LESS)

    windowId
  }

  private def loop(windowId: Long): Unit = {
    glClearColor(0, 0, 0, 1)

    while (glfwWindowShouldClose(windowId) == GL_FALSE) {

      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

      glColor4f(1, 0, 0, 1)
      glBegin(GL_TRIANGLES)
        glVertex3d( 0,  1, -1)
        glVertex3d( 1, -1, -1)
        glVertex3d(-1, -1, -1)
      glEnd()
      glFlush()

      glfwSwapBuffers(windowId)
      glfwPollEvents()
    }
  }

  private def setupGeometry(): Unit = {
    val m = GLM.perspective(45.0f, 4.0f / 3.0f, 0.5f, 1000.0f)

    val buf = ByteBuffer.allocateDirect(16*4).asFloatBuffer()
    m.store(buf)
    buf.rewind()
    // buf = Vector(1.8106601, 0.0, 0.0, 0.0, 0.0, 2.4142134, 0.0, 0.0, 0.0, 0.0, -1.0010005, -1.0, 0.0, 0.0, -1.0005002, 1.0)
    println(s"buf = ${for(_ <- 1 to buf.capacity()) yield buf.get()}")
    buf.rewind()

    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    //glMultMatrixf(buf)

    glMatrixMode(GL_MODELVIEW)
    glLoadIdentity()
  }
}

如果我取消注释行glMultMatrixf(buf)三角形消失。但我很确定它只是在摄像机前面,在投射的近和远参数之间。我没有忘记启用z-buffer。我做错了什么?

GLM课程已从here复制粘贴,而ShardLibraryLoader则来自here

1 个答案:

答案 0 :(得分:0)

好的,我想出来了。首先,GLM.perspective方法中存在一个错误,因为根据C ++ glm库buf [15]应该是0而不是1.但即使你出于某种原因修复它也不会解决问题。显然,缓冲区和指针存在某种错误,我不知道。

所以我切换到了JOML库。它只是有效。