]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Made ray casting example
authordrumsetmonkey <abelleisle@roadrunner.com>
Wed, 4 Nov 2015 13:42:23 +0000 (08:42 -0500)
committerdrumsetmonkey <abelleisle@roadrunner.com>
Wed, 4 Nov 2015 13:42:23 +0000 (08:42 -0500)
include/common.h
main.cpp
shader.frag

index 41cc7b91e3376f1955c4d4e02d5e91b30543a67b..bc713af2c5300f0bec994cc72fb288a1a3cb97f1 100644 (file)
@@ -39,7 +39,13 @@ typedef unsigned int uint;
  *     This flag lets the compiler know that we are using shaders
 */
 
-#define SHADERSf
+#define SHADERSd
+
+/*
+ * This is pi...
+*/
+
+#define PI 3.141592653598723846433
 
 /*
  *     Create a basic 2-point structure for coordinate saving
index a49873c76beacd7108cbb71088485fa1e57432ac..41e12276efdb11ff9dddb54e281457d49f85d6e8 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -768,81 +768,100 @@ void render(){
                glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 640,100);
                glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1,1,1);
                glUniform1f(glGetUniformLocation(shaderProgram, "lightStrength"), 100 + (1000-(shade*10)));
-               std::cout << 100 + (1000-(shade*10)) << std::endl;
+               //std::cout << 100 + (1000-(shade*10)) << std::endl;
                glColor4ub(0,0,0,200);
                glRectf(offset.x-SCREEN_WIDTH/2,0,offset.x+SCREEN_WIDTH/2,SCREEN_HEIGHT);
                glUseProgramObjectARB(0);
        #endif //SHADERS
 
-       #define pi 3.1415926535
-       #define raysNOPE
+
+
+
+
+
+       #define rays
 
        #ifdef rays
        //LIGHT
-       static vec2 light;
-       float lightStr = 100;
+       vec2 light;
+       int lightStr = 150;
        vec2 curCoord;
-       light.x = player->loc.x;
-       light.y = player->loc.y;
-       std::vector<Ray>ray(30);
+
+       light.x = 0;
+       light.y = 300;
+
+       std::vector<Ray>ray(180);
        unsigned int a = 0;
-       int angle = 0;
+       float angle = 0;
+
        glColor3f(0.0f, 0.0f, 0.0f);
        for(auto &r : ray){
                r.start = light;
-               angle = 12*a;
-               for(int l = 0;l<lightStr;l+=36){
-                       //switch
-                       switch(angle){
-                       case 0:
+               curCoord = r.start;
+               angle = 2*a;
+               //for length
+               for(int l = 0;l<=lightStr;l++){
+                       //std::cout << a << ": " << curCoord.x << "," << curCoord.y << "\n";
+                       if(angle == 0){
                                curCoord.x += HLINE;
                                curCoord.y += 0;
-                               break;
-                       case 180:
+                       }
+                       if(angle == 90){
+                               curCoord.y += HLINE;
+                               curCoord.x += 0;
+                       }
+                       if(angle == 180){
                                curCoord.x -= HLINE;
-                               curCoord.y -= 0;
-                               break;
-                       case 360:
+                               curCoord.y += 0;
+                       }
+                       if(angle == 270){
+                               curCoord.y -= HLINE;
+                               curCoord.x += 0;
+                       }
+                       if(angle == 360){
                                curCoord.x += HLINE;
                                curCoord.y += 0;
-                               angle = 0;
-                               break;
-                       default:
-                               if(angle > 0 && angle < 90){
-                                       curCoord.x = r.start.x + float(HLINE / cos(angle*pi/180));
-                                       curCoord.y = r.start.y + float(HLINE / sin(angle*pi/180));
-                               }
-                               if(angle > 90 && angle < 180){
-                                       curCoord.x = r.start.x - float(HLINE / cos((90-angle)*pi/180));
-                                       curCoord.y = r.start.y + float(HLINE / sin((90-angle)*pi/180));
-                               }
-                               if(angle > 180 && angle < 270){
-                                       curCoord.x = r.start.x - float(HLINE / cos((270-angle)*pi/180));
-                                       curCoord.y = r.start.y - float(HLINE / sin((270-angle)*pi/180));
-                               }
-                               if(angle > 270 && angle < 360){
-                                       curCoord.x = r.start.x + float(HLINE / cos((360-angle)*pi/180));
-                                       curCoord.y = r.start.y - float(HLINE / sin((360-angle)*pi/180));
-                               }
-                       }//end swtich
-                       /*for(auto &en : entity){
+                       }
+                       else{
+                               curCoord.x += float((HLINE) * cos(angle*PI/180));
+                               curCoord.y += float((HLINE) * sin(angle*PI/180));
+                       }
+                       for(auto &en : entity){
                                if(curCoord.x > en->loc.x && curCoord.x < en->loc.x + en->width){
                                        if(curCoord.y > en->loc.y && curCoord .y < en->loc.y + en->height){
                                                r.end = curCoord;
+                                               l=lightStr;
                                        }
                                }
-                       }*/
-
+                       }
+                       if(curCoord.x > player->loc.x && curCoord.x < player->loc.x + player->width){
+                                       if(curCoord.y > player->loc.y && curCoord .y < player->loc.y + player->height){
+                                               r.end = curCoord;
+                                               l=lightStr;
+                                       }
+                       }if(l==lightStr)r.end = curCoord;
                }//end length
