]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Added shader loading and running
authordrumsetmonkey <abelleisle@roadrunner.com>
Fri, 30 Oct 2015 12:51:35 +0000 (08:51 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Fri, 30 Oct 2015 12:51:35 +0000 (08:51 -0400)
include/common.h
main.cpp
shader.frag
test.frag [new file with mode: 0644]

index ce176a6d6c96a5c036ebe2f49e8b724eed312204..359e82528eb943098cfb244cbaf7e67b3404f209 100644 (file)
@@ -20,6 +20,8 @@
 #include <SDL2/SDL_opengl.h>
 #include <SDL2/SDL_image.h>
 #include <SDL2/SDL_mixer.h>
+#include <string>
+#include <fstream>
 
 /*
  *     Include file headers
@@ -30,7 +32,7 @@
  *     This flag lets the compiler know that we are using shaders
 */
 
- #define SHADERS
+ #define SHADERSere
 
 /*
  *     Create a basic 2-point structure for coordinate saving
index e7ecf37077c963fcadd1096a4a18bee0ffa2ef71..f017023c5b61647c51951f87eb766fcfbac3133e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -14,7 +14,8 @@
  *     The call for rendering is made every time the main loop loops, and then
  *     uses interpolation for smooth drawing to the screen. However, the call
  *     for logic would be preferred to be run every set amount of time.
- * 
+ *  
+
  *     The logic loop is currently implemented to run at a certain interval
  *     that we call a 'tick'. As one may now guess, TICKS_PER_SEC defines the
  *     amount of ticks that should be made every second. MSEC_PER_TICK then
@@ -103,6 +104,12 @@ extern std::vector<Entity *        >        entity;
 unsigned int tickCount = 0;
 unsigned int deltaTime = 0;
 
+/*
+ *
+*/
+GLuint fragShader;
+GLuint shaderProgram;
+
 /*
  *     names is used to open a file containing all possible NPC names. It is externally
  *     referenced in src/entities.cpp for getting random names.
@@ -158,6 +165,25 @@ void logic(void);
 void render(void);
 void mainLoop(void);
 
+std::string readFile(const char *filePath) {
+    std::string content;
+    std::ifstream fileStream(filePath, std::ios::in);
+
+    if(!fileStream.is_open()) {
+        std::cerr << "Could not read file " << filePath << ". File does not exist." << std::endl;
+        return "";
+    }
+
+    std::string line = "";
+    while(!fileStream.eof()) {
+        std::getline(fileStream, line);
+        content.append(line + "\n");
+    }
+
+    fileStream.close();
+    return content;
+}
+
 /* 
  *     This offset is used as the player offset in the world drawing so
  *     everything can be moved according to the player
@@ -331,31 +357,41 @@ int main(int argc, char *argv[]){
        /*
         *      Initializes our shaders so that the game has shadows.
        */
-
        #ifdef SHADERS
                std::cout << "Initializing shaders!" << std::endl;
 
-               GLuint fragShader;
-               GLuint shaderProgram;
+               fragShader = glCreateShader(GL_FRAGMENT_SHADER);
+
+               std::string shaderFileContents = readFile("test.frag");
+               const GLchar *shaderSource = shaderFileContents.c_str();
+
+               GLint bufferln = GL_FALSE;
+               int logLength;  
 
-               const GLchar *shaderSource = "shader.frag";
-               GLint bufferln = GL_FALSE;      
 
-               fragShader = glCreateShader(GL_FRAGMENT_SHADER);
                glShaderSource(fragShader, 1, &shaderSource, NULL);
                glCompileShader(fragShader);
 
-               shaderProgram = glCreateProgram();
-               
                glGetShaderiv(fragShader, GL_COMPILE_STATUS, &bufferln);
+               glGetShaderiv(fragShader, GL_INFO_LOG_LENGTH, &logLength);
+               std::vector<char>fragShaderError((logLength > 1) ? logLength : 1);
+               glGetShaderInfoLog(fragShader, logLength, NULL, &fragShaderError[0]);
+               std::cout << &fragShaderError[0] << std::endl;
                
                if(bufferln == GL_FALSE){
                        std::cout << "Error compiling shader" << std::endl;
                }
-               
+
+               shaderProgram = glCreateProgram();              
                glAttachShader(shaderProgram, fragShader);
                glLinkProgram(shaderProgram);
                glValidateProgram(shaderProgram);
+
+               glGetProgramiv(shaderProgram, GL_LINK_STATUS, &bufferln);
+           glGetProgramiv(shaderProgram, GL_INFO_LOG_LENGTH, &logLength);
+           std::vector<char> programError( (logLength > 1) ? logLength : 1 );
+           glGetProgramInfoLog(shaderProgram, logLength, NULL, &programError[0]);
+           std::cout << &programError[0] << std::endl;
        #endif //SHADERS
        
        //glEnable(GL_DEPTH_TEST); //THIS DOESN'T WORK ON LINUX
@@ -572,6 +608,7 @@ void render(){
        glMatrixMode(GL_MODELVIEW);                                     //set the matrix to modelview so we can draw objects
        glPushMatrix();                                                                 //push the  matrix to the top of the matrix stack
        glLoadIdentity();                                                               //replace the entire matrix stack with the updated GL_MODELVIEW mode
+       glEnable(GL_STENCIL_TEST);
        glPushMatrix();                                                                 //basically here we put a blank canvas (new matrix) on the screen to draw on
        
        /*
@@ -687,11 +724,23 @@ void render(){
        */
 
        player->near=true;                      // Draw the player's name
+
+       #ifdef SHADERS
+               glUseProgramObjectARB(shaderProgram);
+               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 0,100);
+               glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 255,255,255);
+               //glBlendFunc(GL_ONE, GL_ONE);
+       #endif //SHADERS
        
        currentWorld->draw(player);
+
+       #ifdef SHADERS
+               glUseProgramObjectARB(0);
+       #endif //SHADERS
        
        player->inv->draw();
 
+
        /*
         *      Draw UI elements. As of 10/20/2015 this includes the player's health bar and the dialog box.
        */
index 7627a120ca70db74e1eb5332c4523784d557f8b5..83c8bdcb2eec9f8063b98158622488395c584a15 100644 (file)
@@ -1,4 +1,4 @@
-#version 140\r
+#version 130\r
 \r
 uniform vec2 lightLocation;\r
 uniform vec3 lightColor;\r
@@ -6,7 +6,7 @@ uniform float screenHeight;
 \r
 void main(){\r
        float distance = length(lightLocation - gl_FragCoord.xy);\r
-       float attenuation = 1.0 / distance;\r
+       float attenuation = 3.0 / distance;\r
        vec4 color = vec4(attenuation, attenuation, attenuation, pow(attenuation, 3)) * vec4(lightColor, 1);\r
        \r
        gl_FragColor = color;\r
diff --git a/test.frag b/test.frag
new file mode 100644 (file)
index 0000000..131a3e6
--- /dev/null
+++ b/test.frag
@@ -0,0 +1,4 @@
+#version 130\r
+void main(){\r
+       gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);\r
+}
\ No newline at end of file