我正在创建一个以三角形形式显示 OpenGL 基元的应用程序,但由于某种原因,该应用程序实际上不会将图形绘制到上下文中。
问题是三角形不会从黑色改变它们的颜色。我可以将背景颜色设置为白色并查看三角形,但尽管我输入了什么,但我无法更改它们的颜色。
我的主要课程:
package tests
import scala.collection.mutable.HashMap
import org.lwjgl.util.vector.Vector2f
import zeus.core.Color
import zeus.core.Window
import zeus.core.geom.Polygon
import zeus.core.maths.Delta
import scala.collection.mutable.LinkedHashMap
object DrawingTest {
def main(args: Array[String]) : Unit = {
val win: Window = new Window(800, 600, "Drawing Test")
val deltaObj: Delta = new Delta()
val info: LinkedHashMap[Vector2f, Color] = new LinkedHashMap[Vector2f, Color]
info.put(new Vector2f(0f, 0f), new Color(1.0f, 1.0f, 1.0f, 1.0f))
info.put(new Vector2f(1f, 0f), new Color(1.0f, 1.0f, 1.0f, 1.0f))
info.put(new Vector2f(1f, 1f), new Color(1.0f, 1.0f, 1.0f, 1.0f))
info.put(new Vector2f(1f, 1f), new Color(1.0f, 1.0f, 1.0f, 1.0f))
info.put(new Vector2f(0f, 1f), new Color(1.0f, 1.0f, 1.0f, 1.0f))
info.put(new Vector2f(0f, 0f), new Color(1.0f, 1.0f, 1.0f, 1.0f))
win.create()
val p: Polygon = new Polygon(info)
while(!win.getIsCloseRequested()) {
win.clear()
val delta: Int = deltaObj.getDelta()
p.update(delta)
p.draw()
deltaObj.updateFps(true)
win.update(120)
}
p.dispose();
win.dispose()
}
}
我的多边形类:
package zeus.core.geom
import zeus.core.traits.Disposable
import org.lwjgl.util.vector.Vector2f
import zeus.core.Color
import scala.collection.mutable.HashMap
import org.lwjgl.BufferUtils
import java.util.ArrayList
import scala.collection.JavaConversions._
import org.lwjgl.opengl.GL30
import org.lwjgl.opengl.GL20
import org.lwjgl.opengl.GL15
import org.lwjgl.opengl.GL11
import scala.collection.mutable.LinkedHashMap
class Polygon(INFO: LinkedHashMap[Vector2f, Color]) extends Disposable {
private var colorVbo = 0
private val colorList: ArrayList[Float] = new ArrayList[Float]
private val vertiList: ArrayList[Float] = new ArrayList[Float]
INFO.foreach(i => {
vertiList.add(i._1.getX)
vertiList.add(i._1.getY)
vertiList.add(0f)
colorList.add(i._2.getRed)
colorList.add(i._2.getGreen)
colorList.add(i._2.getBlue)
colorList.add(i._2.getAlpha)
})
/**
* Vertex Buffer
*/
private val vertexBuffer: java.nio.FloatBuffer = BufferUtils.createFloatBuffer(vertiList.length);
vertexBuffer.put({
var a: Array[Float] = new Array[Float](vertiList.size())
var i = 0;
for(f: Float <- vertiList) {
a(i) = f
i += 1
}
a
})
vertexBuffer.flip();
/**
* VAO
*/
private val VAO: Int = GL30.glGenVertexArrays()
GL30.glBindVertexArray(VAO)
/**
* VBO
*/
private val VBO: Int = GL15.glGenBuffers()
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBO)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW)
GL20.glEnableVertexAttribArray(0)
GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 0, 0)
GL30.glBindVertexArray(VAO)
/**
* Color VBO
*/
val colorBuffer: java.nio.FloatBuffer = BufferUtils.createFloatBuffer(colorList.length)
colorBuffer.put({
var a: Array[Float] = new Array[Float](colorList.size())
var i = 0;
for(f: Float <- colorList) {
a(i) = f
i += 1
}
a
})
colorBuffer.flip()
colorVbo = GL15.glGenBuffers()
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, colorVbo)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorBuffer, GL15.GL_STATIC_DRAW)
GL20.glVertexAttribPointer(1, 4, GL11.GL_FLOAT, false, 0, 0)
GL20.glEnableVertexAttribArray(1)
def update(delta: Int) : Unit = {
}
def draw() : Unit = {
GL30.glBindVertexArray(VAO)
GL11.glDrawArrays(GL11.GL_TRIANGLES, 0, vertiList.length)
GL30.glBindVertexArray(0)
}
override def dispose() : Unit = {
println("Destroying polygon with VAO of : " + VAO)
GL20.glDisableVertexAttribArray(0)
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0)
GL15.glDeleteBuffers(VBO)
GL30.glBindVertexArray(0)
GL30.glDeleteVertexArrays(VAO)
GL15.glDeleteBuffers(colorVbo)
}
def getVAO() : Int = return VAO
def getVBO() : Int = return VBO
def getVertexBuffer() : java.nio.FloatBuffer = return vertexBuffer
def getColorBuffer() : java.nio.FloatBuffer = return colorBuffer
}
颜色类:
package zeus.core
class Color(R: Float, G: Float, B: Float, A: Float) {
private var r: Float = R
private var g: Float = G
private var b: Float = B
private var a: Float = A
def getRed() : Float = r
def getGreen() : Float = g
def getBlue() : Float = b
def getAlpha() : Float = a
def setRed(to: Float) : Unit = r = to
def setGreen(to: Float) : Unit = g = to
def setBlue(to: Float) : Unit = b = to
def setAlpha(to: Float) : Unit = a = to
override def toString() : String = "Color[" + r + ", " + g + ", " + b + ", " + a + "]"
}