JOGL / OpenGL VBO - 如何渲染顶点?

时间:2013-07-13 13:43:21

标签: java opengl jogl

3我有以下SceneRenderer类,实现了GLEventListener。我想我理解创建缓冲区,存储指向这些缓冲区的指针以及用数据填充这些缓冲区的过程(参见init方法)。

我奋斗的地方是 display()方法。我已经尝试了几乎所有我在互联网上找到的所有组合,我仍然无法绘制任何东西。有人可以解释我现在要做什么,所有缓冲区都填充了等待渲染的数据吗?

package cz.pscheidl.gui;

import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;


public class SceneRenderer implements GLEventListener {

private IntBuffer buffers = IntBuffer.allocate(2);
private float[] square = {
        -1.0f, -1.0f,
        1.0f, -1.0f,
        1.0f, 1.0f,
        1.0f, 1.0f,
        -1.0f, 1.0f,
        -1.0f, -1.0f,
};

private float[] colorData = {
        255, 0, 0,
        255, 255, 0,
        0, 255, 0,
        0, 255, 0,
        0, 0, 255,
        255, 0, 0
};

FloatBuffer vertexFB = FloatBuffer.wrap(square);
FloatBuffer colorFB = FloatBuffer.wrap(colorData);


GLU glu = new GLU();

@Override
public void init(GLAutoDrawable glAutoDrawable) {
    GL3 gl = glAutoDrawable.getGL().getGL3();

    gl.glEnableClientState(GL2.GL_VERTEX_ARRAY);


    gl.glEnable(GL2.GL_DEPTH_TEST);
    gl.glClearDepthf(10.0f);
    gl.glClearColor(0.8f, 0.6f, 0.8f, 1.0f);
    gl.glDepthFunc(GL2.GL_LEQUAL);

    gl.glGenBuffers(2, buffers);

    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffers.get(0));
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, 4 * 6 * 2, vertexFB, GL3.GL_STATIC_DRAW);


    gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffers.get(1));
    gl.glBufferData(GL2.GL_ARRAY_BUFFER, 4 * 6 * 3, colorFB, GL2.GL_STREAM_DRAW);

}

@Override
public void dispose(GLAutoDrawable glAutoDrawable) {
}

@Override
public void display(GLAutoDrawable glAutoDrawable) {
    GL3 gl = glAutoDrawable.getGL().getGL3();
    gl.glClear(GL3.GL_DEPTH_BUFFER_BIT | GL3.GL_COLOR_BUFFER_BIT);


    gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, buffers.get(0));
    //After binding the buffer, now WHAT ?

}

@Override
public void reshape(GLAutoDrawable glAutoDrawable, int i, int i2, int i3, int i4) {
    GL3 gl = glAutoDrawable.getGL().getGL3();
    glu.gluPerspective(80.0f, 1920.0f / 1080.0f, 0.1f, 100f);

}
}

1 个答案:

答案 0 :(得分:7)

有几件事你不知道:

<强> 1。着色器
着色器是在gpu上运行的程序。这就是你如何告诉gpu如何处理你的数据。这就是你如何创建一个非常基本的程序,它有一个Vertex和Fragment Shader,可以显示你的网格。

用你的类级别var声明:

private int program;

这将进入你的init()函数:

// Create program.
program = gl.glCreateProgram();

// Create vertexShader.
int vertexShader = gl.glCreateShader(GL2ES2.GL_VERTEX_SHADER);
String[] vertexShaderSource = new String[1];
vertexShaderSource[0] = "#version 330\n" +
    "layout(location=0) in vec2 position;\n" +
    "layout(location=1) in vec3 color;\n" +
    "out vec3 vColor;\n" +
    "void main(void)\n" +
    "{\n" +
    "gl_Position = vec4(position, 0.0, 1.0);\n"
    "vColor = vec4(color, 1.0);\n"
    "}\n";
gl.glShaderSource(vertexShader, 1, vertexShaderSource, null);
gl.glCompileShader(vertexShader);

// Create and fragment shader.
int fragmentShader = gl.glCreateShader(GL2ES2.GL_FRAGMENT_SHADER);
String[] fragmentShaderSource = new String[1];
fragmentShaderSource[0] = "#version 330\n" +
    "in vec4 vColor;\n" +
    "out vec4 fColor;\n" +
    "void main(void)\n" +
    "{\n" +
    "fColor = vColor;\n" +
    "}\n";
gl.glShaderSource(fragmentShader, 1, fragmentShaderSource, null);
gl.glCompileShader(fragmentShader);

// Attach shaders to program.
gl.glAttachShader(program, vertexShader);
gl.glAttachShader(program, fragmentShader);
gl.glLinkProgram(program);


2。顶点阵列 绘制时需要使用两个缓冲区。 Vertex Array允许您存储多个缓冲区的状态,并将数据发送到着色器程序中的不同位置。

将此与您的类级别var声明一起使用:

IntBuffer vertexArray = IntBuffer.allocate(1);

在创建缓冲区之后,这也会出现在init()函数中:

// Create Vertex Array.
gl.glGenVertexArrays(1, vertexArray);
gl.glBindVertexArray(vertexArray.get(0));

// Specify how data should be sent to the Program.

// VertexAttribArray 0 corresponds with location 0 in the vertex shader.
gl.glEnableVertexAttribArray(0);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffers.get(0));
gl.glVertexAttribPointer(0, 2, GL.GL_FLOAT, false, 0, 0);

// VertexAttribArray 1 corresponds with location 1 in the vertex shader.
gl.glEnableVertexAttribArray(1);
gl.glBindBuffer(GL2.GL_ARRAY_BUFFER, buffers.get(1));
gl.glVertexAttribPointer(1, 3, GL.GL_FLOAT, false, 0, 0);

您的display()函数现在应如下所示:

@Override
public void display(GLAutoDrawable glAutoDrawable) {
    GL3 gl = glAutoDrawable.getGL().getGL3();
    gl.glClear(GL3.GL_DEPTH_BUFFER_BIT | GL3.GL_COLOR_BUFFER_BIT);

    gl.glUseProgram(program)
    gl.glBindVertexArray(vertexArray.get(0));
    gl.glDrawArrays(GL.GL_TRIANGLES, 0, 6)
}

杂项说明:
- 您的颜色值应介于0到1之间,而不是0到255之间 - 您应该研究破坏您创建的OpenGL对象,以便在程序关闭或您不再需要它们时使用它们 - 我强烈建议您阅读以下教程:OpenGL BookLearning Modern 3D Graphics Programming。代码示例使用的是C ++,但我发现它们非常有用。