2D_Game_Engine
Loading...
Searching...
No Matches
renderer.cpp
Go to the documentation of this file.
1#include <pch.hpp>
2#include <renderer.hpp>
3#include <window.hpp>
4
5#ifdef EDITOR
6 extern bool ANIMATIONS_PREVIEW;
7#endif
8
9RendererData::RendererData(const char *vertex_path,const char *fragment_path,unsigned int vertex_size):
10 VBO(MAX_VERTICES,vertex_size,GL_DYNAMIC_DRAW),S(vertex_path,fragment_path),NumVertices(0){}
11
13 m_Textures("resources/shaders/textures/vertex.glsl","resources/shaders/textures/fragment.glsl",sizeof(Vertex)),
14 m_Points("resources/shaders/points/vertex.glsl","resources/shaders/points/fragment.glsl",sizeof(PointVertex)),
15 m_Lines("resources/shaders/lines/vertex.glsl","resources/shaders/lines/fragment.glsl",sizeof(LineVertex)),
16 m_Triangles("resources/shaders/triangles/vertex.glsl","resources/shaders/triangles/fragment.glsl",sizeof(TriangleVertex)),
17 m_Lights("resources/shaders/lights/vertex.glsl","resources/shaders/lights/fragment.glsl",4*sizeof(float)),
18 m_SHdr("resources/shaders/lights/vertex.glsl","resources/shaders/hdr/fragment.glsl"),
19 m_SPostProcessing("resources/shaders/textures/vertex.glsl","resources/shaders/post_processing/fragment.glsl"),
20 m_PostProcessingIndex(std::numeric_limits<unsigned int>::max()),m_AmbientLight(0.0f,0.0f,0.0f),m_ClearColor(0.0f,0.0f,0.0f){
21
22 float vertices[]={ 0.0f,0.0f,0.0f,0.0f,
23 0.0f,Window::MAX_HEIGHT,0.0f,1.0f,
25 Window::MAX_WIDTH,0.0f,1.0f,0.0f};
26
27 m_BufferT=(Vertex *)AllocateMemory(MAX_VERTICES*sizeof(Vertex));
31
32 m_Framebuffer=new Framebuffer;
33 m_LightingFramebuffer=new Framebuffer;
34 m_TempFramebuffer=new Framebuffer;
35 IB.Set(MAX_VERTICES);
36
37 for(int i=0;i<32;i++)
38 m_Slots[i]=i;
39
40 AddLayout(m_Textures.VBL,GL_FLOAT,2,false);
41 AddLayout(m_Textures.VBL,GL_FLOAT,2,false);
42 AddLayout(m_Textures.VBL,GL_FLOAT,1,false);
43 AddLayout(m_Textures.VBL,GL_FLOAT,1,false);
44 m_Textures.VAO.AddBuffer(m_Textures.VBO,m_Textures.VBL);
45
46 AddLayout(m_Points.VBL,GL_FLOAT,2,false);
47 AddLayout(m_Points.VBL,GL_FLOAT,4,false);
48 AddLayout(m_Points.VBL,GL_FLOAT,1,false);
49 AddLayout(m_Points.VBL,GL_FLOAT,1,false);
50 m_Points.VAO.AddBuffer(m_Points.VBO,m_Points.VBL);
51
52 AddLayout(m_Lines.VBL,GL_FLOAT,2,false);
53 AddLayout(m_Lines.VBL,GL_FLOAT,4,false);
54 AddLayout(m_Lines.VBL,GL_FLOAT,1,false);
55 m_Lines.VAO.AddBuffer(m_Lines.VBO,m_Lines.VBL);
56
57 AddLayout(m_Triangles.VBL,GL_FLOAT,2,false);
58 AddLayout(m_Triangles.VBL,GL_FLOAT,4,false);
59 AddLayout(m_Triangles.VBL,GL_FLOAT,1,false);
60 m_Triangles.VAO.AddBuffer(m_Triangles.VBO,m_Triangles.VBL);
61
62 AddLayout(m_Lights.VBL,GL_FLOAT,2,false);
63 AddLayout(m_Lights.VBL,GL_FLOAT,2,false);
64 m_Lights.VAO.AddBuffer(m_Lights.VBO,m_Lights.VBL);
65 m_Lights.VBO.SetData(0,vertices,4,4*sizeof(float));
66
68 m_SHdr.Reload();
69
70 m_Points.S.Bind();
71 m_Points.S.SetUniform1f("blurAmount",0.0f);
72
73 m_Textures.S.Bind();
74 m_Textures.S.SetUniform1iv("texID",m_Slots,32);
75
76 m_Lights.S.Bind();
77 m_Lights.S.SetUniform1i("framebuffer",0);
78 m_Lights.S.SetUniform1i("light",1);
79
80 m_SHdr.Bind();
81 m_SHdr.SetUniform1i("tex",0);
82 m_SHdr.SetUniform1f("gamma",2.2f);
83 m_SHdr.SetUniform1f("exposure",1.0f);
84
85 m_Gamma=2.2f;
86 m_Exposure=1.0f;
87
88 m_SPostProcessing.Bind();
89 m_SPostProcessing.SetUniform1iv("texID",m_Slots,32);
90
91 glEnable(GL_LINE_SMOOTH);
92 glEnable(GL_PROGRAM_POINT_SIZE);
93
94 m_Zoom=1.0f;
95 SetPointSize(1.0f);
96 SetLineWidth(0.1f);
97
98 glEnable(GL_BLEND);
99 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
100
101 glEnable(GL_PROGRAM_POINT_SIZE);
102
103 glGetIntegerv(GL_MAX_TEXTURE_IMAGE_UNITS,&m_MaxTextureSlots);
104 #ifdef DEBUG
105 printf("Max texture slots: %d\n",m_MaxTextureSlots);
106 #endif
107
108 segments.push_back(std::make_pair(Vec2(0,0),Vec2(0,Window::MAX_HEIGHT)));
109 segments.push_back(std::make_pair(Vec2(0,Window::MAX_HEIGHT),Vec2(Window::MAX_WIDTH,Window::MAX_HEIGHT)));
110 segments.push_back(std::make_pair(Vec2(Window::MAX_WIDTH,Window::MAX_HEIGHT),Vec2(Window::MAX_WIDTH,0)));
111 segments.push_back(std::make_pair(Vec2(Window::MAX_WIDTH,0),Vec2(0,0)));
112
113 m_AmbientLight=Vec3(1.0f,1.0f,1.0f);
114}
115
117 FreeMemory(m_BufferT);
118 FreeMemory(m_BufferP);
119 FreeMemory(m_BufferL);
120 FreeMemory(m_BufferTR);
121
122 delete m_Framebuffer;
123 delete m_LightingFramebuffer;
124 delete m_TempFramebuffer;
125}
126
128 switch(type){
130 return "None";
132 return "Reinhard";
134 return "Filmic";
136 return "Uncharted2";
138 return "ACES";
140 return "Exponential";
142 return "Logarithmic";
144 return "Mantiuk";
145 default:
146 return "None";
147 }
148
149}
150
151TonemapType StringToTonemapType(const std::string &type){
152 if(type=="None")
153 return TonemapType::None;
154 if(type=="Reinhard")
156 if(type=="Filmic")
157 return TonemapType::Filmic;
158 if(type=="Uncharted2")
160 if(type=="ACES")
161 return TonemapType::ACES;
162 if(type=="Exponential")
164 if(type=="Logarithmic")
166 if(type=="Mantiuk")
168 return TonemapType::None;
169}
170
172 std::string content;
173 char ch;
174
175 FILE *file=fopen("resources/shaders/hdr/fragment_template.glsl","r");
176
177 while((ch=fgetc(file))!=EOF)
178 content+=ch;
179
180 fclose(file);
181
182 file=fopen("resources/shaders/hdr/fragment.glsl","w");
183
184 fprintf(file,"%s",content.c_str());
185
186 char main[]=
187 "\nvoid main(){\n"
188 "\tvec3 hdr_color=texture(tex,v_TexCoord).rgb;\n"
189 "\thdr_color *= exposure;\n"
190 "\tvec3 result=";
191
192 fprintf(file,"%s",main);
193
194 switch(m_TonemapType){
196 fprintf(file,"hdr_color;\n");
197 break;
199 fprintf(file,"reinhardToneMapping(hdr_color);\n");
200 break;
202 fprintf(file,"filmicToneMapping(hdr_color);\n");
203 break;
205 fprintf(file,"uncharted2ToneMapping(hdr_color);\n");
206 break;
208 fprintf(file,"acesToneMapping(hdr_color);\n");
209 break;
211 fprintf(file,"exponentialToneMapping(hdr_color);\n");
212 break;
214 fprintf(file,"logarithmicToneMapping(hdr_color);\n");
215 break;
217 fprintf(file,"mantiukToneMapping(hdr_color);\n");
218 break;
219 default:
220 fprintf(file,"hdr_color;\n");
221 break;
222 }
223
224 fprintf(file,"\tresult = pow(result, vec3(1.0 / gamma));\n");
225 fprintf(file,"\tcolor=vec4(result,1.0f);\n}");
226
227 fclose(file);
228}
229
231 m_TonemapType=type;
232}
233
235 return m_TonemapType;
236}
237
238void Renderer::SetGamma(float gamma){
239 m_Gamma=gamma;
240}
241
243 return m_Gamma;
244}
245
246void Renderer::SetExposure(float exposure){
247 m_Exposure=exposure;
248}
249
251 return m_Exposure;
252}
253
254void Renderer::DrawTexture(Vec2 pos,Vec2 size,int layer,float texID){
255 PROFILE_FUNCTION();
256
257 #ifdef EDITOR
258 Window::VertexCount+=4;
259 #endif
260
261 auto [a,b,c,d]=VertexBuffer::CreateQuad(pos.x,pos.y,size.w,size.h,layer,texID);
262 m_BufferT[m_Textures.NumVertices]=a;
263 m_BufferT[m_Textures.NumVertices+1]=b;
264 m_BufferT[m_Textures.NumVertices+2]=c;
265 m_BufferT[m_Textures.NumVertices+3]=d;
266 m_Textures.NumVertices+=4;
267
268 if(m_Textures.NumVertices==MAX_VERTICES)
269 Render();
270}
271
272void Renderer::DrawTexture(Vec2 pos,Vec2 size,Vec2 texture_pos,Vec2 texture_size,Vec2 texture_total_size,int layer,float texID){
273 PROFILE_FUNCTION();
274
275 #ifdef EDITOR
276 Window::VertexCount+=4;
277 #endif
278
279 auto [a,b,c,d]=VertexBuffer::CreateQuad(pos.x,pos.y,size.w,size.h,texture_pos.x,texture_pos.y,texture_size.w,texture_size.h,texture_total_size.w,texture_total_size.h,layer,texID);
280 m_BufferT[m_Textures.NumVertices]=a;
281 m_BufferT[m_Textures.NumVertices+1]=b;
282 m_BufferT[m_Textures.NumVertices+2]=c;
283 m_BufferT[m_Textures.NumVertices+3]=d;
284 m_Textures.NumVertices+=4;
285
286 if(m_Textures.NumVertices==MAX_VERTICES)
287 Render();
288}
289
290void Renderer::DrawTexture(Vec2 pos,Vec2 size,bool reverse_x,bool reverse_y,int layer,Texture &texture){
291 PROFILE_FUNCTION();
292
293 if(reverse_x && reverse_y)
294 DrawTexture(pos,size,{texture.GetWidth(),texture.GetHeight()},{-texture.GetWidth(),-texture.GetHeight()},{texture.GetWidth(),texture.GetHeight()},layer,texture.GetTexID());
295 else if(reverse_x)
296 DrawTexture(pos,size,{texture.GetWidth(),0},{-texture.GetWidth(),texture.GetHeight()},{texture.GetWidth(),texture.GetHeight()},layer,texture.GetTexID());
297 else if(reverse_y)
298 DrawTexture(pos,size,{0,texture.GetHeight()},{texture.GetWidth(),-texture.GetHeight()},{texture.GetWidth(),texture.GetHeight()},layer,texture.GetTexID());
299 else
300 DrawTexture(pos,size,layer,texture.GetTexID());
301}
302
303void Renderer::DrawSpriteSheet(Vec2 pos,Vec2 size,float row,float col,int layer,SpriteSheet &s){
304 PROFILE_FUNCTION();
305
306 #ifdef EDITOR
307 Window::VertexCount+=4;
308 #endif
309
310 std::array<Vertex,4>quad=s.CreateQuadSpriteSheet(pos.x,pos.y,size.w,size.h,row,col,layer,s.GetTexID());
311 m_BufferT[m_Textures.NumVertices]=quad[0];
312 m_BufferT[m_Textures.NumVertices+1]=quad[1];
313 m_BufferT[m_Textures.NumVertices+2]=quad[2];
314 m_BufferT[m_Textures.NumVertices+3]=quad[3];
315 m_Textures.NumVertices+=4;
316
317 if(m_Textures.NumVertices==MAX_VERTICES)
318 Render();
319}
320
321void Renderer::DrawAnimatedTexture(Vec2 pos,Vec2 size,int layer,SpriteSheet &s,bool &play_animation,bool loop_animation,float animation_delay,float &last_animation_time,int &animation_index){
322 PROFILE_FUNCTION();
323
324 #ifndef EDITOR
325 if(play_animation){
326 #else
327 if(play_animation && ANIMATIONS_PREVIEW){
328 #endif
329 if(glfwGetTime()-last_animation_time>=animation_delay){
330 last_animation_time=glfwGetTime();
331 animation_index++;
332 if(animation_index>=s.m_Width/s.m_TileWidth){
333 if(!loop_animation){
334 play_animation=false;
335 }
336 animation_index=0;
337 }
338 }
339 }
340
341 DrawSpriteSheet(pos,size,ceil((float)s.m_Height/(float)s.m_TileHeight)-1,animation_index,layer,s);
342}
343
344
345void Renderer::DrawTriangle(Vec2 pos1,Vec2 pos2,Vec2 pos3,Vec4 color,int layer){
346 PROFILE_FUNCTION();
347
348 #ifdef EDITOR
349 Window::VertexCount+=3;
350 #endif
351
352 m_BufferTR[m_Triangles.NumVertices]=TriangleVertex(pos1,color,layer);
353 m_BufferTR[m_Triangles.NumVertices+1]=TriangleVertex(pos2,color,layer);
354 m_BufferTR[m_Triangles.NumVertices+2]=TriangleVertex(pos3,color,layer);
355 m_Triangles.NumVertices+=3;
356
357 if(m_Triangles.NumVertices==MAX_VERTICES)
358 Render();
359}
360
361void Renderer::DrawSolidQuad(Vec2 pos,Vec2 size,Vec4 color,int layer){
362 PROFILE_FUNCTION();
363
364 DrawTriangle(pos,{pos.x+size.w,pos.y},{pos.x,pos.y+size.h},color,layer);
365 DrawTriangle({pos.x,pos.y+size.h},{pos.x+size.w,pos.y+size.h},{pos.x+size.w,pos.y},color,layer);
366}
367
368void Renderer::DrawPoint(Vec2 pos,Vec4 color,int layer){
369 PROFILE_FUNCTION();
370
371 #ifdef EDITOR
372 Window::VertexCount++;
373 #endif
374
375 m_BufferP[m_Points.NumVertices]=PointVertex(pos,color,0,layer);
376 ++m_Points.NumVertices;
377
378 if(m_Points.NumVertices==MAX_VERTICES)
379 Render();
380}
381
382void Renderer::DrawCircle(Vec2 pos,Vec4 color,float border,int layer){
383 PROFILE_FUNCTION();
384
385 #ifdef EDITOR
386 Window::VertexCount++;
387 #endif
388
389 m_BufferP[m_Points.NumVertices]=PointVertex(pos,color,border,layer);
390 ++m_Points.NumVertices;
391
392 if(m_Points.NumVertices==MAX_VERTICES)
393 Render();
394}
395
396void Renderer::DrawLine(Vec2 pos1,Vec2 pos2,Vec4 color,int layer){
397 PROFILE_FUNCTION();
398
399 #ifdef EDITOR
400 Window::VertexCount+=2;
401 #endif
402
403 m_BufferL[m_Lines.NumVertices]=LineVertex(pos1,color,layer);
404 m_BufferL[m_Lines.NumVertices+1]=LineVertex(pos2,color,layer);
405 m_Lines.NumVertices+=2;
406
407 if(m_Lines.NumVertices==MAX_VERTICES)
408 Render();
409}
410
411void Renderer::SetLineWidth(float new_size){
412 m_LineWidth=new_size;
413 glLineWidth(glm::max((new_size*m_Zoom)/Window::MAX_WIDTH*Window::Width,1.0f));
414}
415
417 glLineWidth(glm::max((m_LineWidth*m_Zoom)/Window::MAX_WIDTH*Window::Width,1.0f));
418}
419
423void Renderer::SetPointSize(float new_size){
424 //glPointSize(new_size/Window::MAX_WIDTH*Window::Width);
425 m_PointSize=new_size;
426 m_Points.S.Bind();
427 m_Points.S.SetUniform1f("pointSize",new_size/Window::MAX_WIDTH*Window::Width);
428}
429
431 return m_PointSize;
432}
433
435 return m_LineWidth;
436}
437
439 m_AmbientLight=color;
440}
441
443 m_ClearColor=color;
444}
445
447 PROFILE_FUNCTION();
448
449 m_TextureIndex=m_PointIndex=m_LineIndex=m_TriangleIndex=0;
452 delete m_Framebuffer;
453 m_Framebuffer=new Framebuffer;
454
455 #ifndef EDITOR
456 delete m_LightingFramebuffer;
457 m_LightingFramebuffer=new Framebuffer;
458 delete m_TempFramebuffer;
459 m_TempFramebuffer=new Framebuffer;
460
461 float vertices[]={ 0.0f,0.0f,0.0f,0.0f,
462 0.0f,Window::MAX_HEIGHT,0.0f,1.0f,
464 Window::MAX_WIDTH,0.0f,1.0f,0.0f};
465
466 m_Lights.VBO.Bind();
467 m_Lights.VBO.SetData(0,vertices,4,4*sizeof(float));
468 #endif
469 }
470 #ifndef EDITOR
471 m_TempFramebuffer->Bind();
472 glClearColor(0.0f,0.0f,0.0f,1.0f);
473 glClear(GL_COLOR_BUFFER_BIT);
474 m_LightingFramebuffer->Bind();
475 Clear(true);
476 #endif
477 m_Framebuffer->Bind();
478 Clear();
479}
480
482 PROFILE_FUNCTION();
483
484 #ifndef EDITOR
485 m_SHdr.Bind();
486 m_Framebuffer->Bind();
487 m_Lights.VAO.Bind();
488 m_Lights.VBO.Bind();
489 glActiveTexture(GL_TEXTURE0);
490 glBindTexture(GL_TEXTURE_2D,m_Framebuffer->GetColorbufferID());
491 glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);
492 #endif
493
494 m_Framebuffer->Unbind();
495 Clear();
497 Render(true);
498 TEXT_QUEUE->Render(std::numeric_limits<int>::max()-1);
499}
500
501#ifdef EDITOR
502void Renderer::StartEditorScene(Editor *editor){
503 PROFILE_FUNCTION();
504
505 Window::VertexCount=0;
506 Window::DrawCalls=0;
507
509 editor->m_Camera.InitializeProj();
510 Window::ProjUpdate=false;
511 }
512
513 if(Window::SceneFramebufferUpdate){
514 delete editor->m_SceneFramebuffer;
515 editor->m_SceneFramebuffer=new Framebuffer;
516 Window::SceneFramebufferUpdate=false;
517 }
518
519 m_TextureIndex=m_PointIndex=m_LineIndex=m_TriangleIndex=0;
520
522 delete m_LightingFramebuffer;
523 m_LightingFramebuffer=new Framebuffer;
524
525 delete m_TempFramebuffer;
526 m_TempFramebuffer=new Framebuffer;
527
528 float vertices[]={ 0.0f,0.0f,0.0f,0.0f,
529 0.0f,Window::MAX_HEIGHT,0.0f,1.0f,
531 Window::MAX_WIDTH,0.0f,1.0f,0.0f};
532
533 m_Lights.VBO.Bind();
534 m_Lights.VBO.SetData(0,vertices,4,4*sizeof(float));
535 }
536
537 m_TempFramebuffer->Bind();
538 glClearColor(0.0f,0.0f,0.0f,1.0f);
539 glClear(GL_COLOR_BUFFER_BIT);
540
541 m_LightingFramebuffer->Bind();
542 Clear(true);
543
544 editor->m_SceneFramebuffer->Bind();
545 RENDERER->Clear();
546}
547
548void Renderer::DrawEditorScene(Framebuffer *framebuffer){
549 PROFILE_FUNCTION();
550
551 Render();
552 ApplyLight(framebuffer);
553 framebuffer->Bind();
554 m_SHdr.Bind();
555 m_Lights.VAO.Bind();
556 m_Lights.VBO.Bind();
557 glActiveTexture(GL_TEXTURE0);
558 glBindTexture(GL_TEXTURE_2D,framebuffer->GetColorbufferID());
559 glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);
560
561 TEXT_QUEUE->Render(std::numeric_limits<int>::max()-1);
562}
563#endif
564
565void Renderer::Render(bool post_processing){ //if this function gets called because there are MAX_VERTICES vertices, it's not guaranteed that it will respect layer input for subsequent vertices
566 PROFILE_FUNCTION();
567
568 if(m_Textures.NumVertices>0)
569 std::stable_sort(m_BufferT,m_BufferT+m_Textures.NumVertices,cmp1);
570 if(m_Points.NumVertices>0)
571 std::stable_sort(m_BufferP,m_BufferP+m_Points.NumVertices,cmpgeneral<PointVertex>);
572 if(m_Lines.NumVertices>0)
573 std::stable_sort(m_BufferL,m_BufferL+m_Lines.NumVertices,cmpgeneral<LineVertex>);
574 if(m_Triangles.NumVertices>0)
575 std::stable_sort(m_BufferTR,m_BufferTR+m_Triangles.NumVertices,cmpgeneral<TriangleVertex>);
576
577 int min_texture_layer,min_point_layer,min_line_layer,min_triangle_layer,min_text_layer;
578
579 while(true){
580 if(m_Textures.NumVertices==0 && m_Points.NumVertices==0 && m_Lines.NumVertices==0 && m_Triangles.NumVertices==0)
581 break;
582
583 min_texture_layer=GetTexturesMinLayer();
584 min_point_layer=GetPointsMinLayer();
585 min_line_layer=GetLinesMinLayer();
586 min_triangle_layer=GetTrianglesMinLayer();
587 min_text_layer=TEXT_QUEUE->GetQueueMinLayer();
588
589 if(min_texture_layer!=std::numeric_limits<int>::max())
590 RenderTextures(post_processing,std::min({min_point_layer,min_line_layer,min_triangle_layer,min_text_layer}));
591 if(min_point_layer!=std::numeric_limits<int>::max())
592 RenderPoints(std::min({min_texture_layer,min_line_layer,min_triangle_layer,min_text_layer}));
593 if(min_line_layer!=std::numeric_limits<int>::max())
594 RenderLines(std::min({min_texture_layer,min_point_layer,min_triangle_layer,min_text_layer}));
595 if(min_triangle_layer!=std::numeric_limits<int>::max())
596 RenderTriangles(std::min({min_texture_layer,min_point_layer,min_line_layer,min_text_layer}));
597 if(min_text_layer!=std::numeric_limits<int>::max())
598 TEXT_QUEUE->Render(std::min({min_texture_layer,min_point_layer,min_line_layer,min_triangle_layer}));
599 }
600}
601
602void Renderer::RenderTextures(bool post_processing,float max_layer){
603 PROFILE_FUNCTION();
604
605 int lastChecked=-1;
606 int slot=-1;
607 int lastIndex=0;
608
609 m_Textures.VAO.Bind();
610 m_Textures.VBO.Bind();
611 IB.Bind();
612
613 if(m_PostProcessingIndex!=std::numeric_limits<unsigned int>::max() && post_processing)
615 else
616 m_Textures.S.Bind();
617
618 unsigned int i;
619 for(i=m_TextureIndex;i<m_Textures.NumVertices && m_BufferT[i].layer<=max_layer;i++){
620 if(m_BufferT[i].texID!=lastChecked){ //new texture
621 if(slot<m_MaxTextureSlots-1){ //slot available, change last id checked, increment slot and update the vertex
622 lastChecked=m_BufferT[i].texID;
623 ++slot;
624 glActiveTexture(GL_TEXTURE0+slot);
625 glBindTexture(GL_TEXTURE_2D,lastChecked);
626 m_BufferT[i].texID=(float)slot;
627 }else{ //no slots available
628 m_Textures.VBO.SetData(0,(float *)&m_BufferT[lastIndex],i-lastIndex,sizeof(Vertex)); //send the data to the vertex buffer
629 glDrawElements(GL_TRIANGLES,(i-lastIndex)/4*6,GL_UNSIGNED_INT,nullptr); //draw
630 #ifdef EDITOR
631 Window::DrawCalls++;
632 #endif
633 m_TextureIndex=i; //update starting point for the next batch
634 lastChecked=m_BufferT[i].texID;
635 slot=0;
636 m_BufferT[i].texID=(float)slot;
637 }
638 }else{ //same texture, just update the vertex
639 m_BufferT[i].texID=(float)slot;
640 }
641 }
642 if(i-m_TextureIndex>0){ //if there are some vertices remaining, render them
643 m_Textures.VBO.SetData(0,(float *)&m_BufferT[m_TextureIndex],i-m_TextureIndex,sizeof(Vertex));
644 glDrawElements(GL_TRIANGLES,(i-m_TextureIndex)/4*6,GL_UNSIGNED_INT,nullptr);
645 #ifdef EDITOR
646 Window::DrawCalls++;
647 #endif
648 }
649
650 if(m_Textures.NumVertices-i==0){
651 m_TextureIndex=0;
652 m_Textures.NumVertices=0;
653 }else
654 m_TextureIndex=i;
655}
656
657void Renderer::RenderTriangles(float max_layer){
658 PROFILE_FUNCTION();
659
660 m_Triangles.VAO.Bind();
661 m_Triangles.VBO.Bind();
662 m_Triangles.S.Bind();
663
664 unsigned int i;
665 for(i=m_TriangleIndex;i<m_Triangles.NumVertices && m_BufferTR[i].layer<=max_layer;i++);
666
667 if(i-m_TriangleIndex>0){
668 m_Triangles.VBO.SetData(0,(float *)&m_BufferTR[m_TriangleIndex],i-m_TriangleIndex,sizeof(TriangleVertex));
669 glDrawArrays(GL_TRIANGLES,0,i-m_TriangleIndex);
670 #ifdef EDITOR
671 Window::DrawCalls++;
672 #endif
673 }
674
675 if(m_Triangles.NumVertices-i==0){
676 m_TriangleIndex=0;
677 m_Triangles.NumVertices=0;
678 }else
679 m_TriangleIndex=i;
680}
681
682void Renderer::RenderPoints(float max_layer){
683 PROFILE_FUNCTION();
684
685 m_Points.VAO.Bind();
686 m_Points.VBO.Bind();
687 m_Points.S.Bind();
688
689 unsigned int i;
690 for(i=m_PointIndex;i<m_Points.NumVertices && m_BufferP[i].layer<=max_layer;i++);
691
692 if(i-m_PointIndex>0){
693 m_Points.VBO.SetData(0,(float *)&m_BufferP[m_PointIndex],i-m_PointIndex,sizeof(PointVertex));
694 glDrawArrays(GL_POINTS,0,i-m_PointIndex);
695 #ifdef EDITOR
696 Window::DrawCalls++;
697 #endif
698 }
699
700 if(m_Points.NumVertices-i==0){
701 m_PointIndex=0;
702 m_Points.NumVertices=0;
703 }else
704 m_PointIndex=i;
705}
706
707void Renderer::RenderLines(float max_layer){
708 PROFILE_FUNCTION();
709
710 m_Lines.VAO.Bind();
711 m_Lines.VBO.Bind();
712 m_Lines.S.Bind();
713
714 unsigned int i;
715 for(i=m_LineIndex;i<m_Lines.NumVertices && m_BufferL[i].layer<=max_layer;i++);
716
717 if(i-m_LineIndex>0){
718 m_Lines.VBO.SetData(0,(float *)&m_BufferL[m_LineIndex],i-m_LineIndex,sizeof(LineVertex));
719 glDrawArrays(GL_LINES,0,i-m_LineIndex);
720 #ifdef EDITOR
721 Window::DrawCalls++;
722 #endif
723 }
724
725 if(m_Lines.NumVertices-i==0){
726 m_LineIndex=0;
727 m_Lines.NumVertices=0;
728 }else
729 m_LineIndex=i;
730}
731
732void Renderer::Clear(bool ambient_light) const{
733 if(ambient_light)
734 glClearColor(m_AmbientLight.r,m_AmbientLight.g,m_AmbientLight.b,1.0f);
735 else
736 glClearColor(m_ClearColor.r,m_ClearColor.g,m_ClearColor.b,1.0f);
737 glClear(GL_COLOR_BUFFER_BIT);
738}
739
740void Renderer::Clear(Vec4 color) const{
741 glClearColor(color.r,color.g,color.b,color.a);
742 glClear(GL_COLOR_BUFFER_BIT);
743}
744
745void Renderer::SetPostProcessing(const char *uniform_name){
746 m_PostProcessingIndex=Shader::GetSubroutineIndex(uniform_name,m_SPostProcessing.getID());
747}
748
750 m_SPostProcessing.Bind();
751 Shader::SetSubroutineUniform(m_PostProcessingIndex);
752}
753
754void Renderer::AddSegment(Vec2 start_point,Vec2 end_point){
755 segments.push_back(std::make_pair(start_point,end_point));
756}
757
759 segments[0]=std::make_pair(Vec2(0,0),Vec2(0,Window::MAX_HEIGHT));
760 segments[1]=std::make_pair(Vec2(0,Window::MAX_HEIGHT),Vec2(Window::MAX_WIDTH,Window::MAX_HEIGHT));
761 segments[2]=std::make_pair(Vec2(Window::MAX_WIDTH,Window::MAX_HEIGHT),Vec2(Window::MAX_WIDTH,0));
762 segments[3]=std::make_pair(Vec2(Window::MAX_WIDTH,0),Vec2(0,0));
763}
764
766 segments.resize(4);
768}
769
770std::vector<std::pair<Vec2,Vec2>> &Renderer::GetSegments(){
771 return segments;
772}
773
774std::pair<Vec2,float> Renderer::GetIntersection(const std::pair<Vec2,Vec2>&ray,const std::pair<Vec2,Vec2>&seg){
775 PROFILE_FUNCTION();
776
777 float r_px=ray.first.x;
778 float r_py=ray.first.y;
779 float r_dx=ray.second.x-ray.first.x;
780 float r_dy=ray.second.y-ray.first.y;
781
782 float s_px=seg.first.x;
783 float s_py=seg.first.y;
784 float s_dx=seg.second.x-seg.first.x;
785 float s_dy=seg.second.y-seg.first.y;
786
787 float r_mag=glm::sqrt(r_dx*r_dx+r_dy*r_dy);
788 float s_mag=glm::sqrt(s_dx*s_dx+s_dy*s_dy);
789 if(r_dx/r_mag==s_dx/s_mag && r_dy/r_mag==s_dy/s_mag){
790 return std::make_pair(Vec2(-1.0f,-1.0f),-1.0f);
791 }
792
793 float T2=(r_dx*(s_py-r_py)+r_dy*(r_px-s_px))/(s_dx*r_dy-s_dy*r_dx);
794 float T1=(s_px+s_dx*T2-r_px)/r_dx;
795
796 if(T1<0) return std::make_pair(Vec2(-1.0f,-1.0f),-1.0f);
797 if(T2<0 || T2>1) return std::make_pair(Vec2(-1.0f,-1.0f),-1.0f);
798
799 return std::make_pair(Vec2(r_px+r_dx*T1,r_py+r_dy*T1),T1);
800}
801
802void Renderer::DrawLight(Vec2 pos,Vec4 color,LightType light_type,float radius,float blurAmount){
803 PROFILE_FUNCTION();
804
805 if(light_type==ALL_LIGHT || light_type==LIGHT_AROUND_POS_COLL){
806 std::vector<Vec2>points;
807 auto find=[&points](Vec2 point)->bool{
808 for(size_t i=0;i<points.size();i++){
809 if(points[i].x==point.x && points[i].y==point.y)
810 return false;
811 }
812 return true;
813 };
814 for(auto [a,b]:segments){
815 if(find(a))
816 points.push_back(a);
817 if(find(b))
818 points.push_back(b);
819
820 //DrawLine(a.x,a.y,b.x,b.y,0.5f,1.0f,1.0f,1.0f);
821 }
822 std::vector<float>angles;
823
824 for(auto p:points){
825 float angle=glm::atan(p.y-pos.y,p.x-pos.x);
826 angles.push_back(angle-0.0001f);
827 angles.push_back(angle);
828 angles.push_back(angle+0.0001f);
829 }
830
831 std::vector<std::pair<Vec2,float>>intersects;
832 for(auto angle:angles){
833 float dx=glm::cos(angle);
834 float dy=glm::sin(angle);
835
836 std::pair<Vec2,Vec2>ray=std::make_pair(pos,Vec2(pos.x+dx,pos.y+dy));
837 std::pair<Vec2,double> closest_intersect=std::make_pair(Vec2(-1.0f,-1.0f),-1.0f);
838 for(auto seg:segments){
839 std::pair<Vec2,double> intersect=GetIntersection(ray,seg);
840 if(intersect.first.x==-1.0f && intersect.first.y==-1.0f)
841 continue;
842 if((closest_intersect.first.x==-1.0f && closest_intersect.first.y==-1.0f) || (intersect.second<closest_intersect.second && intersect.second!=-1.0f))
843 closest_intersect=intersect;
844 }
845 if(closest_intersect.first.x!=-1.0f && closest_intersect.first.y!=-1.0f)
846 intersects.push_back(std::make_pair(closest_intersect.first,angle));
847 }
848
849 std::sort(begin(intersects),end(intersects),[](const auto& a,const auto& b){
850 return a.second<b.second;
851 });
852
853 Render();
854
855 if(light_type==ALL_LIGHT){
856 m_LightingFramebuffer->Bind();
857 glBlendFunc(GL_ONE,GL_ONE);
858 }else{
859 m_TempFramebuffer->Bind();
860 glClearColor(0.0f,0.0f,0.0f,1.0f);
861 glClear(GL_COLOR_BUFFER_BIT);
862 }
863
864 for(size_t i=0;i<intersects.size()-1;i++){
865 auto a=intersects[i].first;
866 auto b=intersects[i+1].first;
867 DrawTriangle(pos,{a.x,a.y},{b.x,b.y},color,0);
868 }
869 DrawTriangle(pos,intersects[0].first,intersects[intersects.size()-1].first,color,0);
870 RenderTriangles(std::numeric_limits<float>::max());
871
872 if(light_type==LIGHT_AROUND_POS_COLL){
873 KeepCircle(pos,radius,blurAmount);
874 }
875 }else{
876 Render();
877 float previousSize=GetPointSize();
878 SetPointSize(radius*2);
879
880 m_Points.S.Bind();
881 m_Points.S.SetUniform1f("blurAmount",blurAmount);
882 m_LightingFramebuffer->Bind();
883 glBlendFunc(GL_ONE,GL_ONE);
884
885 DrawPoint(pos,color,0);
886 RenderPoints(std::numeric_limits<float>::max());
887 m_Points.S.SetUniform1f("blurAmount",0.0f);
888 SetPointSize(previousSize);
889 }
890 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
891}
892
893void Renderer::KeepCircle(Vec2 pos,float radius,float blurAmount){
894 PROFILE_FUNCTION();
895
896 m_Lights.VBO.Bind();
897
898 glActiveTexture(GL_TEXTURE0);
899 glBindTexture(GL_TEXTURE_2D,m_TempFramebuffer->GetColorbufferID());
900 glActiveTexture(GL_TEXTURE1);
901 glBindTexture(GL_TEXTURE_2D,m_LightingFramebuffer->GetColorbufferID());
902
903 m_LightingFramebuffer->Bind();
904 m_Lights.VAO.Bind();
905 m_Lights.S.Bind();
906
907 m_Lights.S.SetUniform2f("lightPos",pos.x,pos.y);
908 m_Lights.S.SetUniform1f("radius",radius/Window::MAX_WIDTH*Window::Width);
909 m_Lights.S.SetUniform1f("blurAmount",blurAmount);
910
911 int sub_id=Shader::GetSubroutineIndex("KeepCircle",m_Lights.S.getID());
913 glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);
914}
915
917 ApplyLight(m_Framebuffer);
918}
919
921 PROFILE_FUNCTION();
922
923 // m_SHdr.Bind();
924 // m_LightingFramebuffer->Bind();
925 // glActiveTexture(GL_TEXTURE0);
926 // glBindTexture(GL_TEXTURE_2D,m_LightingFramebuffer->GetColorbufferID());
927 // m_Lights.VAO.Bind();
928 // m_Lights.VBO.Bind();
929 // glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);
930
931 glActiveTexture(GL_TEXTURE0);
932 glBindTexture(GL_TEXTURE_2D,framebuffer->GetColorbufferID());
933 glActiveTexture(GL_TEXTURE1);
934 glBindTexture(GL_TEXTURE_2D,m_LightingFramebuffer->GetColorbufferID());
935
936 framebuffer->Bind();
937
938 m_Lights.VAO.Bind();
939 m_Lights.VBO.Bind();
940 m_Lights.S.Bind();
941
942 int sub_id=Shader::GetSubroutineIndex("Merge",m_Lights.S.getID());
944 glDrawElements(GL_TRIANGLES,6,GL_UNSIGNED_INT,nullptr);
945}
946
949
950 m_Points.S.Reload();
951 m_Lines.S.Reload();
952 m_Triangles.S.Reload();
953 m_Textures.S.Reload();
954 m_Lights.S.Reload();
955 m_SHdr.Reload();
956 m_SPostProcessing.Reload();
957
958 m_Points.S.Bind();
959 m_Points.S.SetUniform1f("blurAmount",0.0f);
960
961 m_Textures.S.Bind();
962 m_Textures.S.SetUniform1iv("texID",m_Slots,32);
963
964 m_Lights.S.Bind();
965 m_Lights.S.SetUniform1i("framebuffer",0);
966 m_Lights.S.SetUniform1i("light",1);
967
968 m_SHdr.Bind();
969 m_SHdr.SetUniform1i("tex",0);
970 m_SHdr.SetUniform1f("gamma",m_Gamma);
971 m_SHdr.SetUniform1f("exposure",m_Exposure);
972
973 m_SPostProcessing.Bind();
974 m_SPostProcessing.SetUniform1iv("texID",m_Slots,32);
975
976 SetPointSize(m_PointSize);
977 SetLineWidth(m_LineWidth);
978
979 m_Points.S.Bind();
980 m_Points.S.SetUniformMat4fv("u_PM",glm::value_ptr(m_ViewProj),1);
981 m_Points.S.SetUniform1f("zoom",m_Zoom);
982
983 m_Lines.S.Bind();
984 m_Lines.S.SetUniformMat4fv("u_PM",glm::value_ptr(m_ViewProj),1);
985
986 m_Textures.S.Bind();
987 m_Textures.S.SetUniformMat4fv("u_PM",glm::value_ptr(m_ViewProj),1);
988
989 m_Triangles.S.Bind();
990 m_Triangles.S.SetUniformMat4fv("u_PM",glm::value_ptr(m_ViewProj),1);
991
992 m_Lights.S.Bind();
993 m_Lights.S.SetUniformMat4fv("view_matrix",glm::value_ptr(m_View),1);
994 m_Lights.S.SetUniformMat4fv("u_PM",glm::value_ptr(m_Proj),1);
995 m_Lights.S.SetUniform1f("window_width",Window::Width);
996 m_Lights.S.SetUniform1f("zoom",m_Zoom);
997
998 m_SHdr.Bind();
999 m_SHdr.SetUniformMat4fv("u_PM",glm::value_ptr(m_Proj),1);
1000 m_SHdr.SetUniform1f("gamma",m_Gamma);
1001 m_SHdr.SetUniform1f("exposure",m_Exposure);
1002
1003 m_SPostProcessing.Bind();
1004 m_SPostProcessing.SetUniformMat4fv("u_PM",glm::value_ptr(m_ViewProj),1);
1005}
1006
1008 ImGui::CreateContext();
1009 ImGui::StyleColorsDark();
1010 ImGui_ImplGlfw_InitForOpenGL(Window::Window,true);
1011 ImGui_ImplOpenGL3_Init("#version 460 core");
1012
1013 ImGui::GetIO().FontGlobalScale=1.5f; // Increase the font size (adjust the scale as needed)
1014}
1015
1017ImVec4* colors=ImGui::GetStyle().Colors;
1018 colors[ImGuiCol_Text] = ImVec4(1.00f, 1.00f, 1.00f, 1.00f);
1019 colors[ImGuiCol_TextDisabled] = ImVec4(0.50f, 0.50f, 0.50f, 1.00f);
1020 colors[ImGuiCol_WindowBg] = ImVec4(0.10f, 0.10f, 0.10f, 1.00f);
1021 colors[ImGuiCol_ChildBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
1022 colors[ImGuiCol_PopupBg] = ImVec4(0.19f, 0.19f, 0.19f, 0.92f);
1023 colors[ImGuiCol_Border] = ImVec4(0.19f, 0.19f, 0.19f, 0.29f);
1024 colors[ImGuiCol_BorderShadow] = ImVec4(0.00f, 0.00f, 0.00f, 0.24f);
1025 colors[ImGuiCol_FrameBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
1026 colors[ImGuiCol_FrameBgHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
1027 colors[ImGuiCol_FrameBgActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
1028 colors[ImGuiCol_TitleBg] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
1029 colors[ImGuiCol_TitleBgActive] = ImVec4(0.06f, 0.06f, 0.06f, 1.00f);
1030 colors[ImGuiCol_TitleBgCollapsed] = ImVec4(0.00f, 0.00f, 0.00f, 1.00f);
1031 colors[ImGuiCol_MenuBarBg] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
1032 colors[ImGuiCol_ScrollbarBg] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
1033 colors[ImGuiCol_ScrollbarGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
1034 colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.40f, 0.40f, 0.40f, 0.54f);
1035 colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
1036 colors[ImGuiCol_CheckMark] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
1037 colors[ImGuiCol_SliderGrab] = ImVec4(0.34f, 0.34f, 0.34f, 0.54f);
1038 colors[ImGuiCol_SliderGrabActive] = ImVec4(0.56f, 0.56f, 0.56f, 0.54f);
1039 colors[ImGuiCol_Button] = ImVec4(0.05f, 0.05f, 0.05f, 0.54f);
1040 colors[ImGuiCol_ButtonHovered] = ImVec4(0.19f, 0.19f, 0.19f, 0.54f);
1041 colors[ImGuiCol_ButtonActive] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
1042 colors[ImGuiCol_Header] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
1043 colors[ImGuiCol_HeaderHovered] = ImVec4(0.00f, 0.00f, 0.00f, 0.36f);
1044 colors[ImGuiCol_HeaderActive] = ImVec4(0.20f, 0.22f, 0.23f, 0.33f);
1045 colors[ImGuiCol_Separator] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
1046 colors[ImGuiCol_SeparatorHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
1047 colors[ImGuiCol_SeparatorActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
1048 colors[ImGuiCol_ResizeGrip] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
1049 colors[ImGuiCol_ResizeGripHovered] = ImVec4(0.44f, 0.44f, 0.44f, 0.29f);
1050 colors[ImGuiCol_ResizeGripActive] = ImVec4(0.40f, 0.44f, 0.47f, 1.00f);
1051 colors[ImGuiCol_Tab] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
1052 colors[ImGuiCol_TabHovered] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
1053 colors[ImGuiCol_TabActive] = ImVec4(0.20f, 0.20f, 0.20f, 0.36f);
1054 colors[ImGuiCol_TabUnfocused] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
1055 colors[ImGuiCol_TabUnfocusedActive] = ImVec4(0.14f, 0.14f, 0.14f, 1.00f);
1056 colors[ImGuiCol_PlotLines] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
1057 colors[ImGuiCol_PlotLinesHovered] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
1058 colors[ImGuiCol_PlotHistogram] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
1059 colors[ImGuiCol_PlotHistogramHovered] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
1060 colors[ImGuiCol_TableHeaderBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
1061 colors[ImGuiCol_TableBorderStrong] = ImVec4(0.00f, 0.00f, 0.00f, 0.52f);
1062 colors[ImGuiCol_TableBorderLight] = ImVec4(0.28f, 0.28f, 0.28f, 0.29f);
1063 colors[ImGuiCol_TableRowBg] = ImVec4(0.00f, 0.00f, 0.00f, 0.00f);
1064 colors[ImGuiCol_TableRowBgAlt] = ImVec4(1.00f, 1.00f, 1.00f, 0.06f);
1065 colors[ImGuiCol_TextSelectedBg] = ImVec4(0.20f, 0.22f, 0.23f, 1.00f);
1066 colors[ImGuiCol_DragDropTarget] = ImVec4(0.33f, 0.67f, 0.86f, 1.00f);
1067 colors[ImGuiCol_NavHighlight] = ImVec4(1.00f, 0.00f, 0.00f, 1.00f);
1068 colors[ImGuiCol_NavWindowingHighlight] = ImVec4(1.00f, 0.00f, 0.00f, 0.70f);
1069 colors[ImGuiCol_NavWindowingDimBg] = ImVec4(1.00f, 0.00f, 0.00f, 0.20f);
1070 colors[ImGuiCol_ModalWindowDimBg] = ImVec4(1.00f, 0.00f, 0.00f, 0.35f);
1071
1072 ImGuiStyle& style = ImGui::GetStyle();
1073 style.WindowPadding = ImVec2(8.00f, 8.00f);
1074 style.FramePadding = ImVec2(5.00f, 2.00f);
1075 style.CellPadding = ImVec2(6.00f, 6.00f);
1076 style.ItemSpacing = ImVec2(6.00f, 6.00f);
1077 style.ItemInnerSpacing = ImVec2(6.00f, 6.00f);
1078 style.TouchExtraPadding = ImVec2(0.00f, 0.00f);
1079 style.IndentSpacing = 25;
1080 style.ScrollbarSize = 15;
1081 style.GrabMinSize = 10;
1082 style.WindowBorderSize = 1;
1083 style.ChildBorderSize = 1;
1084 style.PopupBorderSize = 1;
1085 style.FrameBorderSize = 1;
1086 style.TabBorderSize = 1;
1087}
1088
1090 ImGui_ImplGlfw_NewFrame();
1091 ImGui_ImplOpenGL3_NewFrame();
1092 ImGui::NewFrame();
1093}
1094
1096 ImGui::SetNextWindowPos(ImVec2(0,0));
1097 ImGui::SetNextWindowSize(ImVec2(0,0));
1098 ImGui::Begin("FPS",(bool *)__null,ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoTitleBar);
1099
1101 ImGui::Text("FPS: %.1f",Window::FPS);
1102 ImGui::Text("Frame Time: %.4f",Window::DeltaTime*1000);
1103
1104 if(ImGui::Checkbox("V-Sync",&Window::IsVSync))
1105 glfwSwapInterval(Window::IsVSync);
1106
1107 if(ImGui::Checkbox("Fullscreen",&ISFULLSCREEN))
1109 ImGui::End();
1110}
1111
1113 ImGui::EndFrame();
1114 ImGui::Render();
1115 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
1116}
1117
1119 ImGui_ImplOpenGL3_Shutdown();
1120 ImGui_ImplGlfw_Shutdown();
1121 ImGui::DestroyContext();
1122}
void InitializeProj()
Definition camera.cpp:11
void Bind() const
unsigned int GetColorbufferID()
void Unbind() const
void Set(unsigned int num_elem)
void Bind() const
static void ImGui_End_Frame()
void SetClearColor(Vec3 color)
Definition renderer.cpp:442
void SetPointSize(float new_size)
Definition renderer.cpp:423
void Clear(bool ambient_light=false) const
Definition renderer.cpp:732
static void ImGui_Close()
void RenderPoints(float max_layer)
Definition renderer.cpp:682
void RenderTextures(bool post_processing, float max_layer)
Definition renderer.cpp:602
void RenderTriangles(float max_layer)
Definition renderer.cpp:657
void SetPostProcessing(const char *uniform_name)
Definition renderer.cpp:745
void CreateShaders()
Definition renderer.cpp:171
std::pair< Vec2, float > GetIntersection(const std::pair< Vec2, Vec2 > &ray, const std::pair< Vec2, Vec2 > &seg)
Definition renderer.cpp:774
void DrawSolidQuad(Vec2 pos, Vec2 size, Vec4 color, int layer)
Definition renderer.cpp:361
void UpdateLineWidth()
Definition renderer.cpp:416
void DrawCircle(Vec2 pos, Vec4 color, float border, int layer)
Definition renderer.cpp:382
std::vector< std::pair< Vec2, Vec2 > > & GetSegments()
Definition renderer.cpp:770
void PostProcessing()
Definition renderer.cpp:749
void DrawTexture(Vec2 pos, Vec2 size, int layer, float texID)
Definition renderer.cpp:254
void UpdateScreenSegments()
Definition renderer.cpp:758
TonemapType GetTonemapType()
Definition renderer.cpp:234
void ClearSegments()
Definition renderer.cpp:765
void DrawSpriteSheet(Vec2 pos, Vec2 size, float row, float col, int layer, SpriteSheet &s)
Definition renderer.cpp:303
void KeepCircle(Vec2 pos, float radius, float blurAmount)
Definition renderer.cpp:893
void SetLineWidth(float new_size)
Definition renderer.cpp:411
float GetGamma()
Definition renderer.cpp:242
void SetGamma(float gamma)
Definition renderer.cpp:238
void SetAmbientLight(Vec3 color)
Definition renderer.cpp:438
float GetExposure()
Definition renderer.cpp:250
void SetExposure(float exposure)
Definition renderer.cpp:246
void AddSegment(Vec2 start_point, Vec2 end_point)
Definition renderer.cpp:754
void DrawLight(Vec2 pos, Vec4 color, LightType light_type, float radius=0.0f, float blurAmount=0.0f)
Definition renderer.cpp:802
void DrawPoint(Vec2 pos, Vec4 color, int layer)
Definition renderer.cpp:368
void StartScene()
Definition renderer.cpp:446
void DrawTriangle(Vec2 pos1, Vec2 pos2, Vec2 pos3, Vec4 color, int layer)
Definition renderer.cpp:345
static void ImGui_Theme()
void SetTonemapType(TonemapType type)
Definition renderer.cpp:230
void ReloadShaders()
Definition renderer.cpp:947
void DrawLine(Vec2 pos1, Vec2 pos2, Vec4 color, int layer)
Definition renderer.cpp:396
void DrawScene()
Definition renderer.cpp:481
void ApplyLight()
Definition renderer.cpp:916
float GetLineWidth()
Definition renderer.cpp:434
void DrawAnimatedTexture(Vec2 pos, Vec2 size, int layer, SpriteSheet &s, bool &play_animation, bool loop_animation, float animation_delay, float &last_animation_time, int &animation_index)
Definition renderer.cpp:321
static void ImGui_Start_Frame()
static void ImGui_Content()
static void ImGui_Init()
void RenderLines(float max_layer)
Definition renderer.cpp:707
void Render(bool post_processing=false)
Definition renderer.cpp:565
float GetPointSize()
Definition renderer.cpp:430
static unsigned int GetSubroutineIndex(const char *uniform_name, unsigned int shader_id)
Definition shader.cpp:154
void SetUniform2f(const std::string &name, float v0, float v1)
Definition shader.cpp:124
void Bind() const
Definition shader.cpp:68
void SetUniform1iv(const std::string &name, int *v, unsigned int num_elem)
Definition shader.cpp:128
void Reload()
Definition shader.cpp:63
void SetUniform1f(const std::string &name, float v0)
Definition shader.cpp:132
void SetUniform1i(const std::string &name, int v0)
Definition shader.cpp:140
void SetUniformMat4fv(const std::string &name, float *proj, unsigned int num_elem)
Definition shader.cpp:136
static void SetSubroutineUniform(unsigned int uniform_index)
Definition shader.cpp:163
unsigned int getID()
Definition shader.hpp:15
std::array< Vertex, 4 > CreateQuadSpriteSheet(float x, float y, float width, float height, float row, float col, int layer, float texID)
Definition texture.cpp:89
void Render(int layer)
Definition textqueue.cpp:8
int GetQueueMinLayer()
Definition textqueue.hpp:30
unsigned int GetTexID() const
Definition texture.hpp:22
int GetHeight() const
Definition texture.hpp:21
int GetWidth() const
Definition texture.hpp:20
int m_Height
Definition texture.hpp:81
int m_Width
Definition texture.hpp:81
void AddBuffer(const VertexBuffer &vb, const VertexBufferLayout &layout)
void Bind() const
void SetData(unsigned int vertex_index, float *data, unsigned int num_vertices, unsigned int VertexSize)
void Bind() const
static std::array< Vertex, 4 > CreateQuad(float x, float y, float w, float h, int layer, float texID)
int main(int argc, char **argv)
Definition entrypoint.cpp:4
constexpr unsigned int MAX_VERTICES
Max number of vertices per draw call.
Definition global.hpp:4
LightType
Definition global.hpp:7
@ LIGHT_AROUND_POS_COLL
Definition global.hpp:8
@ ALL_LIGHT
Definition global.hpp:8
void FreeMemory(void *ptr, std::source_location src=std::source_location::current())
Definition memory.cpp:11
void * AllocateMemory(size_t size, std::source_location src=std::source_location::current())
Definition memory.cpp:3
GLFWwindow * Window
Definition window.cpp:225
float FPS
Definition window.cpp:231
bool IsVSync
Definition window.cpp:238
const float MAX_WIDTH
Definition window.cpp:251
void ToggleFullScreen()
Definition window.cpp:128
bool ProjUpdate
Definition window.cpp:242
float Width
Definition window.cpp:227
float MAX_HEIGHT
Definition window.cpp:252
bool FramebufferUpdate
Definition window.cpp:241
float DeltaTime
Definition window.cpp:234
std::string TonemapTypeToString(TonemapType type)
Definition renderer.cpp:127
TonemapType StringToTonemapType(const std::string &type)
Definition renderer.cpp:151
TonemapType
Definition renderer.hpp:26
RENDERER m_AmbientLight
fclose(fkey)
int i
char ch
RENDERER m_ClearColor
unsigned int NumVertices
Definition renderer.hpp:21
VertexBuffer VBO
Definition renderer.hpp:17
VertexArray VAO
Definition renderer.hpp:18
RendererData(const char *vertex_path, const char *fragment_path, unsigned int vertex_size)
Definition renderer.cpp:9
VertexBufferLayout VBL
Definition renderer.hpp:19
float y
Definition structs.hpp:9
float w
Definition structs.hpp:6
float h
Definition structs.hpp:10
float x
Definition structs.hpp:5
float b
Definition structs.hpp:17
float r
Definition structs.hpp:17
float g
Definition structs.hpp:17
float g
Definition structs.hpp:23
float b
Definition structs.hpp:23
float a
Definition structs.hpp:23
float r
Definition structs.hpp:23
float texID
Definition structs.hpp:32
int layer
Definition structs.hpp:31
Renderer * RENDERER
The main renderer.
Definition window.cpp:12
bool ISFULLSCREEN
Definition window.cpp:11
TextQueue * TEXT_QUEUE
The text queue.
Definition window.cpp:17