Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <stdarg.h>
- #include <math.h>
- #include <GL/glut.h>
- using namespace std;
- void display();
- void specialKeys();
- double rotate_y=0;
- double rotate_x=0;
- int N = 5;
- float de = 0.2;
- float d = 0.5;
- float D = 0.7;
- float P = 0.3;
- float h = 0;
- void drawAxis(){
- glBegin(GL_LINES);
- glColor3f( 1.0, 0.0, 0.0 );
- glVertex3i(-100,0,0);
- glVertex3i(100,0,0);
- glColor3f( 0.0, 1.0, 0.0 );
- glVertex3i(0,-100,0);
- glVertex3i(0,100,0);
- glColor3f( 0.0, 0.0, 1.0 );
- glVertex3i(0,0,-100);
- glVertex3i(0,0,100);
- glEnd();
- }
- void display(){
- glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
- glLoadIdentity();
- glRotatef( rotate_x, 1.0, 0.0, 0.0 );
- glRotatef( rotate_y, 0.0, 1.0, 0.0 );
- drawAxis();
- glPointSize(5);
- glLineWidth(2);
- glBegin(GL_LINES);
- h = D - d;
- h = h *N / 2000;
- glColor3f( 1.0, 0.0, 1.0 );
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x;
- float y;
- if (i%(2000/N)==0 ){
- x = (D - h* (i%(2000/N))) * cosf(theta);
- y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,-P/2);
- x = (D - h* (i%(2000/N))) * cosf(theta);
- y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,P/2);
- x = (d + 0.02f ) * cosf(2.0f * 3.1415926f * float(i-1) / float(2000));
- y = (d + 0.02f ) * sinf(2.0f * 3.1415926f * float(i-1) / float(2000));
- glVertex3f(x,y,-P/2);
- x = (d + 0.02f ) * cosf(2.0f * 3.1415926f * float(i-1) / float(2000));
- y = (d + 0.02f ) * sinf(2.0f * 3.1415926f * float(i-1) / float(2000));
- glVertex3f(x,y,P/2);
- }
- }
- glEnd();
- glBegin(GL_LINE_LOOP);
- h = D - d;
- h = h *N / 2000;
- glColor3f( 1.0, 0.0, 1.0 );
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = (D - h* (i%(2000/N))) * cosf(theta);
- float y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,-P/2);
- }
- glEnd();
- glBegin(GL_LINE_LOOP);
- h = D - d;
- h = h *N / 2000;
- glColor3f( 1.0, 0.0, 1.0 );
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = de * cosf(theta);
- float y = de * sinf(theta);
- glVertex3f(x,y,-P/2);
- }
- glEnd();
- glBegin(GL_LINE_LOOP);
- h = D - d;
- h = h *N / 2000;
- glColor3f( 1.0, 0.0, 1.0 );
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = (D - h* (i%(2000/N))) * cosf(theta);
- float y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,P/2);
- }
- glEnd();
- glBegin(GL_LINE_LOOP);
- h = D - d;
- h = h *N / 2000;
- glColor3f( 1.0, 0.0, 1.0 );
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = de * cosf(theta);
- float y = de * sinf(theta);
- glVertex3f(x,y,P/2);
- }
- glEnd();
- glBegin(GL_QUAD_STRIP);
- h = D - d;
- h = h *N / 2000;
- glColor3f( 0.0, 1.0, 1.0 );
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = (D - h* (i%(2000/N))) * cosf(theta);
- float y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,-P/2);
- x = de * cosf(theta);
- y = de * sinf(theta);
- glVertex3f(x,y,-P/2);
- }
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = (D - h* (i%(2000/N))) * cosf(theta);
- float y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,P/2);
- x = de * cosf(theta);
- y = de * sinf(theta);
- glVertex3f(x,y,P/2);
- }
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = (D - h* (i%(2000/N))) * cosf(theta);
- float y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,-P/2);
- x = (D - h* (i%(2000/N))) * cosf(theta);
- y = (D - h* (i%(2000/N))) * sinf(theta);
- glVertex3f(x,y,P/2);
- }
- for(int i=0;i<2000;i++){
- float theta = 2.0f * 3.1415926f * float(i) / float(2000);
- float x = de * cosf(theta);
- float y = de * sinf(theta);
- glVertex3f(x,y,-P/2);
- x = de * cosf(theta);
- y = de * sinf(theta);
- glVertex3f(x,y,P/2);
- }
- glEnd();
- glFlush();
- glutSwapBuffers();
- }
- void specialKeys( int key, int x, int y ) {
- if (key == GLUT_KEY_RIGHT)
- rotate_y += 5;
- else if (key == GLUT_KEY_LEFT)
- rotate_y -= 5;
- else if (key == GLUT_KEY_UP)
- rotate_x += 5;
- else if (key == GLUT_KEY_DOWN)
- rotate_x -= 5;
- glutPostRedisplay();
- }
- int main(int argc, char* argv[]){
- glutInit(&argc,argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(600,600);
- glutCreateWindow("Awesome Cube");
- glEnable(GL_DEPTH_TEST);
- glutDisplayFunc(display);
- glutSpecialFunc(specialKeys);
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment