]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Flickering lights and light that follows player
authordrumsetmonkey <abelleisle@roadrunner.com>
Wed, 23 Mar 2016 12:24:55 +0000 (08:24 -0400)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Wed, 23 Mar 2016 12:24:55 +0000 (08:24 -0400)
frig.frag
include/entities.h
include/world.h
main.cpp
src/ui.cpp
src/world.cpp

index 07b4a8aaa43629d6beaa6bb7c13ae9adfb92d072..f750f2d989a78bdd68e7925395b62b595d84bef3 100644 (file)
--- a/frig.frag
+++ b/frig.frag
@@ -3,29 +3,33 @@ uniform sampler2D sampler;
 \r
 uniform int numLight;\r
 uniform vec2 lightLocation[64];\r
+uniform float fireFlicker[64];\r
 uniform vec3 lightColor;\r
 uniform float amb;\r
 \r
-float b = .0005;\r
-float minLight = .05;\r
-float radius = sqrt(1.0 / (b * minLight));\r
-\r
+float b = .0005f;\r
+float minLight = .05f;\r
+float radius = sqrt(1.0f / (b * minLight));\r
 //float radius = b*minlight;\r
 \r
+float rand(vec2 co){\r
+    return fract(sin(dot(co.xy ,vec2(12.9898,78.233))) * 43758.5453);\r
+}\r
+\r
 void main(){\r
-       vec4 color = vec4(0.0,0.0,0.0,0.0);\r
+       vec4 color = vec4(0.0f, 0.0f, 0.0f, 0.0f);\r
        for(int i = 0; i < numLight; i++){\r
                vec2 loc = lightLocation[i];\r
                float dist = length(loc - gl_FragCoord.xy);\r
                //float attenuation=1.0/(1.0+0.01*dist+0.00000000001*dist*dist);\r
-               float attenuation = clamp(1.0 - dist*dist/(radius*radius), 0.0, 1.0); attenuation *= attenuation;\r
+               float attenuation = clamp(1.0f - dist*dist/(radius*radius), 0.0f, 1.0f); attenuation *= attenuation;\r
 \r
-               color += vec4(attenuation, attenuation, attenuation, 1.0) * vec4(lightColor, 1.0);\r
+               color += vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(lightColor, 1.0f) * fireFlicker[i];\r
        }\r
        vec2 coords = gl_TexCoord[0].st;\r
        vec4 tex = texture2D(sampler, coords);\r
 \r
-       color += vec4(amb,amb,amb,1.0+amb);\r
+       color += vec4(amb,amb,amb,1.0f+amb);\r
        gl_FragColor = tex * vec4(color)*tex.a;\r
 }\r
 \r
@@ -36,4 +40,4 @@ void main(){
        .00008          500\r
        .00002          1000\r
        .00005          2000\r
-*/
\ No newline at end of file
+*/\r
index 450975f32579fc3a78ad23fb96aeaae1022ae1b8..442219cdb16f7416f3d4a02aad0163ae939f25aa 100644 (file)
@@ -139,10 +139,10 @@ public:
 
        vec2 loc;
        vec2 vel;
-       
+
        float width;
        float height;
-       
+
        float speed;    // A speed factor for X movement
 
        /*
@@ -172,7 +172,7 @@ public:
 
        char   *name;
        GENDER  gender;
-       
+
        Texturec *tex;
        Texturec *ntex;
 
@@ -180,22 +180,21 @@ public:
 
        void draw(void);
        void spawn(float, float);
-       
+
        int ticksToUse;                         // Used by wander()
-       
+
        virtual void wander(int){}
        virtual void interact(){}
 
        void follow(Entity *e);
-       
+
        virtual ~Entity(){}
 };
 
 class Player : public Entity{
 public:
        QuestHandler qh;
-       bool light = false;
-       
+
        Player();
        ~Player();
        void save(void);
@@ -206,10 +205,10 @@ class NPC : public Entity{
 public:
        std::vector<int (*)(NPC *)>aiFunc;
        int dialogIndex;
-       
+
        NPC();
        ~NPC();
-       
+
        void addAIFunc(int (*func)(NPC *),bool preload);
        void clearAIFunc(void);
        virtual void interact();
@@ -233,10 +232,10 @@ public:
        World *inWorld;
        std::string inside;
        std::string textureLoc;
-       
+
        Structures();
        ~Structures();
-       
+
        unsigned int spawn(BUILD_SUB, float, float);
 };
 
@@ -246,10 +245,10 @@ public:
        double init_y;
        void (*hey)(Mob *callee);
        std::string heyid;
-       
+
        Mob(int);
        ~Mob();
-       
+
        void wander(int);
 };
 
@@ -259,13 +258,13 @@ private:
 public:
        std::string pickupDialog;
        bool questObject = false;
-       
+
        Object();
        Object(std::string in,std::string pd);
        ~Object();
-       
+
        void reloadTexture(void);
-       
+
        void interact(void);
 };
 #endif // ENTITIES_H
index 152d654e3de7cf21376aa02a2b32128cc364f1c4..85b5370e1cfbd661c331e495f4c446953c328600 100644 (file)
@@ -46,16 +46,6 @@ enum class WorldWeather : unsigned char {
        Rain            /**< Rain (to be implemented)*/
 };
 
