Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <bevgrafmath2017.h>
- #include <math.h>
- #include <stdio.h>
- #include <algorithm>
- GLsizei winWidth = 800, winHeight = 600;
- bool vetites = true;
- float angle = 0, radius = 4, height = 2;
- float R = 1.5, r = 0.5, u, v, lambdaU = pi() / 8, lambdaV = pi() / 8;
- float s = 5, alfa = 0;
- int torusCount = 0;
- GLint keyStates[256];
- vec3 cube[8] = {
- { 0.5, -0.5, -0.5 },
- { 0.5 , -0.5, 0.5 },
- { -0.5 , -0.5, 0.5 },
- { -0.5 , -0.5 , -0.5 },
- { 0.5 ,0.5 ,-0.5 },
- { 0.5 , 0.5 , 0.5 },
- { -0.5 ,0.5 ,0.5 },
- { -0.5 ,0.5 ,-0.5 }
- };
- vec3 torus[4];
- vec3 camera;
- vec3 CO, Cz, Cx, Cy;
- vec3 up = { 0, 0, 1 };
- vec3 origo = { 0, 0, 0 };
- vec3 light = { 0,5,10 };
- vec3 normLight = normalize(light);
- mat4 w2v, scaled, projection, K , torusRZ;
- struct face {
- vec3 p[4];
- vec3 normal;
- vec3 cog;
- };
- vec3 setNormalVector(face f) {
- return cross(f.p[1] - f.p[0], f.p[3] - f.p[0]);
- }
- vec3 setCoG(face f) {
- return (f.p[0] + f.p[1] + f.p[2] + f.p[3])/4;
- }
- void drawFace(const face &f) {
- vec3 a = normalize(f.normal);
- vec4 b = (transpose(inverse(K))* vec4(normLight.x,normLight.y,normLight.z,0));
- float l = (dot(a,vec3(b.x, b.y, b.z))+1)/2;
- glColor3f(l, l, l);
- glBegin(GL_TRIANGLE_FAN);
- for (int i = 0; i < 4; i++)
- {
- glVertex2f(f.p[i].x, f.p[i].y);
- }
- glEnd();
- glColor3f(0, 0, 0);
- glBegin(GL_LINE_LOOP);
- for (int i = 0; i < 4; i++)
- {
- glVertex2f(f.p[i].x, f.p[i].y);
- }
- glEnd();
- }
- face cubeFaces[6] = {};
- face torusFaces[500] = {};
- face allFaces[500] = {};
- void keyPressed(unsigned char key, int x, int y) {
- keyStates[key] = 1;
- }
- void keyUp(unsigned char key, int x, int y) {
- keyStates[key] = 0;
- }
- void keyOperations() {
- if (keyStates['w'] && height < 10) { height += 0.01; }
- if (keyStates['s'] && height > -10) { height -= 0.01; }
- if (keyStates['a']) { angle -= 0.01; }
- if (keyStates['d']) { angle += 0.01; }
- if (keyStates['q'] && radius > 3) { radius -= 0.01; }
- if (keyStates['e'] && radius < 5) { radius += 0.01; }
- if (keyStates['+']) { R -= 0.001; }
- if (keyStates['-']) { R += 0.001; }
- if (keyStates['*']) { r -= 0.001; }
- if (keyStates['/']) { r += 0.001; }
- if (keyStates['z']) { s += 0.01; }
- if (keyStates['c']) { s -= 0.01; }
- if (keyStates[',']) { vetites = true; }
- if (keyStates['.']) { vetites = false; }
- glutPostRedisplay();
- }
- void update(int value) {
- alfa += 0.001;
- torusRZ = rotateZ(alfa);
- glutPostRedisplay();
- glutTimerFunc(10, update, 0);
- }
- void setCam() {
- camera = { 0,0,0 };
- camera.x = radius*cos(angle);
- camera.y = radius*sin(angle);
- camera.z = height;
- CO = origo - camera;
- Cz = normalize(-CO);
- Cx = normalize(cross(up, Cz));
- Cy = normalize(cross(Cz, Cx));
- K = coordinateTransform(camera, Cx, Cy, Cz);
- if (vetites) {
- projection = perspective(s);
- }
- else
- {
- projection = ortho();
- }
- }
- void setCube() {
- cubeFaces[0].p[0] = cube[5];
- cubeFaces[0].p[1] = cube[6];
- cubeFaces[0].p[2] = cube[2];
- cubeFaces[0].p[3] = cube[1];
- cubeFaces[1].p[0] = cube[6];
- cubeFaces[1].p[1] = cube[7];
- cubeFaces[1].p[2] = cube[3];
- cubeFaces[1].p[3] = cube[2];
- cubeFaces[2].p[0] = cube[7];
- cubeFaces[2].p[1] = cube[4];
- cubeFaces[2].p[2] = cube[0];
- cubeFaces[2].p[3] = cube[3];
- cubeFaces[3].p[0] = cube[4];
- cubeFaces[3].p[1] = cube[5];
- cubeFaces[3].p[2] = cube[1];
- cubeFaces[3].p[3] = cube[0];
- cubeFaces[4].p[0] = cube[3];
- cubeFaces[4].p[1] = cube[0];
- cubeFaces[4].p[2] = cube[1];
- cubeFaces[4].p[3] = cube[2];
- cubeFaces[5].p[0] = cube[7];
- cubeFaces[5].p[1] = cube[6];
- cubeFaces[5].p[2] = cube[5];
- cubeFaces[5].p[3] = cube[4];
- }
- void setTorus() {
- torusCount = 0;
- for (u = 0; u < 2.0 * pi(); u += lambdaU) {
- for (v = 0; v < 2.0 * pi(); v += lambdaV) {
- torus[0].x = (R + r * cos(u)) * cos(v);
- torus[0].y = (R + r * cos(u)) * sin(v);
- torus[0].z = r * sin(u);
- torus[1].x = (R + r * cos(u)) * cos(v + lambdaV);
- torus[1].y = (R + r * cos(u)) * sin(v + lambdaV);
- torus[1].z = r * sin(u);
- torus[2].x = (R + r * cos(u + lambdaU)) * cos(v + lambdaV);
- torus[2].y = (R + r * cos(u + lambdaU)) * sin(v + lambdaV);
- torus[2].z = r * sin(u + lambdaU);
- torus[3].x = (R + r * cos(u + lambdaU)) * cos(v);
- torus[3].y = (R + r * cos(u + lambdaU)) * sin(v);
- torus[3].z = r * sin(u + lambdaU);
- torusFaces[torusCount].p[0] = torus[0];
- torusFaces[torusCount].p[1] = torus[1];
- torusFaces[torusCount].p[2] = torus[2];
- torusFaces[torusCount].p[3] = torus[3];
- torusCount++;
- }
- }
- }
- void initMatrices() {
- vec2 windowSize = { 1,1 };
- vec2 windowPosition = { -1, -1 };
- vec2 viewportSize = { 150, 150 };
- vec2 viewportPosition = { 250, 150 };
- w2v = windowToViewport3(windowPosition, windowSize, viewportPosition, viewportSize);
- }
- void init() {
- glClearColor(1.0, 1.0, 1.0, 0.0);
- glMatrixMode(GL_PROJECTION);
- gluOrtho2D(0.0, winWidth, 0.0, winHeight);
- glShadeModel(GL_FLAT);
- glEnable(GL_POINT_SMOOTH);
- glPointSize(5.0);
- glLineWidth(1.0);
- initMatrices();
- }
- bool perspSort(const face &a,const face &b) {
- return length(vec3(0, 0, s) - a.cog) > length(vec3(0, 0, s) - b.cog);
- }
- bool othoSort(const face &a,const face &b) {
- return a.cog.z < b.cog.z;
- }
- void paint() {
- if (vetites)
- std::sort(allFaces, allFaces + (torusCount + 6), perspSort);
- else
- std::sort(allFaces, allFaces + (torusCount + 6), othoSort);
- }
- void calculate() {
- keyOperations();
- setCam();
- setCube();
- setTorus();
- mat4 M = w2v *projection;
- for (int idx = 0; idx < 6; idx++) {
- glColor3f(0, 0, 0);
- for (int i = 0; i < 4; i++) {
- vec4 pointH = ihToH(cubeFaces[idx].p[i]);
- cubeFaces[idx].p[i] = hToIh(K * pointH);
- }
- cubeFaces[idx].normal = setNormalVector(cubeFaces[idx]);
- cubeFaces[idx].cog = setCoG(cubeFaces[idx]);
- for (int i = 0; i < 4; i++) {
- vec4 pointH = ihToH(cubeFaces[idx].p[i]);
- cubeFaces[idx].p[i] = hToIh(M * pointH);
- }
- }
- for (int idx = 0; idx < torusCount; idx++) {
- for (int i = 0; i < 4; i++) {
- vec4 pointH = ihToH(torusFaces[idx].p[i]);
- torusFaces[idx].p[i] = hToIh(K*torusRZ * pointH);
- }
- torusFaces[idx].normal = setNormalVector(torusFaces[idx]);
- torusFaces[idx].cog = setCoG(torusFaces[idx]);
- for (int i = 0; i < 4; i++) {
- vec4 pointH = ihToH(torusFaces[idx].p[i]);
- torusFaces[idx].p[i] = hToIh(M * pointH);
- }
- }
- for (int i = 0; i < 6; i++) {
- allFaces[i] = cubeFaces[i];
- }
- for (int i = 0; i < torusCount; i++) {
- allFaces[i + 6] = torusFaces[i];
- }
- paint();
- }
- void draw() {
- for (int i = 0; i < torusCount+6; i++) {
- if ((allFaces[i].normal.z > 0 && !vetites) || (vetites && dot(allFaces[i].normal, vec3(0, 0, s) - allFaces[i].cog) > 0))
- drawFace(allFaces[i]);
- }
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0, 0, 0);
- calculate();
- draw();
- glutSwapBuffers();
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(winWidth, winHeight);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("cube");
- glutKeyboardFunc(keyPressed);
- glutKeyboardUpFunc(keyUp);
- init();
- glutDisplayFunc(display);
- glutTimerFunc(10, update, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement