Matrices not working in openGL

I am trying to create an orthographic matrix and a suitable model and viewing matrix to view a simple triangle, just to check the matrices. I keep getting a blank screen, and I have no idea what is going on. I use LWJGL, and for vertices and matrices I use a library called JOML.

Main class (single class):

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();
}
}

Vertex Shader:

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);
}
+4
source share
3 answers

Try changing the fragment shader as follows:

varying vec3 color;

out vec4 out_color;

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

Fragment shaders should output color after processing the results from the vertex shader.

0
source

glUniformMatrix4fv, glUseProgram, opengl 4.1 glProgramUniformMatrix4fv shaderProgram.

, glGetProgramiv. .

, getProjectionMatrix(0, 800, 0, 600, -1, 1).get(projectionFloat); "1" "", - , . , : glVertex3f(100, 100, 50); glVertex3f(100, 400, 50); glVertex3f(400, 400, 50); 50. - 50, -50, "". "".

, opengl.

0

, .

I would highly recommend reading and understanding this article on how to get projection matrices: deriving projection matrices

And as a bonus, here is another implementation that you can adapt to your personal classes:

Matrix4 orthographic_matrix(float width, float height, float z_near, float z_far)
{
    float x_right = width * 0.5f;
    float x_left = width * -0.5f;
    float y_top = height * 0.5f;
    float y_bottom = height * -0.5f;

    float one_delta_x = 1.0f / (x_right - x_left);
    float one_delta_y = 1.0f / (y_top - y_bottom);
    float one_delta_z = 1.0f / (z_far - z_near);

    Matrix4 proj;

    proj(0, 0) = 2.0f * one_delta_x;
    proj(1, 1) = 2.0f * one_delta_y;
    proj(2, 2) = -2.0f * one_delta_z;

    proj(3, 0) = -1.0f * (x_right + x_left) * one_delta_x;
    proj(3, 1) = -1.0f * (y_top + y_bottom) * one_delta_y;
    proj(3, 2) = 1.0f * (z_far + z_near) * one_delta_z;
    proj(3, 3) = 1.0f;

    return proj;
}
0
source

Source: https://habr.com/ru/post/1622656/


All Articles