矩阵不在openGL中工作

时间:2016-01-03 22:40:05

标签: java opengl lwjgl

我正在尝试创建一个正交矩阵,以及一个合适的模型和视图矩阵来查看一个简单的三角形,只是为了测试矩阵。我一直在看一个空白的屏幕,我不知道发生了什么。我正在使用LWJGL,对于顶点和矩阵,我正在使用一个名为JOML的库。

主类(唯一的类):

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.system.MemoryUtil.NULL;

import java.io.*;
import java.nio.*;

import org.joml.*;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;

public class Main {

    private GLFWErrorCallback errorCallback;
    private GLFWKeyCallback keyCallback;

    private long window;

    int WIDTH = 800, HEIGHT = 600;

    public void run() {
        System.out.println("Hello LWJGL " + Version.getVersion() + "!");

        try {
            init();
            loop();

            glfwDestroyWindow(window);
            keyCallback.release();
        } finally {
            glfwTerminate();
            errorCallback.release();
        }
    }

    private void init() {
        //initializes window
        glfwSetErrorCallback(errorCallback =                                                         GLFWErrorCallback.createPrint(System.err));

    if (glfwInit() != GLFW_TRUE)
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints(); 
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); 
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL);
    if (window == NULL)
        throw new RuntimeException("Failed to create the GLFW window");

    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
                glfwSetWindowShouldClose(window, GLFW_TRUE); 
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - WIDTH) / 2, (vidmode.height() - HEIGHT) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);

    glfwShowWindow(window);
}

private void loop() {
    //Loads and initializes shaders
    GL.createCapabilities();
    //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

    int shaderProgram = glCreateProgram();
    int vertexShader = glCreateShader(GL_VERTEX_SHADER);
    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    StringBuilder vertexShaderSource = new StringBuilder();
    StringBuilder fragmentShaderSource = new StringBuilder();

    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new FileReader("src/test/com/shader.vert"));

        String line;
        while((line = reader.readLine()) != null) {
            vertexShaderSource.append(line).append('\n');
        }

        reader.close();
    } catch(IOException e) {
        System.err.println("Vertex shader not loaded properly!");
        e.printStackTrace();
    }

    BufferedReader reader2 = null;
    try {
        reader2 = new BufferedReader(new FileReader("src/test/com/shader.frag"));

        String line;
        while((line = reader2.readLine()) != null) {
            fragmentShaderSource.append(line).append('\n');
        }

        reader2.close();
    } catch(IOException e) {
        System.err.println("Fragment shader not loaded properly!");
        e.printStackTrace();
    }

    glShaderSource(vertexShader, vertexShaderSource);
    glCompileShader(vertexShader);
    if(glGetShaderi(vertexShader, GL_COMPILE_STATUS) == GL_FALSE) {
        System.err.println("Vertex shader not compiled correctly!");
    }

    glShaderSource(fragmentShader, fragmentShaderSource);
    glCompileShader(fragmentShader);
    if(glGetShaderi(fragmentShader, GL_COMPILE_STATUS) == GL_FALSE) {
        System.err.println("Fragment Shader not compiled correctly!");
    }

    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glLinkProgram(shaderProgram);
    glValidateProgram(shaderProgram);

    //Matrices setup
    FloatBuffer projectionFloat = BufferUtils.createFloatBuffer(16);
    getProjectionMatrix(0, 800, 0, 600, -1, 1).get(projectionFloat);
    glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), false, projectionFloat);



    FloatBuffer viewFloat = BufferUtils.createFloatBuffer(16);
    getViewMatrix(new Vector3f(0, 0, 1), new Vector3f(0, 0, 0), new Vector3f(0, 1, 0)).get(viewFloat);
    glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), false, viewFloat);



    FloatBuffer modelFloat = BufferUtils.createFloatBuffer(16);
    getModelMatrix().get(modelFloat);
    glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), false, modelFloat);

    //Main loop
    while (glfwWindowShouldClose(window) == GLFW_FALSE) {
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);

        glBegin(GL_TRIANGLES);
        {
            glVertex3f(100, 100, 50);
            glVertex3f(100, 400, 50);
            glVertex3f(400, 400, 50);
        }
        glEnd();

        glUseProgram(0);

        glfwSwapBuffers(window); 
        glfwPollEvents();
    }
}

public Matrix4f getModelMatrix() {
    return new Matrix4f(
            1, 0, 0, 0,
            0, 1, 0, 0,
            0, 0, 1, 0,
            0, 0, 0, 1
            );
}

public Matrix4f getProjectionMatrix(float bottom, float top, float left, float right, float near, float far) {
    return new Matrix4f(
            (2 / (right - left)), 0,                  0,                              -((right + left) / (right - left)),
            0,                    (2/(top - bottom)), 0,                              -((top + bottom) / (top - bottom)),
            0,                    0,                  (2 / (far - near)),             -((far + near) / (far - near)),
            0,                    0,                  0,                              1
            );
}

public Matrix4f getViewMatrix(Vector3f eye, Vector3f target, Vector3f up) {
    Vector3f zAxis = eye.sub(target);
    Vector3f xAxis = up.cross(zAxis);
    Vector3f yAxis = zAxis.cross(xAxis);

    return new Matrix4f(
            xAxis.x,         yAxis.x,         zAxis.x,        0,
            xAxis.y,         yAxis.y,         zAxis.y,        0,
            xAxis.z,         yAxis.z,         zAxis.z,        0,
           -xAxis.dot(eye), -yAxis.dot(eye), -zAxis.dot(eye), 1
           );
}

public static void main(String[] args) {
    new Main().run();
}
}

顶点着色器:

varying vec3 color;

in vec4 position;

uniform mat4 projection;
uniform mat4 view;
uniform mat4 model;

void main() {
    color = gl_Color.rgb;
    gl_Position = projection * view * model * position;
}

Fragment Shader:

varying vec3 color;

void main() {
    gl_FragColor = vec4(color, 1);
}

3 个答案:

答案 0 :(得分:0)

尝试将片段着色器更改为:

varying vec3 color;

out vec4 out_color;

void main() {
    out_color = vec4(color, 1);
}

片段着色器应该在处理顶点着色器的输出后输出颜色。

答案 1 :(得分:0)

要使用glProgramUniformMatrix4fv,您需要先调用glGetProgramiv,或者在opengl 4.1及更高版本中,您可以使用getProjectionMatrix(0, 800, 0, 600, -1, 1).get(projectionFloat);并将其传递给shaderProgram。

您还应使用glVertex3f(100, 100, 50); glVertex3f(100, 400, 50); glVertex3f(400, 400, 50);检查着色器链接和验证错误。它可能会失败。

你的顶点也被剪掉了,{{1}}在这里你说1为“深度”,如果超出了它将无法渲染。这一切都很好,但在这里: {{1}} 你正在传递它50.如果你用50以上的东西改变投影矩阵你将不得不将它传递给-50,因为那是“向前”的方向。否则它将“落后于你”。

你应该使用顶点缓冲区而不是旧的固定opengl管道。

答案 2 :(得分:0)

我认为你创建正交投影矩阵的功能是正确的。

我强烈建议您阅读并理解有关如何推导投影矩阵的文章:query_devices()

作为奖励,这里有一个不同的实现,你可以适应你的个人课程:

public void registerTouchListener(ScrollView scrollview) {
  scrollview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
      Log.d("SDK", event.toString());
      return false;
    }
  });
}