Pull shader code into main #1
@@ -45,13 +45,63 @@ void cameraInitOrthographic(camera_t *camera) {
|
||||
camera->_2d.zoom = 1.0f;
|
||||
}
|
||||
|
||||
void cameraPushMatrix(camera_t *camera) {
|
||||
assertNotNull(camera, "Invalid camera");
|
||||
cameraPushMatrixPlatform(camera);
|
||||
void cameraGetProjectionMatrix(camera_t *camera, mat4 dest) {
|
||||
assertNotNull(camera, "Not a camera component");
|
||||
assertNotNull(dest, "Destination matrix must not be null");
|
||||
|
||||
if(
|
||||
camera->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE ||
|
||||
camera->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED
|
||||
) {
|
||||
glm_mat4_identity(dest);
|
||||
glm_perspective(
|
||||
camera->perspective.fov,
|
||||
SCREEN.aspect,
|
||||
camera->nearClip,
|
||||
camera->farClip,
|
||||
dest
|
||||
);
|
||||
|
||||
if(camera->projType == CAMERA_PROJECTION_TYPE_PERSPECTIVE_FLIPPED) {
|
||||
dest[1][1] *= -1.0f;
|
||||
}
|
||||
} else if(camera->projType == CAMERA_PROJECTION_TYPE_ORTHOGRAPHIC) {
|
||||
glm_mat4_identity(dest);
|
||||
glm_ortho(
|
||||
camera->orthographic.left,
|
||||
camera->orthographic.right,
|
||||
camera->orthographic.top,
|
||||
camera->orthographic.bottom,
|
||||
camera->nearClip,
|
||||
camera->farClip,
|
||||
dest
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void cameraPopMatrix(void) {
|
||||
#ifdef cameraPopMatrixPlatform
|
||||
cameraPopMatrixPlatform();
|
||||
#endif
|
||||
void cameraGetViewMatrix(camera_t *camera, mat4 dest) {
|
||||
assertNotNull(camera, "Not a camera component");
|
||||
assertNotNull(dest, "Destination matrix must not be null");
|
||||
|
||||
if(camera->viewType == CAMERA_VIEW_TYPE_MATRIX) {
|
||||
glm_mat4_copy(camera->view, dest);
|
||||
} else if(camera->viewType == CAMERA_VIEW_TYPE_LOOKAT) {
|
||||
glm_mat4_identity(dest);
|
||||
glm_lookat(
|
||||
camera->lookat.position,
|
||||
camera->lookat.target,
|
||||
camera->lookat.up,
|
||||
dest
|
||||
);
|
||||
} else if(camera->viewType == CAMERA_VIEW_TYPE_2D) {
|
||||
glm_mat4_identity(dest);
|
||||
glm_lookat(
|
||||
(vec3){ camera->_2d.position[0], camera->_2d.position[1], 0.5f },
|
||||
(vec3){ camera->_2d.position[0], camera->_2d.position[1], 0.0f },
|
||||
(vec3){ 0.0f, 1.0f, 0.0f },
|
||||
dest
|
||||
);
|
||||
} else if(camera->viewType == CAMERA_VIEW_TYPE_LOOKAT_PIXEL_PERFECT) {
|
||||
assertUnreachable("LOOKAT_PIXEL_PERFECT view type is not implemented yet");
|
||||
}
|
||||
}
|
||||
@@ -86,13 +86,17 @@ void cameraInitPerspective(camera_t *camera);
|
||||
void cameraInitOrthographic(camera_t *camera);
|
||||
|
||||
/**
|
||||
* Pushes the camera's view matrix onto the matrix stack.
|
||||
* Gets the projection matrix for a camera.
|
||||
*
|
||||
* @param id The ID of the camera entity to use.
|
||||
* @param camera Camera to get the projection matrix for
|
||||
* @param dest Matrix to store the projection matrix in
|
||||
*/
|
||||
void cameraPushMatrix(camera_t *camera);
|
||||
void cameraGetProjectionMatrix(camera_t *camera, mat4 dest);
|
||||
|
||||
/**
|
||||
* Pops the camera's view matrix off the matrix stack.
|
||||
* Gets the view matrix for a camera.
|
||||
*
|
||||
* @param camera Camera to get the view matrix for
|
||||
* @param dest Matrix to store the view matrix in
|
||||
*/
|
||||
void cameraPopMatrix(void);
|
||||
void cameraGetViewMatrix(camera_t *camera, mat4 dest);
|
||||
@@ -57,67 +57,27 @@ errorret_t displayUpdate(void) {
|
||||
|
||||
errorChain(shaderBind(&SHADER_UNLIT));
|
||||
|
||||
mat4 proj;
|
||||
glm_mat4_identity(proj);
|
||||
glm_ortho(0.0f, (float_t)SCREEN.width, (float_t)SCREEN.height, 0.0f, -1.0f, 1.0f, proj);
|
||||
camera_t camera;
|
||||
cameraInitOrthographic(&camera);
|
||||
camera.orthographic.left = 0.0f;
|
||||
camera.orthographic.right = SCREEN.width;
|
||||
camera.orthographic.top = SCREEN.height;
|
||||
camera.orthographic.bottom = 0.0f;
|
||||
|
||||
mat4 view;
|
||||
glm_mat4_identity(view);
|
||||
|
||||
mat4 model;
|
||||
mat4 proj, view, model;
|
||||
cameraGetProjectionMatrix(&camera, proj);
|
||||
cameraGetViewMatrix(&camera, view);
|
||||
glm_mat4_identity(model);
|
||||
glm_translate(model, (vec3){
|
||||
sinf(TIME.time * 10.0f) * 30.0f + 100.0f,
|
||||
cosf(TIME.time * 10.0f) * 50.0f + 100.0f,
|
||||
0.0f
|
||||
});
|
||||
|
||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj));
|
||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view));
|
||||
errorChain(shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model));
|
||||
|
||||
meshvertex_t verts[6] = {
|
||||
{
|
||||
.pos = { 0.0f, 0.0f, 0.0f },
|
||||
.uv = { 0.0f, 0.0f },
|
||||
.color = COLOR_WHITE
|
||||
},
|
||||
|
||||
{
|
||||
.pos = { 100.0f, 0.0f, 0.0f },
|
||||
.uv = { 1.0f, 0.0f },
|
||||
.color = COLOR_WHITE
|
||||
},
|
||||
|
||||
{
|
||||
.pos = { 100.0f, 100.0f, 0.0f },
|
||||
.uv = { 1.0f, 1.0f },
|
||||
.color = COLOR_WHITE
|
||||
},
|
||||
|
||||
{
|
||||
.pos = { 100.0f, 100.0f, 0.0f },
|
||||
.uv = { 1.0f, 1.0f },
|
||||
.color = COLOR_WHITE
|
||||
},
|
||||
|
||||
{
|
||||
.pos = { 0.0f, 100.0f, 0.0f },
|
||||
.uv = { 0.0f, 1.0f },
|
||||
.color = COLOR_WHITE
|
||||
},
|
||||
|
||||
{
|
||||
.pos = { 0.0f, 0.0f, 0.0f },
|
||||
.uv = { 0.0f, 0.0f },
|
||||
.color = COLOR_WHITE
|
||||
}
|
||||
};
|
||||
|
||||
mesh_t mesh;
|
||||
meshInit(&mesh, MESH_PRIMITIVE_TYPE_TRIANGLES, 6, verts);
|
||||
meshDraw(&mesh, 0, -1);
|
||||
meshDispose(&mesh);
|
||||
errorChain(spriteBatchPush(
|
||||
NULL,
|
||||
TIME.time * 2.0f, TIME.time * 3.0f, 100, 100, COLOR_WHITE, 0, 0, 1, 1
|
||||
));
|
||||
errorChain(spriteBatchFlush());
|
||||
|
||||
// errorCatch(errorPrint(sceneRender()));
|
||||
|
||||
|
||||
@@ -25,6 +25,29 @@ errorret_t meshInit(
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t meshFlush(
|
||||
mesh_t *mesh,
|
||||
const int32_t vertexOffset,
|
||||
const int32_t vertexCount
|
||||
) {
|
||||
#ifdef meshFlushPlatform
|
||||
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||
assertTrue(vertexOffset >= 0, "Vertex offset must be non-negative.");
|
||||
assertTrue(vertexCount == -1 || vertexCount > 0, "Vertex count incorrect.");
|
||||
|
||||
int32_t vertCount = meshGetVertexCount(mesh);
|
||||
assertTrue(vertexOffset < (vertCount - 1), "Need at least one vert to draw");
|
||||
|
||||
int32_t drawCount = vertexCount;
|
||||
if(vertexCount == -1) {
|
||||
drawCount = vertCount - vertexOffset;
|
||||
}
|
||||
|
||||
errorChain(meshFlushPlatform(mesh, vertexOffset, vertexCount));
|
||||
#endif
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t meshDraw(
|
||||
const mesh_t *mesh,
|
||||
const int32_t vertexOffset,
|
||||
@@ -32,7 +55,7 @@ errorret_t meshDraw(
|
||||
) {
|
||||
assertNotNull(mesh, "Mesh cannot be NULL");
|
||||
assertTrue(vertexOffset >= 0, "Vertex offset must be non-negative");
|
||||
assertTrue(vertexCount >= -1, "Vertex count must be -1 or non-negative");
|
||||
assertTrue(vertexCount == -1 || vertexCount > 0, "Incorrect vert count");
|
||||
|
||||
int32_t vertDrawCount = vertexCount;
|
||||
if(vertexCount == -1) {
|
||||
|
||||
@@ -40,6 +40,22 @@ errorret_t meshInit(
|
||||
const meshvertex_t *vertices
|
||||
);
|
||||
|
||||
/**
|
||||
* Instructs the mesh to flush the vertices to the GPU. This is surprisingly
|
||||
* only really necessary on modern devices, as we tend to let older devices
|
||||
* read the vertices from the main memory directly.
|
||||
*
|
||||
* @param mesh Mesh to flush the vertices for.
|
||||
* @param vertexOffset Start vertex to flush.
|
||||
* @param vertexCount Count of vertices to flush, set to -1 for all.
|
||||
* @return Error state.
|
||||
*/
|
||||
errorret_t meshFlush(
|
||||
mesh_t *mesh,
|
||||
const int32_t vertexOffset,
|
||||
const int32_t vertexCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Draws a mesh.
|
||||
*
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "assert/assert.h"
|
||||
#include "util/memory.h"
|
||||
#include "display/mesh/quad.h"
|
||||
#include "display/shader/shaderunlit.h"
|
||||
|
||||
screen_t SCREEN;
|
||||
|
||||
@@ -370,10 +371,18 @@ errorret_t screenRender() {
|
||||
FRAMEBUFFER_CLEAR_COLOR | FRAMEBUFFER_CLEAR_DEPTH,
|
||||
COLOR_BLACK
|
||||
);
|
||||
cameraPushMatrix(&SCREEN.framebufferCamera);
|
||||
errorChain(textureBind(&SCREEN.framebuffer.texture));
|
||||
|
||||
shaderBind(&SHADER_UNLIT);
|
||||
mat4 proj, view, model;
|
||||
cameraGetProjectionMatrix(&SCREEN.framebufferCamera, proj);
|
||||
cameraGetViewMatrix(&SCREEN.framebufferCamera, view);
|
||||
glm_mat4_identity(model);
|
||||
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_PROJECTION, proj);
|
||||
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_VIEW, view);
|
||||
shaderSetMatrix(&SHADER_UNLIT, SHADER_UNLIT_MODEL, model);
|
||||
|
||||
// errorChain(textureBind(&SCREEN.framebuffer.texture));
|
||||
errorChain(meshDraw(&SCREEN.frameBufferMesh, 0, -1));
|
||||
cameraPopMatrix();
|
||||
|
||||
errorOk();
|
||||
};
|
||||
|
||||
@@ -19,7 +19,7 @@ errorret_t spriteBatchInit() {
|
||||
&SPRITEBATCH.mesh,
|
||||
QUAD_PRIMITIVE_TYPE,
|
||||
SPRITEBATCH_VERTEX_COUNT,
|
||||
&SPRITEBATCH_VERTICES[0]
|
||||
SPRITEBATCH_VERTICES
|
||||
));
|
||||
errorOk();
|
||||
}
|
||||
@@ -83,10 +83,10 @@ errorret_t spriteBatchFlush() {
|
||||
errorOk();
|
||||
}
|
||||
|
||||
size_t vertexCount = QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount;
|
||||
errorChain(meshFlush(&SPRITEBATCH.mesh, 0, vertexCount));
|
||||
errorChain(textureBind(SPRITEBATCH.currentTexture));
|
||||
errorChain(meshDraw(
|
||||
&SPRITEBATCH.mesh, 0, QUAD_VERTEX_COUNT * SPRITEBATCH.spriteCount
|
||||
));
|
||||
errorChain(meshDraw(&SPRITEBATCH.mesh, 0, vertexCount));
|
||||
spriteBatchClear();
|
||||
errorOk();
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
#include "display/mesh/quad.h"
|
||||
#include "display/texture/texture.h"
|
||||
|
||||
#define SPRITEBATCH_SPRITES_MAX 16
|
||||
#define SPRITEBATCH_SPRITES_MAX 1
|
||||
#define SPRITEBATCH_VERTEX_COUNT (SPRITEBATCH_SPRITES_MAX * QUAD_VERTEX_COUNT)
|
||||
|
||||
|
||||
|
||||
@@ -69,8 +69,6 @@ void moduleCamera(scriptcontext_t *context) {
|
||||
|
||||
// Methods
|
||||
lua_register(context->luaState, "cameraCreate", moduleCameraCreate);
|
||||
lua_register(context->luaState, "cameraPushMatrix", moduleCameraPushMatrix);
|
||||
lua_register(context->luaState, "cameraPopMatrix", moduleCameraPopMatrix);
|
||||
}
|
||||
|
||||
int moduleCameraCreate(lua_State *L) {
|
||||
@@ -118,25 +116,6 @@ int moduleCameraCreate(lua_State *L) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int moduleCameraPushMatrix(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL.");
|
||||
assertTrue(lua_gettop(L) >= 1, "cameraPushMatrix requires 1 arg.");
|
||||
assertTrue(lua_isuserdata(L, 1), "cameraPushMatrix arg must be userdata.");
|
||||
|
||||
// Camera should be provided (pointer to camera_t).
|
||||
camera_t *cam = (camera_t *)luaL_checkudata(L, 1, "camera_mt");
|
||||
assertNotNull(cam, "Camera pointer cannot be NULL.");
|
||||
|
||||
cameraPushMatrix(cam);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleCameraPopMatrix(lua_State *L) {
|
||||
assertNotNull(L, "Lua state cannot be NULL.");
|
||||
cameraPopMatrix();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int moduleCameraIndex(lua_State *l) {
|
||||
assertNotNull(l, "Lua state cannot be NULL.");
|
||||
|
||||
|
||||
@@ -23,22 +23,6 @@ void moduleCamera(scriptcontext_t *context);
|
||||
*/
|
||||
int moduleCameraCreate(lua_State *L);
|
||||
|
||||
/**
|
||||
* Script binding for pushing the camera matrix onto the matrix stack.
|
||||
*
|
||||
* @param L The Lua state.
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleCameraPushMatrix(lua_State *L);
|
||||
|
||||
/**
|
||||
* Script binding for popping the camera matrix from the matrix stack.
|
||||
*
|
||||
* @param L The Lua state.
|
||||
* @return Number of return values on the Lua stack.
|
||||
*/
|
||||
int moduleCameraPopMatrix(lua_State *L);
|
||||
|
||||
/**
|
||||
* Getter for camera structure fields.
|
||||
*
|
||||
|
||||
@@ -30,11 +30,11 @@ void uiRender(void) {
|
||||
UI.camera.orthographic.right = SCREEN.width;
|
||||
UI.camera.orthographic.top = SCREEN.height;
|
||||
|
||||
cameraPushMatrix(&UI.camera);
|
||||
// cameraPushMatrix(&UI.camera);
|
||||
spriteBatchClear();
|
||||
|
||||
spriteBatchFlush();
|
||||
cameraPopMatrix();
|
||||
// cameraPopMatrix();
|
||||
}
|
||||
|
||||
void uiDispose(void) {
|
||||
|
||||
@@ -21,9 +21,10 @@ errorret_t meshInitGL(
|
||||
|
||||
mesh->primitiveType = primitiveType;
|
||||
mesh->vertexCount = vertexCount;
|
||||
mesh->vertices = vertices;
|
||||
|
||||
#ifdef DUSK_OPENGL_LEGACY
|
||||
mesh->vertices = vertices;
|
||||
// Nothing needed.
|
||||
#else
|
||||
// Generate Vertex Buffer Object
|
||||
glGenBuffers(1, &mesh->vboId);
|
||||
@@ -34,7 +35,7 @@ errorret_t meshInitGL(
|
||||
GL_ARRAY_BUFFER,
|
||||
vertexCount * sizeof(meshvertex_t),
|
||||
vertices,
|
||||
GL_STATIC_DRAW
|
||||
GL_DYNAMIC_DRAW
|
||||
);
|
||||
errorChain(errorGLCheck());
|
||||
|
||||
@@ -93,6 +94,27 @@ errorret_t meshInitGL(
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t meshFlushGL(
|
||||
meshgl_t *mesh,
|
||||
const int32_t vertOffset,
|
||||
const int32_t vertCount
|
||||
) {
|
||||
#ifdef DUSK_OPENGL_LEGACY
|
||||
// Nothing doing, we use the glClientState stuff.
|
||||
#else
|
||||
glBindBuffer(GL_ARRAY_BUFFER, mesh->vboId);
|
||||
errorChain(errorGLCheck());
|
||||
glBufferData(
|
||||
GL_ARRAY_BUFFER,
|
||||
vertCount * sizeof(meshvertex_t),
|
||||
&mesh->vertices[vertOffset],
|
||||
GL_DYNAMIC_DRAW
|
||||
);
|
||||
errorChain(errorGLCheck());
|
||||
#endif
|
||||
errorOk();
|
||||
}
|
||||
|
||||
errorret_t meshDrawGL(
|
||||
const meshgl_t *mesh,
|
||||
const int32_t offset,
|
||||
|
||||
@@ -18,9 +18,10 @@ typedef enum {
|
||||
typedef struct {
|
||||
int32_t vertexCount;
|
||||
meshprimitivetypegl_t primitiveType;
|
||||
const meshvertex_t *vertices;
|
||||
|
||||
#ifdef DUSK_OPENGL_LEGACY
|
||||
const meshvertex_t *vertices;
|
||||
// Nothing needed
|
||||
#else
|
||||
GLuint vaoId;
|
||||
GLuint vboId;
|
||||
@@ -43,6 +44,20 @@ errorret_t meshInitGL(
|
||||
const meshvertex_t *vertices
|
||||
);
|
||||
|
||||
/**
|
||||
* Flushes the vertices (stored in memory) to the GPU.
|
||||
*
|
||||
* @param mesh Mesh to flush vertices for.
|
||||
* @param vertOffset First vertice index to flush.
|
||||
* @param vertCount Count of vertices to flush.
|
||||
* @return Error state.
|
||||
*/
|
||||
errorret_t meshFlushGL(
|
||||
meshgl_t *mesh,
|
||||
const int32_t vertOffset,
|
||||
const int32_t vertCount
|
||||
);
|
||||
|
||||
/**
|
||||
* Draws a mesh using OpenGL.
|
||||
*
|
||||
|
||||
@@ -12,6 +12,7 @@ typedef meshprimitivetypegl_t meshprimitivetypeplatform_t;
|
||||
typedef meshgl_t meshplatform_t;
|
||||
|
||||
#define meshInitPlatform meshInitGL
|
||||
#define meshFlushPlatform meshFlushGL
|
||||
#define meshDrawPlatform meshDrawGL
|
||||
#define meshGetVertexCountPlatform meshGetVertexCountGL
|
||||
#define meshDisposePlatform meshDisposeGL
|
||||
Reference in New Issue
Block a user