pull/12/head
hYdos 2020-03-03 15:56:37 +10:00
parent d8191d9171
commit 1feffa5cd0
18 changed files with 346 additions and 146 deletions

View File

@ -20,8 +20,13 @@ import com.github.hydos.ginger.engine.common.obj.ModelLoader;
import com.github.hydos.ginger.engine.opengl.api.*;
import com.github.hydos.ginger.engine.opengl.postprocessing.PostProcessing;
import com.github.hydos.ginger.engine.opengl.render.MasterRenderer;
<<<<<<< Upstream, based on origin/liteCraft
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.utils.GLLoader;
=======
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import com.github.hydos.ginger.engine.opengl.utils.GlLoader;
>>>>>>> 9a8fdc4 yEs
import tk.valoeghese.gateways.client.io.*;
@ -130,7 +135,7 @@ public class Litecraft extends Game
// set up ginger utilities
GingerUtils.init();
//Set the player model
TexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png");
GLTexturedModel playerModel = ModelLoader.loadGenericCube("block/cubes/stone/brick/stonebrick.png");
Light sun = new Light(new Vector3f(0, 105, 0), new Vector3f(0.9765625f, 0.98828125f, 0.05859375f), new Vector3f(0.002f, 0.002f, 0.002f));
FontType font = new FontType(GLLoader.loadFontAtlas("candara.png"), "candara.fnt");
this.engine = new GingerGL();

View File

