/*========================================== Title: Torus Author: Filip Bojovic Date: 20.12.2020. ==========================================*/ #include #include #include #include #include #include #define NUM_OF_CIRCLES 100 #define CIRCLE_DOTS 15 #define INNER_TORUS_R 1 #define OUTER_TORUS_R 1.25 #define FPS 60 using namespace std; using namespace glm; vec3 camera = vec3(2.0, 2.0, 2.0); vector> torus; void drawPolygon(vector points) { glColor3f(1.0, 1.0, 1.0); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glBegin(GL_POLYGON); for(int i = 0; i < points.size(); i++) glVertex3f(points[i].x, points[i].y, points[i].z); glEnd(); } void createTorus() { double alpha = 2 * M_PI / NUM_OF_CIRCLES; torus.resize(2); torus[0].resize(NUM_OF_CIRCLES); torus[1].resize(NUM_OF_CIRCLES); torus[0][0] = vec3(INNER_TORUS_R + ((OUTER_TORUS_R - INNER_TORUS_R) / 2.0f), 0.0, 0.0); torus[1][0] = vec3((OUTER_TORUS_R - INNER_TORUS_R) / 2.0f, 0.0, 0.0); for(int i = 1; i < NUM_OF_CIRCLES; i++) { torus[0][i] = rotateY(vec<3, double, defaultp>(torus[0][i - 1]), alpha); torus[1][i] = rotateY(vec<3, double, defaultp>(torus[1][i - 1]), alpha); } } void drawTorus() { double alpha = 2 * M_PI / CIRCLE_DOTS; double diff = (OUTER_TORUS_R - INNER_TORUS_R) / 2.0f; vec3 rotateVec; //vektor oko kog se rotira vector poly; poly.resize(CIRCLE_DOTS); for(int i = 0; i < torus[0].size(); i++) { poly[0] = torus[1][i]; rotateVec = rotateY(vec<3, double, defaultp> (poly[0]), M_PI_2); for(int j = 1; j < poly.size(); j++) poly[j] = rotate(vec<3, double, defaultp> (poly[j - 1]), alpha, vec<3, double, defaultp> (rotateVec)); for(int j = 0; j < poly.size(); j++) poly[j] += torus[0][i]; drawPolygon(poly); } } void drawAxis() { glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(1.0, 0.0, 0.0); glEnd(); glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glEnd(); glColor3f(0.0, 0.0, 1.0); glBegin(GL_LINES); glVertex3f(0.0, 0.0, 0.0); glVertex3f(0.0, 0.0, 1.0); glEnd(); } void display() { glClear(GL_COLOR_BUFFER_BIT); drawTorus(); drawAxis(); glFlush(); } void initGL() { glClearColor(0.0, 0.0, 0.0, 1.0); } void timer(int v) { camera = rotateY(vec<3, double, defaultp> (camera), 1 * M_PI / 180.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camera.x, camera.y, camera.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutTimerFunc(1000 / FPS, timer, v); glutPostRedisplay(); } void reshape(GLsizei width, GLsizei height) { if(!height) height = 1; GLfloat aspect = (GLfloat)width / (GLfloat)height; glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(80.0f, aspect, 0.1f, 50.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camera.x, camera.y, camera.z, 0.0 ,0.0, 0.0, 0.0, 1.0, 0.0); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(500, 500); glutInitWindowPosition(200, 200); glutCreateWindow("Torus"); createTorus(); glMatrixMode(GL_MODELVIEW); //primenjuje dodatne operacija matrica na stek modela glLoadIdentity(); //postavlja matricu modelView na jedinicnu. U sustini je restartuje na pocetno stanje. gluLookAt(camera.x, camera.y, camera.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutDisplayFunc(display); glutTimerFunc(100, timer, 0); glutReshapeFunc(reshape); //callBack fja kada se promeni velicina prozora initGL(); glutMainLoop(); }