3D Rendering oh yeaaaahhhhh
This commit is contained in:
parent
84020a922b
commit
907afc6577
8 changed files with 119 additions and 23 deletions
|
|
@ -1,8 +1,9 @@
|
|||
#version 450
|
||||
|
||||
layout(location = 0) in vec2 inTextCoords;
|
||||
layout(location = 0) out vec4 outFragColor;
|
||||
|
||||
void main()
|
||||
{
|
||||
outFragColor = vec4(1, 0, 0, 1);
|
||||
outFragColor = vec4(inTextCoords.x, inTextCoords.y, 0, 1);
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1,8 +1,16 @@
|
|||
#version 450
|
||||
|
||||
layout(location = 0) in vec3 inPos;
|
||||
layout(location = 1) in vec2 intTextCoords;
|
||||
layout(location = 0) out vec2 outTextCoords;
|
||||
|
||||
layout(push_constant) uniform matrices{
|
||||
mat4 projectionMatrix;
|
||||
mat4 modelMatrix;
|
||||
} push_constants;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = vec4(inPos,1);
|
||||
gl_Position = push_constants.projectionMatrix * push_constants.modelMatrix * vec4(inPos,1);
|
||||
outTextCoords = intTextCoords;
|
||||
}
|
||||
Binary file not shown.
|
|
@ -1,15 +1,25 @@
|
|||
package net.halbear.Terrain4J.EngineCore.Main;
|
||||
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.EngineConfig;
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.GameLogic;
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.InitData;
|
||||
import net.halbear.Terrain4J.EngineCore.Main.Scene.Actor;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.MeshData;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.ModelData;
|
||||
import org.joml.Vector3f;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class GameCore implements GameLogic {
|
||||
|
||||
private final Vector3f rotatingAngle = new Vector3f(1, 1, 1);
|
||||
private float angle = 0;
|
||||
private List<Float> angles = new ArrayList<>();
|
||||
private Actor CubeActor;
|
||||
private List<Actor> CubeActors = new ArrayList<>();
|
||||
|
||||
@Override
|
||||
public void cleanup() {
|
||||
|
||||
|
|
@ -17,22 +27,52 @@ public class GameCore implements GameLogic {
|
|||
|
||||
@Override
|
||||
public InitData Initialise(EngineInstance engineInstance) {
|
||||
var ModelID = "TriangleModel";
|
||||
List<MeshData> MeshDataList = new ArrayList<>();
|
||||
for(int i = 0; i < 5; i++){
|
||||
float PosXOffset = -1 + (float)Math.random() * 2;
|
||||
float PosYOffset = -1 + (float)Math.random() * 2;
|
||||
MeshDataList.add(
|
||||
new MeshData("triangle-mesh" + i, new float[]{
|
||||
PosXOffset, PosYOffset, 0.00f,
|
||||
PosXOffset + 0.05f,PosYOffset + 0.15f, 0.0f,
|
||||
PosXOffset + 0.1f, PosYOffset, 0.0f},
|
||||
new int[]{0, 1, 2})
|
||||
);
|
||||
float[] positions = new float[]{
|
||||
-0.25f, 0.25f, 0.25f,
|
||||
-0.25f, -0.25f, 0.25f,
|
||||
0.25f, -0.25f, 0.25f,
|
||||
0.25f, 0.25f, 0.25f,
|
||||
-0.25f, 0.25f, -0.25f,
|
||||
0.25f, 0.25f, -0.25f,
|
||||
-0.25f, -0.25f, -0.25f,
|
||||
0.25f, -0.25f, -0.25f,
|
||||
};
|
||||
float[] textCoords = new float[]{
|
||||
0.0f, 0.0f,
|
||||
0.5f, 0.0f,
|
||||
1.0f, 0.0f,
|
||||
1.0f, 0.5f,
|
||||
1.0f, 1.0f,
|
||||
0.5f, 1.0f,
|
||||
0.0f, 1.0f,
|
||||
0.0f, 0.5f,
|
||||
};
|
||||
int[] indices = new int[]{
|
||||
// Front face
|
||||
0, 1, 3, 3, 1, 2,
|
||||
// Top Face
|
||||
4, 0, 3, 5, 4, 3,
|
||||
// Right face
|
||||
3, 2, 7, 5, 3, 7,
|
||||
// Left face
|
||||
6, 1, 0, 6, 0, 4,
|
||||
// Bottom face
|
||||
2, 1, 6, 2, 6, 7,
|
||||
// Back face
|
||||
7, 6, 4, 7, 4, 5,
|
||||
};
|
||||
var modelID = "CubeModel";
|
||||
MeshData meshData = new MeshData("cube_mesh", positions, textCoords, indices);
|
||||
List<MeshData> meshDataList = new ArrayList<>();
|
||||
meshDataList.add(meshData);
|
||||
ModelData modelData = new ModelData(modelID, meshDataList);
|
||||
List<ModelData> models = new ArrayList<>();
|
||||
models.add(modelData);
|
||||
for(int i = 0; i < 150; i++) {
|
||||
angles.add((float)Math.floor(Math.random() * 360));
|
||||
CubeActors.add(new Actor("CubeActor", modelID, new Vector3f(-4f + (float)Math.floor(Math.random() * 8), -4f + (float)Math.floor(Math.random() * 8), -2f + (float)Math.floor(Math.random() * -8))));
|
||||
engineInstance.scene().AddActor(CubeActors.get(i));
|
||||
}
|
||||
ModelData modelData = new ModelData(ModelID, MeshDataList);
|
||||
List<ModelData> models = new ArrayList<>();
|
||||
models.add(modelData);
|
||||
return new InitData(models);
|
||||
}
|
||||
|
||||
|
|
@ -43,7 +83,11 @@ public class GameCore implements GameLogic {
|
|||
|
||||
@Override
|
||||
public void Update(EngineInstance engineInstance, long FrameDiffNanoSeconds) {
|
||||
|
||||
for(int i = 0; i < angles.size(); i++) {
|
||||
angles.set(i, (angles.get(i) + 1.0f) % 360);
|
||||
CubeActors.get(i).GetRotation().identity().rotateAxis((float) EngineConfig.DegToRad * angles.get(i), rotatingAngle);
|
||||
CubeActors.get(i).UpdateModelMatrix();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -27,8 +27,8 @@ public class PrimaryRuntime {
|
|||
|
||||
public PrimaryRuntime(String windowTitle, GameLogic appLogic) {
|
||||
window = new Window(windowTitle);
|
||||
InitData initData = appLogic.Initialise(engineInstance);
|
||||
engineInstance = new EngineInstance(window, new Scene(window));
|
||||
InitData initData = appLogic.Initialise(engineInstance);
|
||||
PrimaryThread = new MainThread(engineInstance, appLogic);
|
||||
FastThread = new FastTickThread(engineInstance, appLogic);
|
||||
DrawingThread = new RenderThread(window, engineInstance, initData, appLogic);
|
||||
|
|
|
|||
|
|
@ -26,6 +26,9 @@ public class Project3D {
|
|||
public float GetNearZ(){
|
||||
return ZNearPlane;
|
||||
}
|
||||
|
||||
public Matrix4f GetProjectionMatrix(){return ProjectionMatrix;}
|
||||
|
||||
public void Resize(int Width, int Height){
|
||||
ProjectionMatrix.identity();
|
||||
ProjectionMatrix.perspective(FOV, (float)Width/(float)Height, ZNearPlane, ZFarPlane, true);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,11 @@
|
|||
package net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DisplayToScreen;
|
||||
|
||||
import net.halbear.Terrain4J.EngineCore.Display.Render;
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.EngineConfig;
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.EngineInstance;
|
||||
import net.halbear.Terrain4J.EngineCore.Logic.VulkanContext;
|
||||
import net.halbear.Terrain4J.EngineCore.Main.Scene.Actor;
|
||||
import net.halbear.Terrain4J.EngineCore.Main.Scene.Scene;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.Pipeline.Images.Attachment;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.Pipeline.Pipeline;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.Pipeline.PipelineBuildInfo;
|
||||
|
|
@ -14,6 +18,7 @@ import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.VulkanMesh;
|
|||
import net.halbear.Terrain4J.EngineCore.Vulkan.Rendering.VkModel.VulkanModel;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Structure.DeviceLayers.CommandBuffer;
|
||||
import net.halbear.Terrain4J.EngineCore.Vulkan.Util.VulkanUtils;
|
||||
import org.joml.Matrix4f;
|
||||
import org.lwjgl.system.MemoryStack;
|
||||
import org.lwjgl.system.MemoryUtil;
|
||||
import org.lwjgl.util.shaderc.Shaderc;
|
||||
|
|
@ -22,6 +27,7 @@ import org.lwjgl.vulkan.*;
|
|||
import java.nio.ByteBuffer;
|
||||
import java.nio.LongBuffer;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.lwjgl.vulkan.KHRSwapchain.VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
||||
import static org.lwjgl.vulkan.KHRSynchronization2.VK_IMAGE_LAYOUT_ATTACHMENT_OPTIMAL_KHR;
|
||||
|
|
@ -143,7 +149,7 @@ public class SceneRender {//dynamic rendering
|
|||
RenderInfo = CreateRenderInfo(vulkanContext, AttachmentInfoColour, AttInfoDepth);
|
||||
}
|
||||
|
||||
public void Render(VulkanContext vulkanContext, CommandBuffer commandBuffer,ModelsCache modelsCache, int ImageIndex){
|
||||
public void Render(EngineInstance engineInstance,VulkanContext vulkanContext, CommandBuffer commandBuffer, ModelsCache modelsCache, int ImageIndex){
|
||||
UpdateColours(vulkanContext);
|
||||
try(var MemStack = MemoryStack.stackPush()){
|
||||
SwapChain swapChain = vulkanContext.GetSwapChain();
|
||||
|
|
@ -184,8 +190,25 @@ public class SceneRender {//dynamic rendering
|
|||
LongBuffer offsets = MemStack.mallocLong(1).put(0,0L);
|
||||
LongBuffer vertexBuffer = MemStack.mallocLong(1);
|
||||
|
||||
//Fetch Actor Code to be written Later
|
||||
var VulkanModels = modelsCache.GetModelMap().values();
|
||||
Scene scene = engineInstance.scene();
|
||||
List<Actor> Actors = scene.GetActors();
|
||||
int ActorCount = Actors.size();
|
||||
for(int i = 0; i < ActorCount; i++){
|
||||
var Actor = Actors.get(i);
|
||||
VulkanModel model = modelsCache.GetModel(Actor.GetModelID());
|
||||
List<VulkanMesh> VkMeshList = model.GetVkMeshList();
|
||||
int MeshCount = VkMeshList.size();
|
||||
SetPushConstants(CommandHandle, scene.GetProjection().GetProjectionMatrix(), Actor.GetModelMatrix());
|
||||
for(int j = 0; j < MeshCount; j++){
|
||||
var VkMesh = VkMeshList.get(j);
|
||||
vertexBuffer.put(0,VkMesh.VerticesBuffer().GetBuffer());
|
||||
vkCmdBindVertexBuffers(CommandHandle,0,vertexBuffer,offsets);
|
||||
vkCmdBindIndexBuffer(CommandHandle,VkMesh.IndicesBuffer().GetBuffer(),0,VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(CommandHandle,VkMesh.IndicesCount(),1,0,0,0);
|
||||
}
|
||||
}
|
||||
|
||||
/*var VulkanModels = modelsCache.GetModelMap().values(); // old outdated code
|
||||
for(VulkanModel VkModel : VulkanModels){
|
||||
|
||||
for(VulkanMesh mesh : VkModel.GetVkMeshList()){
|
||||
|
|
@ -194,7 +217,7 @@ public class SceneRender {//dynamic rendering
|
|||
vkCmdBindIndexBuffer(CommandHandle,mesh.IndicesBuffer().GetBuffer(),0,VK_INDEX_TYPE_UINT32);
|
||||
vkCmdDrawIndexed(CommandHandle,mesh.IndicesCount(),1,0,0,0);
|
||||
}
|
||||
}
|
||||
}*/
|
||||
vkCmdEndRendering(CommandHandle);
|
||||
|
||||
VulkanUtils.ImageBarrier(MemStack, CommandHandle, SwapChainImage,
|
||||
|
|
@ -205,6 +228,23 @@ public class SceneRender {//dynamic rendering
|
|||
}
|
||||
}
|
||||
|
||||
private void SetPushConstants(VkCommandBuffer CmdHandle, Matrix4f ProjectionMatrix, Matrix4f modelMatrix){
|
||||
ProjectionMatrix.get(PushConstBuffer);
|
||||
modelMatrix.get(VulkanUtils.MATRIX4X4_SIZE,PushConstBuffer);
|
||||
vkCmdPushConstants(CmdHandle, VkPipeline.GetVulkanPipelineLayout(), VK_SHADER_STAGE_VERTEX_BIT, 0, PushConstBuffer);
|
||||
}
|
||||
|
||||
public void Resize(VulkanContext VkCtx){
|
||||
Arrays.asList(RenderInfo).forEach(VkRenderingInfo::free);
|
||||
Arrays.asList(AttInfoDepth).forEach(VkRenderingAttachmentInfo::free);
|
||||
Arrays.asList(AttachmentInfoColour).forEach(VkRenderingAttachmentInfo.Buffer::free);
|
||||
Arrays.asList(AttDepth).forEach(Attachment->Attachment.CleanUp(VkCtx));
|
||||
AttDepth = createDepthAttachments(VkCtx);
|
||||
AttachmentInfoColour = CreateColourAttachmentsInfo(VkCtx, ClearValueColour);
|
||||
AttInfoDepth = createDepthAttachmentsInfo(VkCtx, AttDepth, ClearValueDepth);
|
||||
RenderInfo = CreateRenderInfo(VkCtx, AttachmentInfoColour, AttInfoDepth);
|
||||
}
|
||||
|
||||
private static VkRenderingInfo[] CreateRenderInfo(VulkanContext vulkanContext, VkRenderingAttachmentInfo.Buffer[] ColourAttachments, VkRenderingAttachmentInfo[] DepthAttachments){
|
||||
SwapChain swapChain = vulkanContext.GetSwapChain();
|
||||
int ImageCount = swapChain.GetImageCount();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue