SHARE
TWEET

Untitled

a guest Apr 23rd, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================================
  2. // Computer Graphics Sample Program: GPU ray casting
  3. //=============================================================================================
  4. #include "framework.h"
  5.  
  6. // vertex shader in GLSL
  7. const char *vertexSource = R"(
  8.     #version 450
  9.     precision highp float;
  10.  
  11.     uniform vec3 wLookAt, wRight, wUp;          // pos of eye
  12.  
  13.     layout(location = 0) in vec2 cCamWindowVertex;  // Attrib Array 0
  14.     out vec3 p;
  15.  
  16.     void main() {
  17.         gl_Position = vec4(cCamWindowVertex, 0, 1);
  18.         p = wLookAt + wRight * cCamWindowVertex.x + wUp * cCamWindowVertex.y;
  19.     }
  20. )";
  21. // fragment shader in GLSL
  22. const char *fragmentSource = R"(
  23.     #version 450
  24.     precision highp float;
  25.  
  26.     struct MyRectangle {
  27.         vec3 position;
  28.         vec3 normal;
  29.         float width;
  30.         float height;
  31.     };
  32.     struct Material {
  33.         vec3 ka, kd, ks;
  34.         float  shininess;
  35.         vec3 F0;
  36.         int rough, reflective;
  37.     };
  38.  
  39.     struct Light {
  40.         vec3 direction;
  41.         vec3 Le, La;
  42.     };
  43.  
  44.     //struct Sphere {
  45.     //  vec3 center;
  46.     //  float radius;
  47.     //};
  48.  
  49.     struct Hit {
  50.         float t;
  51.         vec3 position, normal;
  52.         int mat;    // material index
  53.     };
  54.  
  55.     struct Ray {
  56.         vec3 start, dir;
  57.     };
  58.  
  59.     const int nMaxObjects = 10;
  60.     uniform MyRectangle rects[10];
  61.  
  62.     uniform vec3 wEye;
  63.     uniform Light light;    
  64.     uniform Material materials[2];  // diffuse, specular, ambient ref
  65.     uniform int nObjects;
  66.  
  67.     in  vec3 p;                 // point on camera window corresponding to the pixel
  68.     out vec4 fragmentColor;     // output that goes to the raster memory as told by glBindFragDataLocation
  69.  
  70.     /*Hit intersect(const Sphere object, const Ray ray) {
  71.         Hit hit;
  72.         hit.t = -1;
  73.         vec3 dist = ray.start - object.center;
  74.  
  75.         float a = dot(ray.dir, ray.dir);
  76.         float b = dot(dist, ray.dir) * 2.0;
  77.         float c = dot(dist, dist) - object.radius * object.radius;
  78.         float discr = b * b - 4.0 * a * c;
  79.  
  80.         if (discr < 0)
  81.             return hit;
  82.  
  83.         float sqrt_discr = sqrt(discr);
  84.         float t1 = (-b + sqrt_discr) / 2.0 / a; // t1 >= t2 for sure
  85.         float t2 = (-b - sqrt_discr) / 2.0 / a;
  86.  
  87.         if (t1 <= 0)
  88.             return hit;
  89.  
  90.         hit.t = (t2 > 0) ? t2 : t1;
  91.         hit.position = ray.start + ray.dir * hit.t;
  92.         hit.normal = (hit.position - object.center) / object.radius;
  93.         return hit;
  94.     }*/
  95.     Hit intersect(const MyRectangle object, const Ray ray)
  96.     {
  97.         Hit hit;
  98.         hit.t = -1;
  99.  
  100.       /*  float d = object.normal.x * object.position.x
  101.                 + object.normal.y * object.position.y
  102.                 + object.normal.z * object.position.z;
  103.  
  104.         float t = ray.dir.x * object.normal.x
  105.                 + ray.dir.y * object.normal.y
  106.                 + ray.dir.z * object.normal.z;
  107.        
  108.         float tnum = d - (ray.start.x * object.normal.x
  109.                         + ray.start.y * object.normal.y
  110.                         + ray.start.z * object.normal.z);
  111.  
  112.         float tres = tnum / t;
  113.  
  114.         hit.position = ray.start.x + ray.dir * tres;*/
  115.  
  116.         return hit;
  117.     }
  118.  
  119.     Hit firstIntersect(Ray ray) {
  120.         Hit bestHit;
  121.         bestHit.t = -1;
  122.         for (int o = 0; o < nObjects; o++) {
  123.             Hit hit = intersect(rects[o], ray); //  hit.t < 0 if no intersection
  124.             if (o < nObjects/2) hit.mat = 0;     // half of the rects are rough
  125.             else                hit.mat = 1;     // half of the rects are reflective
  126.             if (hit.t > 0 && (bestHit.t < 0 || hit.t < bestHit.t))  bestHit = hit;
  127.         }
  128.         if (dot(ray.dir, bestHit.normal) > 0) bestHit.normal = bestHit.normal * (-1);
  129.         return bestHit;
  130.     }
  131.  
  132.     bool shadowIntersect(Ray ray) { // for directional lights
  133.         for (int o = 0; o < nObjects; o++) if (intersect(rects[o], ray).t > 0) return true; //  hit.t < 0 if no intersection
  134.         return false;
  135.     }
  136.  
  137.     vec3 Fresnel(vec3 F0, float cosTheta) {
  138.         return F0 + (vec3(1, 1, 1) - F0) * pow(cosTheta, 5);
  139.     }
  140.  
  141.     const float epsilon = 0.0001f;
  142.     const int maxdepth = 5;
  143.  
  144.     vec3 trace(Ray ray) {
  145.         vec3 weight = vec3(1, 1, 1);
  146.         vec3 outRadiance = vec3(0, 0, 0);
  147.         for(int d = 0; d < maxdepth; d++) {
  148.             Hit hit = firstIntersect(ray);
  149.             if (hit.t < 0) return weight * light.La;
  150.             if (materials[hit.mat].rough == 1) {
  151.                 outRadiance += weight * materials[hit.mat].ka * light.La;
  152.                 Ray shadowRay;
  153.                 shadowRay.start = hit.position + hit.normal * epsilon;
  154.                 shadowRay.dir = light.direction;
  155.                 float cosTheta = dot(hit.normal, light.direction);
  156.                 if (cosTheta > 0 && !shadowIntersect(shadowRay)) {
  157.                     outRadiance += weight * light.Le * materials[hit.mat].kd * cosTheta;
  158.                     vec3 halfway = normalize(-ray.dir + light.direction);
  159.                     float cosDelta = dot(hit.normal, halfway);
  160.                     if (cosDelta > 0) outRadiance += weight * light.Le * materials[hit.mat].ks * pow(cosDelta, materials[hit.mat].shininess);
  161.                 }
  162.             }
  163.  
  164.             if (materials[hit.mat].reflective == 1) {
  165.                 weight *= Fresnel(materials[hit.mat].F0, dot(-ray.dir, hit.normal));
  166.                 ray.start = hit.position + hit.normal * epsilon;
  167.                 ray.dir = reflect(ray.dir, hit.normal);
  168.             } else return outRadiance;
  169.         }
  170.     }
  171.  
  172.     void main() {
  173.         Ray ray;
  174.         ray.start = wEye;
  175.         ray.dir = normalize(p - wEye);
  176.         fragmentColor = vec4(trace(ray), 1);
  177.     }
  178. )";
  179.  
  180. float cameraX;
  181. class Material {
  182. protected:
  183.     vec3 ka, kd, ks;
  184.     float  shininess;
  185.     vec3 F0;
  186.     bool rough, reflective;
  187. public:
  188.     Material RoughMaterial(vec3 _kd, vec3 _ks, float _shininess) {
  189.         ka = _kd * M_PI;
  190.         kd = _kd;
  191.         ks = _ks;
  192.         shininess = _shininess;
  193.         rough = true;
  194.         reflective = false;
  195.     }
  196.     Material SmoothMaterial(vec3 _F0) {
  197.         F0 = _F0;
  198.         rough = false;
  199.         reflective = true;
  200.     }
  201.     void SetUniform(unsigned int shaderProg, int mat) {
  202.         char buffer[256];
  203.         sprintf(buffer, "materials[%d].ka", mat);
  204.         ka.SetUniform(shaderProg, buffer);
  205.         sprintf(buffer, "materials[%d].kd", mat);
  206.         kd.SetUniform(shaderProg, buffer);
  207.         sprintf(buffer, "materials[%d].ks", mat);
  208.         ks.SetUniform(shaderProg, buffer);
  209.         sprintf(buffer, "materials[%d].shininess", mat);
  210.         int location = glGetUniformLocation(shaderProg, buffer);
  211.  
  212.         if (location >= 0)
  213.             glUniform1f(location, shininess);
  214.         else
  215.             printf("uniform material.shininess cannot be set\n");
  216.         sprintf(buffer, "materials[%d].F0", mat);
  217.         F0.SetUniform(shaderProg, buffer);
  218.  
  219.         sprintf(buffer, "materials[%d].rough", mat);
  220.         location = glGetUniformLocation(shaderProg, buffer);
  221.         if (location >= 0)
  222.             glUniform1i(location, rough ? 1 : 0);
  223.         else
  224.             printf("uniform material.rough cannot be set\n");
  225.  
  226.         sprintf(buffer, "materials[%d].reflective", mat);
  227.         location = glGetUniformLocation(shaderProg, buffer);
  228.         if (location >= 0)
  229.             glUniform1i(location, reflective ? 1 : 0);
  230.         else
  231.             printf("uniform material.reflective cannot be set\n");
  232.     }
  233. };
  234.  
  235. class RoughMaterial : public Material {
  236. public:
  237.     RoughMaterial(vec3 _kd, vec3 _ks, float _shininess) {
  238.         ka = _kd * M_PI;
  239.         kd = _kd;
  240.         ks = _ks;
  241.         shininess = _shininess;
  242.         rough = true;
  243.         reflective = false;
  244.     }
  245. };
  246.  
  247. class SmoothMaterial : public Material {
  248. public:
  249.     SmoothMaterial(vec3 _F0) {
  250.         F0 = _F0;
  251.         rough = false;
  252.         reflective = true;
  253.     }
  254. };
  255.  
  256. struct Sphere {
  257.     vec3 center;
  258.     float radius;
  259.  
  260.     Sphere(const vec3& _center, float _radius)
  261.     {
  262.         center = _center;
  263.         radius = _radius;
  264.     }
  265.     void SetUniform(unsigned int shaderProg, int o) {
  266.         char buffer[256];
  267.         sprintf(buffer, "rects[%d].center", o);
  268.         center.SetUniform(shaderProg, buffer);
  269.  
  270.         sprintf(buffer, "rects[%d].radius", o);
  271.         int location = glGetUniformLocation(shaderProg, buffer);
  272.         if (location >= 0)
  273.             glUniform1f(location, radius);
  274.         else
  275.             printf("uniform %s cannot be set\n", buffer);
  276.     }
  277. };
  278. struct MyRectangle
  279. {
  280.     vec3 position, normal;
  281.     float height, width;
  282.     MyRectangle(const vec3& _position, float _height, float _width)
  283.     {
  284.         position = _position;
  285.         height = _height;
  286.         width = _width;
  287.         vec3 p2 = position;
  288.         p2.x += width;
  289.  
  290.         vec3 p3 = position;
  291.         p3.y += height;
  292.  
  293.         vec3 V1 = p2 - position;
  294.         vec3 V2 = p3 - position;
  295.  
  296.         normal = cross(V1, V2);
  297.  
  298.         normal = normal * (1.0f / sqrtf((normal.x* normal.x) + (normal.y* normal.y) + (normal.z* normal.z)));
  299.         printf("POSITION: %f %f %f \nNORMAL: %f %f %f\nHEIGHT: %f\nWIDTH: %f\n",
  300.             position.x, position.y, position.z, normal.x, normal.y, normal.z, height, width);
  301.     }
  302.     void SetUniform(unsigned int shaderProg, int o) {
  303.         char buffer[256];
  304.         /*  sprintf(buffer, "rects[%d].position", o);
  305.             position.SetUniform(shaderProg, buffer);
  306.  
  307.  
  308.             sprintf(buffer, "rects[%d].normal", o);
  309.             normal.SetUniform(shaderProg, buffer);
  310.  
  311.             sprintf(buffer, "rects[%d].height", o);
  312.             int location = glGetUniformLocation(shaderProg, buffer);
  313.  
  314.             if (location >= 0)
  315.                 glUniform1f(location, height);
  316.             else
  317.                 printf("uniform height cannot be set\n");
  318.     */
  319.         sprintf(buffer, "rects[%d].width", o);
  320.         int location = glGetUniformLocation(shaderProg, buffer);
  321.         if (location >= 0)
  322.             glUniform1f(location, width);
  323.         else
  324.             printf("uniform %s cannot be set\n", buffer);
  325.  
  326.     }
  327.     void print()
  328.     {
  329.  
  330.         printf("----------------------------------------------------\nPOSITION: %f %f %f \nNORMAL: %f %f %f\nHEIGHT: %f\nWIDTH: %f\n-------------------------------------------------",
  331.             position.x, position.y, position.z, normal.x, normal.y, normal.z, height, width);
  332.     }
  333. };
  334.  
  335. class Camera {
  336.     vec3 eye, lookat, right, up;
  337.     float fov;
  338. public:
  339.     void set(vec3 _eye, vec3 _lookat, vec3 vup, double _fov) {
  340.         eye = _eye;
  341.         lookat = _lookat;
  342.         fov = _fov;
  343.         vec3 w = eye - lookat;
  344.         float f = length(w);
  345.         right = normalize(cross(vup, w)) * f * tan(fov / 2);
  346.         up = normalize(cross(w, right)) * f * tan(fov / 2);
  347.     }
  348.     void Animate(float dt) {
  349.         eye = vec3((eye.x - lookat.x) * cos(dt) + (eye.z - lookat.z) * sin(dt) + lookat.x,
  350.             eye.y,
  351.             -(eye.x - lookat.x) * sin(dt) + (eye.z - lookat.z) * cos(dt) + lookat.z);
  352.         set(eye, lookat, up, fov);
  353.     }
  354.     void SetUniform(unsigned int shaderProg) {
  355.         eye.SetUniform(shaderProg, "wEye");
  356.         lookat.SetUniform(shaderProg, "wLookAt");
  357.         right.SetUniform(shaderProg, "wRight");
  358.         up.SetUniform(shaderProg, "wUp");
  359.     }
  360. };
  361.  
  362. struct Light {
  363.     vec3 direction;
  364.     vec3 Le, La;
  365.     Light(vec3 _direction, vec3 _Le, vec3 _La) {
  366.         direction = normalize(_direction);
  367.         Le = _Le; La = _La;
  368.     }
  369.     void SetUniform(unsigned int shaderProg) {
  370.         La.SetUniform(shaderProg, "light.La");
  371.         Le.SetUniform(shaderProg, "light.Le");
  372.         direction.SetUniform(shaderProg, "light.direction");
  373.     }
  374. };
  375.  
  376. float rnd() { return (float)rand() / RAND_MAX; }
  377.  
  378. class Scene {
  379.     std::vector<MyRectangle*> rects;
  380.     std::vector<Light *> lights;
  381.     Camera camera;
  382.     std::vector<Material *> materials;
  383. public:
  384.     void build() {
  385.         vec3 eye = vec3(0, 0, 2);
  386.         vec3 vup = vec3(0, 1, 0);
  387.         vec3 lookat = vec3(0, 0, 0);
  388.         float fov = 45 * M_PI / 180;
  389.         camera.set(eye, lookat, vup, fov);
  390.  
  391.         lights.push_back(new Light(vec3(1, 1, 1), vec3(3, 3, 3), vec3(0.4, 0.3, 0.3)));
  392.  
  393.         vec3 kd(0.3f, 0.2f, 0.1f), ks(10, 10, 10);
  394.         for (int i = 0; i < 10; i++)
  395.             rects.push_back(new MyRectangle(vec3(rnd() - 0.5, rnd() - 0.5, rnd() - 0.5), 2.0f, 0.5f));
  396.         for (size_t i = 0; i < 10; i++)
  397.         {
  398.             rects[i]->print();
  399.         }
  400.         materials.push_back(new RoughMaterial(kd, ks, 50));
  401.         materials.push_back(new SmoothMaterial(vec3(0.9, 0.85, 0.8)));
  402.     }
  403.     void SetUniform(unsigned int shaderProg) {
  404.         int location = glGetUniformLocation(shaderProg, "nObjects");
  405.         if (location >= 0)
  406.             glUniform1i(location, rects.size());
  407.         else
  408.             printf("uniform nObjects cannot be set\n");
  409.  
  410.         for (int o = 0; o < rects.size(); o++)
  411.             rects[o]->SetUniform(shaderProg, o);
  412.  
  413.         lights[0]->SetUniform(shaderProg);
  414.         camera.SetUniform(shaderProg);
  415.         for (int mat = 0; mat < materials.size(); mat++)
  416.             materials[mat]->SetUniform(shaderProg, mat);
  417.     }
  418.     void Animate(float dt) { camera.Animate(dt); }
  419. };
  420.  
  421. GPUProgram gpuProgram; // vertex and fragment shaders
  422. Scene scene;
  423.  
  424. class FullScreenTexturedQuad {
  425.     unsigned int vao;   // vertex array object id and texture id
  426. public:
  427.     void Create() {
  428.         glGenVertexArrays(1, &vao); // create 1 vertex array object
  429.         glBindVertexArray(vao);     // make it active
  430.  
  431.         unsigned int vbo;       // vertex buffer rects
  432.         glGenBuffers(1, &vbo);  // Generate 1 vertex buffer rects
  433.  
  434.         // vertex coordinates: vbo0 -> Attrib Array 0 -> vertexPosition of the vertex shader
  435.         glBindBuffer(GL_ARRAY_BUFFER, vbo); // make it active, it is an array
  436.         float vertexCoords[] = { -1, -1,  1, -1,  1, 1,  -1, 1 };   // two triangles forming a quad
  437.         glBufferData(GL_ARRAY_BUFFER, sizeof(vertexCoords), vertexCoords, GL_STATIC_DRAW);     // copy to that part of the memory which is not modified
  438.         glEnableVertexAttribArray(0);
  439.         glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL);     // stride and offset: it is tightly packed
  440.     }
  441.  
  442.     void Draw() {
  443.         glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
  444.         glDrawArrays(GL_TRIANGLE_FAN, 0, 4);    // draw two triangles forming a quad
  445.     }
  446. };
  447. class Line
  448. {
  449. public:
  450.     int size = 4;
  451.     float vertices[4];
  452.     float color[3];
  453.  
  454.     float xShift = 0.0f;
  455.     float yShift = 0.0f;
  456.     float rShift = 0.0f;
  457.  
  458.     unsigned int vao;
  459.     unsigned int vbo;
  460.  
  461.     float x;
  462.     float y;
  463.     float x2;
  464.     float y2;
  465.     Line() {}
  466.     void SetShift(float X, float Y)
  467.     {
  468.         xShift = X;
  469.         yShift = Y;
  470.     }
  471.     void SetColor(float r, float g, float b)
  472.     {
  473.         color[0] = r;
  474.         color[1] = g;
  475.         color[2] = b;
  476.     }
  477.     void LoadVertices(float X1, float Y1, float X2, float Y2)
  478.     {
  479.         x = X1;
  480.         y = Y1;
  481.         x2 = X2;
  482.         y2 = Y2;
  483.  
  484.         vertices[0] = x;
  485.         vertices[1] = y;
  486.         vertices[2] = x2;
  487.         vertices[3] = y2;
  488.     }
  489.     void Create()
  490.     {
  491.         glGenVertexArrays(1, &vao); // create 1 vertex array object
  492.         glBindVertexArray(vao);     // make it active
  493.  
  494.         glGenBuffers(1, &vbo);  // Generate 1 buffer
  495.         glBindBuffer(GL_ARRAY_BUFFER, vbo);
  496.         // Geometry with 24 bytes (6 floats or 3 x 2 coordinates)
  497.  
  498.         glBufferData(GL_ARRAY_BUFFER,   // Copy to GPU target
  499.             sizeof(vertices),  // # bytes
  500.             &vertices,          // address
  501.             GL_STATIC_DRAW);    // we do not change later
  502.  
  503.         glEnableVertexAttribArray(0);  // AttribArray 0
  504.         glVertexAttribPointer(0,       // vbo -> AttribArray 0
  505.             2, GL_FLOAT, GL_FALSE, // two floats/attrib, not fixed-point
  506.             0, NULL);            // stride, offset: tightly packed
  507.     }
  508.     void Draw()
  509.     {
  510.         int location;
  511.         // Set color to (0, 1, 0) = green
  512.         location = glGetUniformLocation(gpuProgram.getId(), "color");
  513.         glUniform3f(location, color[0], color[1], color[2]); // 3 floats
  514.  
  515.         float MVPtransf[4][4] = { (float)cos(rShift), -(float)sin(rShift), 0, 0,    // MVP matrix,
  516.                                   (float)sin(rShift), (float)cos(rShift), 0, 0,    // row-major!
  517.                                   0, 0, 1, 0,
  518.                                   xShift + cameraX, yShift, 0, 1 };
  519.  
  520.         location = glGetUniformLocation(gpuProgram.getId(), "MVP"); // Get the GPU location of uniform variable MVP
  521.         glUniformMatrix4fv(location, 1, GL_TRUE, &MVPtransf[0][0]); // Load a 4x4 row-major float matrix to the specified location
  522.         glBindVertexArray(vao);  // Draw call
  523.         glDrawArrays(GL_LINE_STRIP, 0 /*startIdx*/, size / 2 /*# Elements*/);
  524.     }
  525.     void Animate(float t)
  526.     {
  527.         rShift = t;
  528.     }
  529. };
  530. class DynamicPolygon
  531. {
  532.     unsigned int vao;
  533.     unsigned int vbo[2];
  534.  
  535.     float sidelength = 0.50f;
  536.  
  537.     float xShift = 0;
  538.     float yShift = 0;
  539.  
  540.     int size = 3;
  541.  
  542.     float rotation = 0.0f;
  543.     float color[3] = { 1,1,1 };
  544.     float *vertices;
  545.  
  546. public:
  547.     DynamicPolygon() {}
  548.     void LoadVertices(float x, float y, float R)
  549.     {
  550.         delete[]vertices;
  551.         vertices = new float[size * 2];
  552.         for (int i = 0; i < size * 2; i += 2)
  553.         {
  554.             float angle = float(i) / size * float(M_PI);
  555.             float x_ = x + (cos(angle) * R);
  556.             float y_ = y + (sin(angle) * R);
  557.             vertices[i] = x_;
  558.             vertices[i + 1] = y_;
  559.         }
  560.     }
  561.     void Create()
  562.     {
  563.         LoadVertices(0, 0, sidelength);
  564.  
  565.         glGenVertexArrays(1, &vao); // create 1 vertex array object
  566.         glBindVertexArray(vao);     // make it active
  567.  
  568.         unsigned int vbo[2];        // vertex buffer rects
  569.         glGenBuffers(2, &vbo[0]);   // Generate 2 vertex buffer rects
  570.  
  571.         // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
  572.         glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); // make it active, it is an array
  573.         glBufferData(GL_ARRAY_BUFFER,      // copy to the GPU
  574.             sizeof(float) * 2 * size, // number of the vbo in bytes
  575.             vertices,          // address of the data array on the CPU
  576.             GL_DYNAMIC_DRAW);      // copy to that part of the memory which is not modified
  577. // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
  578.         glEnableVertexAttribArray(0);
  579.         // Data organization of Attribute Array 0
  580.         glVertexAttribPointer(0,            // Attribute Array 0
  581.             2, GL_FLOAT,  // components/attribute, component type
  582.             GL_FALSE,       // not in fixed point format, do not normalized
  583.             0, NULL);     // stride and offset: it is tightly packed
  584.  
  585. // vertex colors: vbo[1] -> Attrib Array 1 -> vertexColor of the vertex shader
  586.         glBindBuffer(GL_ARRAY_BUFFER, vbo[1]); // make it active, it is an array
  587.         float vertexColors[] = { 1, 0, 0,  0, 1, 0,  0, 0, 1 }; // vertex data on the CPU
  588.         glBufferData(GL_ARRAY_BUFFER, sizeof(vertexColors), vertexColors, GL_DYNAMIC_DRAW); // copy to the GPU
  589.         // Map Attribute Array 1 to the current bound vertex buffer (vbo[1])
  590.         glEnableVertexAttribArray(1);  // Vertex position
  591.         // Data organization of Attribute Array 1
  592.         glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL); // Attribute Array 1, components/attribute, component type, normalize?, tightly packed
  593.  
  594.     }
  595.     void Draw() {
  596.         int location;
  597.         // Set color to (0, 1, 0) = green
  598.  
  599.         mat4 MVPtransf = { (float)cos(rotation), -(float)sin(rotation), 0, 0,    // MVP matrix,
  600.                           (float)sin(rotation), (float)cos(rotation), 0, 0,    // row-major!
  601.                           0, 0, 1, 0,
  602.                           xShift + cameraX, yShift, 0, 1 };
  603.  
  604.         location = glGetUniformLocation(gpuProgram.getId(), "MVP"); // Get the GPU location of uniform variable MVP
  605.         glUniformMatrix4fv(location, 1, GL_TRUE, &MVPtransf.m[0][0]);   // Load a 4x4 row-major float matrix to the specified location
  606.         glBindVertexArray(vao);  // Draw call
  607.         glDrawArrays(GL_TRIANGLE_FAN, 0 /*startIdx*/, size /*# Elements*/);
  608.     }
  609.     void IncreaseSides()
  610.     {
  611.         size++;
  612.         Create();
  613.     }
  614.     void Animate(float t)
  615.     {
  616.         rotation = t;
  617.     }
  618. };
  619.  
  620. DynamicPolygon p;
  621. FullScreenTexturedQuad fullScreenTexturedQuad;
  622.  
  623. // Initialization, create an OpenGL context
  624. void onInitialization() {
  625.     glViewport(0, 0, windowWidth, windowHeight);
  626.     scene.build();
  627.     fullScreenTexturedQuad.Create();
  628.  
  629.     // create program for the GPU
  630.     gpuProgram.Create(vertexSource, fragmentSource, "fragmentColor");
  631.     gpuProgram.Use();
  632. }
  633.  
  634. // Window has become invalid: Redraw
  635. void onDisplay() {
  636.     static int nFrames = 0;
  637.     nFrames++;
  638.     static long tStart = glutGet(GLUT_ELAPSED_TIME);
  639.     long tEnd = glutGet(GLUT_ELAPSED_TIME);
  640.     printf("%d msec\r", (tEnd - tStart) / nFrames);
  641.  
  642.     glClearColor(1.0f, 0.5f, 0.8f, 1.0f);                           // background color
  643.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the screen
  644.     scene.SetUniform(gpuProgram.getId());
  645.     fullScreenTexturedQuad.Draw();
  646.     glutSwapBuffers();                                  // exchange the two buffers
  647. }
  648.  
  649. // Key of ASCII code pressed
  650. void onKeyboard(unsigned char key, int pX, int pY) {
  651. }
  652.  
  653. // Key of ASCII code released
  654. void onKeyboardUp(unsigned char key, int pX, int pY) {
  655.  
  656. }
  657.  
  658. // Mouse click event
  659. void onMouse(int button, int state, int pX, int pY) {
  660. }
  661.  
  662. // Move mouse with key pressed
  663. void onMouseMotion(int pX, int pY) {
  664. }
  665.  
  666. // Idle event indicating that some time elapsed: do animation here
  667. void onIdle() {
  668.     scene.Animate(0.01f);
  669.     glutPostRedisplay();
  670. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top