【发布时间】:2014-08-30 23:45:38
【问题描述】:
我正在尝试使用 JOGL 渲染一个简单的线框立方体。
但是我遇到了黑屏,比我更有经验的人能告诉我我做错了什么吗?
着色器编译并不会抛出任何错误。
private Mat4 projectionMatrix;
//reference to the uniform MPV matrix in the vertex shader
private int mpvUnif;
//Holds the references to GL objects
private final int[] glPointers = new int[3];
private static final int VBO = 0;
private static final int INDEXBUFFER = 1;
private static final int VAO = 2;
private int shaderProgram;
private FloatBuffer vertexDataBuffer;
private ShortBuffer indexDataBuffer;
private float[] testCube = new float[] {
//Top
-1, 1, 1, //0
1, 1, 1, //1
-1, -1, 1, //2
1, -1, 1, //3
//Bottom
-1, 1, -1, //4
1, 1, -1, //5
-1, -1, -1, //6
1, -1, -1, //7
};
private short[] testCubeIndexes = new short[] {
0,1,2,3,
4,5,6,7,
0,1,4,5,
2,3,6,7,
0,2,4,6,
1,3,5,7
};
public GLController() {
this.vertexDataBuffer = FloatBuffer.wrap(testCube);
this.indexDataBuffer = ShortBuffer.wrap(testCubeIndexes);
}
@Override
public void init(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
shaderProgram = gl.glCreateProgram();
try {
InitializeVertexBuffer(gl);
InitializeVertexArrayObjects(gl);
initShaders(gl);
} catch (IOException e) {
e.printStackTrace();
}
gl.glPolygonMode(GL3.GL_FRONT_AND_BACK, GL3.GL_LINE);
gl.glFrontFace(GL3.GL_CW);
}
@Override
public void display(GLAutoDrawable drawable) {
GL3 gl = drawable.getGL().getGL3();
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glClear(GL3.GL_COLOR_BUFFER_BIT);
gl.glUseProgram(shaderProgram);
gl.glBindVertexArray(glPointers[VAO]);
gl.glDrawElements(GL3.GL_QUADS, indexDataBuffer.capacity(), GL3.GL_UNSIGNED_SHORT, 0);
gl.glBindVertexArray(0);
gl.glUseProgram(0);
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
GL3 gl = drawable.getGL().getGL3();
mpvUnif = gl.glGetUniformLocation(shaderProgram, "MPV");
projectionMatrix = Matrices.perspective(60f, width/height, .1f, 100f);
gl.glUseProgram(shaderProgram);
gl.glUniformMatrix4fv(mpvUnif, 1, GLHelper.glBool(GL3.GL_FALSE), projectionMatrix.getBuffer());
gl.glUseProgram(0);
}
/**
* Called in GLEventListener.init()
* @param gl
*/
private void InitializeVertexBuffer(GL3 gl) {
gl.glGenBuffers(1, glPointers, VBO);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, glPointers[VBO]);
gl.glBufferData(GL3.GL_ARRAY_BUFFER, GLHelper.sizeof(vertexDataBuffer), vertexDataBuffer, GL3.GL_STATIC_DRAW);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, 0);
gl.glGenBuffers(1, glPointers, INDEXBUFFER);
gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, glPointers[INDEXBUFFER]);
gl.glBufferData(GL3.GL_ELEMENT_ARRAY_BUFFER, GLHelper.sizeof(indexDataBuffer), indexDataBuffer, GL3.GL_STATIC_DRAW);
gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, 0);
}
/**
* Called in GLEventListener.init()
* @param gl
*/
private void InitializeVertexArrayObjects(GL3 gl) {
gl.glGenVertexArrays(1, glPointers, VAO);
gl.glBindVertexArray(glPointers[VAO]);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, glPointers[VBO]);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 3, GL3.GL_FLOAT, GLHelper.glBool(GL3.GL_FALSE), 0, 0);
gl.glBindBuffer(GL3.GL_ELEMENT_ARRAY_BUFFER, glPointers[INDEXBUFFER]);
gl.glBindVertexArray(0);
}
我的顶点着色器:
#version 330
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP;
void main()
{
vec4 v = vec4(vertexPosition_modelspace, 1);
gl_Position = MVP * v;
}
这个着色器确实在屏幕上绘制了一些东西:
#version 330
layout(location = 0) in vec3 vertexPosition_modelspace;
uniform mat4 MVP;//not used
void main()
{
gl_Position = vec4(vertexPosition_modelspace, 1);
}
我的片段着色器:
#version 330
out vec4 outputColor;
void main()
{
outputColor = vec4(1.0, 1.0, 1.0, 1.0);
}
【问题讨论】:
-
请发一个 sscce:sscce.org 此外,我不喜欢你的问题的标题,因为 JOGL 本身不是罪魁祸首,问题可能来自你自己的代码。请提供一个包含完整代码示例和 BSP 文件示例的测试用例。也许你的截头锥是错的。
-
感谢您的意见,我会尽我所能更新我的问题。
-
@gouessej 我已经更新了我的问题
-
BSPFile 类在哪里?你能画出更简单的东西吗?这个例子有效吗? jogamp.org/git/?p=jogl-demos.git;a=blob;f=src/demos/es2/…
-
@gouessej 我已将代码简化为使用简单的多维数据集,并且没有来自 BSP 的数据。如果我将着色器更改为仅行
gl_Position = vec4(vertexPosition_modelspace, 1);我确实会在屏幕上绘制一些东西。所以我提供的 MPV 一定有问题,或者我没有以正确的方式使用它。