Browse Source

Fix shaders handling

Phyks (Lucas Verney) 4 years ago
parent
commit
62e858f7ac

+ 2
- 2
res/config.json View File

@@ -1,6 +1,6 @@
1 1
 {
2
-    "VERTEX_SHADER_FILENAME": "",
3
-    "FRAGMENT_SHADER_FILENAME": "",
2
+    "VERTEX_SHADER_FILENAME": "raw/shader.vert",
3
+    "FRAGMENT_SHADER_FILENAME": "raw/shader.frag",
4 4
 
5 5
     "CLEAR_COLOR": [0.0, 0.63, 0.9, 1.0],
6 6
 

res/raw/f_shader.txt → res/raw/shader.frag View File

@@ -33,7 +33,7 @@ void main() {
33 33
             dot(v_ec_normal, u_directional_light.direction));
34 34
     float ec_normal_dot_light_halfplane = max(
35 35
             0.0,
36
-            dot(ec_normal, u_directional_light.halfplane));
36
+            dot(v_ec_normal, u_directional_light.halfplane));
37 37
 
38 38
     // Calculate ambient light
39 39
     vec4 ambient_light = (
@@ -51,8 +51,8 @@ void main() {
51 51
     if (ec_normal_dot_light_halfplane > 0.0) {
52 52
         specular_light = (
53 53
                 pow(ec_normal_dot_light_halfplane, u_material.shininess) *
54
-                u_directionalLight.specularColor *
55
-                u_material.specularFactor);
54
+                u_directional_light.specular_color *
55
+                u_material.specular_factor);
56 56
     }
57 57
 
58 58
     vec4 light = ambient_light + diffuse_light + specular_light;

res/raw/v_shader.txt → res/raw/shader.vert View File


+ 2
- 2
src/core/SkiMapRenderer.cc View File

@@ -11,8 +11,8 @@
11 11
 SkiMapRenderer::SkiMapRenderer(int width, int height)
12 12
     : Renderer(width, height)  // This parent constructor will initialize config
13 13
     , terrain_(
14
-            Config::getValue("FRAGMENT_SHADER_FILENAME").GetString(),
15
-            Config::getValue("VERTEX_SHADER_FILENAME").GetString())
14
+            Config::getValue("VERTEX_SHADER_FILENAME").GetString(),
15
+            Config::getValue("FRAGMENT_SHADER_FILENAME").GetString())
16 16
     , directional_light_({
17 17
             {
18 18
                 static_cast<float>(Config::getValue("DIRECTIONAL_LIGHT")[0][0].GetDouble()),

+ 8
- 2
src/core/Terrain.cc View File

@@ -14,8 +14,14 @@ TerrainProgram::TerrainProgram(
14 14
 }
15 15
 
16 16
 
17
-GLint TerrainProgram::get_location(const GLchar* uniform) const {
18
-    return glGetUniformLocation(program_id_, uniform);
17
+GLint TerrainProgram::get_location(const GLchar* uniform_or_attrib) const {
18
+    // Try to find a uniform
19
+    GLint position = glGetUniformLocation(program_id_, uniform_or_attrib);
20
+    if (-1 == position) {
21
+        // If uniform is not found, try to find an attribute
22
+        position = glGetAttribLocation(program_id_, uniform_or_attrib);
23
+    }
24
+    return position;
19 25
 }
20 26
 
21 27
 

+ 1
- 1
src/core/Terrain.h View File

@@ -46,7 +46,7 @@ class TerrainProgram: public Program {
46 46
         /**
47 47
          * Getter for the locations of the uniforms and attributes in the shader.
48 48
          */
49
-        GLint get_location(const GLchar* uniform) const;
49
+        GLint get_location(const GLchar* uniform_or_attrib) const;
50 50
 };
51 51
 
52 52
 

+ 30
- 12
src/framework/Program.cc View File

@@ -3,27 +3,31 @@
3 3
 
4 4
 // System headers
5 5
 #include <fstream>
6
+#include <iostream>
6 7
 #include <sstream>
7 8
 
8 9
 
9 10
 Program::Program(
10 11
         std::string vertex_shader_source_file,
11 12
         std::string fragment_shader_source_file) {
12
-    // TODO[Lucas] Loading of vertex / fragment shader
13 13
     // Load vertex shader from file
14
-    std::ifstream ifs(vertex_shader_source_file);
15
-    std::stringstream vertex_shader_source;
16
-    vertex_shader_source << ifs.rdbuf();
14
+    std::ifstream ifs(vertex_shader_source_file, std::ios::in);
15
+    std::string vertex_shader_source(
16
+            (std::istreambuf_iterator<char>(ifs)),
17
+            std::istreambuf_iterator<char>());
18
+    ifs.close();
17 19
 
18 20
     // Load fragment shader from file
19
-    ifs.open(fragment_shader_source_file);
20
-    std::stringstream fragment_shader_source;
21
-    fragment_shader_source << ifs.rdbuf();
21
+    ifs.open(fragment_shader_source_file, std::ios::in);
22
+    std::string fragment_shader_source(
23
+            (std::istreambuf_iterator<char>(ifs)),
24
+            std::istreambuf_iterator<char>());
25
+    ifs.close();
22 26
 
23 27
     // Build the program
24 28
     build(
25
-            vertex_shader_source.str().c_str(),
26
-            fragment_shader_source.str().c_str());
29
+            vertex_shader_source.c_str(),
30
+            fragment_shader_source.c_str());
27 31
 }
28 32
 
29 33
 
@@ -35,6 +39,7 @@ GLuint Program::get_program_id(void) const {
35 39
 GLuint Program::compile_shader(const GLenum type, const GLchar* source) {
36 40
     if (NULL == source) {
37 41
         // TODO[Error]
42
+        throw 1;
38 43
     }
39 44
 
40 45
     // Create a shader object of the correct type
@@ -44,14 +49,21 @@ GLuint Program::compile_shader(const GLenum type, const GLchar* source) {
44 49
     }
45 50
 
46 51
     // Compile the shader
47
-    glShaderSource(shader_object_id, 1, (const GLchar**)&source, NULL);
52
+    glShaderSource(shader_object_id, 1, &source, NULL);
48 53
     glCompileShader(shader_object_id);
49 54
 
50 55
     // Check the compilation status
51 56
     GLint compile_status;
52 57
     glGetShaderiv(shader_object_id, GL_COMPILE_STATUS, &compile_status);
53
-    if (0 == compile_status) {
58
+
59
+    if (GL_FALSE == compile_status) {
54 60
         // TODO[Error]
61
+        const int max_length = 1000;
62
+        GLchar infoLog[max_length] = "";
63
+        glGetShaderInfoLog(shader_object_id, max_length, NULL, infoLog);
64
+        std::cerr << infoLog << std::endl;
65
+
66
+        throw 1;
55 67
     }
56 68
 
57 69
     // Return the id of the compiled shader
@@ -74,8 +86,13 @@ GLuint Program::link(const GLuint vertex_shader, const GLuint fragment_shader) {
74 86
     // Check link status
75 87
     GLint link_status;
76 88
     glGetProgramiv(program_object_id, GL_LINK_STATUS, &link_status);
77
-    if (0 == link_status) {
89
+    if (GL_FALSE == link_status) {
78 90
         // TODO[Error]
91
+        const int max_length = 1000;
92
+        GLchar infoLog[max_length] = "";
93
+        glGetProgramInfoLog(program_object_id, max_length, NULL, infoLog);
94
+        std::cerr << infoLog << std::endl;
95
+        throw 1;
79 96
     }
80 97
 
81 98
     // Return the id of the linked program
@@ -88,6 +105,7 @@ void Program::build(
88 105
         const GLchar* fragment_shader_source) {
89 106
     if ((NULL == vertex_shader_source) || (NULL == fragment_shader_source)) {
90 107
         // TODO[Error]
108
+        throw 1;
91 109
     }
92 110
 
93 111
     // Compile vertex shader

+ 1
- 1
src/platform/emscripten/CMakeLists.txt View File

@@ -4,7 +4,7 @@ SET (CMAKE_USE_RELATIVE_PATHS TRUE)
4 4
 # Use C++11, use GLFW 3 and preload asset files, exposing them at the root
5 5
 SET (CMAKE_CXX_FLAGS "-std=c++11  -s USE_GLFW=3 --preload-file ${PROJECT_SOURCE_DIR}/res/@/")
6 6
 # In debug, compile with warnings and pedantic
7
-SET (CMAKE_CXX_FLAGS_DEBUG "-Wall -pedantic")
7
+SET (CMAKE_CXX_FLAGS_DEBUG "-g -Wall -pedantic")
8 8
 
9 9
 FILE (GLOB SKIMAP_PLATFORM_EMSCRIPTEN_SRC *.cc)
10 10