-/**
- * The light structure, used to store light coordinates and color.
- */
-
-typedef struct {
-       vec2 loc;               /**< Light location */
-       Color color;    /**< Light color */
-       float radius;
-} Light;
-
 /**
  * The line structure.
  * This structure is used to store the world's ground, stored in vertical
@@ -85,6 +75,32 @@ extern std::string currentXML;
 // prototype so Village can reference it
 class World;
 
+/**
+ * The light structure, used to store light coordinates and color.
+ */
+
+typedef struct {
+       vec2 loc;               /**< Light location */
+       Color color;    /**< Light color */
+       float radius;   /**< Light radius */
+
+       bool belongsTo;
+       Entity *following;
+
+       bool flame;
+       float fireFlicker;
+       vec2 fireLoc;
+
+       // Light(vec2 l, Color c, float r){
+       //      loc = l;
+       //      color = c;
+       //      radius = r;
+       //      belongsTo = false;
+       //      following = nullptr;
+       // }
+} Light;
+
+
 /**
  * The village class, used to group structures into villages.
  */
index 0b3984e9673789e0e068cf301ee5f7889d5b1219..f9968b62f8b0feeb6c565e24ecfd0c8ac057e1b9 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -343,7 +343,7 @@ int main(int argc, char *argv[]){
 
        std::cout << "Initializing shaders!" << std::endl;
 
-       const GLchar *shaderSource = readFile("test.frag");
+       const GLchar *shaderSource = readFile("frig.frag");
 
        GLint bufferln = GL_FALSE;
        int logLength;
@@ -672,7 +672,7 @@ void render(){
 
                ui::putText(offset.x-SCREEN_WIDTH/2,
                                        (offset.y+SCREEN_HEIGHT/2)-ui::fontSize,
-                                       "FPS: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nTc: %u\nHA: %+.2f\nPl: %d\n Vol: %f",
+                                       "FPS: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nTc: %u\nHA: %+.2f\nVol: %f",
                                        fps,
                                        player->ground,
                                        SCREEN_WIDTH,                           // Window dimensions
@@ -682,7 +682,6 @@ void render(){
                                        debugY,                                         // The player's y coordinate
                                        tickCount,
                                        handAngle,
-                                       player->light,
                                        VOLUME_MASTER
                                        );
 
index 458e797ae6923073399fc84ac856e6747bbe6e9d..f0736a545c4b9e9693adfb57de98938ccd7f7b10 100644 (file)
@@ -1445,10 +1445,18 @@ EXIT:
                                        heyOhLetsGo = 0;
                                        break;
                                case SDLK_l:
-                                       player->light^=true;
+                                       currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
+                                       currentWorld->light.back().belongsTo = true;
+                                       currentWorld->light.back().following = player;
+                                       currentWorld->light.back().flame = true;
                                        break;
                                case SDLK_f:
                                        currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
+                                       std::cout << currentWorld->light.back().belongsTo << std::endl;
+                                       currentWorld->light.back().belongsTo = false;
+                                       std::cout << currentWorld->light.back().belongsTo << std::endl;
+                                       currentWorld->light.back().following = nullptr;
+                                       currentWorld->light.back().flame = true;
                                        break;
                                case SDLK_g:
                                        //currentWorld->addStructure(LAMP_POST, player->loc.x, player->loc.y, NULL);
@@ -1465,13 +1473,15 @@ EXIT:
                                case SDLK_b:
                                        currentWorld->addStructure(FIRE_PIT, player->loc.x, player->loc.y, "", "");
                                        currentWorld->addLight({player->loc.x + SCREEN_WIDTH/2, player->loc.y},{1.0f,1.0f,1.0f});
+                                       currentWorld->light.back().belongsTo = false;
+                                       currentWorld->light.back().following = nullptr;
+                                       currentWorld->light.back().flame = true;
                                        break;
                                case SDLK_F12:
                                        // Make the BYTE array, factor of 3 because it's RBG.
                                        static GLubyte* pixels;
                                        pixels = new GLubyte[ 3 * SCREEN_WIDTH * SCREEN_HEIGHT];
                                        glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, pixels);
-
                                        //static std::thread scr;
                                        //scr = std::thread(takeScreenshot,pixels);
                                        //scr.detach();
index 1a7322fd400584d2efe0475bfff6b31795aef2f7..ca070a84e99c08d751968d7d3580f9b72e42bf65 100644 (file)
@@ -529,7 +529,13 @@ draw( Player *p )
        glActiveTexture( GL_TEXTURE0 );
        bgTex->bindNext();
 
-       std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size() + p->light));
+    for(auto &l : light){
+        if(l.belongsTo){
+            l.loc.x = l.following->loc.x + SCREEN_WIDTH/2;
+        }
+    }
+
+       std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size()));
        auto pointArray = pointArrayBuf.get();
 
        for ( i = 0; i < (int)light.size(); i++ ) {
@@ -544,16 +550,11 @@ draw( Player *p )
        glUniform1i( glGetUniformLocation( shaderProgram, "sampler"), 0 );
        glUniform1f( glGetUniformLocation( shaderProgram, "amb"    ), shadeAmbient );
 
-       if ( p->light ) {
-               pointArray[2 * (light.size() + 1)    ] = (float)( p->loc.x + SCREEN_WIDTH / 2 );
-               pointArray[2 * (light.size() + 1) + 1] = (float)( p->loc.y );
-       }
-
-       if ( light.size() + (int)p->light == 0)
+       if ( light.size() == 0)
                glUniform1i( glGetUniformLocation( shaderProgram, "numLight"), 0);
        else {
-               glUniform1i ( glGetUniformLocation( shaderProgram, "numLight"     ), light.size() + (int)p->light );
-               glUniform2fv( glGetUniformLocation( shaderProgram, "lightLocation"), light.size() + (int)p->light, pointArray );
+               glUniform1i ( glGetUniformLocation( shaderProgram, "numLight"     ), light.size());
+               glUniform2fv( glGetUniformLocation( shaderProgram, "lightLocation"), light.size(), pointArray );
                glUniform3f ( glGetUniformLocation( shaderProgram, "lightColor"   ), 1.0f, 1.0f, 1.0f );
        }
 
@@ -1219,36 +1220,70 @@ void IndoorWorld::draw(Player *p){
         *      Draw the background.
        */
 
-       glEnable(GL_TEXTURE_2D);
+       //glEnable(GL_TEXTURE_2D);
 
-       std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size() + p->light));
-       auto pointArray = pointArrayBuf.get();
+    std::cout << "Lights and shit" << std::endl;
+    for(auto &l : light){
+        if(l.belongsTo){
+            std::cout << "Is following" << std::endl;
+            l.loc.x = l.following->loc.x + SCREEN_WIDTH/2;
+            l.loc.y = l.following->loc.y;
+        }
+        if(l.flame){
+            l.fireFlicker = .9+((rand()%2)/10.0f);
+            l.fireLoc.x = l.loc.x + (rand()%2-1)*3;
+            l.fireLoc.y = l.loc.y + (rand()%2-1)*3;
+
+            std::cout << l.fireLoc.x << "," << l.fireLoc.y << std::endl;
+            std::cout << l.loc.x << "," << l.loc.y << std::endl << std::endl;
+        }else{
+            l.fireFlicker = 1.0f;
+        }
+    }
 
