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>
- GLsizei winWidth = 800, winHeight = 600;
- bool vetites = true;
- float angle = 0, radius = 4, height = 0;
- 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 }, origo = { 0, 0, 0 };
- 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(face f) {
- 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];
- torusFaces[torusCount].normal = setNormalVector(torusFaces[torusCount]);
- 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();
- }
- 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);
- }
- }
- }
- void draw() {
- for (int i = 0; i < 6; i++) {
- if((cubeFaces[i].normal.z > 0 && !vetites) || (vetites && dot(cubeFaces[i].normal, vec3(0,0,s) - cubeFaces[i].cog ) > 0))
- drawFace(cubeFaces[i]);
- }
- for (int i = 0; i < torusCount; i++) {
- if ((torusFaces[i].normal.z > 0 && !vetites) || (vetites && dot(torusFaces[i].normal, vec3(0, 0, s) - torusFaces[i].cog) > 0))
- drawFace(torusFaces[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