-               r.end = curCoord;
-               glBegin(GL_LINES);
+               /*glBegin(GL_LINES);
                        glVertex2f(r.start.x,r.start.y);
                        glVertex2f(r.end.x, r.end.y);
-               glEnd();
+               glEnd();*/
                //std::cout << angle << "\n";
                a++;
        }
-       #endif //rays
+       glUseProgramObjectARB(shaderProgram);
+       glUniform2f(glGetUniformLocation(shaderProgram, "lightLocation"), 640,300);
+       glUniform3f(glGetUniformLocation(shaderProgram, "lightColor"), 1,1,1);
+       glUniform1f(glGetUniformLocation(shaderProgram, "lightStrength"), 5);
+       glColor4f(1.0f, 1.0f, 1.0f, .5f);
+       for(auto r = 0; r < ray.size(); r++){
+               glBegin(GL_TRIANGLES);
+                       glVertex2f(ray[r].start.x, ray[r].start.y);
+                       glVertex2f(ray[r].end.x, ray[r].end.y);
+                       r==ray.size()-1 ? glVertex2f(ray[0].end.x, ray[0].end.y) : glVertex2f(ray[r+1].end.x, ray[r+1].end.y);
+               glEnd();
+       }
+       glUseProgramObjectARB(0);
+       #endif //ray
 
 
        //LIGHT
@@ -1092,7 +1111,6 @@ void logic(){
         *      Calculate an in-game shading value (as opposed to GLSL shading).
        */
 
-       #define PI 3.1415926535
        worldShade=50*sin((tickCount+(DAY_CYCLE/2))/(DAY_CYCLE/PI));
 
        /*
index 857c6b9b213577b03c751becf0a41037d8d3f639..55527a1653cde77354c7e45ef8020a13d05cf4e3 100644 (file)
@@ -5,16 +5,10 @@ uniform vec3 lightColor;
 uniform float lightStrength;\r
 uniform float screenHeight;\r
 \r
-uniform vec2 rayStart;\r
-uniform vec2 rayEnd;\r
-\r
-uniform sampler2D tex;\r
-uniform vec2 resolution;\r
-\r
 void main(){\r
        float distance = length(lightLocation - gl_FragCoord.xy);\r
        float attenuation = lightStrength / distance;\r
-       vec4 color = vec4(0, 0, 0, 0.8f - pow(attenuation, 3)) * vec4(lightColor, 1);\r
+       vec4 color = vec4(attenuation, attenuation, attenuation, (pow(attenuation, 3)) * vec4(lightColor, 2))+.5;\r
        \r
        gl_FragColor = color;\r
 }\r