diff options
-rw-r--r-- | assets/colorIndex.png | bin | 0 -> 149 bytes | |||
-rw-r--r-- | frig.frag | 39 | ||||
-rw-r--r-- | include/Texture.h | 2 | ||||
-rw-r--r-- | include/common.h | 14 | ||||
-rw-r--r-- | include/entities.h | 21 | ||||
-rw-r--r-- | main.cpp | 9 | ||||
-rw-r--r-- | src/Texture.cpp | 42 | ||||
-rw-r--r-- | src/entities.cpp | 6 |
8 files changed, 126 insertions, 7 deletions
diff --git a/assets/colorIndex.png b/assets/colorIndex.png Binary files differnew file mode 100644 index 0000000..620b6ec --- /dev/null +++ b/assets/colorIndex.png diff --git a/frig.frag b/frig.frag new file mode 100644 index 0000000..07b4a8a --- /dev/null +++ b/frig.frag @@ -0,0 +1,39 @@ +#version 120
+uniform sampler2D sampler;
+
+uniform int numLight;
+uniform vec2 lightLocation[64];
+uniform vec3 lightColor;
+uniform float amb;
+
+float b = .0005;
+float minLight = .05;
+float radius = sqrt(1.0 / (b * minLight));
+
+//float radius = b*minlight;
+
+void main(){
+ vec4 color = vec4(0.0,0.0,0.0,0.0);
+ for(int i = 0; i < numLight; i++){
+ vec2 loc = lightLocation[i];
+ float dist = length(loc - gl_FragCoord.xy);
+ //float attenuation=1.0/(1.0+0.01*dist+0.00000000001*dist*dist);
+ float attenuation = clamp(1.0 - dist*dist/(radius*radius), 0.0, 1.0); attenuation *= attenuation;
+
+ color += vec4(attenuation, attenuation, attenuation, 1.0) * vec4(lightColor, 1.0);
+ }
+ vec2 coords = gl_TexCoord[0].st;
+ vec4 tex = texture2D(sampler, coords);
+
+ color += vec4(amb,amb,amb,1.0+amb);
+ gl_FragColor = tex * vec4(color)*tex.a;
+}
+
+/* b values
+ .002 10
+ .008 50
+ .0005 200
+ .00008 500
+ .00002 1000
+ .00005 2000
+*/
\ No newline at end of file diff --git a/include/Texture.h b/include/Texture.h index 85225d8..7160d37 100644 --- a/include/Texture.h +++ b/include/Texture.h @@ -28,6 +28,8 @@ namespace Texture{ */ GLuint loadTexture(const char *fileName); + void initColorIndex(); + vec2 getIndex(Color c); } /** diff --git a/include/common.h b/include/common.h index ae832f1..f5952eb 100644 --- a/include/common.h +++ b/include/common.h @@ -30,14 +30,22 @@ typedef unsigned int uint; #undef near #endif -#include <Texture.h> - /** * This flag lets the compiler know that we want to use shaders. */ #define SHADERS +template<typename N> +N abso(N v){ + if(v < 0.0){ + return v * -1; + }else + return v; +} + +extern GLuint colorIndex; + /** * This structure contains a set of coordinates for ease of coding. */ @@ -68,6 +76,8 @@ typedef struct{ float blue; } Color; +#include <Texture.h> + /** * Define the game's name (displayed in the window title). */ diff --git a/include/entities.h b/include/entities.h index d434546..51032ef 100644 --- a/include/entities.h +++ b/include/entities.h @@ -57,6 +57,8 @@ public: float velx; float vely; Color color; + vec2 index; + //GLuint tex; float duration; bool canMove; bool fountain; @@ -76,13 +78,26 @@ public: fountain = false; gravity = true; behind = false; + index = Texture::getIndex(c); + //tex = text; } ~Particles(){ } void draw(){ - glColor3f(color.red,color.green,color.blue); - glRectf(loc.x,loc.y,loc.x+width,loc.y+height); + //glColor3f(color.red,color.green,color.blue); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, colorIndex); + glEnable(GL_TEXTURE_2D); + glColor3ub(255,255,255); + glBegin(GL_QUADS); + glTexCoord2f(.25*index.x, .125*(index.y + 1)); glVertex2i(loc.x, loc.y); + glTexCoord2f(.25*(index.x+1), .125*(index.y + 1)); glVertex2i(loc.x + width, loc.y); + glTexCoord2f(.25*(index.x+1), .125*index.y); glVertex2i(loc.x + width, loc.y + height); + glTexCoord2f(.25*index.x, .125*index.y); glVertex2i(loc.x, loc.y + width); + glEnd(); + glDisable(GL_TEXTURE_2D); + //glRectf(loc.x,loc.y,loc.x+width,loc.y+height); } bool kill(float delta){ duration -= delta; @@ -93,6 +108,8 @@ public: } }; +void initEntity(); + class Entity{ public: Inventory *inv; @@ -119,6 +119,7 @@ unsigned int deltaTime = 0; GLuint fragShader; GLuint shaderProgram; +GLuint colorIndex; Mix_Chunk *crickets; @@ -349,6 +350,8 @@ int main(/*int argc, char *argv[]*/){ SDL_ShowCursor(SDL_DISABLE); + Texture::initColorIndex(); + initEntity(); /* * Initializes our shaders so that the game has shadows. */ @@ -357,7 +360,7 @@ int main(/*int argc, char *argv[]*/){ fragShader = glCreateShader(GL_FRAGMENT_SHADER); - std::string shaderFileContents = readFile("test.frag"); + std::string shaderFileContents = readFile("frig.frag"); const GLchar *shaderSource = shaderFileContents.c_str(); GLint bufferln = GL_FALSE; @@ -956,13 +959,13 @@ void logic(){ switch(b->bsubtype){ case FOUNTAIN: for(int r = 0; r < (rand()%25)+10;r++){ - currentWorld->addParticle(rand()%HLINE*3 + b->loc.x + b->width/2,b->loc.y + b->height, HLINE,HLINE, rand()%2 == 0?-(rand()%7)*.01:(rand()%7)*.01,((4+rand()%6)*.05), {0,0,1.0f}, 2500); + currentWorld->addParticle(rand()%HLINE*3 + b->loc.x + b->width/2,b->loc.y + b->height, HLINE,HLINE, rand()%2 == 0?-(rand()%7)*.01:(rand()%7)*.01,((4+rand()%6)*.05), {0,0,255}, 2500); currentWorld->particles.back()->fountain = true; } break; case FIRE_PIT: for(int r = 0; r < (rand()%20)+10;r++){ - currentWorld->addParticle(rand()%(int)(b->width/2) + b->loc.x+b->width/4, b->loc.y+3*HLINE, HLINE, HLINE, rand()%2 == 0?-(rand()%3)*.01:(rand()%3)*.01,((4+rand()%6)*.005), {1.0f,0.0f,0.0f}, 400); + currentWorld->addParticle(rand()%(int)(b->width/2) + b->loc.x+b->width/4, b->loc.y+3*HLINE, HLINE, HLINE, rand()%2 == 0?-(rand()%3)*.01:(rand()%3)*.01,((4+rand()%6)*.005), {255,0,0}, 400); currentWorld->particles.back()->gravity = false; currentWorld->particles.back()->behind = true; } diff --git a/src/Texture.cpp b/src/Texture.cpp index 5e367a9..2965959 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -10,6 +10,7 @@ struct texture_t *LoadedTexture[256]; unsigned int LoadedTextureCounter = 0; namespace Texture{ + Color pixels[8][4]; GLuint loadTexture(const char *fileName){ SDL_Surface *image; GLuint object = 0; @@ -63,6 +64,47 @@ namespace Texture{ return object; } + + void initColorIndex(){ + colorIndex = loadTexture("assets/colorIndex.png"); + glActiveTexture(GL_TEXTURE0); + glBindTexture(GL_TEXTURE_2D, colorIndex); + GLubyte* buffer = new GLubyte[8*4*3]; + glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer); + uint i = 0; + for(uint o = 0; o < 8; o++){ + for(uint t = 0; t < 4; t++){ + for (int r = 0; r < 3; r++){ + pixels[o][t].red = buffer[i++]; + pixels[o][t].green = buffer[i++]; + pixels[o][t].blue = buffer[i++]; + std::cout << pixels[o][t].red << "," << pixels[o][t].green << "," << pixels[o][t].blue << std::endl; + } + //std::cout << std::endl; + } + } + + } + + //sqrt((255-145)^2+(90-145)^2+(0-0)^2); + vec2 getIndex(Color c){ + uint buf[2]; + float buff = 999; + float shit = 999; + for(uint o = 0; o < 8; o++){ + for(uint t = 0; t < 4; t++){ + buff = sqrt(pow((c.red-pixels[o][t].red),2)+pow((c.green-pixels[o][t].green),2)+pow((c.blue-pixels[o][t].blue),2)); + //std::cout << buff << std::endl; + if(buff < shit){ + shit = buff; + buf[0] = o; + buf[1] = t; + } + } + } + std::cout << float(buf[1]) << ", " << float(buf[0]) << std::endl; + return {float(buf[1]),float(buf[0])}; + } } Texturec::Texturec(uint amt, ...){ diff --git a/src/entities.cpp b/src/entities.cpp index ec335c7..76b9aa4 100644 --- a/src/entities.cpp +++ b/src/entities.cpp @@ -22,6 +22,12 @@ std::string sTexLoc[] = { "assets/townhall.png", "assets/lampPost1.png", "assets/brazzier.png"}; +GLuint waterTex; + +void initEntity(){ + waterTex = Texture::loadTexture("assets/waterTex.png"); +} + void getRandomName(Entity *e){ unsigned int tempNum,max=0; char *bufs; |