gpt4 book ai didi

java - 三角形不在 OSX 上的 OpenGL 2.1 中绘制

转载 作者:太空宇宙 更新时间:2023-11-04 13:09:34 25 4
gpt4 key购买 nike

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

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

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

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

设置

  • 我已经尝试检查 OpenGL 版本并相信我有 2.1。
  • Mac 操作系统
  • Java - Eclipse

Mesh.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
}
}

Util.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 的无效组合。

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

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

关于java - 三角形不在 OSX 上的 OpenGL 2.1 中绘制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40548000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com