Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <bevgrafmath2017.h>
- #include <math.h>
- float alpha = -1.4, radius = 4, m = 1; // kamera ertekei
- float R = 1.5, r = 0.5, u, v, lambdaU = pi() / 8, lambdaV = pi() / 8, rotateTorusValue; // torusz ertekei
- float projectionDist = 4; // centralis vetites tavolsag
- int torusCount = 0;
- GLsizei winWidth = 800, winHeight = 600;
- GLint keyStates[256];
- bool projectionValue = true;
- 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, Cx, Cy, Cz;
- vec3 up = { 0, 0, 1 };
- vec3 origo = { 0, 0, 0 };
- mat4 w2v, scaled, projection, K, rotateTorus;
- struct face {
- vec3 p[4]; // 4 pontu lap
- vec3 normal; // normalvektor a laphozű
- vec3 sulypont;
- };
- face cubeFaces[6] = {}; // 6 lapu a kocka
- face torusFaces[500] = {};
- face uniteFaces[500] = {};
- vec3 calculateNormalVectors(face param) {
- return cross(param.p[1] - param.p[0], param.p[3] - param.p[0]);
- }
- vec3 calculateSulypont(face param) {
- return (param.p[0] + param.p[1] + param.p[2] + param.p[3]) / 4;
- }
- void setProjection() {
- if (projectionValue) {
- projection = ortho();
- }
- else {
- projection = perspective(projectionDist);
- }
- }
- void initializeCamera() {
- camera = { 0, 0, 0 };
- camera.x = radius*cos(alpha);
- camera.y = radius*sin(alpha);
- camera.z = m;
- CO = origo - camera; // kamera pontjából az eredeti origóba mutató vektor
- Cz = normalize(-CO); // CO vektornak a negaltjat normalizalva => z tengely
- Cx = normalize(cross(up, Cz)); // up vektor es a z tengely vektorialis szorzata => x tengelyet
- Cy = normalize(cross(Cz, Cx)); // z es x tengely vektorialis szorzata => y tengely
- K = coordinateTransform(camera, Cx, Cy, Cz);
- }
- void initPoints() {
- // kocka pontjainak beallitasa
- 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];
- for (int i = 0; i < 6; i++) {
- cubeFaces[i].normal = calculateNormalVectors(cubeFaces[i]);
- }
- // torusz pontjainak kiszamitasa
- 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 = calculateNormalVectors(torusFaces[torusCount]);
- torusCount++;
- }
- printf("Toruscount: %d ", torusCount);
- }
- }
- void initMatrices() {
- vec2 windowSize = { 1, 1 };
- vec2 windowPosition = { -1, -1 };
- vec2 viewportSize = { 150, 150 };
- vec2 viewportPosition = { winWidth / 2 - viewportSize.x, winHeight / 2 - viewportSize.y};
- w2v = windowToViewport3(windowPosition, windowSize, viewportPosition, viewportSize);
- }
- 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['r']) { if (m < 5) m += 0.1; }
- if (keyStates['f']) { if (m > -5) m -= 0.1; }
- if (keyStates['w']) { if (radius < 6) radius += 0.05; }
- if (keyStates['s']) { if (radius > 2) radius -= 0.05; }
- if (keyStates['d']) { alpha += 0.05; }
- if (keyStates['g']) { alpha -= 0.05; }
- if (keyStates['u']) { if (R < 2.1) R += 0.01; }
- if (keyStates['j']) { if (R > 1.4) R -= 0.01; }
- if (keyStates['i']) { if (r < 0.7) r += 0.01; }
- if (keyStates['k']) { if (r > 0.2) r -= 0.01; }
- if (keyStates['[']) { projectionValue = true; }
- if (keyStates[']']) { projectionValue = false; }
- if (keyStates['e']) { if (projectionDist < 6) projectionDist += 0.1; }
- if (keyStates['t']) { if (projectionDist > 2) projectionDist -= 0.1; }
- glutPostRedisplay();
- }
- void calculateFaces() {
- keyOperations();
- initializeCamera();
- initPoints();
- setProjection();
- glColor3f(0, 0, 0);
- mat4 M = w2v * projection;
- for (int i = 0; i < 6; i++) {
- for (int j = 0; j < 4; j++) {
- vec4 pointH = ihToH(cubeFaces[i].p[j]);
- cubeFaces[i].p[j] = hToIh(K * pointH);
- }
- cubeFaces[i].normal = calculateNormalVectors(cubeFaces[i]);
- cubeFaces[i].sulypont = calculateSulypont(cubeFaces[i]);
- for (int j = 0; j < 4; j++) {
- vec4 pointH = ihToH(cubeFaces[i].p[j]);
- cubeFaces[i].p[j] = hToIh(M * pointH);
- }
- }
- /*
- for (int i = 0; i < torusCount; i++) {
- for (int j = 0; j < 4; j++) {
- vec4 pointH = ihToH(torusFaces[i].p[i]);
- vec4 transformedPoint = M * pointH;
- vec4 vectorH = ihToH(torusFaces[i].normal);
- vec4 transformedVector = transpose(M) * vectorH;
- torusFaces[i].normal = hToIh(transformedVector);
- }
- }
- */
- for (int i = 0; i < torusCount; i++) {
- for (int j = 0; j < 4; j++) {
- vec4 pointH = ihToH(torusFaces[i].p[j]);
- torusFaces[i].p[j] = hToIh(K * rotateTorus * pointH);
- }
- torusFaces[i].normal = calculateNormalVectors(torusFaces[i]);
- torusFaces[i].sulypont = calculateSulypont(torusFaces[i]);
- for (int j = 0; j < 4; j++) {
- vec4 pointH = ihToH(torusFaces[i].p[j]);
- torusFaces[i].p[j] = hToIh(M * pointH);
- }
- }
- for (int i = 0; i < 6; i++) {
- uniteFaces[i] = cubeFaces[i];
- }
- for (int i = 0; i < torusCount; i++) {
- uniteFaces[i+6] = torusFaces[i];
- }
- }
- void drawFaces() {
- for (int i = 0; i < torusCount + 6; i++) {
- glBegin(GL_LINE_LOOP);
- for (int j = 0; j < 4; j++) {
- glVertex2f(uniteFaces[i].p[j].x, uniteFaces[i].p[j].y);
- }
- glEnd();
- }
- }
- 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.5);
- initPoints();
- initMatrices();
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0, 0, 0);
- drawFaces();
- glutSwapBuffers();
- }
- void update(int value) {
- rotateTorusValue += 0.01;
- rotateTorus = rotateZ(rotateTorusValue);
- glutPostRedisplay();
- glutTimerFunc(10, update, 0);
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(winWidth, winHeight);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Masodik vedesi feladat");
- glutKeyboardFunc(keyPressed);
- glutKeyboardUpFunc(keyUp);
- init();
- glutDisplayFunc(display);
- glutTimerFunc(10, update, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement