0

我正在创建一个以三角形形式显示 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 + "]"

}
4

2 回答 2

0

你需要创建和绑定一个程序对象,它告诉 OpenGL 如何渲染你的数据。

此页面可能会有所帮助。

设置程序后可能导致问题的另一件事是顶点缓冲区中的每个坐标都有两个分量,但您在 VAO 中指定它有 3 个分量。

于 2013-12-14T18:03:45.970 回答
0

因为您提到您没有使用着色器,所以我冒昧地更正了您的几行代码。

最重要的是,您不应该glVertexAttribPointer (...)在不使用着色器时使用。你对attrib非常幸运。指针0保证等同于,glVertexPointer (...)但此行为不适用于任何其他属性指针。昨天我什至回答了一个关于这个的问题。

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,则无需使用 3D 顶点位置浪费内存。OpenGL 会将二维顶点位置数组解释为:(x, y, 0.0, 1.0)。这样可以节省一点内存。将数据设为 2D 并将大小更改glVertexPointer (...)2

于 2013-12-14T22:02:23.307 回答