저는이 OpenGL lwjgl에 대해 비교적 새로운데, 제 코드를 철저히 검사했습니다.하지만 두 개의 코드를 넣었음에도 불구하고 하나의 삼각형 만 그려져 있습니다. 요점을 증명하기 위해 6 점이 그려집니다 GL_POINTS와 함께.OpenGL 단 하나의 삼각형이 그려집니다
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()
}
}
죄송합니다. GL_QUADS가 아니며 GL_TRIANGLES입니다. 어느 쪽이든 – user7868086