Browse Source

Start writing openGL renderer implementation.

Sam Jaffe 6 năm trước cách đây
mục cha
commit
36878c3955

+ 8 - 0
graphics/graphics.xcodeproj/project.pbxproj

@@ -17,6 +17,8 @@
 		CD62FCFA2290E2E500376440 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FCF92290E2E500376440 /* OpenGL.framework */; };
 		CD62FD062291970F00376440 /* libgameutils.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FD052291970F00376440 /* libgameutils.dylib */; };
 		CD62FD1E2292412900376440 /* renderer.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD1C2292412900376440 /* renderer.cxx */; };
+		CD62FD222292C76B00376440 /* renderer_impl.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CD62FD202292C76B00376440 /* renderer_impl.hpp */; };
+		CD62FD232292C76B00376440 /* opengl_renderer.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD212292C76B00376440 /* opengl_renderer.cxx */; };
 		CDA34D9A22517A3D008036A7 /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDA34D9922517A3D008036A7 /* libmath.dylib */; };
 /* End PBXBuildFile section */
 
@@ -64,6 +66,8 @@
 		CD62FCF92290E2E500376440 /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = System/Library/Frameworks/OpenGL.framework; sourceTree = SDKROOT; };
 		CD62FD052291970F00376440 /* libgameutils.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libgameutils.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 		CD62FD1C2292412900376440 /* renderer.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = renderer.cxx; sourceTree = "<group>"; };
+		CD62FD202292C76B00376440 /* renderer_impl.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = renderer_impl.hpp; sourceTree = "<group>"; };
+		CD62FD212292C76B00376440 /* opengl_renderer.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = opengl_renderer.cxx; sourceTree = "<group>"; };
 		CDA34D86225171AA008036A7 /* game */ = {isa = PBXFileReference; lastKnownFileType = folder; name = game; path = include/game; sourceTree = "<group>"; };
 		CDA34D9922517A3D008036A7 /* libmath.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; path = libmath.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
 /* End PBXFileReference section */
@@ -139,6 +143,8 @@
 			isa = PBXGroup;
 			children = (
 				CD62FD1C2292412900376440 /* renderer.cxx */,
+				CD62FD202292C76B00376440 /* renderer_impl.hpp */,
+				CD62FD212292C76B00376440 /* opengl_renderer.cxx */,
 			);
 			name = renderer;
 			sourceTree = "<group>";
@@ -160,6 +166,7 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD62FD222292C76B00376440 /* renderer_impl.hpp in Headers */,
 				CD62FCF72290DC9000376440 /* helper.hpp in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
@@ -285,6 +292,7 @@
 				CD3AC6F81D2C0518002B4BB0 /* texture.cpp in Sources */,
 				CD3AC7261D2C0C63002B4BB0 /* object.cpp in Sources */,
 				CD62FCF82290DC9000376440 /* opengl_helper.cxx in Sources */,
+				CD62FD232292C76B00376440 /* opengl_renderer.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};

+ 3 - 4
graphics/include/game/graphics/renderer.hpp

@@ -15,11 +15,10 @@
 
 namespace graphics {
   class material;
-  class renderer_impl;
+  struct renderer_impl;
   struct vertex;
 
-  class renderer {
-  public:
+  struct renderer {
     virtual ~renderer() {}
     virtual void draw(flyweight<material>, math::matr4 const &,
                       std::vector<vertex> const &) = 0;
@@ -29,7 +28,7 @@ namespace graphics {
 
   class direct_renderer : public renderer {
   public:
-    direct_renderer(renderer_impl * impl) : pimpl(impl) {}
+    direct_renderer(renderer_impl * impl);
     void draw(flyweight<material>, math::matr4 const &,
               std::vector<vertex> const &) override;
     void clear() override;

+ 70 - 0
graphics/src/opengl_renderer.cxx

@@ -0,0 +1,70 @@
+//
+//  opengl_renderer.cxx
+//  graphics
+//
+//  Created by Sam Jaffe on 5/20/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "renderer_impl.hpp"
+
+#ifdef __APPLE__
+#include <OpenGL/gl3.h>
+#endif
+
+#include "matrix/matrix.hpp"
+#include "matrix/matrix_helpers.hpp"
+
+using namespace graphics;
+
+class opengl_renderer : public renderer_impl {
+private:
+  opengl_renderer();
+  ~opengl_renderer();
+
+  void draw(flyweight<material>, math::matr4 const &,
+            std::vector<vertex> const &) override;
+  void clear() override;
+  void flush() override;
+
+private:
+  const math::matr4 identity{math::matrix::identity<float, 4>()};
+  math::matr4 world_to_clip{identity};
+  double current_time{0.0};
+  unsigned int vertex_array_object{0}, vertex_buffer_object{0};
+};
+
+opengl_renderer::opengl_renderer() {
+  glGenVertexArrays(1, &vertex_array_object);
+  glBindVertexArray(vertex_array_object);
+  glGenBuffers(1, &vertex_buffer_object);
+  glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer_object);
+}
+
+opengl_renderer::~opengl_renderer() {
+  glDeleteBuffers(1, &vertex_buffer_object);
+  glDeleteVertexArrays(1, &vertex_array_object);
+}
+
+void opengl_renderer::clear() {
+  world_to_clip = identity;
+
+  // vec2i resolution = env::resolution();
+  // orthogonal_view(0.0, resolution[0], 0.0, resolution[1], -1.0, 1.0);
+
+  glClearDepth(1.f);
+  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
+  glClearColor(0.f, 0.f, 0.0f, 1.f);
+  glEnable(GL_DEPTH_TEST);
+
+  // TODO: Use a unified time calculation
+  current_time = time(NULL);
+
+  // TODO: ???
+  // scale(0.5, 0.5, 1); // Don't know why I need to do this
+}
+
+void opengl_renderer::flush() { glFlush(); }
+
+void opengl_renderer::draw(flyweight<material>, math::matr4 const &,
+                           std::vector<vertex> const &) {}

+ 11 - 0
graphics/src/renderer.cxx

@@ -10,6 +10,7 @@
 #include <vector>
 
 #include "matrix/matrix.hpp"
+#include "renderer_impl.hpp"
 
 using namespace graphics;
 
@@ -19,6 +20,16 @@ struct graphics::vertex {
   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 &,
+                           std::vector<vertex> const & verts) {
+  pimpl->draw(material, {}, verts);
+}
+
+void direct_renderer::clear() { pimpl->clear(); }
+void direct_renderer::flush() { pimpl->flush(); }
+
 batch_renderer::batch_renderer(renderer * impl, std::size_t batch_size)
     : impl_(impl), batches_(), batch_size_(batch_size), elements_(0) {}
 

+ 25 - 0
graphics/src/renderer_impl.hpp

@@ -0,0 +1,25 @@
+//
+//  renderer_impl.hpp
+//  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 "game/util/flyweight.hpp"
+
+namespace graphics {
+  class material;
+  struct vertex;
+
+  struct renderer_impl {
+    virtual ~renderer_impl() {}
+    virtual void draw(flyweight<material>, math::matr4 const &,
+                      std::vector<vertex> const &) = 0;
+    virtual void clear() = 0;
+    virtual void flush() = 0;
+  };
+}