@ -44,12 +44,13 @@ namespace lunarium
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 );
mQuadData . mVertexBuffer = VertexBuffer : : Create ( mQuadData . mBufferLayout , mQuadData . MaxVertices , nullptr , mQuadData . MaxIndices , nullptr );
mQuadData . mIndexBuffer = new IndexBuffer ( mQuadData . MaxIndices * sizeof ( u32 ) ) ;
//mQuadData.mIndexBuffer = new IndexBuffer(mQuadData.MaxIndices * sizeof(u32));
std : : string vert_source = File : : ReadTextFile ( " quad.vert " ) ;
mQuadData . mQuadShader = new Shader ( OGLDefaultShaders . DefaultShapeVertex , nullptr , OGLDefaultShaders . DefaultShapeFragment ) ;
std : : string frag_source = File : : ReadTextFile ( " quad.frag " ) ;
mQuadData . mQuadShader = new Shader ( vert_source . c_str ( ) , nullptr , frag_source . c_str ( ) ) ;
if ( ! mQuadData . mQuadShader - > IsValid ( ) )
if ( ! mQuadData . mQuadShader - > IsValid ( ) )
{
{
return OpRes : : Fail ( " Failed to create the quad shader " ) ;
return OpRes : : Fail ( " Failed to create the quad shader " ) ;
@ -63,37 +64,40 @@ namespace lunarium
mpDebugTexture = Texture : : Create ( data , 1 , 1 ) ;
mpDebugTexture = Texture : : Create ( data , 1 , 1 ) ;
// DEBUG: INIT TEST DATA
// DEBUG: INIT TEST DATA
mTestData . mBufferLayout . PushVertexAttribute ( VertexAttribute { VertexAttributeType : : FLOAT32 , 2 } ) ; // Position
// mTestData.mBufferLayout.PushVertexAttribute(VertexAttribute { VertexAttributeType::FLOAT32, 2}); // Position
GLfloat vertices [ 6 ] [ 2 ] = {
// GLfloat vertices[6][2] = {
{ - 0.5f , 0.5f , } ,
// { -0.5f, 0.5f, },
{ 0.5f , - 0.5f , } ,
// { 0.5f, -0.5f, },
{ - 0.5f , - 0.5f , } ,
// { -0.5f, -0.5f, },
{ - 0.5f , 0.5f , } ,
// { -0.5f, 0.5f, },
{ 0.5f , 0.5f , } ,
// { 0.5f, 0.5f, },
{ 0.5f , - 0.5f , }
// { 0.5f, -0.5f, }
} ;
// };
mTestData . mVertexBuffer = VertexBuffer : : Create ( mTestData . mBufferLayout , 6 , vertices ) ;
// u32 indices[6] = { 0, 1, 2, 3, 4, 5 };
std : : string vert_source = File : : ReadTextFile ( " test.vert " ) ;
std : : string frag_source = File : : ReadTextFile ( " test.frag " ) ;
// // mTestData.mIndexBuffer = new IndexBuffer(6, indices);
mTestData . mQuadShader = new Shader ( vert_source . c_str ( ) , nullptr , frag_source . c_str ( ) ) ;
// mTestData.mVertexBuffer = VertexBuffer::Create(mTestData.mBufferLayout, 6, nullptr, 6, indices);
if ( ! mTestData . mQuadShader - > IsValid ( ) )
// std::string vert_source = File::ReadTextFile("test.vert");
{
// std::string frag_source = File::ReadTextFile("test.frag");
return OpRes : : Fail ( " Failed to create the test shader " ) ;
// mTestData.mQuadShader = new Shader(vert_source.c_str(), nullptr, frag_source.c_str());
}
// if (!mTestData.mQuadShader->IsValid())
// {
glGenVertexArrays ( 1 , & mTVAO ) ;
// return OpRes::Fail("Failed to create the test shader");
glGenBuffers ( 1 , & mTVBO ) ;
// }
glBindBuffer ( GL_ARRAY_BUFFER , mTVBO ) ;
// glGenVertexArrays(1, &mTVAO);
glBufferData ( GL_ARRAY_BUFFER , sizeof ( GLfloat ) * 12 , vertices , GL_STATIC_DRAW ) ;
// glGenBuffers(1, &mTVBO);
glBindVertexArray ( mTVAO ) ;
// glBindBuffer(GL_ARRAY_BUFFER, mTVBO);
glEnableVertexAttribArray ( 0 ) ;
// glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 12, vertices, GL_STATIC_DRAW);
glVertexAttribPointer ( 0 , 2 , GL_FLOAT , GL_FALSE , 2 * sizeof ( GLfloat ) , ( GLvoid * ) 0 ) ;
glBindBuffer ( GL_ARRAY_BUFFER , 0 ) ;
// glBindVertexArray(mTVAO);
glBindVertexArray ( 0 ) ;
// glEnableVertexAttribArray(0);
// glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
// glBindVertexArray(0);
return OpRes : : OK ( ) ;
return OpRes : : OK ( ) ;
}
}
@ -101,7 +105,7 @@ namespace lunarium
void Renderer2D : : Shutdown ( )
void Renderer2D : : Shutdown ( )
{
{
VertexBuffer : : Destroy ( & mQuadData . mVertexBuffer ) ;
VertexBuffer : : Destroy ( & mQuadData . mVertexBuffer ) ;
delete [ ] mQuadData . mIndexBuffer ;
//delete[] mQuadData.mIndexBuffer;
}
}
void Renderer2D : : SetClearColor ( Color color )
void Renderer2D : : SetClearColor ( Color color )
@ -129,10 +133,10 @@ namespace lunarium
void Renderer2D : : BeginDraw ( OrthographicCamera * pCamera )
void Renderer2D : : BeginDraw ( OrthographicCamera * pCamera )
{
{
mpCamera = pCamera ;
mpCamera = pCamera ;
//glViewport(0, 0, pCamera->GetViewport().Width, pCamera->GetViewport().Height);
glViewport ( 0 , 0 , pCamera - > GetViewport ( ) . Width , pCamera - > GetViewport ( ) . Height ) ;
int w , h ;
// int w, h;
Core : : MainWindow ( ) . GetFramebufferSize ( & w , & h ) ;
// Core::MainWindow().GetFramebufferSize(&w, &h);
glViewport ( 0 , 0 , w , h ) ;
// glViewport(0, 0, w, h);
glClear ( GL_COLOR_BUFFER_BIT ) ;
glClear ( GL_COLOR_BUFFER_BIT ) ;
//ResetFrameStats();
//ResetFrameStats();
}
}
@ -147,7 +151,7 @@ namespace lunarium
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
void Renderer2D : : DrawQuad ( Rectangle quad , Color color , Texture * texture , float angle )
void Renderer2D : : DrawQuad ( Rectangle quad , Color color , Texture * texture , float angle )
{
{
/*
mTestData . color = color ;
mTestData . color = color ;
//mTestData.mVertexBuffer->Bind();
//mTestData.mVertexBuffer->Bind();
mTestData . mQuadShader - > Use ( ) ;
mTestData . mQuadShader - > Use ( ) ;
@ -155,40 +159,61 @@ namespace lunarium
Uniform uni { " spriteColor " , UniformType : : F3 , - 1 } ;
Uniform uni { " spriteColor " , UniformType : : F3 , - 1 } ;
mTestData . mQuadShader - > SetUniform ( uni , color . arr ) . LogIfFailed ( LogCategory : : GRAPHICS , " Failed to set uniform for the test shader! " ) ;
mTestData . mQuadShader - > SetUniform ( uni , color . arr ) . LogIfFailed ( LogCategory : : GRAPHICS , " Failed to set uniform for the test shader! " ) ;
glBindVertexArray ( mTVAO ) ;
//glBindVertexArray(mTVAO);
glDrawArrays ( GL_TRIANGLES , 0 , 6 ) ;
GLfloat vertices [ 6 ] [ 2 ] = {
{ - 0.5f , 0.5f , } ,
{ 0.5f , - 0.5f , } ,
{ - 0.5f , - 0.5f , } ,
{ - 0.5f , 0.5f , } ,
{ 0.5f , 0.5f , } ,
{ 0.5f , - 0.5f , }
} ;
mTestData . mVertexBuffer - > PushVertices ( vertices , 6 ) ;
mTestData . mVertexBuffer - > Bind ( ) ;
// mTestData.mIndexBuffer->Bind();
glDrawElements ( GL_TRIANGLES , 6 , GL_UNSIGNED_INT , nullptr ) ;
//glDrawArrays(GL_TRIANGLES, 0, 6);
mTestData . mVertexBuffer - > Clear ( ) ;
*/
/*
int texture_slot = - 1 ;
if ( texture )
{
for ( int i = 0 ; i < mLoadedTextures . size ( ) ; i + + )
{
if ( texture = = mLoadedTextures [ i ] )
{
texture_slot = i ;
break ;
}
}
if ( - 1 = = texture_slot )
int texture_slot = 0 ;
{
// int texture_slot = -1;
mLoadedTextures . push_back ( texture ) ;
// if (texture)
texture_slot = mLoadedTextures . size ( ) - 1 ;
// {
}
// for (int i = 0; i < mLoadedTextures.size(); i++)
}
// {
// if (texture == mLoadedTextures[i])
// {
// texture_slot = i;
// break;
// }
// }
// if (-1 == texture_slot)
// {
// mLoadedTextures.push_back(texture);
// texture_slot = mLoadedTextures.size() - 1;
// }
// }
float vertices [ 40 ] ;
float vertices [ 40 ] ;
unsigned char * vertices_wl = ( unsigned char * ) vertices ; // vertices write location pointer
unsigned char * vertices_wl = ( unsigned char * ) vertices ; // vertices write location pointer
glm : : mat4 model ( 1.0f ) ;
// glm::mat4 model(1.0f);
model = glm : : translate ( model , glm : : vec3 ( quad . CenterPoint ( ) . x , quad . CenterPoint ( ) . y , 0.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 ( quad . X , quad . Y , 0.0f ) ) ;
model = glm : : rotate ( model , angle , glm : : vec3 ( 0.0f , 0.0f , 1.0f ) ) ;
model = glm : : rotate ( model , angle , glm : : vec3 ( 0.0f , 0.0f , 1.0f ) ) ;
model = glm : : scale ( model , glm : : vec3 ( quad . HalfWidth * 2 , quad . HalfHeight * 2 , 1.0f ) ) ;
// FIRST
// FIRST
QuadData : : Vertex v1 ;
QuadData : : Vertex v1 ;
int vert_size = sizeof ( QuadData : : Vertex ) ;
int vert_size = sizeof ( QuadData : : Vertex ) ;
v1 . pos = mQuadData . vert_pos [ 0 ] ; // * model;
v1 . pos = m odel * m QuadData. vert_pos [ 0 ] ;
v1 . tex_coord = mQuadData . vert_tex [ 0 ] ;
v1 . tex_coord = mQuadData . vert_tex [ 0 ] ;
v1 . color = color ;
v1 . color = color ;
v1 . tex_slot = texture_slot ;
v1 . tex_slot = texture_slot ;
@ -197,7 +222,7 @@ namespace lunarium
// SECOND
// SECOND
QuadData : : Vertex v2 ;
QuadData : : Vertex v2 ;
v2 . pos = m QuadData. vert_pos [ 1 ] ; // * model;
v2 . pos = m odel * m QuadData. vert_pos [ 1 ] ;
v2 . tex_coord = mQuadData . vert_tex [ 1 ] ;
v2 . tex_coord = mQuadData . vert_tex [ 1 ] ;
v2 . color = color ;
v2 . color = color ;
v2 . tex_slot = texture_slot ;
v2 . tex_slot = texture_slot ;
@ -206,7 +231,7 @@ namespace lunarium
// THIRD
// THIRD
QuadData : : Vertex v3 ;
QuadData : : Vertex v3 ;
v3 . pos = mQuadData . vert_pos [ 2 ] ; // * model;
v3 . pos = model * mQuadData . vert_pos [ 2 ] ;
v3 . tex_coord = mQuadData . vert_tex [ 2 ] ;
v3 . tex_coord = mQuadData . vert_tex [ 2 ] ;
v3 . color = color ;
v3 . color = color ;
v3 . tex_slot = texture_slot ;
v3 . tex_slot = texture_slot ;
@ -215,7 +240,7 @@ namespace lunarium
// FOURTH
// FOURTH
QuadData : : Vertex v4 ;
QuadData : : Vertex v4 ;
v4 . pos = m QuadData. vert_pos [ 3 ] ; // * model;
v4 . pos = m odel * m QuadData. vert_pos [ 3 ] ;
v4 . tex_coord = mQuadData . vert_tex [ 3 ] ;
v4 . tex_coord = mQuadData . vert_tex [ 3 ] ;
v4 . color = color ;
v4 . color = color ;
v4 . tex_slot = texture_slot ;
v4 . tex_slot = texture_slot ;
@ -229,7 +254,7 @@ namespace lunarium
}
}
// INDICES
// INDICES
if ( ! mQuadData . m Ind exBuffer- > PushIndices ( mQuadData . indices , 6 * sizeof ( u32 ) ) )
if ( ! mQuadData . m Vert exBuffer- > PushIndices ( mQuadData . indices , 6 ) )
{
{
Logger : : Error ( LogCategory : : GRAPHICS , " Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first! " ) ;
Logger : : Error ( LogCategory : : GRAPHICS , " Quad IndexBuffer is full - This shouldn't happen because the VertexBuffer should fill up first! " ) ;
return ;
return ;
@ -237,7 +262,7 @@ namespace lunarium
mQuadData . mNumQuads + + ;
mQuadData . mNumQuads + + ;
mFrameStats . NumTris + = 2 ;
mFrameStats . NumTris + = 2 ;
*/
}
}
@ -298,34 +323,39 @@ namespace lunarium
{
{
if ( mQuadData . mNumQuads > 0 )
if ( mQuadData . mNumQuads > 0 )
{
{
mQuadData . mVertexBuffer - > Bind ( ) ;
//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 + + )
{
{
mLoadedTextures [ i ] - > Bind ( i ) ;
mLoadedTextures [ i ] - > Bind ( i ) ;
}
}
Uniform umodel ;
// Uniform umodel;
umodel . Type = UniformType : : FMAT4 ;
// umodel.Type = UniformType::FMAT4;
umodel . Location = - 1 ;
// umodel.Location = -1;
umodel . Name = " model " ;
// umodel.Name = "model";
Uniform uprojview ;
Uniform uprojview ;
uprojview . Type = UniformType : : FMAT4 ;
uprojview . Type = UniformType : : FMAT4 ;
uprojview . Location = - 1 ;
uprojview . Location = - 1 ;
uprojview . Name = " projview " ;
uprojview . Name = " projview " ;
mQuadData . mQuadShader - > SetUniform ( umodel , ( void * ) glm : : value_ptr ( glm : : mat4 ( 1.0f ) ) ) . LogIfFailed ( LogCategory : : GRAPHICS ) ;
//mQuadData.mQuadShader->SetUniform(umodel, (void*)glm::value_ptr(glm::mat4(1.0f))).LogIfFailed(LogCategory::GRAPHICS);
mQuadData . mQuadShader - > SetUniform ( uprojview , ( void * ) glm : : value_ptr ( mpCamera - > GetViewProjection ( ) ) ) . LogIfFailed ( LogCategory : : GRAPHICS ) ;
mQuadData . mQuadShader - > SetUniform ( uprojview , ( void * ) glm : : value_ptr ( mpCamera - > GetViewProjection ( ) ) ) . LogIfFailed ( LogCategory : : GRAPHICS ) ;
Uniform uni { " spriteColor " , UniformType : : F3 , - 1 } ;
//Color c = ;
//mQuadData.mQuadShader->SetUniform(uni, Color::Red().arr).LogIfFailed(LogCategory::GRAPHICS, "Failed to set uniform for the test shader!");
mQuadData . mVertexBuffer - > Bind ( ) ;
glDrawElements ( GL_TRIANGLES , mQuadData . mNumQuads * 6 , GL_UNSIGNED_INT , nullptr ) ;
glDrawElements ( GL_TRIANGLES , mQuadData . mNumQuads * 6 , GL_UNSIGNED_INT , nullptr ) ;
//glDrawArrays(GL_TRIANGLES, 0, mQuadData.mNumQuads * 2);
mFrameStats . DrawCalls + + ;
mFrameStats . DrawCalls + + ;
}
}
// Reset drawing data
// Reset drawing data
mLoadedTextures . clear ( ) ;
mLoadedTextures . clear ( ) ;
mQuadData . mVertexBuffer - > Clear ( ) ;
mQuadData . mVertexBuffer - > Clear ( ) ;
mQuadData . mIndexBuffer - > Clear ( ) ;
//mQuadData.mIndexBuffer->Clear();
mQuadData . mNumQuads = 0 ;
mQuadData . mNumQuads = 0 ;
// TODO: Add the debug texture back to the map
// TODO: Add the debug texture back to the map