OpenGL projection matrix/view matrix/model matrix

Started by Mihai_Ionut_Floares, November 10, 2020, 23:50:37

Previous topic - Next topic


Since there are few tutorials for OpenGL in java I followed this tutorial in c++ and make some adaptations:
I successfully loaded shaders like that:
package me.mikey.engine;

import java.nio.IntBuffer;
import java.util.Scanner;

import org.lwjgl.BufferUtils;
import org.lwjgl.PointerBuffer;

import static org.lwjgl.opengl.GL.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.opengl.GL15.*;
import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.*;

public class loadShader {
	int LoadShaders(String vertex_file_path, String fragment_file_path) throws FileNotFoundException{

		// Create the shaders
		int VertexShaderID = glCreateShader(GL_VERTEX_SHADER);
		int FragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);

		// Read the Vertex Shader code from the file
		String VertexShaderCode;
		File fileVertex = new File(vertex_file_path); 
		Scanner scVertex = new Scanner(fileVertex); 
		VertexShaderCode =;

		// Read the Fragment Shader code from the file
		String FragmentShaderCode;
		File fileFragment = new File(fragment_file_path); 
		Scanner scFragment = new Scanner(fileFragment); 
		FragmentShaderCode =;

		int Result = GL_FALSE;
		int InfoLogLength = 0;

		// Compile Vertex Shader
		System.out.println("Compiling shader: " + vertex_file_path);
		glShaderSource(VertexShaderID, VertexShaderCode);
		IntBuffer ResultBuffer = BufferUtils.createIntBuffer(1);
		IntBuffer InfoLogLengthBuffer = BufferUtils.createIntBuffer(1);
		// Check Vertex Shader
		glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, ResultBuffer);
		glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, InfoLogLengthBuffer);

		// Compile Fragment Shader
		System.out.println("Compiling shader: " + fragment_file_path);
		glShaderSource(FragmentShaderID, FragmentShaderCode);

		// Check Fragment Shader
		glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, ResultBuffer);
		glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, InfoLogLengthBuffer);

		// Link the program
		System.out.println("Linking program");
		int ProgramID = glCreateProgram();
		glAttachShader(ProgramID, VertexShaderID);
		glAttachShader(ProgramID, FragmentShaderID);

		// Check the program
		glGetProgramiv(ProgramID, GL_LINK_STATUS, ResultBuffer);
		glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, InfoLogLengthBuffer);
		glDetachShader(ProgramID, VertexShaderID);
		glDetachShader(ProgramID, FragmentShaderID);

		return ProgramID;

Maybe not the most pretty way, however it works I have a blue triangle. Now I want to add space into my 3d renderer. The third tutorial is about matrices, but they use a c++ library called glm which I guess it stands for graphics library math or something. I researched and found that lwjgl used to have a solution for matrices in version 2, but now we need to find another way. I found JOML, but I don't want to download it from some reasons that I can't specify. I decided to make my own matrices and vector classes. I used a class with 4 floats for vec4 and a class with 4x4 2d array for matrices. I added the operations(mat4x4 * mat4x4, mat4x4*vec4, vec4 + vec*, and the translate and scale of a matrix using a vec3) I can't figure out how to make the rotation matrix, the projection matrix and the view matrix. I know that the following code is very very messy, full of garbage, please don't judge me for that. I never created a library before and I don't know exactly how static, final and all of these modifiers work:
package math;

public class mat4 {
	public float[][] mat = new float[4][4];
	public vec4 multiplyWithVec(vec4 vector) {
		vec4 result = new vec4(0f, 0f, 0f, 0f);
		result.x = this.mat[0][0] * vector.x + this.mat[0][1] * vector.y + this.mat[0][2] * vector.z + this.mat[0][3] * vector.w;
		result.y = this.mat[1][0] * vector.x + this.mat[1][1] * vector.y + this.mat[1][2] * vector.z + this.mat[1][3] * vector.w;
		result.z = this.mat[2][0] * vector.x + this.mat[2][1] * vector.y + this.mat[2][2] * vector.z + this.mat[2][3] * vector.w;			
		result.w = this.mat[3][0] * vector.x + this.mat[3][1] * vector.y + this.mat[3][2] * vector.z + this.mat[3][3] * vector.w;
		return result;
	public mat4 multiplyWithMat(mat4 mat4x4) {
		mat4 result = new mat4();
		for(int n = 0;n<4;n++) {
			for(int i = 0;i<4;i++) {
				for(int j = 0;j<4;j++) {
					result.mat[i][n] += this.mat[i][j] * mat4x4.mat[j][n];
		return result;
	public void translate(vec3 vector) {
		this.mat[0][0] = 1;
		this.mat[1][1] = 1;
		this.mat[2][2] = 1;
		this.mat[0][3] = vector.x;
		this.mat[1][3] = vector.y;
		this.mat[2][3] = vector.z;
		this.mat[3][3] = 1;
	public mat4 scale(vec3 scale) {
		mat4 result = new mat4();
		result.mat[0][0] = scale.x;
		result.mat[1][1] = scale.y;
		result.mat[2][2] = scale.z;
		result.mat[3][3] = 1;
		return result;

package math;

public class vec3 {
	public float x, y, z;
	public vec3(float x, float y, float z) {
		this.x = x;
		this.y = y;
		this.z = z;

package math;

public class vec4 {
	public float x, y, z, w;
	public vec4(float x, float y, float z, float w) {
		this.x = x;
		this.y = y;
		this.z = z;
		this.w = w;