Browse Source

Adding shader initializer code.
Adding trivial env::resource_path() function.
Adding files::load() function to allocate a new buffer.

Sam Jaffe 6 years ago
parent
commit
1102c02764

+ 6 - 2
graphics/graphics.xcodeproj/project.pbxproj

@@ -15,6 +15,7 @@
 		CD62FCF72290DC9000376440 /* helper.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CD62FCF52290DC9000376440 /* helper.hpp */; };
 		CD62FCF82290DC9000376440 /* opengl_helper.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FCF62290DC9000376440 /* opengl_helper.cxx */; };
 		CD62FCFA2290E2E500376440 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FCF92290E2E500376440 /* OpenGL.framework */; };
+		CD62FD062291970F00376440 /* libgameutils.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CD62FD052291970F00376440 /* libgameutils.dylib */; };
 		CDA34D9A22517A3D008036A7 /* libmath.dylib in Frameworks */ = {isa = PBXBuildFile; fileRef = CDA34D9922517A3D008036A7 /* libmath.dylib */; };
 /* End PBXBuildFile section */
 
@@ -60,6 +61,7 @@
 		CD62FCF52290DC9000376440 /* helper.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = helper.hpp; sourceTree = "<group>"; };
 		CD62FCF62290DC9000376440 /* opengl_helper.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = opengl_helper.cxx; sourceTree = "<group>"; };
 		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; };
 		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 */
@@ -69,6 +71,7 @@
 			isa = PBXFrameworksBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD62FD062291970F00376440 /* libgameutils.dylib in Frameworks */,
 				CD62FCFA2290E2E500376440 /* OpenGL.framework in Frameworks */,
 				CDA34D9A22517A3D008036A7 /* libmath.dylib in Frameworks */,
 			);
@@ -124,6 +127,7 @@
 		CDA34D9822517A3D008036A7 /* Frameworks */ = {
 			isa = PBXGroup;
 			children = (
+				CD62FD052291970F00376440 /* libgameutils.dylib */,
 				CD62FCF92290E2E500376440 /* OpenGL.framework */,
 				CDA34D9922517A3D008036A7 /* libmath.dylib */,
 			);
@@ -316,7 +320,7 @@
 				MTL_ENABLE_DEBUG_INFO = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = macosx;
-				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include/ $(PROJECT_DIR)/../include/expect/include/ $(PROJECT_DIR)/../math/ $(PROJECT_DIR)/../include/ $(PROJECT_DIR)/../math/include/ $(PROJECT_DIR)/../";
+				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include/ $(PROJECT_DIR)/../include/expect/include/ $(PROJECT_DIR)/../math/ $(PROJECT_DIR)/../include/ $(PROJECT_DIR)/../math/include/ $(PROJECT_DIR)/../util/include";
 			};
 			name = Debug;
 		};
@@ -362,7 +366,7 @@
 				MACOSX_DEPLOYMENT_TARGET = 10.10;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = macosx;
-				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include/ $(PROJECT_DIR)/../include/expect/include/ $(PROJECT_DIR)/../math/ $(PROJECT_DIR)/../include/ $(PROJECT_DIR)/../math/include/ $(PROJECT_DIR)/../";
+				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include/ $(PROJECT_DIR)/../include/expect/include/ $(PROJECT_DIR)/../math/ $(PROJECT_DIR)/../include/ $(PROJECT_DIR)/../math/include/ $(PROJECT_DIR)/../util/include";
 			};
 			name = Release;
 		};

+ 1 - 1
graphics/include/game/graphics/material.hpp

@@ -7,7 +7,7 @@
 
 #pragma once
 
-#include "util/identity.hpp"
+#include "game/util/identity.hpp"
 
 #include "shader_program.hpp"
 

+ 1 - 1
graphics/include/game/graphics/shader.hpp

@@ -7,7 +7,7 @@
 
 #pragma once
 
-#include "util/identity.hpp"
+#include "game/util/identity.hpp"
 
 #include <string>
 

+ 1 - 1
graphics/include/game/graphics/shader_program.hpp

@@ -7,7 +7,7 @@
 
 #pragma once
 
