三角形不在OSX上的OpenGL 2.1中绘制 [英] Triangle not drawing in OpenGL 2.1 on OSX
问题描述
我正在学习使用OpenGL在Java中创建Game Engine的教程。
我试图在屏幕上渲染一个三角形。一切运行良好,我可以改变背景颜色,但三角形不会显示。我也尝试运行作为教程系列的一部分提供的代码,但仍然无法运行。
链接到本教程: http://bit.ly/1EUnvz4
链接到视频中使用的代码: http://bit.ly/1z7XUlE
安装程序
- 我尝试过检查OpenGL版本,并且我有2.1。
- Mac OSX
- Java - Eclipse
Mesh.java
import static org.lwjgl.opengl.GL11。*;
import static org.lwjgl.opengl.GL15。*;
import static org.lwjgl.opengl.GL20。*;
公共类网格
{
private int vbo; //指向缓冲区的指针
private int size; //缓冲数据的大小
$ b $ public Mesh()
{
vbo = glGenBuffers();
size = 0;
}
public void addVertices(Vertex [] vertices)
{
size = vertices.length;
//通过首先绑定缓冲区来添加数据
glBindBuffer(GL_ARRAY_BUFFER,vbo); // vbo现在是缓冲区
//然后缓冲数据
glBufferData(GL_ARRAY_BUFFER,Util.createFlippedBuffer(vertices),GL_STATIC_DRAW);
}
public void draw()
{
glEnableVertexAttribArray(0); //将数据分成一个段
glBindBuffer(GL_ARRAY_BUFFER,vbo); // vbo现在是缓冲区
//告诉OpenGL更多关于该段的信息:
// segment = 0,elements = 3,type = float,normalize? = false,顶点大小,从哪里开始= 0)
glVertexAttribPointer(0,3,GL_FLOAT,false,Vertex.SIZE * 4,0);
//从给定'尺寸'开始从'0'绘制GL_TRIANGLES
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 );
}
//将所有内容设置为引擎默认值
public static void initGraphics()
{
glClearColor(0.0f,0.0f,0.0f, 0.0F); //默认颜色
glFrontFace(GL_CW); //方向为可见面
glCullFace(GL_BACK); //背面的方向
glEnable(GL_CULL_FACE); //不要画背面
glEnable(GL_DEPTH_TEST); //通过像素深度测试确定绘制顺序
// TODO:深度钳位,稍后
glEnable(GL_FRAMEBUFFER_SRGB); //对gamma进行指数修正,因此我们不必
code
Util.java
import java.nio.FloatBuffer;
import org.lwjgl.BufferUtils;
public class Util
{
//创建一个浮点缓冲区(我们需要这个是因为java很奇怪)
public static FloatBuffer createFloatBuffer(int size)
{
return BufferUtils.createFloatBuffer(size);
}
//将缓冲区翻转以适应OpenGL的需求
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();
返回缓冲区;
$ div $解析方案
你正在使用传统和现代OpenGL的无效组合。
$ b $
glVertexAttribPointer()
和您正在调用的glEnableVertexAttribArray()
函数用于设置泛型顶点属性。这是在当前版本的OpenGL(桌面OpenGL的核心配置文件或OpenGL ES 2.0及更高版本)中设置顶点属性的唯一方法。它们也可以用于较旧版本的OpenGL,但只能与提供GLSL中实现的着色器结合使用。 如果您刚刚开始使用,您的最佳选择可能会坚持你所拥有的,并研究如何开始实现你自己的着色器。如果您希望使用旧版固定管道(仅在OpenGL兼容性配置文件中支持该代码),您需要使用 glVertexPointer()
和 glEnableClientState()
函数。
I'm following a tutorial on creating a Game Engine in Java using OpenGL.
I'm trying to render a triangle on the screen. Everything is running fine and I can change the background color but the triangle won't show. I've also tried running the code provided as part of the tutorial series and it still doesn't work.
Link to the tutorial: http://bit.ly/1EUnvz4
Link to the code used in the video: http://bit.ly/1z7XUlE
Setup
- I've tried checking for OpenGL version and belive I have 2.1.
- Mac OSX
- 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;
}
}
You are using an invalid mix of legacy and modern OpenGL.
The glVertexAttribPointer()
and glEnableVertexAttribArray()
functions you are calling are used for setting up generic vertex attributes. This is the only way to set up vertex attribues in current versions of OpenGL (Core Profile of desktop OpenGL, or OpenGL ES 2.0 and later). They can be used in older versions of OpenGL as well, but only in combination with providing your own shaders implemented in GLSL.
If you are just getting started, your best option is probably to stick with what you have, and study how to start implementing your own shaders. If you wanted to get the code working with the legacy fixed pipeline (which is only supported in the Compatibility Profile of OpenGL), you would need to use the glVertexPointer()
and glEnableClientState()
functions instead.
这篇关于三角形不在OSX上的OpenGL 2.1中绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!