LWJGL Pojection视图模型矩阵无法正常工作

时间:2017-10-14 18:48:12

标签: java opengl matrix lwjgl

我是OpenGL的新手,我刚刚开始尝试LWJGL,一切正常,直到我添加了PVM矩阵,我不知道为什么在添加它们时没有显示任何内容。 与矩阵相关的代码被评论。 这是代码:

public class Window {
    private int vaoid;
    private int vbover;
    private int vboind;

    private int vid;
    private int fid;
    private int pid;

    //private int pml;projection matrix location
    //private int vml;view matrix location
    //private int mml;model matrix location
    //private Matrix4f projectionMatrix;
    //private Matrix4f viewMatrix;
    //private Matrix4f modelMatrix;
    //private FloatBuffer projbuffer = BufferUtils.createFloatBuffer(16);

    public void start() {
        try {
            Display.setDisplayMode(new DisplayMode(800,600));
            Display.setTitle("OpenGL Try");
            Display.create();
        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(0);
        }
         init();
        while (!Display.isCloseRequested()) {
             update();
             Display.sync(60);
             Display.update();
        }
        cleanUp();
        Display.destroy();
    }

    public static void main(String[] argv) {
        Window displayExample = new Window();
        displayExample.start();
    }

    public void init() {
        //projectionMatrix = createProjectionMatrix();

        float[] vertices = {
                -0.5f, 0.5f, 0f,    // Left top         ID: 0
                -0.5f, -0.5f, 0f,   // Left bottom      ID: 1
                0.5f, -0.5f, 0f,    // Right bottom     ID: 2
                0.5f, 0.5f, 0f      // Right left       ID: 3
        };
        FloatBuffer verticesbuffer = BufferUtils.createFloatBuffer(vertices.length);
        verticesbuffer.put(vertices);
        verticesbuffer.flip();

        byte[] indices = {
                // Left bottom triangle
                0, 1, 2,
                // Right top triangle
                2, 3, 0
        };
        ByteBuffer indicesbuffer = BufferUtils.createByteBuffer(indices.length);
        indicesbuffer.put(indices);
        indicesbuffer.flip();

        vaoid = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vaoid);

