]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Added support for multiple lights
authordrumsetmonkey <abelleisle@roadrunner.com>
Sat, 9 Jan 2016 02:44:28 +0000 (21:44 -0500)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Sat, 9 Jan 2016 02:44:28 +0000 (21:44 -0500)
main.cpp
src/gameplay.cpp
src/world.cpp
test.frag

index d5b9f780f2dd0f420116f27889e56af1e6224af1..9cc135d4e5d669a607faf74392c95a904b55547e 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -719,7 +719,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",
+                                       "FPS: %d\nG:%d\nRes: %ux%u\nE: %d\nPOS: (x)%+.2f\n     (y)%+.2f\nTc: %u\nHA: %+.2f\nPl: %d",
                                        fps,
                                        player->ground,
                                        SCREEN_WIDTH,                           // Window dimensions
@@ -728,7 +728,8 @@ void render(){
                                        player->loc.x,                          // The player's x coordinate
                                        debugY,                                         // The player's y coordinate
                                        tickCount,
-                                       handAngle
+                                       handAngle,
+                                       player->light
                                        );
                
                if(ui::posFlag){
index e8436e79f7431aaec8cd746305e957450ab52219..e4fb000cf67b8acef0899f70391928e8c15e0ffe 100644 (file)
@@ -179,6 +179,7 @@ void initEverything(void){
 
        worldSpawnHill2->addStructure(STRUCTURET,HOUSE,(rand()%120*HLINE),100,worldSpawnHill2_Building1);
        worldSpawnHill2->addLight({300,100},{1.0f,1.0f,1.0f});
+       worldSpawnHill2->addLight({500,100},{1.0f,1.0f,1.0f});
        worldSpawnHill2->getAvailableNPC()->addAIFunc(worldSpawnHill2_Quest1,false);
        
        worldFirstVillage->addVillage(5,0,0,STRUCTURET,worldSpawnHill2_Building1);
index a52e3f36cf235e07c35464a501204878dc1eb79d..e97ec5635517e1543a0301dd5f655ff46b4e2f87 100644 (file)
@@ -669,22 +669,26 @@ LOOP2:
        glActiveTexture(GL_TEXTURE0);
        bgTex->bindNext();
 
+       GLfloat pointArray[light.size()][2];
+       for(uint w = 0; w < light.size(); w++){
+               pointArray[w][0] = light[w].loc.x - offset.x;
+               pointArray[w][1] = light[w].loc.y;
+       }
        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
        glUseProgram(shaderProgram);
        glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
        glUniform1f(glGetUniformLocation(shaderProgram, "amb"), float(shade+50.0f)/100.0f);
        if(p->light){
-               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), p->loc.x - offset.x+SCREEN_WIDTH/2,p->loc.y);
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 1);
+               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), p->loc.x - offset.x+SCREEN_WIDTH/2, p->loc.y);
                glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1.0f,1.0f,1.0f);
-       }else if(light.size() == 0){
-               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 0,-1000);
-               glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 0.0f,0.0f,0.0f);
+       }else if(!light.size()){
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 0);
        }else{
-               for(auto &l : light){
-                       glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), l.loc.x-offset.x,l.loc.y);
-                       glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), l.color.red,l.color.green,l.color.blue);
-               }
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), light.size());
+               glUniform2fv(glGetUniformLocation(shaderProgram, "lightLocation"), light.size(), (GLfloat *)&pointArray); 
+               glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1.0f,1.0f,1.0f);
        }
 
        glBegin(GL_QUADS);
@@ -1268,20 +1272,24 @@ void IndoorWorld::draw(Player *p){
        */
        
        glEnable(GL_TEXTURE_2D);
+       GLfloat pointArray[light.size()][2];
+       for(uint w = 0; w < light.size(); w++){
+               pointArray[w][0] = light[w].loc.x - offset.x;
+               pointArray[w][1] = light[w].loc.y;
+       }
        glUseProgram(shaderProgram);
        glUniform1i(glGetUniformLocation(shaderProgram, "sampler"), 0);
        glUniform1f(glGetUniformLocation(shaderProgram, "amb"), 0.0f);
        if(p->light){
-               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), p->loc.x - offset.x+SCREEN_WIDTH/2,p->loc.y);
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 1);
+               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), p->loc.x - offset.x+SCREEN_WIDTH/2, p->loc.y);
                glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1.0f,1.0f,1.0f);
-       }else if(light.size() == 0){
-               glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 0,-1000);
-               glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 0.0f,0.0f,0.0f);
+       }else if(!light.size()){
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), 0);
        }else{
-               for(auto &l : light){
-                       glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), l.loc.x-offset.x,l.loc.y);
-                       glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), l.color.red,l.color.green,l.color.blue);
-               }
+               glUniform1i(glGetUniformLocation(shaderProgram, "numLight"), light.size());
+               glUniform2fv(glGetUniformLocation(shaderProgram, "lightLocation"), light.size(), (GLfloat *)&pointArray); 
+               glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1.0f,1.0f,1.0f);
        }
        
        bgTex->bind(0);
index b16ae1a0f967e8956457aaa81532e312d9e56d64..3b11ebb9e49e593499e2fd6c265011a558550551 100644 (file)
--- a/test.frag
+++ b/test.frag
@@ -1,20 +1,23 @@
 uniform sampler2D sampler;\r
 \r
-uniform vec2 lightLocation;\r
+uniform int numLight;\r
+uniform vec2 lightLocation[10];\r
 uniform vec3 lightColor;\r
 uniform float amb;\r
 // uniform float lightStrength;\r
 //uniform float screenHeight;\r
-void main() {\r
-       float lightAdd = 1.0f;\r
-\r
-       float dist = length(lightLocation - gl_FragCoord.xy);\r
-       float attenuation=1.0/(1.0+0.01*dist+0.00000000001*dist*dist);\r
-\r
-       //vec4 color = vec4(1.0f,1.0f,1.0f,1.0f);\r
-       vec4 color = vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(lightColor, 1.0f);\r
-       //color = color + vec4((vec3(lightColor.r + amb, lightColor.g + amb, lightColor.b + amb)*0.25f),1.0f);\r
+void main(){\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
+               //if(loc.x == 0.0f) continue;\r
+               float dist = length(loc - gl_FragCoord.xy);\r
+               float attenuation=1.0/(1.0+0.01*dist+0.00000000001*dist*dist);\r
 \r
+               //vec4 color = vec4(1.0f,1.0f,1.0f,1.0f);\r
+               color += vec4(attenuation, attenuation, attenuation, 1.0f) * vec4(lightColor, 1.0f);\r
+               //color = color + vec4((vec3(lightColor.r + amb, lightColor.g + amb, lightColor.b + amb)*0.25f),1.0f);\r
+       }\r
        vec2 coords = gl_TexCoord[0].st;\r
        vec4 tex = texture2D(sampler, coords);\r
 \r