三角形未在 OSX 上的 OpenGL 2.1 中绘制

2024-04-29

我正在学习有关使用 OpenGL 在 Java 中创建游戏引擎的教程。

我正在尝试在屏幕上渲染一个三角形。一切运行良好,我可以更改背景颜色,但三角形不会显示。我还尝试运行作为教程系列的一部分提供的代码,但它仍然不起作用。

教程链接:http://bit.ly/1EUnvz4 http://bit.ly/1EUnvz4

视频中使用的代码链接:http://bit.ly/1z7XULE http://bit.ly/1z7XUlE

Setup

  • 我尝试检查 OpenGL 版本,相信我有 2.1。
  • Mac OSX
  • Java-Eclipse

网格.java

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;

public class Mesh
{
    private int vbo;    //pointer to the buffer
    private int size;   //size of the data to buffer

    public Mesh ()
    {
        vbo = glGenBuffers();
        size = 0;
    }

    public void addVertices (Vertex[] vertices)
    {
        size = vertices.length;

        //add the data by first binding the buffer
        glBindBuffer (GL_ARRAY_BUFFER, vbo);    //vbo is now the buffer
        //and then buffering the data
        glBufferData (GL_ARRAY_BUFFER, Util.createFlippedBuffer(vertices), GL_STATIC_DRAW);
    }

    public void draw ()
    {
        glEnableVertexAttribArray (0);  //divide up the data into a segment

        glBindBuffer (GL_ARRAY_BUFFER, vbo);    //vbo is now the buffer
        //tell OpenGL more about the segment:
        //segment = 0, elements = 3, type = float, normalize? = false, vertex size, where to start = 0)
        glVertexAttribPointer(0, 3, GL_FLOAT, false, Vertex.SIZE * 4, 0);

        //draw GL_TRIANGLES starting from '0' with a given 'size'
        glDrawArrays (GL_TRIANGLES, 0, size);

        glDisableVertexAttribArray (0);
    }
}

RenderUtil.java

import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL30.*;

public class RenderUtil
{
    public static void clearScreen ()
    {
        //TODO: Stencil Buffer
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    }

//set everything to engine defaults
public static void initGraphics ()
{
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);   // default color

    glFrontFace(GL_CW);         // direction for visible faces
    glCullFace(GL_BACK);        // direction for back faces
    glEnable (GL_CULL_FACE);    // don't draw back faces
    glEnable (GL_DEPTH_TEST);   // determines draw order by pixel depth testing

    //TODO: Depth clamp for later

    glEnable (GL_FRAMEBUFFER_SRGB); // do exponential correction on gamma so we don't have to
}
}

实用工具.java

import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;

public class Util
{
    //create a float buffer (we need this because java is weird)
    public static FloatBuffer createFloatBuffer (int size)
    {
        return BufferUtils.createFloatBuffer(size);
    }

    //flip the buffer to fit what OpenGL expects
    public static FloatBuffer createFlippedBuffer (Vertex[] vertices)
    {
        FloatBuffer buffer = createFloatBuffer(vertices.length * Vertex.SIZE);

        for (int i = 0; i < vertices.length; i++)
        {
            buffer.put(vertices[i].getPos().getX());
            buffer.put(vertices[i].getPos().getY());
            buffer.put(vertices[i].getPos().getZ());
        }

        buffer.flip();

        return buffer;
    }
}

您正在使用传统 OpenGL 和现代 OpenGL 的无效组合。

The glVertexAttribPointer() and glEnableVertexAttribArray()您调用的函数用于设置generic顶点属性。这是在当前版本的 OpenGL(桌面 OpenGL 的核心配置文件,或 OpenGL ES 2.0 及更高版本)中设置顶点属性的唯一方法。它们也可以在旧版本的 OpenGL 中使用,但只能与提供在 GLSL 中实现的您自己的着色器结合使用。

如果您刚刚开始,您最好的选择可能是坚持现有的,并研究如何开始实现您自己的着色器。如果您想让代码与旧版固定管道一起工作(仅在 OpenGL 兼容性配置文件中受支持),您需要使用glVertexPointer() and glEnableClientState()函数代替。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

三角形未在 OSX 上的 OpenGL 2.1 中绘制 的相关文章

随机推荐