OpenGL什么都没显示

时间:2014-01-17 19:24:42

标签: opengl lwjgl

尝试使用LWJGL使用OpenGL渲染三角形时遇到问题。 它什么都不做,既不渲染某些东西,也不抛出任何错误。 glClear()正在工作(如果我改变颜色,颜色会改变)。

您可以在此处找到GLIntercept日志: GLIntercept Log

这是我的OpenGL初始化代码:

glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CW);
glEnable(GL_DEPTH_TEST);
glDepthMask(true);
glDepthFunc(GL_LEQUAL);
glDepthRange(0.0f, 1.0f);

glViewport(0, 0, 800, 600); // my display size

之后我绑定了着色器:

int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader , vertexShaderCode);
glCompileShader(vertexShader );

int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader , fragmentShader Code);
glCompileShader(fragmentShader);

int program = glCreateProgram();
glAttachShader(program, vertexShader);
glAttachShader(program, fragmentShader);
glBindAttribLocation(program, 0, "vert");
glLinkProgram(program);
glDetachShader(program, vertexShader);
glDetachShader(program, fragmentShader);

这是着色器:

colored.vert

#version 150

uniform mat4 camera;
uniform mat4 model;

uniform vec4 color;

in vec3 vert;

out vec4 fragColor;

void main() {
    gl_Position = camera * model * vec4(vert, 1);
    fragColor = color;
}

colored.frag

#version 150

in vec4 fragColor;

out vec4 finalColor;

void main() {
    finalColor = fragColor;
}

然后我创建了vbo,ibo和vao:

// VBO
FloatBuffer vboBuffer = BufferUtils.createFloatBuffer(9);
vboBuffer.put(
    0, 1, 0,
    1, 0, 0,
    -1, 0, 0);
vboBuffer.flip();
vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vboBuffer, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// IBO
ShortBuffer iboBuffer = BufferUtils.createShortBuffer(3);
iboBuffer.put(0, 1, 2);
iboBuffer.flip();
ibo = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iboBuffer,  GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// VAO
vao = glGenVertexArrays();
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glEnableVertexAttribArray(glGetAttribLocation(program, "vert"));
glVertexAttribPointer(glGetAttribLocation(program, "vert"), 3, GL_FLOAT, false, 3 * Float.SIZE, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBindVertexArray(0);

我已根据Andon M. Coleman的建议改变了这一点,以配合核心资料:
它无法解决我的问题!

// IBO
ShortBuffer iboBuffer = BufferUtils.createShortBuffer(3);
iboBuffer.put(0, 1, 2);
iboBuffer.flip();

// VBO
FloatBuffer vboBuffer = BufferUtils.createFloatBuffer(9);
vboBuffer.put(
    0, 1, 0,
    1, 0, 0,
    -1, 0, 0);
vboBuffer.flip();

// VAO
vao = glGenVertexArrays();
glBindVertexArray(vao);

ibo = glGenBuffers();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, iboBuffer,  GL_STATIC_DRAW);

vbo = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vboBuffer, GL_STATIC_DRAW);

glEnableVertexAttribArray(glGetAttribLocation(program, "vert"));
glVertexAttribPointer(glGetAttribLocation(program, "vert"), 3, GL_FLOAT, false, 3 * Float.SIZE, 0);

glBindVertexArray(0);

渲染前:

glUseProgram(program);
glUniform4f(glGetUniformLocation(program, "color"), colorR, colorG, colorB, colorA);
glUseProgram(0);

渲染:

glClearColor(0, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(program);
FloatBuffer cameraBuffer = BufferUtils.createFloatBuffer(16);
// a simple orthographic camera at the position (0|0|1)
// left: -1; right: 1; bottom: -1; top: 1; zNear: -1; zFar: 1
cameraBuffer.put(
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, -1.0, 0.0,
    0.0, 0.0, 1.0, 1.0);
cameraBuffer.flip();
glUniformMatrix4(glGetUniformLocation(program, "camera"), false, cameraBuffer);
FloatBuffer modelBuffer = BufferUtils.createFloatBuffer(16);
// no translation applied, so its an identity matrix
modelBuffer .put(
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 0.0, 0.0, 1.0);
modelBuffer.flip();
glUniformMatrix4(glGetUniformLocation(program, "model"), false, modelBuffer);

glBindVertexArray(vao);
glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_SHORT, 0);
glBindVertexArray(0);

glUseProgram(0);

// check for OpenGL errors
int error_code = glGetError();
if(error_code != GL_NO_ERROR)
    System.err.print("OpenGL Error: " + gluErrorString(error_code));

这是我按正确顺序执行的所有OpenGL命令。

GLIntercept日志: GLIntercept Log

1 个答案:

答案 0 :(得分:1)

错误已解决。 问题是glVertexAttribPointer()期望步幅以字节为单位,而Java中的Float.SIZE则返回以位为单位的大小。

错:

glVertexAttribPointer(..., ..., ..., ..., 3 * Float.SIZE, ...);

右:

glVertexAttribPointer(..., ..., ..., ..., 3 * Float.SIZE / 8, ...);