@ -118,6 +118,11 @@ namespace lunarium
void Renderer2D : : DrawQuad ( Rectangle quad , Color color , Texture * texture , float angle , Rectangle sub_texture_area )
{
if ( ! mQuadData . VertexBuffer - > WillFit ( 4 ) )
{
Flush ( FlushMode : : Quads ) ;
}
int texture_slot = - 1 ;
if ( texture )
{
@ -141,10 +146,6 @@ namespace lunarium
texture_slot = 0 ;
}
if ( mQuadData . RawBufferIndex + 4 > mQuadData . MaxVertices )
{
Flush ( FlushMode : : Quads ) ;
}
float tex_is_grey_scale = ( texture & & texture - > GetFormat ( ) = = TextureFormat : : RED ) ? 1.0f : 0.0f ;
float left = sub_texture_area . X ;
@ -163,58 +164,60 @@ namespace lunarium
offset_y * = - 1.0f ;
// FIRST
QuadData : : Vertex v 1 ;
QuadData : : Vertex v erts[ 4 ] ;
int vert_size = sizeof ( QuadData : : Vertex ) ;
v1 . pos = mQuadData . vert_pos [ 0 ] ;
v1 . tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 0 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 0 ] . y * scale_y - offset_y ) ;
v1 . color = color ;
v1 . tex_slot = texture_slot ;
v1 . translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v1 . angle = angle ;
v1 . scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v1 . tex_is_grey_scale = tex_is_grey_scale ;
memcpy ( & mQuadData . RawVertexBuffer [ mQuadData . RawBufferIndex ] , & v1 , vert_size ) ;
mQuadData . RawBufferIndex + = 1 ;
verts [ 0 ] . pos = mQuadData . vert_pos [ 0 ] ;
verts [ 0 ] . tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 0 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 0 ] . y * scale_y - offset_y ) ;
verts [ 0 ] . color = color ;
verts [ 0 ] . tex_slot = texture_slot ;
verts [ 0 ] . translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
verts [ 0 ] . angle = angle ;
verts [ 0 ] . scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
verts [ 0 ] . tex_is_grey_scale = tex_is_grey_scale ;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v1, vert_size);
//mQuadData.RawBufferIndex += 1;
//mQuadData.VertexBuffer->PushVertices((void*)&v1, 1);
// SECOND
QuadData: : Vertex v2 ;
v 2. pos = mQuadData . vert_pos [ 1 ] ;
v 2. tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 1 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 1 ] . y * scale_y - offset_y ) ;
v 2. color = color ;
v 2. tex_slot = texture_slot ;
v 2. translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v 2. angle = angle ;
v 2. scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v2 . tex_is_grey_scale = tex_is_grey_scale ;
memcpy ( & mQuadData . RawVertexBuffer [ mQuadData . RawBufferIndex ] , & v2 , vert_size ) ;
mQuadData . RawBufferIndex + = 1 ;
verts[ 1 ] . pos = mQuadData . vert_pos [ 1 ] ;
v erts[ 1 ] . tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 1 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 1 ] . y * scale_y - offset_y ) ;
v erts[ 1 ] . color = color ;
v erts[ 1 ] . tex_slot = texture_slot ;
v erts[ 1 ] . translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v erts[ 1 ] . angle = angle ;
v erts[ 1 ] . scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v erts[ 1 ] . tex_is_grey_scale = tex_is_grey_scale ;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v2, vert_size);
//mQuadData.RawBufferIndex += 1;
// mQuadData.VertexBuffer->PushVertices((void*)&v2, 1);
// THIRD
QuadData: : Vertex v3 ;
v 3. pos = mQuadData . vert_pos [ 2 ] ;
v 3. tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 2 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 2 ] . y * scale_y - offset_y ) ;
v 3. color = color ;
v 3. tex_slot = texture_slot ;
v 3. translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v 3. angle = angle ;
v 3. scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v3 . tex_is_grey_scale = tex_is_grey_scale ;
memcpy ( & mQuadData . RawVertexBuffer [ mQuadData . RawBufferIndex ] , & v3 , vert_size ) ;
mQuadData . RawBufferIndex + = 1 ;
verts[ 2 ] . pos = mQuadData . vert_pos [ 2 ] ;
v erts[ 2 ] . tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 2 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 2 ] . y * scale_y - offset_y ) ;
v erts[ 2 ] . color = color ;
v erts[ 2 ] . tex_slot = texture_slot ;
v erts[ 2 ] . translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v erts[ 2 ] . angle = angle ;
v erts[ 2 ] . scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v erts[ 2 ] . tex_is_grey_scale = tex_is_grey_scale ;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v3, vert_size);
//mQuadData.RawBufferIndex += 1;
//mQuadData.VertexBuffer->PushVertices((void*)&v3, 1);
// FOURTH
QuadData: : Vertex v4 ;
v 4. pos = mQuadData . vert_pos [ 3 ] ;
v 4. tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 3 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 3 ] . y * scale_y - offset_y ) ;
v 4. color = color ;
v 4. tex_slot = texture_slot ;
v 4. translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v 4. angle = angle ;
v 4. scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v4 . tex_is_grey_scale = tex_is_grey_scale ;
memcpy ( & mQuadData . RawVertexBuffer [ mQuadData . RawBufferIndex ] , & v4 , vert_size ) ;
mQuadData . RawBufferIndex + = 1 ;
verts[ 3 ] . pos = mQuadData . vert_pos [ 3 ] ;
v erts[ 3 ] . tex_coord = glm : : vec2 ( mQuadData . vert_tex [ 3 ] . x * scale_x + offset_x , mQuadData . vert_tex [ 3 ] . y * scale_y - offset_y ) ;
v erts[ 3 ] . color = color ;
v erts[ 3 ] . tex_slot = texture_slot ;
v erts[ 3 ] . translation = glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.0f ) ;
v erts[ 3 ] . angle = angle ;
v erts[ 3 ] . scale = glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ;
v erts[ 3 ] . tex_is_grey_scale = tex_is_grey_scale ;
//memcpy(&mQuadData.RawVertexBuffer[mQuadData.RawBufferIndex], &v4, vert_size);
//mQuadData.RawBufferIndex += 1;
mQuadData . NumQuads + + ;
mQuadData . VertexBuffer - > PushVertices ( ( void * ) verts , 4 ) ;
// Update render stats
mFrameStats . NumTris + = 2 ;
@ -224,7 +227,7 @@ namespace lunarium
void Renderer2D : : DrawLine ( glm : : vec2 point_a , glm : : vec2 point_b , Color color , float thickness , float angle )
{
if ( thickness ! = mLineData . Thickness | | mLineData . RawBufferIndex + 2 > mLineData . MaxVertices )
if ( thickness ! = mLineData . Thickness | | ! mLineData . VertexBuffer - > WillFit ( 2 ) )
{
Flush ( FlushMode : : Lines ) ;
mLineData . Thickness = thickness ;
@ -235,29 +238,30 @@ namespace lunarium
//model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
// V1
LineData : : Vertex v 1 ;
LineData : : Vertex v erts[ 2 ] ;
int vert_size = sizeof ( LineData : : Vertex ) ;
glm : : vec4 long_pos = model * glm : : vec4 ( - 0.5f , 0.0f , 0.0f , 1.0f ) ;
v 1 . pos = glm : : vec2 ( long_pos . x , long_pos . y ) ;
v erts[ 0 ] . pos = glm : : vec2 ( long_pos . x , long_pos . y ) ;
// v1.translate = glm::vec3(point_a.x, point_a.y, 0.0f);
v 1 . color = color ;
v erts[ 0 ] . color = color ;
//v1.angle = angle;
memcpy ( & mLineData . RawVertexBuffer [ mLineData . RawBufferIndex ] , & v1 , vert_size ) ;
mLineData . RawBufferIndex + = 1 ;
// memcpy(&mLineData.RawVertexBuffer[mLineData.RawBufferIndex], &v1, vert_size);
// mLineData.RawBufferIndex += 1;
model = glm : : mat4 ( 1.0f ) ;
model = glm : : translate ( model , glm : : vec3 ( point_b . x , point_b . y , 0.0f ) ) ;
// model = glm::rotate(model, angle, glm::vec3(0.0f, 0.0f, 1.0f));
// V2
LineData : : Vertex v2 ;
long_pos = model * glm : : vec4 ( 0.5f , 0.0f , 0.0f , 1.0f ) ;
v 2 . pos = glm : : vec2 ( long_pos . x , long_pos . y ) ;
v erts[ 1 ] . pos = glm : : vec2 ( long_pos . x , long_pos . y ) ;
// v2.translate = glm::vec3(point_b.x, point_b.y, 0.0f);
v 2 . color = color ;
v erts[ 1 ] . color = color ;
//v2.angle = angle;
memcpy ( & mLineData . RawVertexBuffer [ mLineData . RawBufferIndex ] , & v2 , vert_size ) ;
mLineData . RawBufferIndex + = 1 ;
// memcpy(&mLineData.RawVertexBuffer[mLineData.RawBufferIndex], &v2, vert_size);
// mLineData.RawBufferIndex += 1;
mLineData . VertexBuffer - > PushVertices ( ( void * ) verts , 2 ) ;
mLineData . NumLines + + ;
mFrameStats . NumVerts + = 2 ;
@ -265,12 +269,17 @@ namespace lunarium
void Renderer2D : : DrawBox ( Rectangle box , Color color , float thickness , float angle )
{
// TODO: Use DrawLine to make a box
}
void Renderer2D : : DrawEllipse ( glm : : vec2 center_point , glm : : vec2 radii , Color color , int resolution , float thickness , float angle )
{
// TODO: Use DrawLine to make ellipse
}
void Renderer2D : : DrawEllipse ( glm : : vec2 center_point , glm : : vec2 radii , Color color , bool filled , float thickness , float angle )
void Renderer2D : : DrawEllipse Filled ( glm : : vec2 center_point , glm : : vec2 radii , Color color , int resolution , float angle )
{
Logger : : Warn ( LogCategory : : GRAPHICS , " Renderer2D::DrawEllipse is not implemented yet " ) ;
}
void Renderer2D : : DrawString ( const char * string , Rectangle bounding_box , Color color , float scale , int font )
@ -289,12 +298,12 @@ namespace lunarium
if ( mQuadData . NumQuads > 0 & & flush_mode & FlushMode : : Quads > 0 )
{
mQuadData . MarkedForReset = true ;
if ( ! mQuadData . VertexBuffer - > PushVertices ( mQuadData . RawVertexBuffer , mQuadData . NumQuads * 4 ) )
{
Logger : : Error ( LogCategory : : GRAPHICS , " Could not push verts into the quad buffer! " ) ;
ResetDrawingData ( ) ;
return ;
}
// if (!mQuadData.VertexBuffer->PushVertices(mQuadData.RawVertexBuffer, mQuadData.NumQuads * 4))
// {
// Logger::Error(LogCategory::GRAPHICS, "Could not push verts into the quad buffer!");
// ResetDrawingData();
// return;
// }
mQuadData . QuadShader - > Use ( ) ;
for ( int i = 0 ; i < mQuadData . LoadedTextures . size ( ) ; i + + )
@ -311,10 +320,12 @@ namespace lunarium
mQuadData . VertexBuffer - > Bind ( ) ;
glDrawElements ( GL_TRIANGLES , mQuadData . NumQuads * 6 , GL_UNSIGNED_INT , nullptr ) ;
mQuadData . VertexBuffer - > DrawArray ( mQuadData . NumQuads * 6 ) ;
// glDrawElements(GL_TRIANGLES, mQuadData.NumQuads * 6, GL_UNSIGNED_INT, nullptr);
// Checking how the rotation looks when lines are drawn instead
// glDrawArrays(GL_LINES, 0, mQuadData.NumQuads * 4);
// glDrawArrays(GL_LINES, 0, mQuadData.NumQuads * 4);
// mQuadData.WireFrameShader->Use();
// uprojview.Location = -1;
@ -339,12 +350,12 @@ namespace lunarium
if ( mLineData . NumLines > 0 & & draw > 0 )
{
mLineData . MarkedForReset = true ;
if ( ! mLineData . VertexBuffer - > PushVertices ( mLineData . RawVertexBuffer , mLineData . NumLines * 2 ) )
{
Logger : : Error ( LogCategory : : GRAPHICS , " Could not push verts into the line buffer! " ) ;
ResetDrawingData ( ) ;
return ;
}
// if (!mLineData.VertexBuffer->PushVertices(mLineData.RawVertexBuffer, mLineData.NumLines * 2))
// {
// Logger::Error(LogCategory::GRAPHICS, "Could not push verts into the line buffer!");
// ResetDrawingData();
// return;
// }
mLineData . Shader - > Use ( ) ;
Uniform uprojview ;
@ -358,7 +369,8 @@ namespace lunarium
//glDrawElements(GL_LINES, mLineData.NumLines * 2, GL_UNSIGNED_INT, nullptr);
glLineWidth ( mLineData . Thickness ) ;
glDrawArrays ( GL_LINES , 0 , mLineData . NumLines * 2 ) ;
//glDrawArrays(GL_LINES, 0, mLineData.NumLines * 2);
mLineData . VertexBuffer - > DrawArray ( mLineData . NumLines * 2 ) ;
mFrameStats . DrawCalls + + ;
}
@ -404,9 +416,10 @@ namespace lunarium
mQuadData . VertexBuffer = VertexBuffer : : Create ( mQuadData . BufferLayout , mQuadData . MaxVertices , nullptr , mQuadData . MaxIndices , indices ) ;
// RAW VERTEX BUFFER
mQuadData . RawVertexBuffer = new QuadData : : Vertex [ mQuadData . MaxVertices ] ;
mQuadData . RawBufferIndex = 0 ;
// mQuadData.RawVertexBuffer = new QuadData::Vertex[mQuadData.MaxVertices];
// mQuadData.RawBufferIndex = 0;
// Safety check vertex struct size
int layout_size = mQuadData . BufferLayout . GetLayoutSizeInBytes ( ) ;
int struct_size = sizeof ( QuadData : : Vertex ) ;
if ( struct_size ! = layout_size )
@ -449,10 +462,12 @@ namespace lunarium
mLineData . BufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 4 } ) ; // Color
// mLineData.BufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 1 }); // angle
mLineData . VertexBuffer = VertexBuffer : : Create ( mLineData . BufferLayout , mLineData . MaxVertices ) ;
mLineData . VertexBuffer - > SetType ( VertexBuffer : : LayoutType : : LINE ) ;
mLineData . RawVertexBuffer = new LineData : : Vertex [ mLineData . MaxVertices ] ;
mLineData . RawBufferIndex = 0 ;
// mLineData.RawVertexBuffer = new LineData::Vertex[mLineData.MaxVertices];
// mLineData.RawBufferIndex = 0;
// Safety check vertex struct size
int layout_size = mLineData . BufferLayout . GetLayoutSizeInBytes ( ) ;
int struct_size = sizeof ( LineData : : Vertex ) ;
if ( struct_size ! = layout_size )
@ -475,6 +490,20 @@ namespace lunarium
OpRes Renderer2D : : InitEllipseData ( )
{
mEllipseData . BufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 2 } ) ; // Position
mEllipseData . BufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 4 } ) ; // Color
mEllipseData . VertexBuffer = VertexBuffer : : Create ( mEllipseData . BufferLayout , mEllipseData . MaxVertices , nullptr , mEllipseData . MaxIndices , nullptr ) ;
// Safety check vertex struct size
int layout_size = mEllipseData . BufferLayout . GetLayoutSizeInBytes ( ) ;
int struct_size = sizeof ( EllipseData : : Vertex ) ;
if ( struct_size ! = layout_size )
{
Logger : : Error ( LogCategory : : GRAPHICS ,
" EllipseData::Vertex struct size does not match the vertex layout size! struct: %d, layout: %d " , struct_size , layout_size ) ;
}
return OpRes : : OK ( ) ;
}
@ -496,7 +525,7 @@ namespace lunarium
mQuadData . VertexBuffer - > Clear ( ) ;
mQuadData . NumQuads = 0 ;
mQuadData . RawBufferIndex = 0 ;
//mQuadData.RawBufferIndex = 0;
mQuadData . MarkedForReset = false ;
}
@ -506,7 +535,7 @@ namespace lunarium
{
mLineData . VertexBuffer - > Clear ( ) ;
mLineData . NumLines = 0 ;
mLineData . RawBufferIndex = 0 ;
// mLineData.RawBufferIndex = 0;
mLineData . Thickness = 1.0f ;
mLineData . MarkedForReset = false ;