我正在尝试使用JOGL将教程从http://open.gl/drawing移植到Java
我能够获得输出,但是我收到了意想不到的输出。
预期结果:
我的输出:
三角形似乎没有完全面向相机,但教程不包括平移或旋转。
GLSL程序编译和链接:
float[] verts = //Our triangle as a set of 2d coordinates
{
0.0f, 0.5f, //vertex 1, top at 0x, -0.5y
0.5f, -0.5f //vertex 2, right at 0.5x, -0.5y
-0.5f, -0.5f //vertex 3, left at -0.5x, -0.5y
};
IntBuffer vao = Buffers.newDirectIntBuffer(1);
gl.glGenVertexArrays(1, vao);
gl.glBindVertexArray(vao.get(0));
IntBuffer vbo = Buffers.newDirectIntBuffer(1);
FloatBuffer vertBuffer = Buffers.newDirectFloatBuffer(verts);
gl.glGenBuffers(1, vbo);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo.get(0));
gl.glBufferData(GL3.GL_ARRAY_BUFFER, vertBuffer.limit() * Buffers.SIZEOF_FLOAT, vertBuffer, GL3.GL_STATIC_DRAW);
int vertexShader = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
gl.glShaderSource(vertexShader, 1, vertProgram, null);
gl.glCompileShader(vertexShader);
int fragmentShader = gl.glCreateShader(GL3.GL_FRAGMENT_SHADER);
gl.glShaderSource(fragmentShader, 1, fragProgram, null);
gl.glCompileShader(fragmentShader);
program = gl.glCreateProgram();
gl.glAttachShader(program, vertexShader);
gl.glAttachShader(program, fragmentShader);
gl.glBindFragDataLocation(program, 0, "outColor");
gl.glLinkProgram(program);
gl.glUseProgram(program);
int positionAttribute = gl.glGetAttribLocation(program, "position");
gl.glEnableVertexAttribArray(positionAttribute);
gl.glVertexAttribPointer(positionAttribute, 2, GL.GL_FLOAT, false, 0, 0L);
显示方法:
gl.glClearColor(0f, 0f, 0f, 1f);
gl.glClear(GL3.GL_COLOR_BUFFER_BIT);
gl.glDrawArrays(GL.GL_TRIANGLES, 0, 3);
顶点着色器:
#version 330
in vec2 position;
void main()
{
gl_Position = vec4(position, 0.0, 1.0);
}
片段着色器:
#version 330
out vec4 outColor;
void main()
{
outColor = vec4(1.0, 1.0, 1.0, 1.0);
}
答案 0 :(得分:2)
我认为你在这里有错误:
float[] verts = //Our triangle as a set of 2d coordinates
{
0.0f, 0.5f, //vertex 1, top at 0x, -0.5y
0.5f, -0.5f //vertex 2, right at 0.5x, -0.5y
-0.5f, -0.5f //vertex 3, left at -0.5x, -0.5y
};
在第一个-0.5f
(对于顶点2)之后,您缺少逗号。