Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 7 of 7

Thread: 3D Engine does not render model

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    6

    3D Engine does not render model

    Hello guys,

    I am new to these forums, but not that entirely new to openGL. I am working on a 3D engine in java, following some tutorials, but also trying different stuff out. After refactoring my engine, however, I am unable to display any models in the scene. In fact what I get is the background color I set in a uniform in my fragment shader.

    Main area of refactoring:
    • I switched from using lwjgl classes for Matrix4f and Vertex3f and 2f to using my own classes.
    • I also added the Quaternion class and changed the way rotation is calculated.
    • due to points 1 and 2 I also had to modify the generation of transofrmation matrix, viewMatrix and projectionMatrix.



    I set up a small test to check if it works and lo and behold it does not. The test initializes a model.
    • Then I create a player object (which is controlled with wsad keys).
    • Then I create the camera object (which is ocntrolled by mouse and arrow keys).
    • Then I add the light.
    • Next I set up a renderer class and enter the main loop.
    • There I gather information about input
    • Update camera and player
    • Render player
    • Update display


    Here is the test file with the main methods responsible for the logic. I would really appreciate any feedback.
    The project can be also found here: https://github.com/TomaszNaglik/Another-attempt


    Code :
    public class MainGameLoop {
    	public static void main(String[] args) {
    		DisplayManager.createDisplay();
    		Loader loader = new Loader();
     
    		Vector3f standardScale = new Vector3f(1, 1, 1);
    		Quaternion standardRotation = new Quaternion(0, 0, 0, 1);
     
    		TexturedModel dragonModel = new TexturedModel(OBJFileLoader.loadOBJ("dragon", loader),
    				new ModelTexture(loader.loadTexture("white")));
    		dragonModel.print();
    		System.out.println("");
     
    		Player player = new Player(dragonModel, new Vector3f(0, 0, -20), standardRotation, standardScale); 	// problem could be here
    		Camera camera = new Camera(new Vector3f(0, 0, 0), standardRotation, standardScale); 				// problem could be here
     
    		Light light = new Light(new Vector3f(0, 2000000, 20), new Vector3f(1, 1, 1)); 						// problem could be here
    		player.printoutDirection();
    		camera.printoutDirection();
    		light.printoutDirection();
    		System.out.println("");
     
    		MasterRenderer renderer = new MasterRenderer(); // problem could be here
     
    		while (DisplayManager.notClose() && !Input.GetKey(Input.KEY_Q)) {
     
     
     
    			Input.update();
    			camera.update();
    			player.update();
     
     
    			renderer.processEntity(player);
    			renderer.render(light, camera);
     
    			DisplayManager.updateDisplay();
    			// renderer.checkError();
    		}
    		renderer.cleanUp();
    		loader.cleanUp();
    		DisplayManager.closeDisplay();
    	}
    }

    Code :
    public MasterRenderer() {
    		createProjectionMatrix();
    		this.renderer = new EntityRenderer(shader, projectionMatrix);
    		this.terrainRenderer = new TerrainRenderer(terrainShader, projectionMatrix);
    		enableCulling();
     
    	}
     
    public void render(Light sun, Camera camera) {
    		prepare();
     
    		shader.start();
    		shader.loadSkyColour(new Vector3f(RED, GREEN, BLUE));
    		shader.loadFogVariables(density, gradient);
    		shader.loadLight(sun);
    		shader.loadViewMatrix(camera);
    		renderer.render(entities);
    		shader.stop();
     
    		terrainShader.start();
    		terrainShader.loadSkyColour(new Vector3f(RED, GREEN, BLUE));
    		terrainShader.loadFogVariables(density, gradient);
    		terrainShader.loadLight(sun);
    		terrainShader.loadViewMatrix(camera);
    		terrainRenderer.render(terrains);
    		terrainShader.stop();
    		entities.clear();
    		terrains.clear();
    	}
     
    private void createProjectionMatrix() {
    		float aspectRatio = (float) Display.getWidth() / (float) Display.getHeight();
    		projectionMatrix = Maths.createPerspectiveMatrix(FOV, aspectRatio, NEAR_PLANE, FAR_PLANE);
     
    	}

    Code :
    public static Matrix4f createPerspectiveMatrix(float fov, float aspectRatio, float zNear, float zFar) {
    		return new Matrix4f().InitPerspective(fov, aspectRatio, zNear, zFar);
    	}

    Code :
    public Matrix4f InitPerspective(float fov, float aspectRatio, float zNear, float zFar) {
    		float tanHalfFOV = (float) Math.tan(fov / 2);
    		float zRange = zNear - zFar;
     
    		m[0][0] = 1.0f / (tanHalfFOV * aspectRatio);
    		m[0][1] = 0;
    		m[0][2] = 0;
    		m[0][3] = 0;
    		m[1][0] = 0;
    		m[1][1] = 1.0f / tanHalfFOV;
    		m[1][2] = 0;
    		m[1][3] = 0;
    		m[2][0] = 0;
    		m[2][1] = 0;
    		m[2][2] = (-zNear - zFar) / zRange;
    		m[2][3] = 2 * zFar * zNear / zRange;
    		m[3][0] = 0;
    		m[3][1] = 0;
    		m[3][2] = 1;
    		m[3][3] = 0;
     
    		return this;
    	}
    Code :
    public void loadViewMatrix(Camera camera) {
    		Matrix4f viewMatrix = Maths.createViewMatrix(camera);
    		super.loadMatrix(location_viewMatrix, viewMatrix);
    	}

    Code :
    public static Matrix4f createViewMatrix(Camera camera)
    	{
     
    		Matrix4f viewMatrix = new Matrix4f();
    		Matrix4f cameraRotation = camera.getTransform().GetTransformedRot().Conjugate().ToRotationMatrix();
    		Vector3f cameraPos = camera.getTransform().GetTransformedPos().Mul(-1);
     
    		Matrix4f cameraTranslation = new Matrix4f().InitTranslation(cameraPos);
     
    		return viewMatrix.Mul(cameraRotation.Mul(cameraTranslation));
     
     
    	}

  2. #2
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    6
    I would really appreciate any insight.

    What is odd, is the fact that when I run the program and print out the state of every initialized object, all initializations are correct.

  3. #3
    Junior Member Regular Contributor
    Join Date
    Sep 2013
    Posts
    171
    From your descriptions its save to assume that your matrix and vector implementations are just wrong. If it worked with the other version but doesnt with yours then you made a mistake somewhere. Instead of going through your code and doing your debugging work for you I would rather ask: Why do you try to roll your own version of matrix math? If the official versions worked, then why would you need to do it all again?

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    6
    Well, the framework that I was using used three variables for rotation (yaw, pitch and roll). I found out that if I want to have freedom of movement in all directions this solution is not optimal (I would get strange effects where when the camera is pointing down and I roll, I actually yaw, because the the local grid system is always the same as the world grid system).

    I wanted to implement quaternions to be able to have a seperate local coordinate system for the camera. The matrix calculations used are from a different framework. Plus when I compared both calculation versions, they seem to do the same thing (although one is working with yaw pitch variables and the other with quaternions).

  5. #5
    Junior Member Regular Contributor
    Join Date
    Sep 2013
    Posts
    171
    What kind of Matrix package from LWJGL are you using? It sounds like you are not using the JOML packages as they definitely have support for quaternions. This is the current official matrix package to be used with LWJGL, it is tested and very efficient. Could it be that you are still using the old LWJGL2 packages?

    By the way, the problem you were having is called Gimbal Lock. Link: https://en.wikipedia.org/wiki/Gimbal_lock

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    6
    I used the Matrix4f class written by BennyBox for his tutorials, which works well for him, but I get your point. I could use the JOML package instead. I am using LWJGL 3 by the way.

  7. #7
    Junior Member Newbie
    Join Date
    Oct 2017
    Posts
    6
    I switched to JMOL and can now actually see the dragon, although it is flickering all over the place. I must have screwed up something in the process then. It is still an improvment. Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •