OpenGL只绘制了一个三角形

时间:2017-04-14 15:47:23

标签: java scala opengl lwjgl

我对这个opengl lwjgl的东西比较新,并且已经彻底检查了我的代码,但是只绘制了一个三角形,尽管我输入了2个坐标。为了证明我的观点,用GL_POINTS绘制了6个点。

Main.scala

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

class Main {

  import org.lwjgl.glfw.GLFWErrorCallback
  import org.lwjgl.opengl.GL

  private var window = 0L

  def run(): Unit = {
    System.out.println("Version: " + Version.getVersion + "!")
    init()
    run_loop()
    glfwFreeCallbacks(window)
    glfwDestroyWindow(window)
    glfwTerminate()
    glfwSetErrorCallback(null).free()
  }

  private def init() = {

    GLFWErrorCallback.createPrint(System.err).set

    if (!glfwInit) throw new IllegalStateException("Unable to initialize GLFW")

    glfwDefaultWindowHints()

    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)

    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)

    window = glfwCreateWindow(640, 480, "WINDOW", 0, 0)
    if (window == NULL) throw new RuntimeException("Failed to create the GLFW window")
    glfwMakeContextCurrent(window)
    glfwSwapInterval(1)
    glfwShowWindow(window)
  }

  def run_loop() {

    GL.createCapabilities

    glEnable(GL_TEXTURE_2D)

val vertices: Array[Float] = Array(

  -0.5f, 0.5f, 0, //TOP LEFT
  0.5f, 0.5f, 0,  //TOP RIGHT
  0.5f, -0.5f, 0, //BOTTOM RIGHT

  -0.5f, 0.5f, 0, //BOTTOM RIGHT
  -0.5f, -0.5f, 0,//BOTTOM LEFT
  -0.5f, 0.5f, 0  //TOP LEFT

)

val model: Model = Model(vertices)

while (!glfwWindowShouldClose(window)) {
  glfwPollEvents()

  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

  if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GL_TRUE) { glfwSetWindowShouldClose(window, true) }

  model.render()

  glfwSwapBuffers(window)
}
  }
}

object Main {
  def main(args: Array[String]): Unit = {
    val main: Main = new Main
    main.run()
  }
}

Model.scala

import java.nio.FloatBuffer

import org.lwjgl.BufferUtils
import org.lwjgl.opengl.GL11._
import org.lwjgl.opengl.GL15._

case class Model (vertices: Array[Float]){

  private val draw_count: Int = vertices.length / 3
  private val v_id: Int = glGenBuffers

  val buffer: FloatBuffer = BufferUtils.createFloatBuffer(vertices.length)
  buffer.put(vertices)
  buffer.flip()

  glBindBuffer(GL_ARRAY_BUFFER, v_id)
  glBufferData(GL_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW)
  glBindBuffer(GL_ARRAY_BUFFER, 0)

  def render(): Unit = {

    glEnableClientState(GL_VERTEX_ARRAY)
    glBindBuffer(GL_ARRAY_BUFFER, v_id)
    glVertexPointer(3, GL_FLOAT, 0, 0)
    glDrawArrays(GL_QUADS, 0, draw_count)
    glDrawArrays(GL_POINTS, 0, draw_count)
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    glDisableClientState(GL_VERTEX_ARRAY)
    glPopMatrix()

  }

}

My Screenshot

1 个答案:

答案 0 :(得分:0)

您的vertices不正确。如果0.5f, -0.5f位于右下角,那么-0.5f, 0.5f如何才能在右下角?

val vertices: Array[Float] = Array(
    -0.5f,  0.5f, 0, // TOP LEFT
     0.5f,  0.5f, 0, // TOP RIGHT
     0.5f, -0.5f, 0, // BOTTOM RIGHT

     0.5f, -0.5f, 0, // BOTTOM RIGHT
    -0.5f, -0.5f, 0, // BOTTOM LEFT
    -0.5f,  0.5f, 0  // TOP LEFT
)

您还可以使用GL_TRIANGLE_STRIP以及:

val vertices: Array[Float] = Array(
    -0.5f,  0.5f, 0, // TOP LEFT
     0.5f,  0.5f, 0, // TOP RIGHT
    -0.5f, -0.5f, 0, // BOTTOM LEFT
     0.5f, -0.5f, 0, // BOTTOM RIGHT
)

或翻转最后两个顶点,并使用GL_TRIANGLE_FAN

另请注意,对于每个glPushMatrix(),必须有glPopMatrix()。由于我发现您有glPopMatrix()但没有glPushMatrix()