我只想在我的对象上使用一些不同的颜色做一个简单的 Gouraud 着色。基本上我的问题是,我无法将颜色值输入着色器,它只会呈现黑色。如果我在着色器中定义一个向量作为整个对象的颜色,它就可以正常工作。
主程序:
protected void initOpenGL() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
startTime = Sys.getTime();
m = new Matrix4f();
m.m00 = 1;
m.m11 = 1;
m.m22 = -(101.0f / 99);
m.m32 = -(200.0f / 99);
m.m23 = -1;
m.m33 = 0;
makeCube();
sp = new ShaderProgram("gouraud");
glBindAttribLocation(sp.getId(), 0, "corners");
glBindAttribLocation(sp.getId(), 1, "colors");
}
@Override
protected void render() {
gamma = gamma + 1;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Matrix4f newMat = new Matrix4f(m);
Matrix4f.translate(new Vector3f(0, 0, -5), newMat, newMat);
newMat.rotate(gamma/100, new Vector3f(0, 1, 0));
newMat.rotate(gamma/200, new Vector3f(1, 0, 0));
FloatBuffer fb = BufferUtils.createFloatBuffer(16);
newMat.store(fb);
fb.flip();
GL20.glUniformMatrix4(GL20.glGetUniformLocation(sp.getId(), "matrix"),
false, fb);
GL20.glUseProgram(sp.getId());
glBindVertexArray(vaoId);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glDrawArrays(GL_QUADS, 0, corners.length/3);
glDisableVertexAttribArray(1);
glDisableVertexAttribArray(0);
glBindVertexArray(0);
}
private void makeCube(){
corners = new float[] {
// cube
// front
-1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1,
// left
-1, -1, 1, -1, 1, 1, -1, 1, -1, -1, -1, -1,
// bottom
-1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1,
// right
1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1,
// top
-1, 1, 1, 1, 1, 1, 1, 1, -1, -1, 1, -1,
// back
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
};
FloatBuffer eckenBuffer = BufferUtils.createFloatBuffer(corners.length);
eckenBuffer.put(corners);
eckenBuffer.flip();
vaoId = glGenVertexArrays();
glBindVertexArray(vaoId);
int vboId = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER, eckenBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
colors = new float[] {
// front
1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f,
// right
1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
// back
1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
// left
0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f,
// top
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
// bottom
1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f };
FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(colors.length);
colorBuffer.put(colors);
colorBuffer.flip();
int vboIdB = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboIdB);
glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
着色器:
顶点
#version 150
in vec4 corners;
in vec4 colors;
vec4 colorTest = vec4(1.0,0.0,0.0,1.0);
out vec4 colorToFrag;
uniform mat4 matrix;
void main(void) {
colorToFrag = colors ;
gl_Position = matrix * corners;
}
分段
#version 150
in vec4 colorToFrag;
out vec4 colorOut;
void main(void) {
colorOut = colorToFrag;
}