-#include "util/identity.hpp"
+#include "game/util/identity.hpp"
 
 namespace graphics {
   class program : public identity<program> {

+ 1 - 1
graphics/include/game/graphics/texture.hpp

@@ -8,7 +8,7 @@
 #pragma once
 
 #include "game/math/math_fwd.hpp"
-#include "util/identity.hpp"
+#include "game/util/identity.hpp"
 #include "vector/vector.hpp"
 
 namespace graphics {

+ 38 - 1
graphics/src/opengl_helper.cxx

@@ -8,12 +8,17 @@
 
 #include "helper.hpp"
 
+#include <memory>
 #ifdef __APPLE__
 #include <OpenGL/gl3.h>
 #endif
 
+#include "scope_guard/scope_guard.hpp"
 #include "vector/vector.hpp"
 
+#include "game/util/env.hpp"
+#include "game/util/files.hpp"
+
 namespace graphics { namespace textures {
   static int glfmt(format color_fmt) {
     switch (color_fmt) {
@@ -92,5 +97,37 @@ namespace graphics { namespace textures {
 }}
 
 namespace graphics { namespace shaders {
-  unsigned int init(unsigned int type, std::string const & path) { return 0; }
+  unsigned int init(unsigned int type, std::string const & path) {
+    unsigned int id;
+    int return_code;
+
+    std::unique_ptr<char const[]> buffer;
+
+    // 1. Load the vertex shader code (text file) to a new memory buffer
+    if ((buffer = files::load(env::resource_file(path)))) {
+      //      dbgprintf("Shader file %s could not be opened successfully.\n",
+      //      path.c_str());
+      assert(false);
+    }
+
+    // 2. Create a new shader ID
+    id = glCreateShader(type); // GL_VERTEX_SHADER or GL_FRAGMENT_SHADER
+
+    // 3. Associate the shader code with the new shader ID
+    char const * buffer_ptr = buffer.get();
+    glShaderSource(id, 1, &buffer_ptr, nullptr);
+
+    // 4. Compile the shader (the shader compiler is built in to your graphics
+    //    card driver)
+    glCompileShader(id);
+
+    // 5. Check for compile errors
+    glGetShaderiv(id, GL_COMPILE_STATUS, &return_code);
+
+    if (return_code != GL_TRUE) {
+      //      LogShaderError(id, path);
+      //      throw shader_instantiation_exception();
+    }
+    return id;
+  }
 }}

+ 9 - 0
util/env.cxx

@@ -0,0 +1,9 @@
+//
+//  env.cxx
+//  gameutils
+//
+//  Created by Sam Jaffe on 5/19/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/util/env.hpp"

+ 36 - 0
util/files.cxx

@@ -0,0 +1,36 @@
+//
+//  files.cxx
+//  gameutils
+//
+//  Created by Sam Jaffe on 5/19/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#include "game/util/files.hpp"
+
+#include "scope_guard/scope_guard.hpp"
+
+namespace files {
+  std::unique_ptr<char[]> load(std::string const & absolute_path) {
+    FILE * fp = fopen(absolute_path.c_str(), "r");
+    if (!fp) { return nullptr; }
+
+    scope(exit) { fclose(fp); };
+    // Determine file size
+    fseek(fp, 0, SEEK_END);
+    long size = ftell(fp);
+    if (size < 0) { return nullptr; }
+
+    std::unique_ptr<char[]> buffer{new char[size + 1]};
+
+    rewind(fp);
+    size_t read = fread(buffer.get(), sizeof(char), size, fp);
+    if (read != static_cast<size_t>(size)) {
+      fputs("Error reading file", stderr);
+      return nullptr;
+    } else {
+      buffer[read + 1] = '\0';
+    }
+    return buffer;
+  }
+}

+ 33 - 4
util/gameutils.xcodeproj/project.pbxproj

@@ -7,7 +7,8 @@
 	objects = {
 
 /* Begin PBXBuildFile section */
-		CD3C808C1D6646AF00ACC795 /* identity.hpp in Headers */ = {isa = PBXBuildFile; fileRef = CD3AC7161D2C0794002B4BB0 /* identity.hpp */; };
+		CD62FCFE2291951400376440 /* env.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FCFC2291951400376440 /* env.cxx */; };
+		CD62FD04229195FF00376440 /* files.cxx in Sources */ = {isa = PBXBuildFile; fileRef = CD62FD02229195FF00376440 /* files.cxx */; };
 /* End PBXBuildFile section */
 
 /* Begin PBXContainerItemProxy section */
@@ -43,8 +44,10 @@
 
 /* Begin PBXFileReference section */
 		CD3AC7081D2C0726002B4BB0 /* libgameutils.dylib */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.dylib"; includeInIndex = 0; path = libgameutils.dylib; sourceTree = BUILT_PRODUCTS_DIR; };
-		CD3AC7161D2C0794002B4BB0 /* identity.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = identity.hpp; sourceTree = "<group>"; };
 		CD62FCE622904AD500376440 /* GoogleMock.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = GoogleMock.xcodeproj; path = "../../gmock-xcode-master/GoogleMock.xcodeproj"; sourceTree = "<group>"; };
+		CD62FCFC2291951400376440 /* env.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = env.cxx; sourceTree = "<group>"; };
+		CD62FCFF2291953700376440 /* game */ = {isa = PBXFileReference; lastKnownFileType = folder; name = game; path = include/game; sourceTree = "<group>"; };
+		CD62FD02229195FF00376440 /* files.cxx */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = files.cxx; sourceTree = "<group>"; };
 /* End PBXFileReference section */
 
 /* Begin PBXFrameworksBuildPhase section */
@@ -62,6 +65,7 @@
 			isa = PBXGroup;
 			children = (
 				CD62FCE622904AD500376440 /* GoogleMock.xcodeproj */,
+				CD62FCFF2291953700376440 /* game */,
 				CD3AC70A1D2C0726002B4BB0 /* src */,
 				CD3AC7091D2C0726002B4BB0 /* Products */,
 			);
@@ -78,7 +82,8 @@
 		CD3AC70A1D2C0726002B4BB0 /* src */ = {
 			isa = PBXGroup;
 			children = (
-				CD3AC7161D2C0794002B4BB0 /* identity.hpp */,
+				CD62FCFC2291951400376440 /* env.cxx */,
+				CD62FD02229195FF00376440 /* files.cxx */,
 			);
 			name = src;
 			sourceTree = "<group>";
@@ -101,7 +106,6 @@
 			isa = PBXHeadersBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
-				CD3C808C1D6646AF00ACC795 /* identity.hpp in Headers */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -115,6 +119,7 @@
 				CD3AC7041D2C0726002B4BB0 /* Sources */,
 				CD3AC7051D2C0726002B4BB0 /* Frameworks */,
 				CD3AC7061D2C0726002B4BB0 /* Headers */,
+				CD62FD002291955400376440 /* ShellScript */,
 			);
 			buildRules = (
 			);
@@ -193,11 +198,33 @@
 		};
 /* End PBXReferenceProxy section */
 
+/* Begin PBXShellScriptBuildPhase section */
+		CD62FD002291955400376440 /* ShellScript */ = {
+			isa = PBXShellScriptBuildPhase;
+			buildActionMask = 2147483647;
+			files = (
+			);
+			inputFileListPaths = (
+			);
+			inputPaths = (
+			);
+			outputFileListPaths = (
+			);
+			outputPaths = (
+			);
+			runOnlyForDeploymentPostprocessing = 0;
+			shellPath = /bin/sh;
+			shellScript = "mkdir -p ${BUILT_PRODUCTS_DIR}/usr/local/include/\ncp -r ${PROJECT_DIR}/include/* ${BUILT_PRODUCTS_DIR}/usr/local/include/\n";
+		};
+/* End PBXShellScriptBuildPhase section */
+
 /* Begin PBXSourcesBuildPhase section */
 		CD3AC7041D2C0726002B4BB0 /* Sources */ = {
 			isa = PBXSourcesBuildPhase;
 			buildActionMask = 2147483647;
 			files = (
+				CD62FCFE2291951400376440 /* env.cxx in Sources */,
+				CD62FD04229195FF00376440 /* files.cxx in Sources */,
 			);
 			runOnlyForDeploymentPostprocessing = 0;
 		};
@@ -254,6 +281,7 @@
 				MTL_ENABLE_DEBUG_INFO = YES;
 				ONLY_ACTIVE_ARCH = YES;
 				SDKROOT = macosx;
+				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include $(PROJECT_DIR)/../include/";
 			};
 			name = Debug;
 		};
@@ -300,6 +328,7 @@
 				MACOSX_DEPLOYMENT_TARGET = 10.10;
 				MTL_ENABLE_DEBUG_INFO = NO;
 				SDKROOT = macosx;
+				USER_HEADER_SEARCH_PATHS = "$(PROJECT_DIR)/include $(PROJECT_DIR)/../include/";
 			};
 			name = Release;
 		};

+ 17 - 0
util/include/game/util/env.hpp

@@ -0,0 +1,17 @@
+//
+//  env.hpp
+//  gameutils
+//
+//  Created by Sam Jaffe on 5/19/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <string>
+
+namespace env {
+  std::string resource_file(std::string const & relative_path) {
+    return relative_path;
+  }
+}

+ 16 - 0
util/include/game/util/files.hpp

@@ -0,0 +1,16 @@
+//
+//  files.hpp
+//  gameutils
+//
+//  Created by Sam Jaffe on 5/19/19.
+//  Copyright © 2019 Sam Jaffe. All rights reserved.
+//
+
+#pragma once
+
+#include <memory>
+#include <string>
+
+namespace files {
+  std::unique_ptr<char[]> load(std::string const & absolute_path);
+}

util/identity.hpp → util/include/game/util/identity.hpp