diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-10 00:41:59 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2015-03-10 00:41:59 -0400 |
commit | 7f0e8c7ef70c62814c274f110367db92f01cbb26 (patch) | |
tree | 750bcfec923a826609034ebe9185014521400c68 /src/renderer.cpp | |
parent | b53826079429939cdfbda073608cb85be8ba0738 (diff) | |
download | therapy-7f0e8c7ef70c62814c274f110367db92f01cbb26.tar.gz therapy-7f0e8c7ef70c62814c274f110367db92f01cbb26.tar.bz2 therapy-7f0e8c7ef70c62814c274f110367db92f01cbb26.zip |
C++11'd everything!
Also moved location information from physics components into entity.
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r-- | src/renderer.cpp | 256 |
1 files changed, 98 insertions, 158 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index e7db069..cf6b2bd 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp | |||
@@ -6,6 +6,8 @@ | |||
6 | #include <cstring> | 6 | #include <cstring> |
7 | #include <cstdlib> | 7 | #include <cstdlib> |
8 | #include "game.h" | 8 | #include "game.h" |
9 | #include <glm/glm.hpp> | ||
10 | #include <glm/gtc/matrix_transform.hpp> | ||
9 | 11 | ||
10 | // include stb_image | 12 | // include stb_image |
11 | #define STB_IMAGE_IMPLEMENTATION | 13 | #define STB_IMAGE_IMPLEMENTATION |
@@ -86,27 +88,27 @@ GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) | |||
86 | // Compile Vertex Shader | 88 | // Compile Vertex Shader |
87 | printf("Compiling shader : %s\n", vertex_file_path); | 89 | printf("Compiling shader : %s\n", vertex_file_path); |
88 | char const * VertexSourcePointer = VertexShaderCode.c_str(); | 90 | char const * VertexSourcePointer = VertexShaderCode.c_str(); |
89 | glShaderSource(VertexShaderID, 1, &VertexSourcePointer , NULL); | 91 | glShaderSource(VertexShaderID, 1, &VertexSourcePointer , nullptr); |
90 | glCompileShader(VertexShaderID); | 92 | glCompileShader(VertexShaderID); |
91 | 93 | ||
92 | // Check Vertex Shader | 94 | // Check Vertex Shader |
93 | glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); | 95 | glGetShaderiv(VertexShaderID, GL_COMPILE_STATUS, &Result); |
94 | glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); | 96 | glGetShaderiv(VertexShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); |
95 | std::vector<char> VertexShaderErrorMessage(InfoLogLength); | 97 | std::vector<char> VertexShaderErrorMessage(InfoLogLength); |
96 | glGetShaderInfoLog(VertexShaderID, InfoLogLength, NULL, &VertexShaderErrorMessage[0]); | 98 | glGetShaderInfoLog(VertexShaderID, InfoLogLength, nullptr, &VertexShaderErrorMessage[0]); |
97 | fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); | 99 | fprintf(stdout, "%s\n", &VertexShaderErrorMessage[0]); |
98 | 100 | ||
99 | // Compile Fragment Shader | 101 | // Compile Fragment Shader |
100 | printf("Compiling shader : %s\n", fragment_file_path); | 102 | printf("Compiling shader : %s\n", fragment_file_path); |
101 | char const * FragmentSourcePointer = FragmentShaderCode.c_str(); | 103 | char const * FragmentSourcePointer = FragmentShaderCode.c_str(); |
102 | glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , NULL); | 104 | glShaderSource(FragmentShaderID, 1, &FragmentSourcePointer , nullptr); |
103 | glCompileShader(FragmentShaderID); | 105 | glCompileShader(FragmentShaderID); |
104 | 106 | ||
105 | // Check Fragment Shader | 107 | // Check Fragment Shader |
106 | glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); | 108 | glGetShaderiv(FragmentShaderID, GL_COMPILE_STATUS, &Result); |
107 | glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); | 109 | glGetShaderiv(FragmentShaderID, GL_INFO_LOG_LENGTH, &InfoLogLength); |
108 | std::vector<char> FragmentShaderErrorMessage(InfoLogLength); | 110 | std::vector<char> FragmentShaderErrorMessage(InfoLogLength); |
109 | glGetShaderInfoLog(FragmentShaderID, InfoLogLength, NULL, &FragmentShaderErrorMessage[0]); | 111 | glGetShaderInfoLog(FragmentShaderID, InfoLogLength, nullptr, &FragmentShaderErrorMessage[0]); |
110 | fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); | 112 | fprintf(stdout, "%s\n", &FragmentShaderErrorMessage[0]); |
111 | 113 | ||
112 | // Link the program | 114 | // Link the program |
@@ -120,7 +122,7 @@ GLuint LoadShaders(const char* vertex_file_path, const char* fragment_file_path) | |||
120 | glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); | 122 | glGetProgramiv(ProgramID, GL_LINK_STATUS, &Result); |
121 | glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); | 123 | glGetProgramiv(ProgramID, GL_INFO_LOG_LENGTH, &InfoLogLength); |
122 | std::vector<char> ProgramErrorMessage( glm::max(InfoLogLength, int(1)) ); | 124 | std::vector<char> ProgramErrorMessage( glm::max(InfoLogLength, int(1)) ); |
123 | glGetProgramInfoLog(ProgramID, InfoLogLength, NULL, &ProgramErrorMessage[0]); | 125 | glGetProgramInfoLog(ProgramID, InfoLogLength, nullptr, &ProgramErrorMessage[0]); |
124 | fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); | 126 | fprintf(stdout, "%s\n", &ProgramErrorMessage[0]); |
125 | 127 | ||
126 | glDeleteShader(VertexShaderID); | 128 | glDeleteShader(VertexShaderID); |
@@ -147,7 +149,7 @@ void flipImageData(unsigned char* data, int width, int height, int comps) | |||
147 | void loadMesh(const char* filename, std::vector<glm::vec3>& out_vertices, std::vector<glm::vec2>& out_uvs, std::vector<glm::vec3>& out_normals) | 149 | void loadMesh(const char* filename, std::vector<glm::vec3>& out_vertices, std::vector<glm::vec2>& out_uvs, std::vector<glm::vec3>& out_normals) |
148 | { | 150 | { |
149 | FILE* file = fopen(filename, "r"); | 151 | FILE* file = fopen(filename, "r"); |
150 | if (file == NULL) | 152 | if (file == nullptr) |
151 | { | 153 | { |
152 | fprintf(stderr, "Could not open mesh file %s\n", filename); | 154 | fprintf(stderr, "Could not open mesh file %s\n", filename); |
153 | exit(1); | 155 | exit(1); |
@@ -168,17 +170,17 @@ void loadMesh(const char* filename, std::vector<glm::vec3>& out_vertices, std::v | |||
168 | 170 | ||
169 | if (!strncmp(lineHeader, "v", 2)) | 171 | if (!strncmp(lineHeader, "v", 2)) |
170 | { | 172 | { |
171 | vec3 vertex; | 173 | glm::vec3 vertex; |
172 | fscanf(file, "%f %f %f\n", &vertex.x,&vertex.y,&vertex.z); | 174 | fscanf(file, "%f %f %f\n", &vertex.x,&vertex.y,&vertex.z); |
173 | temp_vertices.push_back(vertex); | 175 | temp_vertices.push_back(vertex); |
174 | } else if (!strncmp(lineHeader, "vt", 3)) | 176 | } else if (!strncmp(lineHeader, "vt", 3)) |
175 | { | 177 | { |
176 | vec2 uv; | 178 | glm::vec2 uv; |
177 | fscanf(file, "%f %f\n", &uv.x, &uv.y); | 179 | fscanf(file, "%f %f\n", &uv.x, &uv.y); |
178 | temp_uvs.push_back(uv); | 180 | temp_uvs.push_back(uv); |
179 | } else if (!strncmp(lineHeader, "vn", 3)) | 181 | } else if (!strncmp(lineHeader, "vn", 3)) |
180 | { | 182 | { |
181 | vec3 normal; | 183 | glm::vec3 normal; |
182 | fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z); | 184 | fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z); |
183 | temp_normals.push_back(normal); | 185 | temp_normals.push_back(normal); |
184 | } else if (!strncmp(lineHeader, "f", 2)) | 186 | } else if (!strncmp(lineHeader, "f", 2)) |
@@ -265,8 +267,8 @@ GLFWwindow* initRenderer() | |||
265 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); | 267 | glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); |
266 | 268 | ||
267 | // Create a window | 269 | // Create a window |
268 | window = glfwCreateWindow(1024, 768, "Aromatherapy", NULL, NULL); | 270 | window = glfwCreateWindow(1024, 768, "Aromatherapy", nullptr, nullptr); |
269 | if (window == NULL) | 271 | if (window == nullptr) |
270 | { | 272 | { |
271 | fprintf(stderr, "Failed to open GLFW window\n"); | 273 | fprintf(stderr, "Failed to open GLFW window\n"); |
272 | glfwTerminate(); | 274 | glfwTerminate(); |
@@ -367,15 +369,15 @@ GLFWwindow* initRenderer() | |||
367 | 369 | ||
368 | glGenBuffers(1, &mesh_vertexbuffer); | 370 | glGenBuffers(1, &mesh_vertexbuffer); |
369 | glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); | 371 | glBindBuffer(GL_ARRAY_BUFFER, mesh_vertexbuffer); |
370 | glBufferData(GL_ARRAY_BUFFER, mesh_vertices.size() * sizeof(vec3), &mesh_vertices[0], GL_STATIC_DRAW); | 372 | glBufferData(GL_ARRAY_BUFFER, mesh_vertices.size() * sizeof(glm::vec3), &mesh_vertices[0], GL_STATIC_DRAW); |
371 | 373 | ||
372 | glGenBuffers(1, &mesh_uvbuffer); | 374 | glGenBuffers(1, &mesh_uvbuffer); |
373 | glBindBuffer(GL_ARRAY_BUFFER, mesh_uvbuffer); | 375 | glBindBuffer(GL_ARRAY_BUFFER, mesh_uvbuffer); |
374 | glBufferData(GL_ARRAY_BUFFER, mesh_uvs.size() * sizeof(vec3), &mesh_uvs[0], GL_STATIC_DRAW); | 376 | glBufferData(GL_ARRAY_BUFFER, mesh_uvs.size() * sizeof(glm::vec3), &mesh_uvs[0], GL_STATIC_DRAW); |
375 | 377 | ||
376 | glGenBuffers(1, &mesh_normalbuffer); | 378 | glGenBuffers(1, &mesh_normalbuffer); |
377 | glBindBuffer(GL_ARRAY_BUFFER, mesh_normalbuffer); | 379 | glBindBuffer(GL_ARRAY_BUFFER, mesh_normalbuffer); |
378 | glBufferData(GL_ARRAY_BUFFER, mesh_normals.size() * sizeof(vec3), &mesh_normals[0], GL_STATIC_DRAW); | 380 | glBufferData(GL_ARRAY_BUFFER, mesh_normals.size() * sizeof(glm::vec3), &mesh_normals[0], GL_STATIC_DRAW); |
379 | 381 | ||
380 | // Load the vertices of a flat surface | 382 | // Load the vertices of a flat surface |
381 | GLfloat g_quad_vertex_buffer_data[] = { | 383 | GLfloat g_quad_vertex_buffer_data[] = { |
@@ -471,7 +473,7 @@ void destroyRenderer() | |||
471 | rendererInitialized = false; | 473 | rendererInitialized = false; |
472 | } | 474 | } |
473 | 475 | ||
474 | Texture* createTexture(int width, int height) | 476 | Texture::Texture(int width, int height) |
475 | { | 477 | { |
476 | if (!rendererInitialized) | 478 | if (!rendererInitialized) |
477 | { | 479 | { |
@@ -479,22 +481,19 @@ Texture* createTexture(int width, int height) | |||
479 | exit(-1); | 481 | exit(-1); |
480 | } | 482 | } |
481 | 483 | ||
482 | Texture* tex = new Texture(); | 484 | this->width = width; |
483 | tex->width = width; | 485 | this->height = height; |
484 | tex->height = height; | ||
485 | 486 | ||
486 | glGenTextures(1, &(tex->texID)); | 487 | glGenTextures(1, &texID); |
487 | glBindTexture(GL_TEXTURE_2D, tex->texID); | 488 | glBindTexture(GL_TEXTURE_2D, texID); |
488 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); | 489 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); |
489 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 490 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
490 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 491 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
491 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 492 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
492 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 493 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
493 | |||
494 | return tex; | ||
495 | } | 494 | } |
496 | 495 | ||
497 | void destroyTexture(Texture* tex) | 496 | Texture::Texture(const char* filename) |
498 | { | 497 | { |
499 | if (!rendererInitialized) | 498 | if (!rendererInitialized) |
500 | { | 499 | { |
@@ -502,12 +501,19 @@ void destroyTexture(Texture* tex) | |||
502 | exit(-1); | 501 | exit(-1); |
503 | } | 502 | } |
504 | 503 | ||
505 | glDeleteTextures(1, &(tex->texID)); | 504 | glGenTextures(1, &texID); |
506 | 505 | glBindTexture(GL_TEXTURE_2D, texID); | |
507 | delete tex; | 506 | unsigned char* data = stbi_load(filename, &width, &height, 0, 4); |
507 | flipImageData(data, width, height, 4); | ||
508 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); | ||
509 | stbi_image_free(data); | ||
510 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | ||
511 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | ||
512 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
513 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | ||
508 | } | 514 | } |
509 | 515 | ||
510 | Texture* loadTextureFromFile(char* filename) | 516 | Texture::Texture(Texture& tex) |
511 | { | 517 | { |
512 | if (!rendererInitialized) | 518 | if (!rendererInitialized) |
513 | { | 519 | { |
@@ -515,22 +521,30 @@ Texture* loadTextureFromFile(char* filename) | |||
515 | exit(-1); | 521 | exit(-1); |
516 | } | 522 | } |
517 | 523 | ||
518 | Texture* tex = new Texture(); | 524 | width = tex.width; |
519 | glGenTextures(1, &(tex->texID)); | 525 | height = tex.height; |
520 | glBindTexture(GL_TEXTURE_2D, tex->texID); | 526 | |
521 | unsigned char* data = stbi_load(filename, &(tex->width), &(tex->height), 0, 4); | 527 | unsigned char* data = (unsigned char*) malloc(4 * width * height); |
522 | flipImageData(data, tex->width, tex->height, 4); | 528 | glBindTexture(GL_TEXTURE_2D, tex.texID); |
523 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex->width, tex->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); | 529 | glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); |
524 | stbi_image_free(data); | 530 | |
531 | glGenTextures(1, &texID); | ||
532 | glBindTexture(GL_TEXTURE_2D, texID); | ||
533 | glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); | ||
525 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 534 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
526 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 535 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
527 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
528 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 536 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
537 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | ||
529 | 538 | ||
530 | return tex; | 539 | free(data); |
531 | } | 540 | } |
532 | 541 | ||
533 | void saveTextureToBMP(Texture* tex, char* filename) | 542 | Texture::Texture(Texture&& tex) : Texture(0, 0) |
543 | { | ||
544 | swap(*this, tex); | ||
545 | } | ||
546 | |||
547 | Texture::~Texture() | ||
534 | { | 548 | { |
535 | if (!rendererInitialized) | 549 | if (!rendererInitialized) |
536 | { | 550 | { |
@@ -538,29 +552,24 @@ void saveTextureToBMP(Texture* tex, char* filename) | |||
538 | exit(-1); | 552 | exit(-1); |
539 | } | 553 | } |
540 | 554 | ||
541 | int size = 54 + 3*tex->width*tex->height; | 555 | glDeleteTextures(1, &texID); |
542 | 556 | } | |
543 | char* buf = (char*) calloc(size, sizeof(char)); | 557 | |
544 | buf[0x00] = 'B'; | 558 | Texture& Texture::operator= (Texture tex) |
545 | buf[0x01] = 'M'; | 559 | { |
546 | *(int*)&(buf[0x0A]) = 54; | 560 | swap(*this, tex); |
547 | *(int*)&(buf[0x12]) = tex->width; | ||
548 | *(int*)&(buf[0x16]) = tex->height; | ||
549 | *(int*)&(buf[0x1C]) = 24; | ||
550 | *(int*)&(buf[0x1E]) = 0; | ||
551 | *(int*)&(buf[0x22]) = size; | ||
552 | |||
553 | glBindTexture(GL_TEXTURE_2D, tex->texID); | ||
554 | glGetTexImage(GL_TEXTURE_2D, 0, GL_BGR, GL_UNSIGNED_BYTE, buf + 54); | ||
555 | |||
556 | FILE* f = fopen(filename, "wb"); | ||
557 | fwrite(buf, sizeof(char), size, f); | ||
558 | fclose(f); | ||
559 | 561 | ||
560 | free(buf); | 562 | return *this; |
563 | } | ||
564 | |||
565 | void swap(Texture& tex1, Texture& tex2) | ||
566 | { | ||
567 | std::swap(tex1.width, tex2.width); | ||
568 | std::swap(tex1.height, tex2.height); | ||
569 | std::swap(tex1.texID, tex2.texID); | ||
561 | } | 570 | } |
562 | 571 | ||
563 | void fillTexture(Texture* tex, Rectangle* dstrect, int r, int g, int b) | 572 | void Texture::fill(Rectangle dstrect, int r, int g, int b) |
564 | { | 573 | { |
565 | if (!rendererInitialized) | 574 | if (!rendererInitialized) |
566 | { | 575 | { |
@@ -570,18 +579,13 @@ void fillTexture(Texture* tex, Rectangle* dstrect, int r, int g, int b) | |||
570 | 579 | ||
571 | // Target the framebuffer | 580 | // Target the framebuffer |
572 | glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); | 581 | glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); |
573 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex->texID, 0); | 582 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texID, 0); |
574 | 583 | ||
575 | // Set up the vertex attributes | 584 | // Set up the vertex attributes |
576 | GLfloat minx = (dstrect == NULL) ? 0.0f : dstrect->x; | 585 | GLfloat minx = (GLfloat) dstrect.x / width * 2.0 - 1.0; |
577 | GLfloat miny = (dstrect == NULL) ? 0.0f : dstrect->y; | 586 | GLfloat miny = -((GLfloat) dstrect.y / height * 2.0 - 1.0); |
578 | GLfloat maxx = (dstrect == NULL) ? tex->width : dstrect->x + dstrect->w; | 587 | GLfloat maxx = (GLfloat) (dstrect.x + dstrect.w) / width * 2.0 - 1.0; |
579 | GLfloat maxy = (dstrect == NULL) ? tex->height : dstrect->y + dstrect->h; | 588 | GLfloat maxy = -((GLfloat) (dstrect.y + dstrect.h) / height * 2.0 - 1.0); |
580 | |||
581 | minx = minx / tex->width * 2.0 - 1.0; | ||
582 | miny = -(miny / tex->height * 2.0 - 1.0); | ||
583 | maxx = maxx / tex->width * 2.0 - 1.0; | ||
584 | maxy = -(maxy / tex->height * 2.0 - 1.0); | ||
585 | 589 | ||
586 | GLfloat vertexbuffer_data[] = { | 590 | GLfloat vertexbuffer_data[] = { |
587 | minx, miny, | 591 | minx, miny, |
@@ -598,7 +602,7 @@ void fillTexture(Texture* tex, Rectangle* dstrect, int r, int g, int b) | |||
598 | glEnableVertexAttribArray(0); | 602 | glEnableVertexAttribArray(0); |
599 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); | 603 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); |
600 | 604 | ||
601 | glViewport(0, 0, tex->width, tex->height); | 605 | glViewport(0, 0, width, height); |
602 | glClear(GL_DEPTH_BUFFER_BIT); | 606 | glClear(GL_DEPTH_BUFFER_BIT); |
603 | glUseProgram(fillShader); | 607 | glUseProgram(fillShader); |
604 | glUniform3f(glGetUniformLocation(fillShader, "vecColor"), r / 255.0, g / 255.0, b / 255.0); | 608 | glUniform3f(glGetUniformLocation(fillShader, "vecColor"), r / 255.0, g / 255.0, b / 255.0); |
@@ -609,7 +613,7 @@ void fillTexture(Texture* tex, Rectangle* dstrect, int r, int g, int b) | |||
609 | glDeleteBuffers(1, &vertexbuffer); | 613 | glDeleteBuffers(1, &vertexbuffer); |
610 | } | 614 | } |
611 | 615 | ||
612 | void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle* dstrect) | 616 | void Texture::blit(Texture& srctex, Rectangle srcrect, Rectangle dstrect) |
613 | { | 617 | { |
614 | if (!rendererInitialized) | 618 | if (!rendererInitialized) |
615 | { | 619 | { |
@@ -619,18 +623,13 @@ void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle | |||
619 | 623 | ||
620 | // Target the framebuffer | 624 | // Target the framebuffer |
621 | glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); | 625 | glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); |
622 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, dsttex->texID, 0); | 626 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, texID, 0); |
623 | 627 | ||
624 | // Set up the vertex attributes | 628 | // Set up the vertex attributes |
625 | GLfloat minx = (dstrect == NULL) ? 0.0f : dstrect->x; | 629 | GLfloat minx = (GLfloat) dstrect.x / width * 2.0 - 1.0; |
626 | GLfloat miny = (dstrect == NULL) ? 0.0f : dstrect->y; | 630 | GLfloat miny = -((GLfloat) dstrect.y / height * 2.0 - 1.0); |
627 | GLfloat maxx = (dstrect == NULL) ? dsttex->width : dstrect->x + dstrect->w; | 631 | GLfloat maxx = (GLfloat) (dstrect.x + dstrect.w) / width * 2.0 - 1.0; |
628 | GLfloat maxy = (dstrect == NULL) ? dsttex->height : dstrect->y + dstrect->h; | 632 | GLfloat maxy = -((GLfloat) (dstrect.y + dstrect.h) / height * 2.0 - 1.0); |
629 | |||
630 | minx = minx / dsttex->width * 2.0 - 1.0; | ||
631 | miny = -(miny / dsttex->height * 2.0 - 1.0); | ||
632 | maxx = maxx / dsttex->width * 2.0 - 1.0; | ||
633 | maxy = -(maxy / dsttex->height * 2.0 - 1.0); | ||
634 | 633 | ||
635 | GLfloat vertexbuffer_data[] = { | 634 | GLfloat vertexbuffer_data[] = { |
636 | minx, miny, | 635 | minx, miny, |
@@ -645,15 +644,10 @@ void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle | |||
645 | glEnableVertexAttribArray(0); | 644 | glEnableVertexAttribArray(0); |
646 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); | 645 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); |
647 | 646 | ||
648 | GLfloat minu = (srcrect == NULL) ? 0.0f : srcrect->x; | 647 | GLfloat minu = (GLfloat) srcrect.x / srctex.width; |
649 | GLfloat minv = (srcrect == NULL) ? 0.0f : srcrect->y; | 648 | GLfloat minv = 1 - ((GLfloat) srcrect.y / srctex.height); |
650 | GLfloat maxu = (srcrect == NULL) ? srctex->width : srcrect->x + srcrect->w; | 649 | GLfloat maxu = (GLfloat) (srcrect.x + srcrect.w) / srctex.width; |
651 | GLfloat maxv = (srcrect == NULL) ? srctex->height : srcrect->y + srcrect->h; | 650 | GLfloat maxv = 1 - ((GLfloat) (srcrect.y + srcrect.h) / srctex.height); |
652 | |||
653 | minu = minu / srctex->width; | ||
654 | minv = 1 - (minv / srctex->height); | ||
655 | maxu = maxu / srctex->width; | ||
656 | maxv = 1 - (maxv / srctex->height); | ||
657 | 651 | ||
658 | GLfloat texcoordbuffer_data[] = { | 652 | GLfloat texcoordbuffer_data[] = { |
659 | minu, minv, | 653 | minu, minv, |
@@ -671,10 +665,10 @@ void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle | |||
671 | // Set up the shader | 665 | // Set up the shader |
672 | glUseProgram(blitShader); | 666 | glUseProgram(blitShader); |
673 | glClear(GL_DEPTH_BUFFER_BIT); | 667 | glClear(GL_DEPTH_BUFFER_BIT); |
674 | glViewport(0, 0, dsttex->width, dsttex->height); | 668 | glViewport(0, 0, width, height); |
675 | 669 | ||
676 | glActiveTexture(GL_TEXTURE0); | 670 | glActiveTexture(GL_TEXTURE0); |
677 | glBindTexture(GL_TEXTURE_2D, srctex->texID); | 671 | glBindTexture(GL_TEXTURE_2D, srctex.texID); |
678 | glUniform1i(glGetUniformLocation(blitShader, "srctex"), 0); | 672 | glUniform1i(glGetUniformLocation(blitShader, "srctex"), 0); |
679 | 673 | ||
680 | // Blit! | 674 | // Blit! |
@@ -687,63 +681,7 @@ void blitTexture(Texture* srctex, Texture* dsttex, Rectangle* srcrect, Rectangle | |||
687 | glDeleteBuffers(1, &vertexbuffer); | 681 | glDeleteBuffers(1, &vertexbuffer); |
688 | } | 682 | } |
689 | 683 | ||
690 | void renderWithoutEffects(Texture* tex) | 684 | void bloomPass1(GLuint srcTex, GLuint dstTex, bool horizontal, glm::vec2 srcRes, glm::vec2 dstRes) |
691 | { | ||
692 | if (!rendererInitialized) | ||
693 | { | ||
694 | fprintf(stderr, "Renderer not initialized\n"); | ||
695 | exit(-1); | ||
696 | } | ||
697 | |||
698 | glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||
699 | glViewport(0, 0, buffer_width, buffer_height); | ||
700 | glClear(GL_COLOR_BUFFER_BIT); | ||
701 | glUseProgram(blitShader); | ||
702 | |||
703 | const GLfloat fullBlitVertices_data[] = { | ||
704 | -1.0, -1.0, | ||
705 | 1.0, -1.0, | ||
706 | -1.0, 1.0, | ||
707 | 1.0, 1.0 | ||
708 | }; | ||
709 | |||
710 | GLuint fullBlitVertices; | ||
711 | glGenBuffers(1, &fullBlitVertices); | ||
712 | glBindBuffer(GL_ARRAY_BUFFER, fullBlitVertices); | ||
713 | glBufferData(GL_ARRAY_BUFFER, sizeof(fullBlitVertices_data), fullBlitVertices_data, GL_STATIC_DRAW); | ||
714 | glEnableVertexAttribArray(0); | ||
715 | glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); | ||
716 | |||
717 | const GLfloat fullBlitTex_data[] = { | ||
718 | 0.0, 0.0, | ||
719 | 1.0, 0.0, | ||
720 | 0.0, 1.0, | ||
721 | 1.0, 1.0 | ||
722 | }; | ||
723 | |||
724 | GLuint fullBlitTex; | ||
725 | glGenBuffers(1, &fullBlitTex); | ||
726 | glBindBuffer(GL_ARRAY_BUFFER, fullBlitTex); | ||
727 | glBufferData(GL_ARRAY_BUFFER, sizeof(fullBlitTex_data), fullBlitTex_data, GL_STATIC_DRAW); | ||
728 | glEnableVertexAttribArray(1); | ||
729 | glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0); | ||
730 | |||
731 | glActiveTexture(GL_TEXTURE0); | ||
732 | glBindTexture(GL_TEXTURE_2D, tex->texID); | ||
733 | glUniform1i(glGetUniformLocation(blitShader, "srctex"), 0); | ||
734 | |||
735 | glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); | ||
736 | |||
737 | glDisableVertexAttribArray(1); | ||
738 | glDisableVertexAttribArray(0); | ||
739 | |||
740 | glDeleteBuffers(1, &fullBlitTex); | ||
741 | glDeleteBuffers(1, &fullBlitVertices); | ||
742 | |||
743 | glfwSwapBuffers(window); | ||
744 | } | ||
745 | |||
746 | void bloomPass1(GLuint srcTex, GLuint dstTex, bool horizontal, vec2 srcRes, vec2 dstRes) | ||
747 | { | 685 | { |
748 | glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); | 686 | glBindFramebuffer(GL_FRAMEBUFFER, generic_framebuffer); |
749 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, dstTex, 0); | 687 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, dstTex, 0); |
@@ -755,7 +693,7 @@ void bloomPass1(GLuint srcTex, GLuint dstTex, bool horizontal, vec2 srcRes, vec2 | |||
755 | glBindTexture(GL_TEXTURE_2D, srcTex); | 693 | glBindTexture(GL_TEXTURE_2D, srcTex); |
756 | glUniform1i(glGetUniformLocation(bloom1Shader, "inTex"), 0); | 694 | glUniform1i(glGetUniformLocation(bloom1Shader, "inTex"), 0); |
757 | 695 | ||
758 | vec2 offset = vec2(0.0); | 696 | glm::vec2 offset = glm::vec2(0.0); |
759 | if (horizontal) | 697 | if (horizontal) |
760 | { | 698 | { |
761 | offset.x = 1.2/srcRes.x; | 699 | offset.x = 1.2/srcRes.x; |
@@ -772,7 +710,7 @@ void bloomPass1(GLuint srcTex, GLuint dstTex, bool horizontal, vec2 srcRes, vec2 | |||
772 | glDisableVertexAttribArray(0); | 710 | glDisableVertexAttribArray(0); |
773 | } | 711 | } |
774 | 712 | ||
775 | void renderScreen(Texture* tex) | 713 | void Texture::renderScreen() |
776 | { | 714 | { |
777 | if (!rendererInitialized) | 715 | if (!rendererInitialized) |
778 | { | 716 | { |
@@ -792,7 +730,7 @@ void renderScreen(Texture* tex) | |||
792 | 730 | ||
793 | // Use the current frame texture, nearest neighbor and clamped to edge | 731 | // Use the current frame texture, nearest neighbor and clamped to edge |
794 | glActiveTexture(GL_TEXTURE0); | 732 | glActiveTexture(GL_TEXTURE0); |
795 | glBindTexture(GL_TEXTURE_2D, tex->texID); | 733 | glBindTexture(GL_TEXTURE_2D, texID); |
796 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); | 734 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); |
797 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); | 735 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); |
798 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 736 | glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
@@ -832,7 +770,6 @@ void renderScreen(Texture* tex) | |||
832 | // We're going to render the screen now | 770 | // We're going to render the screen now |
833 | glBindFramebuffer(GL_FRAMEBUFFER, bloom_framebuffer); | 771 | glBindFramebuffer(GL_FRAMEBUFFER, bloom_framebuffer); |
834 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, preBloomTex, 0); | 772 | glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, preBloomTex, 0); |
835 | //glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||
836 | glViewport(0,0,buffer_width,buffer_height); | 773 | glViewport(0,0,buffer_width,buffer_height); |
837 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | 774 | glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); |
838 | glUseProgram(finalShader); | 775 | glUseProgram(finalShader); |
@@ -855,11 +792,10 @@ void renderScreen(Texture* tex) | |||
855 | glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); | 792 | glUniform1i(glGetUniformLocation(finalShader, "scanlinestex"), 1); |
856 | 793 | ||
857 | // Initialize the MVP matrices | 794 | // Initialize the MVP matrices |
858 | mat4 p_matrix = perspective(42.5f, (float) buffer_width / (float) buffer_height, 0.1f, 100.0f); | 795 | glm::mat4 p_matrix = glm::perspective(42.5f, (float) buffer_width / (float) buffer_height, 0.1f, 100.0f); |
859 | mat4 v_matrix = lookAt(vec3(2,0,0), vec3(0,0,0), vec3(0,1,0)); | 796 | glm::mat4 v_matrix = glm::lookAt(glm::vec3(2,0,0), glm::vec3(0,0,0), glm::vec3(0,1,0)); |
860 | mat4 m_matrix = mat4(1.0); | 797 | glm::mat4 m_matrix = glm::mat4(1.0); |
861 | mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; | 798 | glm::mat4 mvp_matrix = p_matrix * v_matrix * m_matrix; |
862 | //mat4 mv_matrix = v_matrix * m_matrix; | ||
863 | 799 | ||
864 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); | 800 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "MVP"), 1, GL_FALSE, &mvp_matrix[0][0]); |
865 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); | 801 | glUniformMatrix4fv(glGetUniformLocation(finalShader, "worldMat"), 1, GL_FALSE, &m_matrix[0][0]); |
@@ -884,7 +820,7 @@ void renderScreen(Texture* tex) | |||
884 | glDisableVertexAttribArray(0); | 820 | glDisableVertexAttribArray(0); |
885 | 821 | ||
886 | // First pass of bloom! | 822 | // First pass of bloom! |
887 | vec2 buffer_size = vec2(buffer_width, buffer_height); | 823 | glm::vec2 buffer_size = glm::vec2(buffer_width, buffer_height); |
888 | bloomPass1(preBloomTex, bloomPassTex1, true, buffer_size, buffer_size / 4.0f); | 824 | bloomPass1(preBloomTex, bloomPassTex1, true, buffer_size, buffer_size / 4.0f); |
889 | bloomPass1(bloomPassTex1, bloomPassTex2, false, buffer_size / 4.0f, buffer_size / 4.0f); | 825 | bloomPass1(bloomPassTex1, bloomPassTex2, false, buffer_size / 4.0f, buffer_size / 4.0f); |
890 | 826 | ||
@@ -903,7 +839,6 @@ void renderScreen(Texture* tex) | |||
903 | glUniform1i(glGetUniformLocation(bloom2Shader, "blurTex"), 1); | 839 | glUniform1i(glGetUniformLocation(bloom2Shader, "blurTex"), 1); |
904 | 840 | ||
905 | glUniform1f(glGetUniformLocation(bloom2Shader, "iGlobalTime"), glfwGetTime()); | 841 | glUniform1f(glGetUniformLocation(bloom2Shader, "iGlobalTime"), glfwGetTime()); |
906 | // glUniform2f(glGetUniformLocation(bloom2Shader, "resolution"), buffer_width, buffer_height); | ||
907 | 842 | ||
908 | glEnableVertexAttribArray(0); | 843 | glEnableVertexAttribArray(0); |
909 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); | 844 | glBindBuffer(GL_ARRAY_BUFFER, quad_vertexbuffer); |
@@ -915,3 +850,8 @@ void renderScreen(Texture* tex) | |||
915 | 850 | ||
916 | curBuf = (curBuf + 1) % 2; | 851 | curBuf = (curBuf + 1) % 2; |
917 | } | 852 | } |
853 | |||
854 | Rectangle Texture::entirety() | ||
855 | { | ||
856 | return {0, 0, width, height}; | ||
857 | } | ||