LWJGL 三角形不显示 kotlin
LWJGL triangle not showing kotlin
我试图用 LWJGL 在 kotlin 中制作三角形着色器,但由于某种原因它不起作用而且我没有收到任何错误。调试它真的很难,因为即使我故意在着色器代码中写错了一些东西,我仍然没有收到任何错误,所以我什至不知道这段代码有什么问题。
三角形class代码:
class Triangle {
private var vertexShaderCode: String = "" +
"attribute vec4 vPosition; \n" +
"void main() { \n" +
" gl_Position = vPosition; \n" +
"}"
private var fragmentShaderCode: String = "" +
"precision mediump float; \n" +
"uniform vec4 vColor; \n" +
"void main() { \n" +
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
"}"
private lateinit var vertexBuffer: FloatBuffer
private val COORDS_PER_VERTEX: Int = 3
private val triangleCoords = floatArrayOf(
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f
)
private val color = floatArrayOf(1.0f, 0.0f, 0.0f, 1.0f)
private val mProgram: Int
var VBO: Int
var VAO: Int
init {
VBO = glGenBuffers()
VAO = glGenVertexArrays()
glBindVertexArray(0)
val vertexShader: Int = Engine().loadShader(GL_VERTEX_SHADER, vertexShaderCode)
val fragmentShader: Int = Engine().loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode)
mProgram = glCreateProgram()
glAttachShader(mProgram, vertexShader)
glAttachShader(mProgram, fragmentShader)
glLinkProgram(mProgram)
}
fun draw() {
glUseProgram(mProgram)
VBO = glGenBuffers()
VAO = glGenVertexArrays()
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
GL15.glBufferData(GL_ARRAY_BUFFER, triangleCoords, GL_STATIC_DRAW)
GL20.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0)
glBindVertexArray(VAO)
glDrawArrays(GL_TRIANGLES, 0, 3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
}
}
申请代码:
class Engine {
private var window: Long = 0
fun run() {
println("Hello LWJGL " + Version.getVersion() + "!")
init()
loop()
glfwFreeCallbacks(window)
glfwDestroyWindow(window)
glfwTerminate()
glfwSetErrorCallback(null)?.free()
}
private fun init() {
GLFWErrorCallback.createPrint(System.err).set()
if(!glfwInit())
throw IllegalStateException("Unable to initialize GLFW!")
glfwDefaultWindowHints()
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0)
window = glfwCreateWindow(600, 800, "Hello World!", NULL, NULL)
if(window==NULL)
throw RuntimeException("Failed to create the GLFW window!")
stackPush().use { stack ->
val pWidth = stack.mallocInt(1) // int*
val pHeight = stack.mallocInt(1) // int*
// Get the window size passed to glfwCreateWindow
glfwGetWindowSize(window, pWidth, pHeight)
// Get the resolution of the primary monitor
val vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor())
// Center the window
glfwSetWindowPos(
window,
(vidmode!!.width() - pWidth[0]) / 2,
(vidmode.height() - pHeight[0]) / 2
)
}
glfwMakeContextCurrent(window)
glfwSwapInterval(1)
glfwShowWindow(window)
}
private fun loop(){
GL.createCapabilities()
var triangle = Triangle1()
glClearColor(1.0f, 1.0f, 1.0f, 1.0f)
while(!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
triangle.draw()
glfwSwapBuffers(window)
glfwPollEvents()
}
}
fun loadShader(type: Int, shaderCode: String): Int {
val shader = glCreateShader(type)
glShaderSource(shader, shaderCode)
glCompileShader(shader)
return shader
}
}
好的,这段代码有很多问题。我的问题下的评论真的很有帮助,谢谢!如果有人有类似的问题,我 post 修复了这段代码的版本。
class Triangle {
private var vertexShaderCode: String = "" +
"#version 330\n" +
"attribute vec4 vPosition; \n" +
"void main() { \n" +
" gl_Position = vPosition; \n" +
"}"
private var fragmentShaderCode: String = "" +
"#version 330 \n" +
"precision mediump float; \n" +
"uniform vec4 vColor; \n" +
"void main() { \n" +
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
"}"
private val COORDS_PER_VERTEX: Int = 3
private val triangleCoords = floatArrayOf(
+0.0f, +0.8f, // Top coordinate
-0.8f, -0.8f, // Bottom-left coordinate
+0.8f, -0.8f // Bottom-right coordinate
)
private var coordsBuffer: FloatBuffer = BufferUtils.createFloatBuffer(triangleCoords.size)
private val color = floatArrayOf(1.0f, 0.0f, 0.0f, 1.0f)
private val mProgram: Int
var VBO: Int
var VAO: Int
var vertexShader: Int
var fragmentShader: Int
init {
coordsBuffer.put(triangleCoords).flip()
VAO = glGenVertexArrays()
glBindVertexArray(VAO)
VBO = glGenBuffers()
glBindBuffer(GL_ARRAY_BUFFER, VBO)
GL15.glBufferData(GL_ARRAY_BUFFER, coordsBuffer, GL_STATIC_DRAW)
GL20.glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0)
vertexShader = Engine().loadShader(GL_VERTEX_SHADER, vertexShaderCode)
fragmentShader = Engine().loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode)
mProgram = glCreateProgram()
glAttachShader(mProgram, vertexShader)
glAttachShader(mProgram, fragmentShader)
glLinkProgram(mProgram)
if (glGetProgrami(mProgram, GL_LINK_STATUS) == GL_FALSE)
throw RuntimeException("Unable to link shader program:")
}
fun draw() {
glUseProgram(mProgram)
glBindVertexArray(VAO)
glEnableVertexAttribArray(0)
glDrawArrays(GL_TRIANGLES, 0, 3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
glUseProgram(0)
}
我试图用 LWJGL 在 kotlin 中制作三角形着色器,但由于某种原因它不起作用而且我没有收到任何错误。调试它真的很难,因为即使我故意在着色器代码中写错了一些东西,我仍然没有收到任何错误,所以我什至不知道这段代码有什么问题。
三角形class代码:
class Triangle {
private var vertexShaderCode: String = "" +
"attribute vec4 vPosition; \n" +
"void main() { \n" +
" gl_Position = vPosition; \n" +
"}"
private var fragmentShaderCode: String = "" +
"precision mediump float; \n" +
"uniform vec4 vColor; \n" +
"void main() { \n" +
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
"}"
private lateinit var vertexBuffer: FloatBuffer
private val COORDS_PER_VERTEX: Int = 3
private val triangleCoords = floatArrayOf(
0.0f, 0.0f, 0.0f,
0.0f, 1.0f, 0.0f,
1.0f, 0.0f, 0.0f
)
private val color = floatArrayOf(1.0f, 0.0f, 0.0f, 1.0f)
private val mProgram: Int
var VBO: Int
var VAO: Int
init {
VBO = glGenBuffers()
VAO = glGenVertexArrays()
glBindVertexArray(0)
val vertexShader: Int = Engine().loadShader(GL_VERTEX_SHADER, vertexShaderCode)
val fragmentShader: Int = Engine().loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode)
mProgram = glCreateProgram()
glAttachShader(mProgram, vertexShader)
glAttachShader(mProgram, fragmentShader)
glLinkProgram(mProgram)
}
fun draw() {
glUseProgram(mProgram)
VBO = glGenBuffers()
VAO = glGenVertexArrays()
glBindVertexArray(VAO)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
GL15.glBufferData(GL_ARRAY_BUFFER, triangleCoords, GL_STATIC_DRAW)
GL20.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0)
glBindVertexArray(VAO)
glDrawArrays(GL_TRIANGLES, 0, 3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
}
}
申请代码:
class Engine {
private var window: Long = 0
fun run() {
println("Hello LWJGL " + Version.getVersion() + "!")
init()
loop()
glfwFreeCallbacks(window)
glfwDestroyWindow(window)
glfwTerminate()
glfwSetErrorCallback(null)?.free()
}
private fun init() {
GLFWErrorCallback.createPrint(System.err).set()
if(!glfwInit())
throw IllegalStateException("Unable to initialize GLFW!")
glfwDefaultWindowHints()
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE)
glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3)
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0)
window = glfwCreateWindow(600, 800, "Hello World!", NULL, NULL)
if(window==NULL)
throw RuntimeException("Failed to create the GLFW window!")
stackPush().use { stack ->
val pWidth = stack.mallocInt(1) // int*
val pHeight = stack.mallocInt(1) // int*
// Get the window size passed to glfwCreateWindow
glfwGetWindowSize(window, pWidth, pHeight)
// Get the resolution of the primary monitor
val vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor())
// Center the window
glfwSetWindowPos(
window,
(vidmode!!.width() - pWidth[0]) / 2,
(vidmode.height() - pHeight[0]) / 2
)
}
glfwMakeContextCurrent(window)
glfwSwapInterval(1)
glfwShowWindow(window)
}
private fun loop(){
GL.createCapabilities()
var triangle = Triangle1()
glClearColor(1.0f, 1.0f, 1.0f, 1.0f)
while(!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
triangle.draw()
glfwSwapBuffers(window)
glfwPollEvents()
}
}
fun loadShader(type: Int, shaderCode: String): Int {
val shader = glCreateShader(type)
glShaderSource(shader, shaderCode)
glCompileShader(shader)
return shader
}
}
好的,这段代码有很多问题。我的问题下的评论真的很有帮助,谢谢!如果有人有类似的问题,我 post 修复了这段代码的版本。
class Triangle {
private var vertexShaderCode: String = "" +
"#version 330\n" +
"attribute vec4 vPosition; \n" +
"void main() { \n" +
" gl_Position = vPosition; \n" +
"}"
private var fragmentShaderCode: String = "" +
"#version 330 \n" +
"precision mediump float; \n" +
"uniform vec4 vColor; \n" +
"void main() { \n" +
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); \n" +
"}"
private val COORDS_PER_VERTEX: Int = 3
private val triangleCoords = floatArrayOf(
+0.0f, +0.8f, // Top coordinate
-0.8f, -0.8f, // Bottom-left coordinate
+0.8f, -0.8f // Bottom-right coordinate
)
private var coordsBuffer: FloatBuffer = BufferUtils.createFloatBuffer(triangleCoords.size)
private val color = floatArrayOf(1.0f, 0.0f, 0.0f, 1.0f)
private val mProgram: Int
var VBO: Int
var VAO: Int
var vertexShader: Int
var fragmentShader: Int
init {
coordsBuffer.put(triangleCoords).flip()
VAO = glGenVertexArrays()
glBindVertexArray(VAO)
VBO = glGenBuffers()
glBindBuffer(GL_ARRAY_BUFFER, VBO)
GL15.glBufferData(GL_ARRAY_BUFFER, coordsBuffer, GL_STATIC_DRAW)
GL20.glVertexAttribPointer(0, 2, GL_FLOAT, false, 0, 0)
vertexShader = Engine().loadShader(GL_VERTEX_SHADER, vertexShaderCode)
fragmentShader = Engine().loadShader(GL_FRAGMENT_SHADER, fragmentShaderCode)
mProgram = glCreateProgram()
glAttachShader(mProgram, vertexShader)
glAttachShader(mProgram, fragmentShader)
glLinkProgram(mProgram)
if (glGetProgrami(mProgram, GL_LINK_STATUS) == GL_FALSE)
throw RuntimeException("Unable to link shader program:")
}
fun draw() {
glUseProgram(mProgram)
glBindVertexArray(VAO)
glEnableVertexAttribArray(0)
glDrawArrays(GL_TRIANGLES, 0, 3)
glDisableVertexAttribArray(0)
glBindVertexArray(0)
glUseProgram(0)
}