JOGL无法渲染

时间:2016-01-26 13:10:02

标签: java opengl jogl

我在屏幕上开发了一个简单的JOGL渲染立方体,遵循与iPhone上的OpenGL ES 2相同的程序。但这段代码在运行时不会显示任何内容。

有人可以帮助浏览并指出问题是什么吗?

这是我的代码

import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.*;
import com.jogamp.opengl.awt.GLCanvas;
import com.jogamp.opengl.util.FPSAnimator;
import org.joml.Matrix4f;
import org.joml.Vector3f;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;

public class TestScene extends JFrame implements GLEventListener {

    private static final float[] CUBE_POSITIONS = {
            -1.0f, -1.0f,  1.0f,
             1.0f, -1.0f,  1.0f,
             1.0f,  1.0f,  1.0f,
            -1.0f,  1.0f,  1.0f,
            -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 static final short[] INDICIES = {
            0, 1, 2, 0, 2, 3,//front
            2, 1, 5, 6, 2, 5,//right
            7, 6, 4, 6, 5, 4,//back
            7, 4, 3, 3, 4, 0,//right
            6, 7, 2, 7, 3, 2,//top
            0, 4, 1, 1, 4, 5,//bottom
    };

    private GL4 gl;
    private int program;
    private Matrix4f projection = new Matrix4f();
    private Matrix4f viewing = new Matrix4f();
    private Matrix4f translation = new Matrix4f();
    private int projectionIndex;
    private int viewingMatrix;
    private int transIndex;
    private int positionIndex;
    private int vbo;
    private int vio;

    public TestScene() throws HeadlessException {
        super("Testing");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final GLCanvas canvas = new GLCanvas(new GLCapabilities(GLProfile.get(GLProfile.GL4)));
        final FPSAnimator animator = new FPSAnimator(25);
        animator.add(canvas);
        animator.start();
        canvas.addGLEventListener(this);

        getContentPane().add(canvas);
        setSize(800, 600);
        setVisible(true);
    }

    @Override
    public void init(final GLAutoDrawable drawable) {
        gl = drawable.getGL().getGL4();

        try {
            //init program and shader
            final int vertexShader = createShader("/Users/gang_liu/Develop/Java/ideaProject/KLM/JOGL_Studies_1/vertex.glsl", GL4.GL_VERTEX_SHADER);
            final int fragmentShader = createShader("/Users/gang_liu/Develop/Java/ideaProject/KLM/JOGL_Studies_1/fragment.glsl", GL4.GL_VERTEX_SHADER);
            program = gl.glCreateProgram();
            gl.glAttachShader(program, vertexShader);
            gl.glAttachShader(program, fragmentShader);
            gl.glLinkProgram(program);

            //generate vbo
            final IntBuffer intBuffer = IntBuffer.allocate(1);
            gl.glGenBuffers(1, intBuffer);
            vbo = intBuffer.get();
            gl.glBindBuffer(GL.GL_ARRAY_BUFFER, vbo);
            gl.glBufferData(GL.GL_ARRAY_BUFFER, CUBE_POSITIONS.length * Buffers.SIZEOF_FLOAT, FloatBuffer.wrap(CUBE_POSITIONS), GL.GL_STATIC_DRAW);

            intBuffer.flip();

            //generate vio
            gl.glGenBuffers(1, intBuffer);
            vio = intBuffer.get();
            gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vio);
            gl.glBufferData(GL.GL_ELEMENT_ARRAY_BUFFER, INDICIES.length * Buffers.SIZEOF_SHORT, ShortBuffer.wrap(INDICIES), GL.GL_STATIC_DRAW);

            //get program indexes
            positionIndex   = gl.glGetAttribLocation(program, "position");
            projectionIndex = gl.glGetUniformLocation(program, "projection");
            viewingMatrix   = gl.glGetUniformLocation(program, "view");
            transIndex      = gl.glGetUniformLocation(program, "trans");

            //init projection matrix;
            final int width = getWidth();
            final int height = getHeight();
            projection.perspective(45.0f, (float)width/height, 1.0f, 100.0f);
            viewing.lookAt(new Vector3f(0.0f, 0.0f, 10.0f), new Vector3f(0.0f, 0.0f, 0.0f), new Vector3f(0.0f, 1.0f, 0.0f));
        } catch (final Exception ex){
            ex.printStackTrace();
        }

    }  

