我正在尝试使用OpenGL Superbibel(第6版)学习现代OpenGL。由于我的主要编程语言是java,我使用的是jogl。
我遇到问题,显示旋转立方体,只显示12个三角形中的3个。每次列表的前3个(如果我注释掉一些顶点,将使用下一个顶点)。
主要问题:
为什么只显示12个三角形中的3个?
顶点着色器:
#version 420 core
layout (location = 1) in vec4 position;
out VS_OUT{
vec4 color;
} vs_out;
uniform mat4 mv_matrix;
uniform mat4 proj_matrix;
void main(void){
gl_Position = proj_matrix * mv_matrix * position;
vs_out.color = position * 2.0 + vec4(0.5, 0.5, 0.5, 0.0);
}
Fragment Shader:
#version 420 core
out vec4 color;
in VS_OUT{
vec4 color;
} fs_in;
void main(void){
color = fs_in.color;
}
Java代码: (从init方法中的应用程序类调用启动,在显示方法内部渲染)。
public class Listening9 extends GLApplication {
private int[] buffer = new int[1];
float aspect = 0.0f;
float[] proj_matrix = new float[16];
int mv_location;
int proj_location;
float st = 0.001f;
public Listening9() {
super();
}
@Override
protected void render(GL4 gl4, float time) {
time = st;
gl4.glViewport(0, 0, GLApplication.windowSize.width, GLApplication.windowSize.height);
gl4.glClearColor(0.0f, 0.25f, 0.0f, 1.0f);
gl4.glClearDepthf(1.0f);
gl4.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
gl4.glUseProgram(program);
gl4.glUniformMatrix4fv(proj_location, 1, false, proj_matrix, 0);
float f = (float)time * 0.3f;
Matrix4 mv_matrix = new Matrix4();
mv_matrix.translate(0.0f, 0.0f, -20.0f);
mv_matrix.translate((float)Math.sin(2.1f * f) * 0.5f,
(float)Math.cos(1.7f * f) * 0.5f,
(float)( Math.sin(1.3f * f) * Math.cos(1.5f * f)) * 2.0f);
mv_matrix.rotate((float)time * 45.0f, 0.0f, 1.0f, 0.0f);
mv_matrix.rotate((float)time * 81.0f, 1.0f, 0.0f, 0.0f);
gl4.glUniformMatrix4fv(mv_location, 1, false, mv_matrix.getMatrix(), 0);
gl4.glDrawArrays(GL.GL_TRIANGLES, 0, 36);
checkError(gl4, "display");
}
@Override
protected void startup(GL4 gl4) {
program = loadProgram(gl4, new String[] { "l9vs", "l9fs" }, new int[] {
GL4.GL_VERTEX_SHADER, GL4.GL_FRAGMENT_SHADER });
mv_location = gl4.glGetUniformLocation(program, "mv_matrix");
proj_location = gl4.glGetUniformLocation(program, "proj_matrix");
System.out.println("MV: "+mv_location);
System.out.println("Proj: "+proj_location);
gl4.glGenVertexArrays(1, objects, Semantic.Object.VAO);
gl4.glBindVertexArray(objects[Semantic.Object.VAO]);
float vertex_positions[] = {
-0.25f, 0.25f, -0.25f,
-0.25f, -0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
0.25f, 0.25f, -0.25f,
-0.25f, 0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
0.25f, -0.25f, 0.25f,
0.25f, 0.25f, -0.25f,
0.25f, -0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
0.25f, 0.25f, -0.25f,
0.25f, -0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
-0.25f, 0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
-0.25f, -0.25f, -0.25f,
-0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, -0.25f,
-0.25f, 0.25f, -0.25f,
-0.25f, 0.25f, 0.25f,
-0.25f, -0.25f, 0.25f,
0.25f, -0.25f, 0.25f,
0.25f, -0.25f, -0.25f,
0.25f, -0.25f, -0.25f,
-0.25f, -0.25f, -0.25f,
-0.25f, -0.25f, 0.25f,
-0.25f, 0.25f, -0.25f,
0.25f, 0.25f, -0.25f,
0.25f, 0.25f, 0.25f,
0.25f, 0.25f, 0.25f,
-0.25f, 0.25f, 0.25f,
-0.25f, 0.25f, -0.25f};
gl4.glGenBuffers(1, buffer, 0);
gl4.glBindBuffer(GL.GL_ARRAY_BUFFER, buffer[0]);
gl4.glBufferData(GL.GL_ARRAY_BUFFER,
vertex_positions.length,
FloatBuffer.wrap(vertex_positions),
GL.GL_STATIC_DRAW);
gl4.glVertexAttribPointer(1, 3, GL.GL_FLOAT, false, 0, 0);
gl4.glEnableVertexAttribArray(1);
//gl4.glEnable(GL.GL_CULL_FACE);
gl4.glFrontFace(GL.GL_CW);
gl4.glEnable(GL.GL_DEPTH_TEST);
gl4.glDepthFunc(GL.GL_LEQUAL);
}
@Override
protected void shutdown(GL4 gl4) {
gl4.glDeleteProgram(program);
gl4.glDeleteVertexArrays(1, objects, objects[Semantic.Object.VAO]);
}
@Override
protected void onResize(GL4 gl4, int w, int h) {
aspect = w / h;
proj_matrix = FloatUtil.makePerspective(proj_matrix,
0,
true,
50.0f,
aspect,
0.1f,
1000.0f);
}
private int loadProgram(GL4 gl, String[] names, int[] types) {
ShaderProgram shaderProgram = new ShaderProgram();
for (int i = 0; i < names.length; i++) {
shaderProgram.add(loadShader(gl, types[i], names[i]));
}
shaderProgram.init(gl);
int id = shaderProgram.program();
shaderProgram.link(gl, System.out);
checkError(gl, "initProgram");
System.out.println("Program loaded! (" + id + ")");
return id;
}
private ShaderCode loadShader(GL4 gl, int type, String name) {
return ShaderCode.create(gl, type, this.getClass(), SHADERS_ROOT, null,
name, "sl", null, true);
}
public static void main(String[] args) {
new Listening9();
}
@Override
protected void onUP() {
st += 0.001f;
}
}
答案 0 :(得分:3)
根据请求:问题似乎是(请参阅问题评论)glBufferData
的第二个参数需要缓冲区大小(以字节为单位)。由于缓冲区由浮点元素组成,因此您需要指定vertex_positions.length * 4
而不是vertex_positions.length
(浮点数的大小为4个字节)。
答案 1 :(得分:0)
您对glBufferData
的来电缺少数据格式。在C ++中,您应该使用sizeof-operator动态读取大小,如下所示:
gl4.glBufferData(GL.GL_ARRAY_BUFFER,
vertex_positions.length*sizeof(GLfloat),
FloatBuffer.wrap(vertex_positions),
GL.GL_STATIC_DRAW);
您还应该将vertex_positions
的类型从float
更改为GLfloat
,因为此数据类型在不同的计算机上具有相同的大小(4字节)。 float
可能会有所不同。