-       for ( i = 0; i < light.size(); i++ ) {
-               pointArray[2 * i    ] = light[i].loc.x - offset.x;
-               pointArray[2 * i + 1] = light[i].loc.y;
+    std::cout << "Making light arrays" << std::endl;
+    std::unique_ptr<GLfloat[]> pointArrayBuf = std::make_unique<GLfloat[]> (2 * (light.size()));
+       auto pointArray = pointArrayBuf.get();
+    GLfloat flameArray[64];
+
+    std::cout << "Setting array locations" << std::endl;
+       for (i = 0; i < light.size(); i++) {
+        if(light[i].flame){
+               pointArray[2 * i    ] = light[i].fireLoc.x - offset.x;
+               pointArray[2 * i + 1] = light[i].fireLoc.y;
+        }else{
+            pointArray[2 * i    ] = light[i].loc.x - offset.x;
+            pointArray[2 * i + 1] = light[i].loc.y;
+        }
        }
 
-       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
-       glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
+    std::cout << "Flame array" << std::endl;
+    for(i = 0; i < light.size(); i++){
+        flameArray[i] = light[i].fireFlicker;
+    }
 
-       glUseProgram( shaderProgram );
-       glUniform1i( glGetUniformLocation( shaderProgram, "sampler"), 0 );
-       glUniform1f( glGetUniformLocation( shaderProgram, "amb"    ), 0.3f );
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+       glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
 
-       if ( p->light ) {
-               pointArray[2 * (light.size() + 1)    ] = (float)( p->loc.x + SCREEN_WIDTH / 2 );
-               pointArray[2 * (light.size() + 1) + 1] = (float)( p->loc.y );
-       }
+       glUseProgram( shaderProgram );
+       glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
+       glUniform1f(glGetUniformLocation(shaderProgram, "amb"    ), 0.02f + light.size()/50.0f);
+    glUniform1i(glGetUniformLocation(shaderProgram, "fire"   ), 1);
 
-       if ( light.size() + (int)p->light == 0)
-               glUniform1i( glGetUniformLocation( shaderProgram, "numLight"), 0);
+    std::cout << "Uniform sending" << std::endl;
+       if ( light.size() == 0)
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 0);
        else {
-               glUniform1i ( glGetUniformLocation( shaderProgram, "numLight"     ), light.size() + (int)p->light );
-               glUniform2fv( glGetUniformLocation( shaderProgram, "lightLocation"), light.size() + (int)p->light, pointArray );
-               glUniform3f ( glGetUniformLocation( shaderProgram, "lightColor"   ), 1.0f, 1.0f, 1.0f );
+               glUniform1i (glGetUniformLocation(shaderProgram, "numLight"     ), light.size());
+               glUniform2fv(glGetUniformLocation(shaderProgram, "lightLocation"), light.size(), pointArray);
+               glUniform3f (glGetUniformLocation(shaderProgram, "lightColor"   ), 1.0f, 1.0f, 1.0f);
+        glUniform1fv(glGetUniformLocation(shaderProgram, "fireFlicker"), light.size(), flameArray);
        }
 
+    //delete[] flameArray;
+
+    std::cout << "Done shading" << std::endl;
+
        bgTex->bind(0);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); //for the s direction
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); //for the t direction
@@ -1262,7 +1297,9 @@ void IndoorWorld::draw(Player *p){
        glEnd();
 
        glUseProgram(0);
-       glDisable(GL_TEXTURE_2D);
+       //glDisable(GL_TEXTURE_2D);
+
+    std::cout << "Faggot" << std::endl;
 
        /*
         *      Calculate the starting and ending points to draw the ground from.
@@ -1297,6 +1334,8 @@ void IndoorWorld::draw(Player *p){
        glEnd();
        glUseProgram(0);
 
+    std::cout << "Queer" << std::endl;
+
        /*
         *      Draw all entities.
        */
@@ -1308,6 +1347,8 @@ void IndoorWorld::draw(Player *p){
                e->draw();
 
        p->draw();
+
+    std::cout << "Cranmore Tubing park" << std::endl;
 }
 
 Arena::Arena(World *leave,Player *p,Mob *m){