@ -39,12 +39,29 @@ namespace lunarium
{
{
mpCamera = nullptr ;
mpCamera = nullptr ;
u32 * indices = new u32 [ mQuadData . MaxIndices ] ;
// Need to adjust the set of indices because they reference verts deeper into the buffer
// See Hazel for example of this
int offset = 0 ;
for ( int i = 0 ; i < mQuadData . MaxIndices ; i + = 6 )
{
for ( int j = 0 ; j < 6 ; j + + )
{
indices [ i + j ] = mQuadData . indices [ ( i + j ) % 6 ] + offset ;
}
offset + = 4 ;
}
// INIT QUAD DATA
// INIT QUAD DATA
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 3 } ) ; // Position
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 3 } ) ; // Position
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 2 } ) ; // Tex_coords
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 2 } ) ; // Tex_coords
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 4 } ) ; // Color
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 4 } ) ; // Color
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : INT32 , 1 } ) ; // Texture Sampler Index
mQuadData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : INT32 , 1 } ) ; // Texture Sampler Index
mQuadData . mVertexBuffer = VertexBuffer : : Create ( mQuadData . mBufferLayout , mQuadData . MaxVertices , nullptr , mQuadData . MaxIndices , nullptr ) ;
mQuadData . mVertexBuffer = VertexBuffer : : Create ( mQuadData . mBufferLayout , mQuadData . MaxVertices , nullptr , mQuadData . MaxIndices , indices ) ;
// RAW VERTEX BUFFER
mQuadData . mRawVertexBuffer = new QuadData : : Vertex [ mQuadData . MaxVertices ] ;
mQuadData . mRawBufferIndex = 0 ;
//mQuadData.mIndexBuffer = new IndexBuffer(mQuadData.MaxIndices * sizeof(u32));
//mQuadData.mIndexBuffer = new IndexBuffer(mQuadData.MaxIndices * sizeof(u32));
@ -246,26 +263,109 @@ namespace lunarium
v4 . tex_slot = texture_slot ;
v4 . tex_slot = texture_slot ;
memcpy ( vertices_wl , & v4 , vert_size ) ;
memcpy ( vertices_wl , & v4 , vert_size ) ;
if ( ! mQuadData . mVertexBuffer - > PushVertices ( vertices , 4 ) )
if ( mQuadData . mRawBufferIndex + 4 > mQuadData . MaxVertices )
{
{
Logger : : Info ( LogCategory : : GRAPHICS , " Quad VertexBuffer is full, flushing and retrying " ) ;
Flush ( ) ;
Flush ( ) ;
DrawQuad ( quad , color , texture ) ;
}
}
memcpy ( mQuadData . mRawVertexBuffer + mQuadData . mRawBufferIndex , vertices , sizeof ( QuadData : : Vertex ) * 4 ) ;
mQuadData . mRawBufferIndex + = 4 ;
// if (!mQuadData.mVertexBuffer->PushVertices(vertices, 4))
// {
// //Logger::Info(LogCategory::GRAPHICS, "Quad VertexBuffer is full, flushing and retrying");
// Flush();
// DrawQuad(quad, color, texture);
// }
// INDICES
// INDICES
if ( ! mQuadData . mVertexBuffer - > PushIndices ( mQuadData . indices , 6 ) )
// if (!mQuadData.mVertexBuffer->PushIndices(mQuadData.indices, 6))
// {
// Logger::Error(LogCategory::GRAPHICS, "Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first!");
// return;
// }
mQuadData . mNumQuads + + ;
mFrameStats . NumTris + = 2 ;
}
void Renderer2D : : DrawQuads ( Rectangle * quads , u32 num_quads , Color * pColors )
{
{
Logger : : Error ( LogCategory : : GRAPHICS , " Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first! " ) ;
for ( int i = 0 ; i < num_quads ; i + + )
return ;
{
float vertices [ 40 ] ;
unsigned char * vertices_wl = ( unsigned char * ) vertices ; // vertices write location pointer
// glm::mat4 model(1.0f);
// model = glm::translate(model, glm::vec3(quad.CenterPoint().x, quad.CenterPoint().y, 0.0f));
// model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
glm : : mat4 model = glm : : mat4 ( 1.0f ) ;
model = glm : : translate ( model , glm : : vec3 ( quads [ i ] . X , quads [ i ] . Y , 0.0f ) ) ;
model = glm : : rotate ( model , 1.0f , glm : : vec3 ( 0.0f , 0.0f , 1.0f ) ) ;
model = glm : : scale ( model , glm : : vec3 ( quads [ i ] . HalfWidth * 2 , quads [ i ] . HalfHeight * 2 , 1.0f ) ) ;
// FIRST
QuadData : : Vertex v1 ;
int vert_size = sizeof ( QuadData : : Vertex ) ;
v1 . pos = model * mQuadData . vert_pos [ 0 ] ;
v1 . tex_coord = mQuadData . vert_tex [ 0 ] ;
v1 . color = pColors [ i ] ;
v1 . tex_slot = 0 ;
memcpy ( vertices_wl , & v1 , vert_size ) ;
vertices_wl + = vert_size ;
// SECOND
QuadData : : Vertex v2 ;
v2 . pos = model * mQuadData . vert_pos [ 1 ] ;
v2 . tex_coord = mQuadData . vert_tex [ 1 ] ;
v2 . color = pColors [ i ] ;
v2 . tex_slot = 0 ;
memcpy ( vertices_wl , & v2 , vert_size ) ;
vertices_wl + = vert_size ;
// THIRD
QuadData : : Vertex v3 ;
v3 . pos = model * mQuadData . vert_pos [ 2 ] ;
v3 . tex_coord = mQuadData . vert_tex [ 2 ] ;
v3 . color = pColors [ i ] ;
v3 . tex_slot = 0 ;
memcpy ( vertices_wl , & v3 , vert_size ) ;
vertices_wl + = vert_size ;
// FOURTH
QuadData : : Vertex v4 ;
v4 . pos = model * mQuadData . vert_pos [ 3 ] ;
v4 . tex_coord = mQuadData . vert_tex [ 3 ] ;
v4 . color = pColors [ i ] ;
v4 . tex_slot = 0 ;
memcpy ( vertices_wl , & v4 , vert_size ) ;
if ( ! mQuadData . mVertexBuffer - > PushVertices ( vertices , 4 ) )
{
//Logger::Info(LogCategory::GRAPHICS, "Quad VertexBuffer is full, flushing and retrying");
Flush ( ) ;
i - - ;
continue ;
}
}
// INDICES
// if (!mQuadData.mVertexBuffer->PushIndices(mQuadData.indices, 6))
// {
// Logger::Error(LogCategory::GRAPHICS, "Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first!");
// return;
// }
mQuadData . mNumQuads + + ;
mQuadData . mNumQuads + + ;
mFrameStats . NumTris + = 2 ;
mFrameStats . NumTris + = 2 ;
}
}
}
void Renderer2D : : DrawSprite ( Texture & image , glm : : vec2 position , Color color , float angle )
void Renderer2D : : DrawSprite ( Texture & image , glm : : vec2 position , Color color , float angle )
{
{
Logger : : Warn ( LogCategory : : GRAPHICS , " Renderer2D::DrawSprite is not yet implemented " ) ;
Logger : : Warn ( LogCategory : : GRAPHICS , " Renderer2D::DrawSprite is not yet implemented " ) ;
@ -323,6 +423,12 @@ namespace lunarium
{
{
if ( mQuadData . mNumQuads > 0 )
if ( mQuadData . mNumQuads > 0 )
{
{
if ( ! mQuadData . mVertexBuffer - > PushVertices ( mQuadData . mRawVertexBuffer , mQuadData . mNumQuads * 4 ) )
{
Logger : : Error ( LogCategory : : GRAPHICS , " Could not push verts into the quad buffer! " ) ;
return ;
}
//mQuadData.mIndexBuffer->Bind();
//mQuadData.mIndexBuffer->Bind();
mQuadData . mQuadShader - > Use ( ) ;
mQuadData . mQuadShader - > Use ( ) ;
for ( int i = 0 ; i < mLoadedTextures . size ( ) ; i + + )
for ( int i = 0 ; i < mLoadedTextures . size ( ) ; i + + )
@ -357,6 +463,7 @@ namespace lunarium
mQuadData . mVertexBuffer - > Clear ( ) ;
mQuadData . mVertexBuffer - > Clear ( ) ;
//mQuadData.mIndexBuffer->Clear();
//mQuadData.mIndexBuffer->Clear();
mQuadData . mNumQuads = 0 ;
mQuadData . mNumQuads = 0 ;
mQuadData . mRawBufferIndex = 0 ;
// TODO: Add the debug texture back to the map
// TODO: Add the debug texture back to the map
mLoadedTextures . push_back ( mpDebugTexture ) ;
mLoadedTextures . push_back ( mpDebugTexture ) ;