        vbover = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbover);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER,verticesbuffer, GL15.GL_STATIC_DRAW);
        GL20.glVertexAttribPointer(0,3,GL11.GL_FLOAT,false,0,0);
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER,0);
        GL30.glBindVertexArray(0);

        vboind = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,vboind);
        GL15.glBufferData(GL15.GL_ELEMENT_ARRAY_BUFFER,indicesbuffer,GL15.GL_STATIC_DRAW);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,0);

        vid = loadShader("./res/shader.ver",GL20.GL_VERTEX_SHADER);
        fid = loadShader("./res/shader.frag",GL20.GL_FRAGMENT_SHADER);

        pid = GL20.glCreateProgram();
        GL20.glAttachShader(pid,vid);
        GL20.glAttachShader(pid, fid);
        GL20.glBindAttribLocation(pid, 0, "in_position");
        GL20.glLinkProgram(pid);
        GL20.glValidateProgram(pid);
        //pml = GL20.glGetUniformLocation(pid, "projMatrix");
        //vml = GL20.glGetUniformLocation(pid, "viewMatrix");
        //mml = GL20.glGetUniformLocation(pid, "modelMatrix");
        GL20.glUseProgram(pid);
        //projectionMatrix.store(projbuffer);
        //projbuffer.flip();
        //GL20.glUniformMatrix4(pml, false, projbuffer);
        GL20.glUseProgram(0);

    }

    public void update() {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT|GL11.GL_DEPTH_BUFFER_BIT);
        GL20.glUseProgram(pid);
        //viewMatrix = createTransformationMatrix(new Vector3f(0,0,0),new Vector3f(0,0,0),1f);
        //viewMatrix.store(projbuffer);projbuffer.flip();
        //GL20.glUniformMatrix4(vml, false, projbuffer);
        //modelMatrix = createTransformationMatrix(new Vector3f(0,0,0),new Vector3f(0,0,0),1f);
        //modelMatrix.store(projbuffer);projbuffer.flip();
        //GL20.glUniformMatrix4(mml, false, projbuffer);
        GL30.glBindVertexArray(vaoid);
        GL20.glEnableVertexAttribArray(0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,vboind);
        GL11.glDrawElements(GL11.GL_TRIANGLES,6,GL11.GL_UNSIGNED_BYTE,0);
        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER,0);
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);

        GL20.glUseProgram(0);

    }

    public void cleanUp() {
        GL20.glUseProgram(0);
        GL20.glDetachShader(pid, vid);
        GL20.glDetachShader(pid, fid);
        GL20.glDeleteShader(vid);
        GL20.glDeleteShader(fid);
        GL20.glDeleteProgram(pid);
        GL20.glDisableVertexAttribArray(0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(vbover);

        GL15.glBindBuffer(GL15.GL_ELEMENT_ARRAY_BUFFER, 0);
        GL15.glDeleteBuffers(vboind);

        GL30.glBindVertexArray(0);
        GL30.glDeleteVertexArrays(vaoid);
    }

    public int loadShader(String filePath,int type) {
        StringBuilder sb = new StringBuilder();
        int shaderId = 0;
        try {
            BufferedReader br = new BufferedReader(new FileReader(filePath));
            String line="";
            while((line = br.readLine())!=null) {
                sb.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(-1);
        }

        shaderId = GL20.glCreateShader(type);
        GL20.glShaderSource(shaderId, sb);
        GL20.glCompileShader(shaderId);

        return shaderId;
    }

       /* public Matrix4f createTransformationMatrix(Vector3f pos,Vector3f 
    rot,float scale) {
            Matrix4f mat = new Matrix4f();
            Matrix4f.scale(new Vector3f(scale,scale,scale),mat,mat);
            Matrix4f.translate(pos,mat,mat);
            Matrix4f.rotate((float) Math.toRadians(rot.x),new 
            Vector3f(1,0,0),mat,mat);
            Matrix4f.rotate((float) Math.toRadians(rot.y),new 
            Vector3f(0,1,0),mat,mat);
            Matrix4f.rotate((float) Math.toRadians(rot.z),new 
            Vector3f(0,0,1),mat,mat);
            return mat;
        }*/

    /*public Matrix4f createProjectionMatrix() {
        Matrix4f pm = new Matrix4f();
        float fieldOfView = 60f;
        float aspectRatio = (float)Display.getWidth()/(float)Display.getHeight();
        float znear = 0.1f;
        float zfar = 100f;
        float frustumLength = zfar-znear;

        float ysize = (float) (1/Math.tan(Math.toRadians(fieldOfView/2f)));
        float xsize = ysize/aspectRatio;

        pm.m00 = xsize;
        pm.m11 = ysize;
        pm.m22 = -((znear+zfar)/frustumLength);
        pm.m23 = -1;
        pm.m32 = -((2*znear*zfar)/frustumLength);
        pm.m33 = 0;

        return pm;

    } */
}

顶点着色器:

#version 150 core

in vec3 in_position;

uniform mat4 projMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

out vec4 pass_color;

void main(void){

    gl_Position = projMatrix * viewMarix * modelMatrix * 
    vec4(in_position,1.0);
    pass_color = vec4(1,0,1,1);
}

片段着色器:

#version 150 core

in vec4 pass_color;

out vec4 out_color;

void main(void){

    out_color = pass_color;
}

感谢任何帮助,阻止我继续学习新的opengl,谢谢。

1 个答案:

答案 0 :(得分:0)

我注意到了3个问题。

  1. 您应该使用IntBuffer而不是ByteBuffer作为索引,因为您将整数存储在此缓冲区中。如果更改缓冲区类型,还需要将glDrawElements中的GL_UNSIGNED_BYTE更改为GL_UNSIGNED_INT。

  2. 绑定索引缓冲区后,不要将GL_ELEMENT_ARRAY_BUFFER绑定为0。

  3. 调用glDarwElements时,您不需要绑定和取消绑定GL_ELEMENT_ARRAY_BUFFER

  4. 我希望这会有所帮助。