我正在创建一个以三角形形式显示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 + "]"
}
您需要创建并绑定一个程序对象,该程序对象告诉OpenGL如何渲染数据。
此页面可能会有所帮助。
设置程序后可能会导致问题的另一件事是,顶点缓冲区中的每个坐标都有两个组件,但在VAO中指定它有3个组件。
由于您提到您没有使用着色器,因此我随意更正了几行代码。
最重要的是,当您不使用着色器时,不应该使用glVertexAttribPointer (...)
。你很幸运有了attrib。指针0保证等价于glVertexPointer (...)
,但这种行为对任何其他attrib指针都不可移植。我昨天甚至回答了一个关于这个的问题。
private val VBO: Int = GL15.glGenBuffers()
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, VBO)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertexBuffer, GL15.GL_STATIC_DRAW)
GL11.glVertexPointer(3, GL11.GL_FLOAT, 0, 0)
GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY)
GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, colorVbo)
GL15.glBufferData(GL15.GL_ARRAY_BUFFER, colorBuffer, GL15.GL_STATIC_DRAW)
GL11.glColorPointer(3, GL11.GL_FLOAT, 0, 0)
GL11.glEnableClientState(GL11.GL_COLOR_ARRAY)
这个应该工作,我不确定某些令牌的GL11
版本是否正确,但它应该让您大致了解需要做什么。
此外,如果第三个坐标总是0,则使用三维顶点位置不需要浪费内存。OpenGL会将2D顶点位置的数组解释为:(x,y,0.0,1.0)。这样可以节省一点内存。使数据为2D,并将glVertexPointer (...)
中的大小更改为2。