我刚刚开始学习如何使用OpenGL 3.2,现在我正在尝试整理一个用于加载几何数据的实用程序。当我查看加载顶点的代码时,一切看起来都是正确的,我输入的调试输出甚至似乎给了我正确的值。代码应该生成一个单一的三角形,每个点都变满r,g或b,但是变得几乎是纯红色/橙色。
以下是包含几何数据的代码:
final float[] vertexPositions3 = new float[] {
0.0f, 0.5f, 0.0f, 1.0f,
0.5f, -0.366f, 0.0f, 1.0f,
-0.5f, -0.366f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f, 1.0f,
};
geometry = new Geometry();
geometry.addBufferOffset(vertexPositions3, GL_STATIC_DRAW, 0, Geometry.VERTEX_4F, Geometry.VERTEX_4F);
加载它的代码:
protected int getDataTypeSize(int dataType) {
if(dataType == VERTEX_1F) {
return 1;
}
else if(dataType == VERTEX_2F) {
return 2;
}
else if(dataType == VERTEX_3F) {
return 3;
}
else {
return 4;
}
}
public void addBufferOffset(float[] data, int usage, int vertexColumn, int... dataTypes) {
if(numAttributes + dataTypes.length >= MAX_VBO) {
throw new IllegalStateException("Can only have up to 16 attributes, requested: " + (numAttributes + dataTypes.length));
}
int rowSize = 0;
for(int type : dataTypes) {
rowSize += getDataTypeSize(type);
}
if(numRows == 0) {
numRows = data.length / rowSize;
}
else if(numRows != (data.length / rowSize)) {
throw new IllegalStateException("Number of rows in buffers don't match: " + numRows + " vs " + (data.length / rowSize));
}
if(numVertices == 0) {
numVertices = getDataTypeSize(dataTypes[0]) * (data.length / rowSize) / getDataTypeSize(dataTypes[0]);
}
else {
int addingVerts = getDataTypeSize(dataTypes[0]) * (data.length / rowSize);
if(addingVerts != numVertices) {
throw new IllegalStateException("Vertex count for buffers don't match: " + numVertices + " vs " + addingVerts);
}
}
glBindVertexArray(vao);
FloatBuffer buf = BufferUtils.createFloatBuffer(data.length);
buf.put(data);
buf.flip();
int vbo = glGenBuffers();
vbos[numBuffers++] = vbo;
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, buf, usage);
int startPos = 0;
for(int i = 0; i < dataTypes.length; i++) {
glEnableVertexAttribArray(numAttributes + i);
glVertexAttribPointer(numAttributes + i, getDataTypeSize(dataTypes[i]), GL_FLOAT, false, 0, startPos);
System.out.println((numAttributes + i) + ", " + getDataTypeSize(dataTypes[i]) + ", " + startPos);
startPos += numRows * getDataTypeSize(dataTypes[i]);
}
System.out.println("Num verts: " + numVertices);
if(usesIndices) {
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indicesRef);
}
numAttributes += dataTypes.length;
glBindVertexArray(0);
}
顶点着色器:
#version 330
layout(location = 0) in vec4 position;
layout(location = 1) in vec4 color;
smooth out vec4 theColor;
uniform float loopDuration;
uniform float time;
uniform vec4 camera;
void main() {
float timeScale = 3.14159f * 2.0f / loopDuration;
vec4 totalOffset = vec4(
cos(time * 1.5f * timeScale) * 0.5f,
sin(time * 1.5f * timeScale) * 0.5f,
0.0f,
0.0f);
gl_Position = position + totalOffset;
theColor = color;
}
最后片段着色器:
#version 330
smooth in vec4 theColor;
out vec4 outputColor;
uniform float fragLoopDuration;
uniform float time;
const vec4 firstColor = vec4(1.0f, 1.0f, 1.0f, 1.0f);
const vec4 secondColor = vec4(0.0f, 1.0f, 0.0f, 1.0f);
void main() {
float lerpTime = time * 1.5f / fragLoopDuration;
outputColor = theColor;
}
当我运行程序时,addBufferOffset方法产生以下输出:
0, 4, 0
1, 4, 12
Num verts: 3
意思是它找到了两组三个顶点,每个顶点有四个浮点值。其中一个从数组偏移0开始,另一个从数组偏移12开始。这与发送给它的数组相对应:3个顶点,x,y,z,w值和3个顶点,r,g,b ,一个价值观。从输出看来,它似乎应该工作,但事实并非如此。我真的很感激帮助搞清楚这一点。
答案 0 :(得分:1)
我不熟悉lwjgl,但是C API中OpenGL glVertexAttribPointer
函数的最后一个参数是数据的字节偏移量 - 似乎你传入的是数组索引。这可以解释为什么你看到一个红色/橙色三角形,因为从第12个字节开始的数据(在3个浮点数之后)看起来像这样:
1.0f, 0.5f, -0.366f, 0.0f,
1.0f, -0.5f, -0.366f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f
将这些值钳位在0和1之间将给出
1.0f, 0.5f, 0.0f, 0.0f,
1.0f, 0.0f, 0.0f, 0.0f,
1.0f, 1.0f, 0.0f, 0.0f
假设您的混合设置是忽略零alpha分量,这些将给出橙色,红色和黄色顶点。