Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/gl.h>
- #include <GL/glut.h>
- #include <cmath>
- #include <cstdio>
- #include <iostream>
- using namespace std;
- bool timeChange = true;
- bool showKeyCode = false;
- bool keypressed[10] = {};
- GLfloat PI2 = 6.283185307179586;
- GLfloat param = 0;
- GLfloat ToroidR = 30;
- GLfloat Toroidr = 10;
- GLfloat Xrotation = 0.0;
- GLfloat Yrotation = 0.0;
- GLfloat Zrotation = 0.0;
- GLfloat RotationSpeed = 2.5;
- GLint CoordinatResolutionX = 100;
- GLint CoordinatResolutionY = 100;
- GLint CoordinatResolutionZ = 100;
- void display() {
- glClearColor(0.1, 0.1, 0.1, 1);
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glPointSize( 5 );
- glRotatef(Xrotation, 1.0f, 0.0f, 0.0f);
- glRotatef(Yrotation, 0.0f, 1.0f, 0.0f);
- glRotatef(Zrotation, 0.0f, 0.0f, 1.0f);
- for( int x = -50; x < 100; x+=1 )
- for( int y = -50; y < 100; y+=1 )
- for( int z = -50; z < 100; z+=1 ){
- float tor = ( x * x + y * y + z * z + ToroidR * ToroidR - Toroidr * Toroidr) * ( x * x + y * y + z * z + ToroidR * ToroidR - Toroidr * Toroidr) - 4 * ToroidR * ToroidR * ( x * x + y * y );
- if ( tor >= -20000 && tor <= 20000 ){
- GLfloat r = ( x + 50 ) / 100.0;
- GLfloat g = ( y + 50 ) / 100.0;
- GLfloat b = ( z + 50 ) / 100.0;
- //GLfloat size = ( ( ( x + y + 100 ) / 200.0 ) ) * 1 * 10;
- GLfloat size = ( sin( ( x + y + z ) / 100.0 * 6 + param ) / 3.0 + 0.5 ) * 10 ;
- glColor3f( r, g, b );
- glPointSize(size);
- glBegin(GL_POINTS);
- glVertex3f(x,y,z);
- glEnd();
- }
- }
- /*
- for( int i = 0; i < point_count; i++ ){
- GLfloat r = i / (float)(point_count);
- GLfloat g = cos( i / (float)(point_count) * PI2 ) ;
- GLfloat b = 0.3 + ( ( sin( param ) + 1 ) / 5.0 );
- glColor3f( r, g, b );
- GLfloat x = ( ( i / (float)(point_count) * CoordinatResolutionX ) ) - ( CoordinatResolutionX / 2.0 );
- GLfloat y = sin( param + i * 0.3 ) * 10;
- glVertex3f( x, y, 0 );
- }
- */
- glColor3f( 1,1,1 );
- glBegin(GL_LINES);
- glVertex3f( -50, 0, 0 );
- glVertex3f( 50, 0, 0 );
- glVertex3f( 45, -5, 0 );
- glVertex3f( 50, 0, 0 );
- glVertex3f( 45, 5, 0 );
- glVertex3f( 50, 0, 0 );
- glVertex3f( 0, -50, 0 );
- glVertex3f( 0, 50, 0 );
- glVertex3f( -5, 45, 0 );
- glVertex3f( 0, 50, 0 );
- glVertex3f( 5, 45, 0 );
- glVertex3f( 0, 50, 0 );
- glEnd();
- glutSwapBuffers();
- }
- void ChangeSize(GLsizei w, GLsizei h){
- GLfloat aspectRatio;
- if (h == 0)
- h = 1;
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- aspectRatio = (GLfloat)w / (GLfloat)h;
- if (w <= h) {
- GLfloat windowWidth = (CoordinatResolutionX / 2.0);
- GLfloat windowHeight = ( CoordinatResolutionY / 2.0 ) / aspectRatio;
- glOrtho(-CoordinatResolutionX/2.0, CoordinatResolutionX/2.0, -windowHeight, windowHeight, -CoordinatResolutionZ/2.0, CoordinatResolutionZ/2.0);
- } else {
- GLfloat windowWidth = ( CoordinatResolutionX / 2.0 ) * aspectRatio;
- GLfloat windowHeight =( CoordinatResolutionY / 2.0 );
- glOrtho(-windowWidth, windowWidth, -CoordinatResolutionY/2.0, -CoordinatResolutionY/2.0, -CoordinatResolutionZ/2.0, CoordinatResolutionZ/2.0);
- }
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void inc( int x ){
- if ( timeChange )
- param += 0.02 * x;
- if ( keypressed[1] )
- Xrotation += RotationSpeed;
- if ( keypressed[2] )
- Xrotation -= RotationSpeed;
- if ( keypressed[3] )
- Yrotation += RotationSpeed;
- if ( keypressed[4] )
- Yrotation -= RotationSpeed;
- if ( keypressed[5] )
- Zrotation += RotationSpeed;
- if ( keypressed[6] )
- Zrotation -= RotationSpeed;
- glutPostRedisplay();
- glutTimerFunc(16, inc , 3);
- }
- void keyDown ( int key, int x, int y)
- {
- if( showKeyCode )
- std::cout << "keydown " << key << "\n";
- if( key == GLUT_KEY_END && !keypressed[0] ){
- timeChange = !timeChange;
- keypressed[0] = true;
- }
- if( key == GLUT_KEY_UP && !keypressed[1] ){
- keypressed[1] = true;
- }
- if( key == GLUT_KEY_DOWN && !keypressed[2] ){
- keypressed[2] = true;
- }
- if( key == GLUT_KEY_LEFT && !keypressed[3] ){
- keypressed[3] = true;
- }
- if( key == GLUT_KEY_RIGHT && !keypressed[4] ){
- keypressed[4] = true;
- }
- if( key == GLUT_KEY_PAGE_UP && !keypressed[5] ){
- keypressed[5] = true;
- }
- if( key == GLUT_KEY_PAGE_DOWN && !keypressed[6] ){
- keypressed[6] = true;
- }
- }
- void keyUp ( int key, int x, int y)
- {
- if( showKeyCode )
- std::cout << "keyup " << key << "\n";
- if( key == GLUT_KEY_END ){
- keypressed[0] = false;
- }
- if( key == GLUT_KEY_UP ){
- keypressed[1] = false;
- }
- if( key == GLUT_KEY_DOWN ){
- keypressed[2] = false;
- }
- if( key == GLUT_KEY_LEFT ){
- keypressed[3] = false;
- }
- if( key == GLUT_KEY_RIGHT ){
- keypressed[4] = false;
- }
- if( key == GLUT_KEY_PAGE_UP ){
- keypressed[5] = false;
- }
- if( key == GLUT_KEY_PAGE_DOWN ){
- keypressed[6] = false;
- }
- }
- int main(int argc, char** argv) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowPosition(1228,500);
- glutInitWindowSize(1000, 1000);
- glutCreateWindow("float");
- glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);
- glutSpecialFunc(keyDown);
- glutSpecialUpFunc(keyUp);
- glutDisplayFunc(display);
- glutReshapeFunc(ChangeSize);
- glutTimerFunc(16, inc , 3);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement