我刚开始学习如何使用 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 开始。这与发送给它的数组相对应:具有 x、y、z、w 值的 3 个顶点和具有 r、g、b 值的 3 个顶点,一个值。从输出来看,它似乎应该工作,但事实并非如此。我真的很感激一些帮助来解决这个问题。