    @Override
    public void dispose(GLAutoDrawable drawable) {

    }

    @Override
    public void display(final GLAutoDrawable drawable) {
        gl.glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
        gl.glEnable(GL4.GL_CULL_FACE);
        gl.glEnable(GL4.GL_DEPTH_TEST);
        gl.glClearDepth(1.0);
        gl.glClear(GL4.GL_COLOR_BUFFER_BIT | GL4.GL_DEPTH_BUFFER_BIT);

        gl.glUseProgram(program);

        gl.glBindBuffer(GL4.GL_ARRAY_BUFFER, vbo);
        gl.glEnableVertexAttribArray(positionIndex);
        gl.glVertexAttribPointer(positionIndex, 3, gl.GL_FLOAT, false, Buffers.SIZEOF_FLOAT * 3, 0);

        gl.glUniformMatrix4fv(projectionIndex, 1, false, FloatBuffer.wrap(projection.get(new float[16])));
        gl.glUniformMatrix4fv(viewingMatrix, 1, false, FloatBuffer.wrap(viewing.get(new float[16])));
        gl.glUniformMatrix4fv(transIndex, 1, false, FloatBuffer.wrap(translation.get(new float[16])));

        gl.glBindBuffer(GL.GL_ELEMENT_ARRAY_BUFFER, vio);
        gl.glDrawElements(gl.GL_TRIANGLES, INDICIES.length, gl.GL_UNSIGNED_SHORT, 0);

        gl.glUseProgram(0);
    }

    @Override
    public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {

    }

    private int createShader(final String fileName, final int type) throws IOException {
        final int shaderID = gl.glCreateShader(type);

        final StringBuilder sb = new StringBuilder();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line).append("\n");
        }
        gl.glShaderSource(shaderID, 1, new String[]{sb.toString()}, null, 0);
        gl.glCompileShader(shaderID);
        final IntBuffer intBuffer = IntBuffer.allocate(1);
        gl.glGetShaderiv(shaderID, gl.GL_COMPILE_STATUS, intBuffer);
        if (intBuffer.get() == gl.GL_FALSE) {
            intBuffer.flip();
            gl.glGetShaderiv(shaderID, gl.GL_INFO_LOG_LENGTH, intBuffer);
            final int logLength = intBuffer.get();
            final ByteBuffer byteBuffer = ByteBuffer.allocate(logLength);
            gl.glGetShaderInfoLog(shaderID, logLength, null, byteBuffer);
            System.out.println("Filed to compile " +
                (type == GL4.GL_VERTEX_SHADER ? "vertex shader" : "fragment shader")
                + " shader :\n" + new String(byteBuffer.array()));
         }
        return shaderID;
    }

     public static void main(String[] args) {
        new TestScene();
    }
}

这里是着色器文件

顶点着色器

#version 400

uniform lowp mat4 trans;
uniform lowp mat4 view;
uniform lowp mat4 projection;
in lowp vec3 position;
out lowp vec3 out_color;

void main(){
    gl_Position = projection * view * trans * vec4(position, 1.0);
    out_color = position;
}

片段着色器

#version 400
in lowp vec3 out_color;
out vec4 fragColor;

void main(){
    fragColor = vec4(out_color, 1.0);
}

我知道我应该计算CPU中的统一矩阵。再次,这对调试很有用。

除了这个问题,我还有另外一个问题。似乎一些与glGetAttribLocation和glGetUniformLocation这样的程序相关的opengl函数都有程序参数,这是否意味着我们不必在这些函数调用之前调用glUseProgram?这个假设是正确的吗?关于这个问题的任何意见都是受欢迎的。

由于

0 个答案:

没有答案