@ -3,7 +3,7 @@ package com.github.halotroop.litecraft.types.block;
import java.util.*;
import com.github.hydos.ginger.engine.common.obj.ModelLoader;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public class Block
{
@ -36,7 +36,7 @@ public class Block
}
}
public TexturedModel model;
public GLTexturedModel model;
private final boolean visible, fullCube;
private final float caveCarveThreshold;
public final String identifier;
@ -52,7 +52,7 @@ public class Block
{ return this.caveCarveThreshold; }
protected Block(Properties properties)
{ this((TexturedModel) null, properties); }
{ this((GLTexturedModel) null, properties); }
protected Block(String texture, Properties properties)
{
@ -60,7 +60,7 @@ public class Block
this.texture = texture;
}
protected Block(TexturedModel model, Properties properties)
protected Block(GLTexturedModel model, Properties properties)
{
this.model = model;
this.visible = properties.visible;

View File

@ -3,10 +3,10 @@ package com.github.halotroop.litecraft.types.entity;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public abstract class Entity extends RenderObject
{
public Entity(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
public Entity(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{ super(model, position, rotX, rotY, rotZ, scale); }
}

View File

@ -15,7 +15,7 @@ import com.github.halotroop.litecraft.world.dimension.Dimension;
import com.github.halotroop.litecraft.world.gen.*;
import com.github.halotroop.litecraft.world.gen.modifier.WorldModifier;
import com.github.hydos.ginger.engine.common.obj.ModelLoader;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import it.unimi.dsi.fastutil.longs.*;
@ -86,9 +86,15 @@ public class World implements BlockAccess, WorldGenConstants
public PlayerEntity spawnPlayer(float x, float y, float z)
{
// Player model and stuff
<<<<<<< Upstream, based on origin/liteCraft
TexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/soil/dirt.png");
this.playerEntity = new PlayerEntity(dirtModel, new Vector3f(x, y, z), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
this.playerEntity.setVisible(false);
=======
GLTexturedModel dirtModel = ModelLoader.loadGenericCube("block/cubes/soil/dirt.png");
this.player = new Player(dirtModel, new Vector3f(x, y, z), 0, 180f, 0, new Vector3f(0.2f, 0.2f, 0.2f));
this.player.setVisible(false);
>>>>>>> 9a8fdc4 yEs
// Generate world around player
long time = System.currentTimeMillis();
System.out.println("Generating world!");

View File

@ -0,0 +1,91 @@
package com.github.halotroop.litecraft.world.block;
import org.joml.Matrix4f;
import org.lwjgl.opengl.*;
import com.github.halotroop.litecraft.types.block.BlockInstance;
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.elements.objects.RenderObject;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.math.Maths;
import com.github.hydos.ginger.engine.opengl.render.Renderer;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import com.github.hydos.ginger.engine.opengl.render.shaders.StaticShader;
import com.github.hydos.ginger.engine.opengl.utils.GlLoader;
public class BlockRenderer extends Renderer implements WorldGenConstants
{
public StaticShader shader;
public int atlasID;
public BlockRenderer(StaticShader shader, Matrix4f projectionMatrix)
{
this.shader = shader;
shader.start();
shader.loadProjectionMatrix(projectionMatrix);
shader.stop();
this.atlasID = GlLoader.createBlockAtlas();
}
private void prepBlockInstance(RenderObject entity)
{
Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(),
entity.getRotY(), entity.getRotZ(), entity.getScale());
shader.loadTransformationMatrix(transformationMatrix);
}
public void prepareModel(GLTexturedModel model)
{
GL30.glBindVertexArray(model.getRawModel().getVaoID());
GL20.glEnableVertexAttribArray(0);
GL20.glEnableVertexAttribArray(1);
GL20.glEnableVertexAttribArray(2);
}
public void unbindModel()
{
GL20.glDisableVertexAttribArray(0);
GL20.glDisableVertexAttribArray(1);
GL20.glDisableVertexAttribArray(2);
GL30.glBindVertexArray(0);
}
public void enableWireframe()
{ if (GingerRegister.getInstance().wireframe)
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_LINE); }
public void disableWireframe()
{ if (GingerRegister.getInstance().wireframe)
GL11.glPolygonMode(GL11.GL_FRONT_AND_BACK, GL11.GL_FILL); }
public void prepareRender()
{
// TODO: combine VBOS
shader.start();
shader.loadSkyColour(Window.getColour());
shader.loadViewMatrix(GingerRegister.getInstance().game.data.camera);
shader.loadFakeLightingVariable(true);
shader.loadShine(1, 1);
GL13.glActiveTexture(GL13.GL_TEXTURE0);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, atlasID);
enableWireframe();
}
public void render(BlockInstance[] renderList)
{
prepareRender();
for (BlockInstance entity : renderList) {
if (entity != null && entity.getModel() != null)
{
GLTexturedModel blockModel = entity.getModel();
GL11.glBindTexture(GL11.GL_TEXTURE_2D, blockModel.getTexture().getTextureID());
prepBlockInstance(entity);
GL11.glDrawElements(GL11.GL_TRIANGLES, blockModel.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
}
}
// disableWireframe();
// shader.stop();
}
}

View File

@ -10,9 +10,10 @@ import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.common.info.RenderAPI;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.common.obj.ModelLoader;
import com.github.hydos.ginger.engine.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.api.VKGinger;
import com.github.hydos.ginger.engine.vulkan.model.VKVertices;
import com.github.hydos.ginger.engine.vulkan.model.*;
import com.github.hydos.ginger.engine.vulkan.render.renderers.*;
import com.github.hydos.ginger.engine.vulkan.render.ubo.*;
import com.github.hydos.ginger.engine.vulkan.shaders.*;
@ -265,7 +266,7 @@ public class VulkanStarter
final VkQueue queue = createDeviceQueue(device, queueFamilyIndex);
final long renderPass = ExampleRenderer.createRenderPass(device, colorAndDepthFormatAndSpace.colorFormat, colorAndDepthFormatAndSpace.depthFormat);
final long renderCommandPool = createCommandPool(device, queueFamilyIndex);
VKVertices vertices = TempMethods.createVertices(memoryProperties, device);
VKVertices vertices = VKModelConverter.convertModel(ModelLoader.getCubeMesh(), memoryProperties, device);
Ubo ubo = new Ubo(memoryProperties, device);
final long descriptorPool = createDescriptorPool(device);
final long descriptorSetLayout = createDescriptorSetLayout(device);

View File

@ -0,0 +1,101 @@
package com.github.hydos.ginger.engine.common.elements.objects;
import org.joml.Vector3f;
import com.github.halotroop.litecraft.Litecraft;
import com.github.halotroop.litecraft.util.RelativeDirection;
import com.github.halotroop.litecraft.world.gen.WorldGenConstants;
import com.github.hydos.ginger.engine.common.Constants;
import com.github.hydos.ginger.engine.common.api.GingerRegister;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public class Player extends RenderObject implements WorldGenConstants
{
private boolean isInAir = false;
private double upwardsSpeed;
private boolean noWeight = true; // because the force of gravity on an object's mass is called WEIGHT, not gravity
private int chunkX, chunkY, chunkZ;
public Player(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{
super(model, position, rotX, rotY, rotZ, scale);
this.chunkX = (int) position.x >> POS_SHIFT;
this.chunkY = (int) position.y >> POS_SHIFT;
this.chunkZ = (int) position.z >> POS_SHIFT;
}
public void move(RelativeDirection direction)
{
float ry = (float) Math.toRadians(GingerRegister.getInstance().game.data.camera.getYaw());
switch (direction)
{
case FORWARD:
default:
position.z -= Math.cos(ry) * Constants.movementSpeed;
position.x += Math.sin(ry) * Constants.movementSpeed;
break;
case BACKWARD:
position.z += Math.cos(ry) * Constants.movementSpeed;
position.x -= Math.sin(ry) * Constants.movementSpeed;
break;
case LEFT:
ry -= RIGHT_ANGLE;
position.z -= Math.cos(ry) * Constants.movementSpeed;
position.x += Math.sin(ry) * Constants.movementSpeed;
break;
case RIGHT:
ry += RIGHT_ANGLE;
position.z -= Math.cos(ry) * Constants.movementSpeed;
position.x += Math.sin(ry) * Constants.movementSpeed;
break;
case UP:
if (this.noWeight) position.y += Constants.movementSpeed;
else this.jump();
break;
case DOWN:
position.y -= Constants.movementSpeed;
break;
}
}
private static final float RIGHT_ANGLE = (float) (Math.PI / 2f);
private void jump()
{
if (!isInAir)
{
isInAir = true;
this.upwardsSpeed = Constants.jumpPower;
}
}
public int getChunkX()
{ return this.chunkX; }
public int getChunkY()
{ return this.chunkY; }
public int getChunkZ()
{ return this.chunkZ; }
public void updateMovement()
{
super.increasePosition(0, (float) (upwardsSpeed * (Window.getTime())), 0);
upwardsSpeed += Constants.gravity.y() * Window.getTime(); // TODO: Implement 3D gravity
isInAir = false;
upwardsSpeed = 0;
int newChunkX = (int) position.x >> POS_SHIFT;
int newChunkY = (int) position.y >> POS_SHIFT;
int newChunkZ = (int) position.z >> POS_SHIFT;
if (newChunkX != this.chunkX || newChunkY != this.chunkY || newChunkZ != this.chunkZ)
{
Litecraft.getInstance().getWorld().updateLoadedChunks(newChunkX, newChunkY, newChunkZ);
this.chunkX = newChunkX;
this.chunkY = newChunkY;
this.chunkZ = newChunkZ;
}
}
}

View File

@ -2,17 +2,17 @@ package com.github.hydos.ginger.engine.common.elements.objects;
import org.joml.Vector3f;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
public class RenderObject
{
private TexturedModel model;
private GLTexturedModel model;
public Vector3f position;
private float rotX = 0, rotY = 0, rotZ = 0;
private Vector3f scale;
private boolean visible = true;
public RenderObject(TexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
public RenderObject(GLTexturedModel model, Vector3f position, float rotX, float rotY, float rotZ, Vector3f scale)
{
this.model = model;
this.position = position;
@ -31,7 +31,7 @@ public class RenderObject
public void z(float z)
{ this.position.z = z; }
public TexturedModel getModel()
public GLTexturedModel getModel()
{ return model; }
public Vector3f getPosition()
@ -63,7 +63,7 @@ public class RenderObject
this.rotZ += dz;
}
public void setModel(TexturedModel model)
public void setModel(GLTexturedModel model)
{ this.model = model; }
public void setPosition(Vector3f position)

View File

@ -1,22 +1,34 @@
package com.github.hydos.ginger.engine.common.obj;
import com.github.hydos.ginger.engine.common.obj.shapes.StaticCube;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture;
import com.github.hydos.ginger.engine.opengl.utils.GLLoader;
public class ModelLoader
{
public static TexturedModel loadGenericCube(String cubeTexture)
public static GLTexturedModel loadGenericCube(String cubeTexture)
{
Mesh data = StaticCube.getCube();
<<<<<<< Upstream, based on origin/liteCraft
TexturedModel tm = new TexturedModel(GLLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(cubeTexture));
=======
GLTexturedModel tm = new GLTexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(cubeTexture));
>>>>>>> 9a8fdc4 yEs
return tm;
}
public static TexturedModel loadModel(String objPath, String texturePath)
public static Mesh getCubeMesh() {
return StaticCube.getCube();
}
public static GLTexturedModel loadModel(String objPath, String texturePath)
{
Mesh data = OBJFileLoader.loadModel(objPath);
<<<<<<< Upstream, based on origin/liteCraft
return new TexturedModel(GLLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath));
=======
return new GLTexturedModel(GlLoader.loadToVAO(data.getVertices(), data.getIndices(), data.getNormals(), data.getTextureCoords()), new ModelTexture(texturePath));
>>>>>>> 9a8fdc4 yEs
}
}

View File

@ -9,16 +9,16 @@ import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture;
public class GingerUtils
{
public static TexturedModel createTexturedModel(String texturePath, String modelPath)
public static GLTexturedModel createTexturedModel(String texturePath, String modelPath)
{
TexturedModel model = ModelLoader.loadModel(modelPath, texturePath);
GLTexturedModel model = ModelLoader.loadModel(modelPath, texturePath);
return model;
}
public static TexturedModel createTexturedModel(String texturePath, String modelPath, String normalMapPath)
public static GLTexturedModel createTexturedModel(String texturePath, String modelPath, String normalMapPath)
{
RawModel model = NormalMappedObjLoader.loadOBJ(modelPath);
TexturedModel texturedModel = new TexturedModel(model, new ModelTexture(texturePath));
GLTexturedModel texturedModel = new GLTexturedModel(model, new ModelTexture(texturePath));
return texturedModel;
}

View File

@ -12,7 +12,7 @@ import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.GuiTexture;
import com.github.hydos.ginger.engine.common.elements.objects.*;
import com.github.hydos.ginger.engine.common.io.Window;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
import com.github.hydos.ginger.engine.opengl.render.renderers.*;
import com.github.hydos.ginger.engine.opengl.render.shaders.*;
import com.github.hydos.ginger.engine.opengl.shadow.ShadowMapMasterRenderer;
@ -41,8 +41,8 @@ public class MasterRenderer
private NormalMappingRenderer normalRenderer;
private Matrix4f projectionMatrix;
private ShadowMapMasterRenderer shadowMapRenderer;
private Map<TexturedModel, List<RenderObject>> entities = new HashMap<TexturedModel, List<RenderObject>>();
private Map<TexturedModel, List<RenderObject>> normalMapEntities = new HashMap<TexturedModel, List<RenderObject>>();
private Map<GLTexturedModel, List<RenderObject>> entities = new HashMap<GLTexturedModel, List<RenderObject>>();
private Map<GLTexturedModel, List<RenderObject>> normalMapEntities = new HashMap<GLTexturedModel, List<RenderObject>>();
public MasterRenderer(Camera camera)
{
@ -95,7 +95,7 @@ public class MasterRenderer
private void processEntity(RenderObject entity)
{
TexturedModel entityModel = entity.getModel();
GLTexturedModel entityModel = entity.getModel();
List<RenderObject> batch = entities.get(entityModel);
if (batch != null)
{
@ -111,7 +111,7 @@ public class MasterRenderer
private void processEntityWithNormal(RenderObject entity)
{
TexturedModel entityModel = entity.getModel();
GLTexturedModel entityModel = entity.getModel();
List<RenderObject> batch = normalMapEntities.get(entityModel);
if (batch != null)
{

View File

@ -2,12 +2,12 @@ package com.github.hydos.ginger.engine.opengl.render.models;
import com.github.hydos.ginger.engine.opengl.render.texture.ModelTexture;
public class TexturedModel
public class GLTexturedModel
{
private RawModel rawModel;
private ModelTexture texture;
public TexturedModel(RawModel model, ModelTexture texture)
public GLTexturedModel(RawModel model, ModelTexture texture)
{
this.rawModel = model;
this.texture = texture;

View File

@ -48,7 +48,7 @@ public class NormalMappingRenderer extends Renderer
shader.loadOffset(0, 0);
}
private void prepareTexturedModel(TexturedModel model)
private void prepareTexturedModel(GLTexturedModel model)
{
RawModel rawModel = model.getRawModel();
GL30.glBindVertexArray(rawModel.getVaoID());
@ -66,11 +66,11 @@ public class NormalMappingRenderer extends Renderer
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getNormalMap());
}
public void render(Map<TexturedModel, List<RenderObject>> entities, Vector4f clipPlane, List<Light> lights, Camera camera)
public void render(Map<GLTexturedModel, List<RenderObject>> entities, Vector4f clipPlane, List<Light> lights, Camera camera)
{
shader.start();
prepare(clipPlane, lights, camera);
for (TexturedModel model : entities.keySet())
for (GLTexturedModel model : entities.keySet())
{
prepareTexturedModel(model);
List<RenderObject> batch = entities.get(model);

View File

@ -36,7 +36,7 @@ public class ObjectRenderer extends Renderer
shader.loadTransformationMatrix(transformationMatrix);
}
private void prepareTexturedModel(TexturedModel model)
private void prepareTexturedModel(GLTexturedModel model)
{
RawModel rawModel = model.getRawModel();
GL30.glBindVertexArray(rawModel.getVaoID());
@ -58,9 +58,9 @@ public class ObjectRenderer extends Renderer
GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getTextureID());
}
public void render(Map<TexturedModel, List<RenderObject>> entities)
public void render(Map<GLTexturedModel, List<RenderObject>> entities)
{
for (TexturedModel model : entities.keySet())
for (GLTexturedModel model : entities.keySet())
{
prepareTexturedModel(model);
List<RenderObject> batch = entities.get(model);
@ -94,7 +94,7 @@ public class ObjectRenderer extends Renderer
{
if (entity != null && entity.getModel() != null)
{
TexturedModel model = entity.getModel();
GLTexturedModel model = entity.getModel();
prepareTexturedModel(model);
prepareInstance(entity);
GL11.glDrawElements(GL11.GL_TRIANGLES, model.getRawModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);

View File

@ -60,9 +60,9 @@ public class ShadowMapEntityRenderer
*
* @param entities
* - the entities to be rendered to the shadow map. */
protected void render(Map<TexturedModel, List<RenderObject>> entities)
protected void render(Map<GLTexturedModel, List<RenderObject>> entities)
{
for (TexturedModel model : entities.keySet())
for (GLTexturedModel model : entities.keySet())
{
RawModel rawModel = model.getRawModel();
bindModel(rawModel);

View File

@ -8,7 +8,7 @@ import org.lwjgl.opengl.GL11;
import com.github.hydos.ginger.engine.common.cameras.Camera;
import com.github.hydos.ginger.engine.common.elements.objects.*;
import com.github.hydos.ginger.engine.opengl.render.models.TexturedModel;
import com.github.hydos.ginger.engine.opengl.render.models.GLTexturedModel;
/** This class is in charge of using all of the classes in the shadows package to
* carry out the shadow render pass, i.e. rendering the scene to the shadow map
@ -130,11 +130,11 @@ public class ShadowMapMasterRenderer
*
* @param entities
* - the lists of entities to be rendered. Each list is
* associated with the {@link TexturedModel} that all of the
* associated with the {@link GLTexturedModel} that all of the
* entities in that list use.
* @param sun
* - the light acting as the sun in the scene. */
public void render(Map<TexturedModel, List<RenderObject>> entities, Light sun)
public void render(Map<GLTexturedModel, List<RenderObject>> entities, Light sun)
{
shadowBox.update();
Vector3f sunPosition = sun.getPosition();

View File

@ -1,103 +0,0 @@
package com.github.hydos.ginger.engine.vulkan;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.*;
import org.lwjgl.PointerBuffer;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.vulkan.memory.VKMemory;
import com.github.hydos.ginger.engine.vulkan.model.VKVertices;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class TempMethods
{
public static VKVertices createVertices(VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device)
{
ByteBuffer vertexBuffer = memAlloc(2 * 3 * (3 + 3) * 4);
FloatBuffer fb = vertexBuffer.asFloatBuffer();
// first triangle
fb.put(-0.5f).put(-0.5f).put(0.5f).put(1.0f).put(0.0f).put(0.0f);
fb.put(0.5f).put(-0.5f).put(0.5f).put(0.0f).put(1.0f).put(0.0f);
fb.put(0.0f).put(0.5f).put(0.5f).put(0.0f).put(0.0f).put(1.0f);
// second triangle
fb.put(0.5f).put(-0.5f).put(-0.5f).put(1.0f).put(1.0f).put(0.0f);
fb.put(-0.5f).put(-0.5f).put(-0.5f).put(0.0f).put(1.0f).put(1.0f);
fb.put(0.0f).put(0.5f).put(-0.5f).put(1.0f).put(0.0f).put(1.0f);
VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc()
.sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
VkMemoryRequirements memReqs = VkMemoryRequirements.calloc();
int err;
// Generate vertex buffer
// Setup
VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc()
.sType(VK12.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
.size(vertexBuffer.remaining())
.usage(VK12.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
LongBuffer pBuffer = memAllocLong(1);
err = VK12.vkCreateBuffer(device, bufInfo, null, pBuffer);
long verticesBuf = pBuffer.get(0);
memFree(pBuffer);
bufInfo.free();
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); }
VK12.vkGetBufferMemoryRequirements(device, verticesBuf, memReqs);
memAlloc.allocationSize(memReqs.size());
IntBuffer memoryTypeIndex = memAllocInt(1);
VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex);
memAlloc.memoryTypeIndex(memoryTypeIndex.get(0));
memFree(memoryTypeIndex);
memReqs.free();
LongBuffer pMemory = memAllocLong(1);
err = VK12.vkAllocateMemory(device, memAlloc, null, pMemory);
long verticesMem = pMemory.get(0);
memFree(pMemory);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); }
PointerBuffer pData = memAllocPointer(1);
err = VK12.vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData);
memAlloc.free();
long data = pData.get(0);
memFree(pData);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); }
memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining());
memFree(vertexBuffer);
VK12.vkUnmapMemory(device, verticesMem);
err = VK12.vkBindBufferMemory(device, verticesBuf, verticesMem, 0);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); }
// Binding description
VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1)
.binding(0) // <- we bind our vertex buffer to point 0
.stride((3 + 3) * 4)
.inputRate(VK12.VK_VERTEX_INPUT_RATE_VERTEX);
// Attribute descriptions
// Describes memory layout and shader attribute locations
VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2);
// Location 0 : Position
attributeDescriptions.get(0)
.binding(0) // <- binding point used in the VkVertexInputBindingDescription
.location(0) // <- location in the shader's attribute layout (inside the shader source)
.format(VK12.VK_FORMAT_R32G32B32_SFLOAT)
.offset(0);
// Location 1 : Color
attributeDescriptions.get(1)
.binding(0) // <- binding point used in the VkVertexInputBindingDescription
.location(1) // <- location in the shader's attribute layout (inside the shader source)
.format(VK12.VK_FORMAT_R32G32B32_SFLOAT)
.offset(3 * 4);
// Assign to vertex buffer
VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc();
vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO);
vi.pVertexBindingDescriptions(bindingDescriptor);
vi.pVertexAttributeDescriptions(attributeDescriptions);
VKVertices ret = new VKVertices();
ret.createInfo = vi;
ret.vkVerticiesBuffer = verticesBuf;
return ret;
}
}

View File

@ -1,11 +1,98 @@
package com.github.hydos.ginger.engine.vulkan.model;
import static org.lwjgl.system.MemoryUtil.*;
import java.nio.*;
import org.lwjgl.PointerBuffer;
import org.lwjgl.vulkan.*;
import com.github.hydos.ginger.engine.common.obj.Mesh;
import com.github.hydos.ginger.engine.vulkan.memory.VKMemory;
import com.github.hydos.ginger.engine.vulkan.utils.VKUtils;
public class VKModelConverter
{
public VKVertices toVKVerticies()
public static VKVertices convertModel(Mesh mesh, VkPhysicalDeviceMemoryProperties deviceMemoryProperties, VkDevice device)
{
return null;
ByteBuffer vertexBuffer = memAlloc(mesh.getVertices().length * 4);
FloatBuffer bufferVertices = vertexBuffer.asFloatBuffer();
for(float vertex: mesh.getVertices()) {
bufferVertices.put(vertex);
}
VkMemoryAllocateInfo memAlloc = VkMemoryAllocateInfo.calloc()
.sType(VK12.VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO);
VkMemoryRequirements memReqs = VkMemoryRequirements.calloc();
int err;
// Generate vertex buffer
// Setup
VkBufferCreateInfo bufInfo = VkBufferCreateInfo.calloc()
.sType(VK12.VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO)
.size(vertexBuffer.remaining())
.usage(VK12.VK_BUFFER_USAGE_VERTEX_BUFFER_BIT);
LongBuffer pBuffer = memAllocLong(1);
err = VK12.vkCreateBuffer(device, bufInfo, null, pBuffer);
long verticesBuf = pBuffer.get(0);
memFree(pBuffer);
bufInfo.free();
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to create vertex buffer: " + VKUtils.translateVulkanResult(err)); }
VK12.vkGetBufferMemoryRequirements(device, verticesBuf, memReqs);
memAlloc.allocationSize(memReqs.size());
IntBuffer memoryTypeIndex = memAllocInt(1);
VKMemory.getMemoryType(deviceMemoryProperties, memReqs.memoryTypeBits(), VK12.VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT, memoryTypeIndex);
memAlloc.memoryTypeIndex(memoryTypeIndex.get(0));
memFree(memoryTypeIndex);
memReqs.free();
LongBuffer pMemory = memAllocLong(1);
err = VK12.vkAllocateMemory(device, memAlloc, null, pMemory);
long verticesMem = pMemory.get(0);
memFree(pMemory);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to allocate vertex memory: " + VKUtils.translateVulkanResult(err)); }
PointerBuffer pData = memAllocPointer(1);
err = VK12.vkMapMemory(device, verticesMem, 0, vertexBuffer.remaining(), 0, pData);
memAlloc.free();
long data = pData.get(0);
memFree(pData);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to map vertex memory: " + VKUtils.translateVulkanResult(err)); }
memCopy(memAddress(vertexBuffer), data, vertexBuffer.remaining());
memFree(vertexBuffer);
VK12.vkUnmapMemory(device, verticesMem);
err = VK12.vkBindBufferMemory(device, verticesBuf, verticesMem, 0);
if (err != VK12.VK_SUCCESS)
{ throw new AssertionError("Failed to bind memory to vertex buffer: " + VKUtils.translateVulkanResult(err)); }
// Binding description
VkVertexInputBindingDescription.Buffer bindingDescriptor = VkVertexInputBindingDescription.calloc(1)
.binding(0) // <- we bind our vertex buffer to point 0
.stride((3 + 3) * 4)
.inputRate(VK12.VK_VERTEX_INPUT_RATE_VERTEX);
// Attribute descriptions
// Describes memory layout and shader attribute locations
VkVertexInputAttributeDescription.Buffer attributeDescriptions = VkVertexInputAttributeDescription.calloc(2);
// Location 0 : Position
attributeDescriptions.get(0)
.binding(0) // <- binding point used in the VkVertexInputBindingDescription
.location(0) // <- location in the shader's attribute layout (inside the shader source)
.format(VK12.VK_FORMAT_R32G32B32_SFLOAT)
.offset(0);
// Location 1 : Color
attributeDescriptions.get(1)
.binding(0) // <- binding point used in the VkVertexInputBindingDescription
.location(1) // <- location in the shader's attribute layout (inside the shader source)
.format(VK12.VK_FORMAT_R32G32B32_SFLOAT)
.offset(3 * 4);
// Assign to vertex buffer
VkPipelineVertexInputStateCreateInfo vi = VkPipelineVertexInputStateCreateInfo.calloc();
vi.sType(VK12.VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO);
vi.pVertexBindingDescriptions(bindingDescriptor);
vi.pVertexAttributeDescriptions(attributeDescriptions);
VKVertices ret = new VKVertices();
ret.createInfo = vi;
ret.vkVerticiesBuffer = verticesBuf;
return ret;
}
}