Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================================
- // Framework for the ray tracing homework
- // ---------------------------------------------------------------------------------------------
- // Name : Kovacs Daniel Kristof
- // Neptun : FSJNT3
- //=============================================================================================
- #define _USE_MATH_DEFINES
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #if defined(__APPLE__)
- #include <GLUT/GLUT.h>
- #include <OpenGL/gl3.h>
- #include <OpenGL/glu.h>
- #else
- #if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
- #include <windows.h>
- #endif
- #include <GL/glew.h> // must be downloaded
- #include <GL/freeglut.h> // must be downloaded unless you have an Apple
- #endif
- const unsigned int windowWidth = 600, windowHeight = 600;
- // OpenGL major and minor versions
- int majorVersion = 3, minorVersion = 3;
- float PI = 3.14159265359;
- struct vec3 {
- float x, y, z;
- vec3(float x0 = 0, float y0 = 0, float z0 = 0) { x = x0; y = y0; z = z0; }
- vec3 operator*(float a) const { return vec3(x * a, y * a, z * a); }
- vec3 operator/(float a) const { return vec3(x / a, y / a, z / a); }
- vec3 operator+(const vec3& v) const {
- return vec3(x + v.x, y + v.y, z + v.z);
- }
- vec3& operator+= (const vec3 & rhs) {
- x += rhs.x;
- y += rhs.y;
- z += rhs.z;
- return *this;
- }
- vec3 operator-(const vec3& v) const {
- return vec3(x - v.x, y - v.y, z - v.z);
- }
- vec3 operator*(const vec3& v) const {
- return vec3(x * v.x, y * v.y, z * v.z);
- }
- vec3 operator-() const {
- return vec3(-x, -y, -z);
- }
- vec3 normalize() const {
- return (*this) * (1 / (Length() + 0.000001));
- }
- float Length() const { return sqrtf(x * x + y * y + z * z); }
- operator float*() { return &x; }
- };
- float dot(const vec3& v1, const vec3& v2) {
- return (v1.x * v2.x + v1.y * v2.y + v1.z * v2.z);
- }
- vec3 cross(const vec3& v1, const vec3& v2) {
- return vec3(v1.y * v2.z - v1.z * v2.y, v1.z * v2.x - v1.x * v2.z, v1.x * v2.y - v1.y * v2.x);
- }
- // row - major matrix 3x3
- struct mat3 {
- float m[3][3];
- public:
- mat3() {}
- mat3(float m00, float m01, float m02,
- float m10, float m11, float m12,
- float m20, float m21, float m22) {
- m[0][0] = m00; m[0][1] = m01; m[0][2] = m02;
- m[1][0] = m10; m[1][1] = m11; m[1][2] = m12;
- m[2][0] = m20; m[2][1] = m21; m[2][2] = m22;
- }
- mat3 operator*(const mat3& right) {
- mat3 result;
- for (int i = 0; i < 3; i++) {
- for (int j = 0; j < 3; j++) {
- result.m[i][j] = 0;
- for (int k = 0; k < 3; k++) result.m[i][j] += m[i][k] * right.m[k][j];
- }
- }
- return result;
- }
- operator float*() { return &m[0][0]; }
- vec3 dot(vec3 v) {
- vec3 res = v;
- res.x = v.x*m[0][0] + v.y*m[1][0] + v.z*m[2][0];
- res.y = v.x*m[0][1] + v.y*m[1][1] + v.z*m[2][1];
- res.z = v.x*m[0][2] + v.y*m[1][2] + v.z*m[2][2];
- return res;
- }
- float getElement(int x, int y) { return m[x][y]; } // visszaadja a a mátrix megfelelo elemet
- };
- float epsilon = 0.001f;
- struct Light {
- vec3 p;
- vec3 Lout;
- Light() {
- }
- Light(vec3 p, vec3 Lout) {
- this->p = p;
- this->Lout = Lout;
- }
- float getDist(vec3 r) {
- return (r - p).Length();
- }
- vec3 getLightDir(vec3 r) {
- return p - r;
- }
- vec3 getInRad(vec3 r) {
- return Lout * (1 / pow(getDist(r), 2));
- }
- };
- struct Ray {
- vec3 P0;
- vec3 dir;
- Ray(vec3 P0, vec3 dir) {
- this->P0 = P0;
- this->dir = dir;
- }
- };
- struct Camera {
- vec3 eye;
- vec3 lookat;
- vec3 up;
- vec3 right;
- float XM;
- float YM;
- Camera() {
- }
- Camera(vec3 eye, vec3 lookat, vec3 up, vec3 right) {
- this->eye = eye;
- this->lookat = lookat;
- this->up = up;
- this->right = right;
- XM = windowWidth;
- YM = windowHeight;
- }
- Ray GetRay(float x, float y) {
- vec3 p = lookat + right*(2 * x / XM - 1) + up * (2 * y / YM - 1);
- return Ray(p, p - eye);
- }
- };
- struct Material {
- //TODO F0 szamolas
- vec3 F0;
- float n;
- vec3 kd;
- vec3 ks;
- vec3 ka;
- float shininess;
- bool reflective;
- bool refractive;
- bool rough;
- Material() {
- }
- Material(vec3 F0, float n, vec3 kd, vec3 ks, float shin, bool refl, bool refr, bool rou) {
- this->F0 = F0;
- this->n = n;
- this->kd = kd;
- this->ks = ks;
- shininess = shin;
- reflective = refl;
- refractive = refr;
- rough = rou;
- }
- vec3 refract(vec3 inDirection, vec3 normal) {
- float ior = n;
- float cosa = -dot(normal, inDirection);
- if (cosa < 0) { cosa = -cosa; normal = -normal; ior = 1 / n; }
- float disc = 1 - (1 - cosa * cosa) / ior / ior;
- if (disc < 0) return reflect(inDirection, normal);
- return inDirection / ior + normal * (cosa / ior - sqrt(disc));
- }
- vec3 reflect(vec3 inDirection, vec3 normal)
- {
- return inDirection - normal * dot(normal, inDirection) * 2.0f;
- };
- vec3 Fresnel(vec3 inDir, vec3 normal) {
- float cosa = fabs(dot(normal, inDir));
- return F0 + (vec3(1, 1, 1) - F0) * pow(1 - cosa, 5);
- }
- vec3 shade(vec3 normal, vec3 viewDirection, vec3 lightDirection, vec3 inRadiance)
- {
- vec3 reflRad(0, 0, 0);
- float cosTheta = dot(normal, lightDirection);
- if (cosTheta < 0) return reflRad;
- reflRad = inRadiance * kd * cosTheta;;
- vec3 halfway = (viewDirection + lightDirection).normalize();
- float cosDelta = dot(normal, halfway);
- if (cosDelta < 0) return reflRad;
- return reflRad + inRadiance * ks * pow(cosDelta, shininess);
- }
- };
- float F0(float n, float k) {
- return ((n - 1)*(n - 1) + k*k) / ((n + 1)*(n + 1) + k*k);
- }
- struct Hit {
- vec3 position;
- vec3 normal;
- float t;
- Material* material;
- Hit() { t = -1; }
- Hit(float t, vec3 pos, vec3 norm, Material* mat) {
- this->t = t;
- position = pos;
- normal = norm;
- material = mat;
- }
- };
- struct Intersectable
- {
- Material* material;
- virtual Hit intersect(const Ray& ray) = 0;
- Intersectable() {
- }
- Intersectable(Material* m) {
- material = m;
- }
- };
- Camera camera;
- struct Plane : public Intersectable {
- vec3 P0;
- vec3 normal;
- Plane() {
- }
- Plane(vec3 P0, vec3 n, Material* m) :Intersectable(m) {
- this->P0 = P0;
- normal = n;
- }
- Hit intersect(const Ray& ray) {
- Hit hit;
- if (dot(normal, ray.dir) == 0) {
- return hit;
- }
- hit.t = (dot(normal, P0) - dot(normal, ray.P0)) / (dot(normal, ray.dir));
- hit.normal = normal;
- hit.material = material;
- hit.position = ray.P0 + ray.dir*hit.t;
- return hit;
- }
- };
- class Torus
- {
- float R = 0.8f;
- float r = 0.1f;
- public:
- float getGaussianCurve(float u) {
- return (1 / r) * (cosf(u) / (R + r * cosf(u)));
- }
- void setR(float R, float r) {
- this->R = R;
- this->r = r;
- }
- float getX(float u, float v)
- {
- u *= (float)2 * PI;
- v *= (float)2 * PI;
- return (R + r*cosf(u))*cosf(v);
- }
- float getY(float u, float v)
- {
- u *= (float)2 * PI;
- v *= (float)2 * PI;
- return (R + r*cosf(u))*sinf(v);
- }
- float getZ(float u, float v)
- {
- u *= (float)2 * PI;
- return r*sinf(u);
- }
- };
- Torus torus;
- class Cylinder : public Intersectable {
- vec3 r0;
- vec3 vh;
- float R;
- public:
- Cylinder() {
- }
- Cylinder(vec3 r0, vec3 vh, float r, Material* m) :Intersectable(m) {
- this->r0 = r0;
- this->vh = vh;
- R = r;
- }
- Hit intersect(const Ray& ray) {
- Hit hit;
- float a = dot(ray.dir, ray.dir) - powf(dot(ray.dir, vh), 2);
- float b = 2 * (dot(ray.P0 - r0, ray.dir) - dot(vh, ray.dir) * dot(ray.P0 - r0, vh));
- float c = dot(ray.P0 - r0, ray.P0 - r0) - powf(dot(ray.P0 - r0, vh), 2) - powf(R, 2);
- float D = powf(b, 2) - 4 * a*c;
- if (D > 0) {
- float t1 = (-1 * b + sqrtf(D)) / (2 * a);
- float t2 = (-1 * b - sqrtf(D)) / (2 * a);
- if (t1 >= 0 && t2 < 0) {
- hit.t = t1;
- }
- else if (t1<0 && t2 >= 0) {
- hit.t = t2;
- }
- else if (t1>t2) {
- hit.t = t2;
- }
- else {
- hit.t = t1;
- }
- }
- else if (D<0) {
- return hit;
- }
- else {
- hit.t = (-1 * b) / (2 * a);
- }
- hit.position = ray.P0 + ray.dir*hit.t;
- hit.normal = ((ray.P0 + ray.dir*hit.t - r0) * 2 - vh * (dot(ray.P0, vh) + dot(ray.dir, vh) * hit.t - dot(r0, vh)) * 2) / R;
- hit.material = material;
- return hit;
- }
- };
- class Triangle :public Intersectable {
- vec3 r1;
- vec3 r2;
- vec3 r3;
- vec3 normal;
- vec3 p;
- public:
- Triangle() {
- }
- Triangle(vec3 a, vec3 b, vec3 c, Material* m) :Intersectable(m) {
- r1 = a;
- r2 = b;
- r3 = c;
- calculateNormal();
- }
- void calculateNormal() {
- normal = cross((r1 - r3), (r2 - r3));
- }
- Hit intersect(const Ray& ray) {
- Hit hit;
- float t = float(dot(r1 - ray.P0, normal)) / float((dot(ray.dir, normal)));
- if (t > 0) {
- p = ray.P0 + ray.dir*t;
- }
- else {
- return hit;
- }
- if (dot(cross(r2 - r1, p - r1), normal) > 0 &&
- dot(cross(r3 - r2, p - r2), normal) > 0 &&
- dot(cross(r1 - r3, p - r3), normal) > 0) {
- hit.t = t;
- }
- else {
- hit.t = 0;
- }
- hit.position = ray.P0 + ray.dir*hit.t;
- hit.normal = normal;
- hit.material = material;
- return hit;
- }
- };
- vec3 background[windowWidth * windowHeight]; // The image, which stores the ray tracing result
- Light lights[3];
- Material gold;
- Material glass;
- Material silver;
- Material m2;
- Material m3;
- Material m4;
- Triangle t1;
- Triangle t6;
- Triangle t5;
- Cylinder room;
- Plane roomFloor;
- Plane roomCeiling;
- int counter = 0;
- vec3 test = vec3(3, 2, 1);
- struct Scene {
- vec3 La = vec3(0.5f, 0.5f, 0.5f);
- int lightsdb = 0;
- int maxdepth = 10;
- vec3 a = vec3(-0.71, 0, 1);
- vec3 b = vec3(0.5, 0.1, 1);
- vec3 c = vec3(-0.45, 0, 1.2);
- vec3 torus1Coord[7][7];
- vec3 torus2Coord[7][7];
- vec3 torus3Coord[7][7];
- Intersectable* objects[150];
- Triangle torus1[90];
- Triangle torus2[90];
- Triangle torus3[90];
- int objectdb = 0;
- void coorinatesOfTorus( float scale, float moveX, float moveY, float moveZ, float alpha, float beta, float g) {
- int i = 0;
- int j = 0;
- mat3 ZrotateM = mat3(cosf(alpha), -sinf(alpha), 0,
- sinf(alpha), cosf(alpha), 0,
- 0, 0, 1);
- mat3 YrotateM = mat3(cosf(beta), 0, sinf(beta),
- 0, 1, 0,
- -sinf(beta), 0, sinf(beta));
- mat3 XrotateM = mat3(1, 0, 0,
- 0, cosf(g), -sinf(g),
- 0, sinf(g), cosf(g));
- for (float u = 0; u < 2 * PI; u = u + 0.8, i++)
- {
- for (float v = 0; v < 2 * PI; v = v + 0.8, j++)
- {
- vec3 Zrotated = ZrotateM.dot(vec3(torus.getX(u, v)*scale, torus.getY(u, v)*scale, torus.getZ(u, v) + 0.30f));
- vec3 Yrotated = YrotateM.dot(Zrotated);
- vec3 Xrotated = XrotateM.dot(Yrotated);
- vec3 fin = vec3(Xrotated.x + moveX, Xrotated.y + moveY, Xrotated.z + moveZ);
- torus1Coord[i][j] = fin;
- }
- j = 0;
- }
- }
- Scene() {
- }
- void tesselate1( Material * m) {
- coorinatesOfTorus( 0.5f, 0.0f, 0.65f, 0.45f, 1.2*PI, 0.57f, PI / 2.0f);
- coorinatesOfTorus( 0.46f, -0.1f, 0.2f, 0.60f, PI / 2.3f, 3.0f, 0.3f);
- coorinatesOfTorus( 0.7f, 0.2f, 0.5f, 0.60f, PI / 4.0f, PI / 2.5f, PI / 5.2f);
- Triangle t[90];
- int c = 0;
- for (size_t i = 0; i < 7; i++)
- {
- for (size_t j = 0; j < 7; j++)
- {
- torus1[c] = Triangle(torus1Coord[i][j], torus1Coord[i + 1][j], torus1Coord[i + 1][j + 1], m);
- c++;
- torus1[c] = Triangle(torus1Coord[i][j], torus1Coord[i + 1][j + 1], torus1Coord[i][j + 1], m);
- c++;
- }
- }
- }
- void build() {
- camera = Camera(vec3(0.0f, 0.0f, -1.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f), vec3(1.0f, 0.0f, 0.0f));
- lights[0] = Light(vec3(1.7f, 1.0f, 3.0f), vec3(1.0f, 1.0f, 1.0f));
- lights[1] = Light(vec3(-2.0f, 1.0f, 2.0f), vec3(1.0f, 1.0f, 1.0f));
- lights[2] = Light(vec3(0.0f, 0.0f, 1.5f), vec3(1.0f, 1.0f, 1.0f));
- lightsdb = 3;
- silver = Material(vec3(F0(0.14, 4.1), F0(0.16, 2.3), F0(0.13, 3.1)), 1.5, vec3(1, 0, 0), vec3(1, 1, 1), 10, true, false, false);
- gold = Material(vec3(F0(0.17, 3.1), F0(0.35, 2.7), F0(1.5, 1.9)), 0, vec3(1, 0, 0), vec3(1, 1, 1), 10, true, false, false);
- m2 = Material(vec3(0, 0, 0), 1.5f, vec3(0, 0, 1), vec3(1, 1, 1), 15, false, false, true);
- m3 = Material(vec3(0, 0, 0), 0, vec3(0.97, 0.7, 0.9), vec3(1, 1, 1), 15, false, false, true);
- m4 = Material(vec3(0, 0, 0), 0, vec3(1, 1, 0), vec3(1, 1, 1), 10, false, false, true);
- glass = Material(vec3(F0(1.5f, 0), F0(1.5f, 0), F0(1.5f, 0)), 1.5, vec3(1, 0, 0), vec3(1, 1, 1), 10, false, true, false);
- t1 = Triangle(a, b, c, &m2);
- room = Cylinder(vec3(0.0f, 0.0f, 2.5f), vec3(0.0f, 1.0f, 0.0f), 3.5f, &m2);
- roomFloor = Plane(vec3(0, -1.5f, 0), vec3(0, 1, 0), &m3);
- roomCeiling = Plane(vec3(0, 1.5f, 0), vec3(0, -1, 0), &m3);
- t5 = Triangle(torus1Coord[0][0], torus1Coord[1][0], torus1Coord[1][1], &gold);
- t6 = Triangle(torus1Coord[0][0], torus1Coord[0][1], torus1Coord[1][1], &gold);
- tesselate1( &gold);
- //tesselate(torus2, torus2Coord, &glass);
- //tesselate(torus3, torus3Coord, &silver);
- objects[objectdb] = &room;
- objectdb++;
- objects[objectdb] = &roomCeiling;
- objectdb++;
- objects[objectdb] = &roomFloor;
- objectdb++;
- for (size_t i = 0; i < 49; i++)
- {
- objects[objectdb] = &torus1[i];
- objectdb++;
- /*
- objects[objectdb] = &torus2[i];
- objectdb++;
- objects[objectdb] = &torus3[i];
- objectdb++;
- */
- }
- }
- void Render() {
- for (int x = 0; x < windowWidth; x++) {
- for (int y = 0; y < windowHeight; y++) {
- Ray ray = camera.GetRay((float)x, (float)y);
- background[y * windowWidth + x] = trace(ray, 0);
- }
- }
- }
- vec3 trace(Ray ray, int depth) {
- if (depth > maxdepth) return La;
- vec3 outRadiance(0, 0, 0);
- Hit hit = firstIntersect(ray);
- if (hit.t < 0) return La;
- vec3 V = (ray.dir*-1).normalize();
- vec3 N = hit.normal.normalize();
- vec3 r = hit.position;
- if (hit.material->rough) {
- outRadiance = hit.material->kd * La; //csak az ambiens fenyt figyelembe veve
- for (int i = 0; i < lightsdb; i++) { //vegigmegyek az osszes absztrakt fenyforrason
- vec3 Ll = lights[i].getLightDir(hit.position).normalize();
- Ray shadowRay(r + Nesign(N, V), Ll);
- Hit shadowHit = firstIntersect(shadowRay); //Ezzel eldontjuk, hogy latszik-e az adott objektum
- if (shadowHit.t < 0 || shadowHit.t > lights[i].getDist(r)) {
- outRadiance += hit.material->shade(N, V, shadowRay.dir, lights[i].getInRad(r));
- }
- }
- }
- if (hit.material->reflective) {
- vec3 reflectionDir = hit.material->reflect(ray.dir, N);
- Ray reflectedRay(r + Nesign(N, V), reflectionDir);
- outRadiance += trace(reflectedRay, depth + 1)*hit.material->Fresnel(V, N);
- }
- if (hit.material->refractive) {
- vec3 refractionDir = hit.material->refract(ray.dir, N);
- Ray refractedRay(r - Nesign(N, V), refractionDir);
- outRadiance += trace(refractedRay, depth + 1)*(vec3(1, 1, 1) - hit.material->Fresnel(V, N));
- }
- return outRadiance;
- }
- Hit firstIntersect(Ray ray) {
- Hit bestHit;
- for (int i = 0;i< objectdb; i++) {
- Intersectable* obj = objects[i];
- Hit hit = obj->intersect(ray); // hit.t < 0 if no intersection
- if (hit.t > 0 && (bestHit.t < 0 || hit.t < bestHit.t)) bestHit = hit;
- }
- return bestHit;
- }
- vec3 Nesign(vec3 N, vec3 V) {
- if (dot(N, V) < 0) {
- return N*-1.0f*epsilon;
- }
- else {
- return N*epsilon;
- }
- }
- };
- void getErrorInfo(unsigned int handle) {
- int logLen;
- glGetShaderiv(handle, GL_INFO_LOG_LENGTH, &logLen);
- if (logLen > 0) {
- char * log = new char[logLen];
- int written;
- glGetShaderInfoLog(handle, logLen, &written, log);
- printf("Shader log:\n%s", log);
- delete log;
- }
- }
- // check if shader could be compiled
- void checkShader(unsigned int shader, char * message) {
- int OK;
- glGetShaderiv(shader, GL_COMPILE_STATUS, &OK);
- if (!OK) {
- printf("%s!\n", message);
- getErrorInfo(shader);
- }
- }
- // check if shader could be linked
- void checkLinking(unsigned int program) {
- int OK;
- glGetProgramiv(program, GL_LINK_STATUS, &OK);
- if (!OK) {
- printf("Failed to link shader program!\n");
- getErrorInfo(program);
- }
- }
- // vertex shader in GLSL
- const char *vertexSource = R"(
- #version 330
- precision highp float;
- layout(location = 0) in vec2 vertexPosition; // Attrib Array 0
- out vec2 texcoord;
- void main() {
- texcoord = (vertexPosition + vec2(1, 1))/2; // -1,1 to 0,1
- gl_Position = vec4(vertexPosition.x, vertexPosition.y, 0, 1); // transform to clipping space
- }
- )";
- // fragment shader in GLSL
- const char *fragmentSource = R"(
- #version 330
- precision highp float;
- uniform sampler2D textureUnit;
- in vec2 texcoord; // interpolated texture coordinates
- out vec4 fragmentColor; // output that goes to the raster memory as told by glBindFragDataLocation
- void main() {
- fragmentColor = texture(textureUnit, texcoord);
- }
- )";
- // handle of the shader program
- unsigned int shaderProgram;
- class FullScreenTexturedQuad {
- unsigned int vao, textureId; // vertex array object id and texture id
- public:
- void Create(vec3 image[windowWidth * windowHeight]) {
- glGenVertexArrays(1, &vao); // create 1 vertex array object
- glBindVertexArray(vao); // make it active
- unsigned int vbo; // vertex buffer objects
- glGenBuffers(1, &vbo); // Generate 1 vertex buffer objects
- // vertex coordinates: vbo[0] -> Attrib Array 0 -> vertexPosition of the vertex shader
- glBindBuffer(GL_ARRAY_BUFFER, vbo); // make it active, it is an array
- static float vertexCoords[] = { -1, -1, 1, -1, -1, 1,
- 1, -1, 1, 1, -1, 1 }; // two triangles forming a quad
- glBufferData(GL_ARRAY_BUFFER, sizeof(vertexCoords), vertexCoords, GL_STATIC_DRAW); // copy to that part of the memory which is not modified
- // Map Attribute Array 0 to the current bound vertex buffer (vbo[0])
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, NULL); // stride and offset: it is tightly packed
- // Create objects by setting up their vertex data on the GPU
- glGenTextures(1, &textureId); // id generation
- glBindTexture(GL_TEXTURE_2D, textureId); // binding
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, windowWidth, windowHeight, 0, GL_RGB, GL_FLOAT, image); // To GPU
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // sampling
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- }
- void Draw() {
- glBindVertexArray(vao); // make the vao and its vbos active playing the role of the data source
- int location = glGetUniformLocation(shaderProgram, "textureUnit");
- if (location >= 0) {
- glUniform1i(location, 0); // texture sampling unit is TEXTURE0
- glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, textureId); // connect the texture to the sampler
- }
- glDrawArrays(GL_TRIANGLES, 0, 6); // draw two triangles forming a quad
- }
- };
- // The virtual world: single quad
- FullScreenTexturedQuad fullScreenTexturedQuad;
- Scene scene;
- // Initialization, create an OpenGL context
- void onInitialization() {
- glViewport(0, 0, windowWidth, windowHeight);
- // Ray tracing fills the image called background
- scene.build();
- scene.Render();
- fullScreenTexturedQuad.Create(background);
- // Create vertex shader from string
- unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
- if (!vertexShader) {
- printf("Error in vertex shader creation\n");
- exit(1);
- }
- glShaderSource(vertexShader, 1, &vertexSource, NULL);
- glCompileShader(vertexShader);
- checkShader(vertexShader, "Vertex shader error");
- // Create fragment shader from string
- unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
- if (!fragmentShader) {
- printf("Error in fragment shader creation\n");
- exit(1);
- }
- glShaderSource(fragmentShader, 1, &fragmentSource, NULL);
- glCompileShader(fragmentShader);
- checkShader(fragmentShader, "Fragment shader error");
- // Attach shaders to a single program
- shaderProgram = glCreateProgram();
- if (!shaderProgram) {
- printf("Error in shader program creation\n");
- exit(1);
- }
- glAttachShader(shaderProgram, vertexShader);
- glAttachShader(shaderProgram, fragmentShader);
- // Connect the fragmentColor to the frame buffer memory
- glBindFragDataLocation(shaderProgram, 0, "fragmentColor"); // fragmentColor goes to the frame buffer memory
- // program packaging
- glLinkProgram(shaderProgram);
- checkLinking(shaderProgram);
- // make this program run
- glUseProgram(shaderProgram);
- }
- void onExit() {
- glDeleteProgram(shaderProgram);
- printf("exit");
- }
- // Window has become invalid: Redraw
- void onDisplay() {
- fullScreenTexturedQuad.Draw();
- glutSwapBuffers(); // exchange the two buffers
- }
- // Key of ASCII code pressed
- void onKeyboard(unsigned char key, int pX, int pY) {
- if (key == 'd') glutPostRedisplay(); // if d, invalidate display, i.e. redraw
- }
- // Key of ASCII code released
- void onKeyboardUp(unsigned char key, int pX, int pY) {
- }
- // Mouse click event
- void onMouse(int button, int state, int pX, int pY) {
- if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { // GLUT_LEFT_BUTTON / GLUT_RIGHT_BUTTON and GLUT_DOWN / GLUT_UP
- }
- }
- // Move mouse with key pressed
- void onMouseMotion(int pX, int pY) {
- }
- // Idle event indicating that some time elapsed: do animation here
- void onIdle() {
- long time = glutGet(GLUT_ELAPSED_TIME); // elapsed time since the start of the program
- }
- int main(int argc, char * argv[]) {
- glutInit(&argc, argv);
- #if !defined(__APPLE__)
- glutInitContextVersion(majorVersion, minorVersion);
- #endif
- glutInitWindowSize(windowWidth, windowHeight); // Application window is initially of resolution 600x600
- glutInitWindowPosition(100, 100); // Relative location of the application window
- #if defined(__APPLE__)
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH | GLUT_3_2_CORE_PROFILE); // 8 bit R,G,B,A + double buffer + depth buffer
- #else
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
- #endif
- glutCreateWindow(argv[0]);
- #if !defined(__APPLE__)
- glewExperimental = true; // magic
- glewInit();
- #endif
- printf("GL Vendor : %s\n", glGetString(GL_VENDOR));
- printf("GL Renderer : %s\n", glGetString(GL_RENDERER));
- printf("GL Version (string) : %s\n", glGetString(GL_VERSION));
- glGetIntegerv(GL_MAJOR_VERSION, &majorVersion);
- glGetIntegerv(GL_MINOR_VERSION, &minorVersion);
- printf("GL Version (integer) : %d.%d\n", majorVersion, minorVersion);
- printf("GLSL Version : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
- onInitialization();
- glutDisplayFunc(onDisplay); // Register event handlers
- glutMouseFunc(onMouse);
- glutIdleFunc(onIdle);
- glutKeyboardFunc(onKeyboard);
- glutKeyboardUpFunc(onKeyboardUp);
- glutMotionFunc(onMouseMotion);
- glutMainLoop();
- onExit();
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement