Ver código fonte

Add draw function to opengl_renderer.
Move vertex to own file.

Sam Jaffe 6 anos atrás
pai
commit
9a478e2f51

+ 19 - 0
graphics/include/game/graphics/vertex.h

@@ -0,0 +1,19 @@
+//
+//  vertex.h
+//  graphics
+//
+//  Created by Sam Jaffe on 5/20/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include "game/math/math_fwd.hpp"
+#include "vector/vector.hpp"
+
+namespace graphics {
+  struct vertex {
+    math::vec2 position, texture_coords;
+    math::rgba color;
+  };
+}

+ 42 - 2
graphics/src/opengl_renderer.cxx

@@ -12,6 +12,8 @@
 #include <OpenGL/gl3.h>
 #endif
 
+#include "game/graphics/material.hpp"
+#include "game/graphics/vertex.h"
 #include "matrix/matrix.hpp"
 #include "matrix/matrix_helpers.hpp"
 
@@ -66,5 +68,43 @@ void opengl_renderer::clear() {
 
 void opengl_renderer::flush() { glFlush(); }
 
-void opengl_renderer::draw(flyweight<material>, math::matr4 const &,
-                           std::vector<vertex> const &) {}
+void opengl_renderer::draw(flyweight<material> material,
+                           math::matr4 const & object_to_world,
+                           std::vector<vertex> const & vertices) {
+  // TODO: Don't activate here?
+  auto const & mat = material.actual();
+  // TODO: Attatch shader-id to material-id
+  unsigned int const id = mat.shaders.id;
+  mat.activate();
+
+  int objectLocation = glGetUniformLocation(id, "u_objectToWorldMatrix");
+  glUniformMatrix4fv(objectLocation, 1, false, &object_to_world(0, 0));
+
+  int clipLocation = glGetUniformLocation(id, "u_worldToClipMatrix");
+  glUniformMatrix4fv(clipLocation, 1, false, &world_to_clip(0, 0));
+
+  int timeLocation = glGetUniformLocation(id, "u_time");
+  glUniform1d(timeLocation, current_time);
+
+  // TODO: Cache attribute locations
+  int positionLocation = glGetAttribLocation(id, "a_position");
+  int colorLocation = glGetAttribLocation(id, "a_color");
+  int texCoordsLocation = glGetAttribLocation(id, "a_texCoords");
+
+  glEnableVertexAttribArray(positionLocation);
+  glEnableVertexAttribArray(colorLocation);
+  glEnableVertexAttribArray(texCoordsLocation);
+
+  glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, sizeof(vertex),
+                        &vertices[0].position);
+  glVertexAttribPointer(colorLocation, 4, GL_UNSIGNED_BYTE, GL_TRUE,
+                        sizeof(vertex), &vertices[0].color);
+  glVertexAttribPointer(texCoordsLocation, 2, GL_FLOAT, GL_FALSE,
+                        sizeof(vertex), &vertices[0].texture_coords);
+
+  glDrawArrays(GL_TRIANGLES, 0, static_cast<GLsizei>(vertices.size()));
+
+  glDisableVertexAttribArray(positionLocation);
+  glDisableVertexAttribArray(colorLocation);
+  glDisableVertexAttribArray(texCoordsLocation);
+}

+ 1 - 6
graphics/src/renderer.cxx

@@ -9,17 +9,12 @@
 #include "game/graphics/renderer.hpp"
 #include <vector>
 
+#include "game/graphics/vertex.h"
 #include "matrix/matrix.hpp"
 #include "renderer_impl.hpp"
 
 using namespace graphics;
 
-// TODO (sjaffe): Move to own file
-struct graphics::vertex {
-  math::vec2 position, texture_coords;
-  math::vector::vector<unsigned char, 4> color;
-};
-
 direct_renderer::direct_renderer(renderer_impl * impl) : pimpl(impl) {}
 
 void direct_renderer::draw(flyweight<material> material, math::matr4 const &,