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 <vector>
- #include <algorithm>
- #include <iostream>
- using namespace std;
- //Fényforrás
- vec3 feny = { 1,0,0 };
- float lepes = pi()/8, r=5, centrum=8;
- struct FACE{
- vec3 t[4];
- float distance=0;
- vec3 normal, suly;
- bool triangle = false;
- };
- vector<FACE> faces;
- bool sorban1(FACE a, FACE b)
- {
- return a.distance > b.distance;
- }
- GLsizei winWidth = 800, winHeight = 800;
- vec3 points[16] = { {-3,-1,-3},{-3,0,-1},{-3,0,1},{-3,-1,3},
- {-1,0,-3},{-1,0,-1},{-1,0,1},{-1,0,3},
- {1,0,-3},{1,0,-1},{1,0,1},{1,0,3},
- {3,-1,-3},{3,0,-1},{3,0,1},{3,-1,3},
- };
- mat4 w2v, projection;
- float alpha = 0;
- float beta = 0;
- float s;
- vec3 eye;
- vec3 target = {0.0,0.0,0.0};
- vec3 up = {0.0,1.0,0.0};
- void initMatrices()
- {
- projection = perspective(centrum);
- vec2 windowSize = {4,4};
- vec2 windowPosition = {-2,2};
- vec2 viewportSize = {800, 800};
- vec2 viewportPosition = {0 , 800};
- 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);
- glLineWidth(3.0);
- glPointSize(10);
- initMatrices();
- }
- float b(int id, float t)
- {
- switch(id)
- {
- case 0:{return -1*pow(t,3)+3*t*t-3*t+1;}break;
- case 1:{return 3*pow(t,3)-6*t*t+3*t;}break;
- case 2:{return -3*pow(t,3)+3*t*t;}break;
- case 3:{return pow(t,3);}break;
- }
- }
- void vaza(){
- glClear(GL_COLOR_BUFFER_BIT);
- eye = { float(r * cos(beta)),alpha,float(r * sin(beta)) };
- glClear ( GL_COLOR_BUFFER_BIT );
- vec3 z = normalize(eye - target);
- vec3 x = normalize(cross(up, z));
- vec3 y = normalize(cross(z, x));
- mat4 kamera = coordinateTransform(eye,x,y,z);
- float u, t;
- vec4 trans;
- vec4 homogen;
- FACE alap;
- std::vector<vec3> controllpoly;
- vec3 temp=vec3{0,0,0};
- for (s = 0; s<1; s += 0.1)
- {
- for (t = 0; t<1; t += 0.1)
- {
- for(int i=0;i<4;i++)
- {
- for(int j=0;j<4;j++)
- {
- temp+=points[i*4+j]*b(i,s)*b(j,t);
- }
- homogen = ihToH(temp);
- trans =kamera * homogen;
- controllpoly.push_back(hToIh(trans));
- }
- }
- /*alap.t[3] = { cos(u)*bx(t),by(t) ,-sin(u)*bx(t) };
- alap.t[2] = { cos(u)*bx(t+0.1),by(t + 0.1) ,-sin(u)*bx(t + 0.1) };
- alap.t[1] = { cos(u+lepes)*bx(t + 0.1),by(t + 0.1) ,-sin(u + lepes)*bx(t + 0.1) };
- alap.t[0] = { cos(u + lepes)*bx(t),by(t) ,-sin(u + lepes)*bx(t) };
- for (int i = 0; i < 4; i++)
- {
- homogen = ihToH(alap.t[i]);
- trans =kamera * homogen;
- alap.t[i] = hToIh(trans);
- }
- alap.normal = normalize(cross(alap.t[0] - alap.t[1], alap.t[0] - alap.t[3]));
- alap.suly = (alap.t[0] + alap.t[1] + alap.t[2] + alap.t[3]) / 4;
- alap.distance = dist(vec3(0, 0, centrum),alap.suly);
- faces.push_back(alap);
- }*/
- }
- /*for (u = 0; u < 2 * pi(); u += lepes)
- {
- alap.t[0] = { 0,0,0 };
- alap.t[1] = { cos(u)*bx(0), by(0), -sin(u)*bx(0) };
- alap.t[2] = {cos(u + lepes)*bx(0), by(0), -sin(u + lepes)*bx(0)};
- alap.t[3] = { 0,0,0 };
- for (int i = 0; i < 4; i++)
- {
- homogen = ihToH(alap.t[i]);
- trans = kamera * homogen;
- alap.t[i] = hToIh(trans);
- }
- alap.normal = normalize(cross(alap.t[0] - alap.t[1], alap.t[0] - alap.t[2]));
- alap.suly = (alap.t[0] + alap.t[1] + alap.t[2] + alap.t[2]) / 4;
- alap.distance = dist(vec3(0, 0, centrum), alap.suly);
- alap.triangle = true;
- faces.push_back(alap);
- }
- sort(faces.begin(), faces.end(), sorban1);
- float shade;
- vec4 fenyk=transpose(inverse(kamera)) * ihToH(feny);
- vec3 fenyt = {fenyk.x, fenyk.y, fenyk.z};*/
- glColor3f(0, 0, 0);
- glBegin(GL_POINTS);
- for(int i=0;i<controllpoly.size();i++)
- {
- homogen = ihToH(controllpoly[i]);
- trans = w2v * projection * homogen;
- controllpoly[i] = hToIh(trans);
- glVertex2f(controllpoly[i].x,controllpoly[i].y);
- }
- glEnd();
- for (int i=0; i < faces.size(); i++)
- {
- alap = faces[i];
- glBegin(GL_POLYGON);
- if (alap.triangle)
- {
- for (int j = 0; j < 3; j++)
- {
- homogen = ihToH(alap.t[j]);
- trans = w2v * projection * homogen;
- alap.t[j] = hToIh(trans);
- glVertex2f(alap.t[j].x, alap.t[j].y);
- }
- }
- else
- {
- for (int j = 0; j < 4; j++)
- {
- homogen = ihToH(alap.t[j]);
- trans = w2v * projection * homogen;
- alap.t[j] = hToIh(trans);
- glVertex2f(alap.t[j].x, alap.t[j].y);
- }
- }
- glEnd();
- glColor3f(0, 0, 0);
- glBegin(GL_LINE_LOOP);
- if (alap.triangle)
- {
- for (int j = 0; j < 3; j++)
- {
- glVertex2f(alap.t[j].x, alap.t[j].y);
- }
- glEnd();
- }
- else
- {
- for (int j = 0; j < 4; j++)
- {
- glVertex2f(alap.t[j].x, alap.t[j].y);
- }
- glEnd();
- }
- }
- faces.clear();
- }
- void display() {
- glClear(GL_COLOR_BUFFER_BIT);
- glColor3f(0, 0, 0);
- vaza();
- glutSwapBuffers();
- }
- //Billentyűparancsok
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key) {
- //Kilépés
- case 27:
- exit(0);
- case 'w':
- alpha += 0.5;
- break;
- case 's':
- alpha -= 0.5;
- break;
- case 'a':
- beta -= 0.05;
- break;
- case 'd':
- beta += 0.05;
- break;
- case 'e':
- r += 0.5;
- break;
- case 'q':
- r -= 0.5;
- break;
- case 'r':
- centrum += 0.5;
- break;
- case 'f':
- centrum -= 0.5;
- break;
- }
- glutPostRedisplay();
- }
- void update(int n)
- {
- glutPostRedisplay();
- glutTimerFunc(50, update, 0);
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(winWidth, winHeight);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Váza");
- init();
- glutDisplayFunc(display);
- glutKeyboardFunc(keyboard);
- glutTimerFunc(5, update, 0);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement