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