Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.08 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <iostream>
  5. #include <GL/gl.h>
  6. #include <GL/glut.h>
  7.  
  8.  
  9. GLuint texture[1];
  10.  
  11. double angle = 0;
  12.  
  13. typedef struct
  14.   {
  15.   GLfloat X;
  16.   GLfloat Y;
  17.   GLfloat Z;
  18.   double U;
  19.   double V;
  20.   } VERTICES;
  21.  
  22. const double PI = 3.1415926535897;
  23.  
  24. const int przestrzen = 10;
  25.  
  26. const int VertexCount = (90 / przestrzen) * (360 / przestrzen) * 4;
  27.  
  28. VERTICES VERTEX[VertexCount];
  29.  
  30. GLuint LoadTextureRAW( const char * filename );
  31.  
  32. void DisplaySphere (double R, GLuint texture){
  33.  int b;
  34.   glScalef(0.0125 *R, 0.0125 *R, 0.0125 *R);
  35.   glRotatef(90, 1, 0, 0);
  36.   glBindTexture(GL_TEXTURE_2D, texture);
  37.   glBegin(GL_TRIANGLE_STRIP);
  38.   for (b = 0; b <= VertexCount; b++)
  39.     {
  40.     glTexCoord2f(VERTEX[b].U, VERTEX[b].V);
  41.     glVertex3f(VERTEX[b].X, VERTEX[b].Y,  - VERTEX[b].Z);
  42.     }
  43.   for (b = 0; b <= VertexCount; b++)
  44.     {
  45.     glTexCoord2f(VERTEX[b].U,  - VERTEX[b].V);
  46.     glVertex3f(VERTEX[b].X, VERTEX[b].Y, VERTEX[b].Z);
  47.     }
  48.   glEnd();
  49. }
  50.  
  51. void CreateSphere (double R, double H, double K, double Z) {
  52.  int n;
  53.   double a;
  54.   double b;
  55.   n = 0;
  56.   for (b = 0; b <= 90-przestrzen; b += przestrzen)
  57.     {
  58.     for (a = 0; a <= 360-przestrzen; a += przestrzen)
  59.       {
  60.       VERTEX[n].X = R * sin((a) / 180 * PI) *sin((b) / 180 * PI) - H;
  61.       VERTEX[n].Y = R * cos((a) / 180 * PI) *sin((b) / 180 * PI) + K;
  62.       VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;
  63.       VERTEX[n].V = (2 *b) / 360;
  64.       VERTEX[n].U = (a) / 360;
  65.       n++;
  66.       VERTEX[n].X = R * sin((a) / 180 * PI) *sin((b + przestrzen) / 180 * PI
  67.       ) - H;
  68.       VERTEX[n].Y = R * cos((a) / 180 * PI) *sin((b + przestrzen) / 180 * PI
  69.       ) + K;
  70.       VERTEX[n].Z = R * cos((b + przestrzen) / 180 * PI) - Z;
  71.       VERTEX[n].V = (2 *(b + przestrzen)) / 360;
  72.       VERTEX[n].U = (a) / 360;
  73.       n++;
  74.       VERTEX[n].X = R * sin((a + przestrzen) / 180 * PI) *sin((b) / 180 * PI
  75.       ) - H;
  76.       VERTEX[n].Y = R * cos((a + przestrzen) / 180 * PI) *sin((b) / 180 * PI
  77.       ) + K;
  78.       VERTEX[n].Z = R * cos((b) / 180 * PI) - Z;
  79.       VERTEX[n].V = (2 *b) / 360;
  80.       VERTEX[n].U = (a + przestrzen) / 360;
  81.       n++;
  82.       VERTEX[n].X = R * sin((a + przestrzen) / 180 * PI) *sin((b + przestrzen) /
  83.       180 *PI) - H;
  84.       VERTEX[n].Y = R * cos((a + przestrzen) / 180 * PI) *sin((b + przestrzen) /
  85.       180 *PI) + K;
  86.       VERTEX[n].Z = R * cos((b + przestrzen) / 180 * PI) - Z;
  87.       VERTEX[n].V = (2 *(b + przestrzen)) / 360;
  88.       VERTEX[n].U = (a + przestrzen) / 360;
  89.       n++;
  90.       }
  91.     }
  92. }
  93.  
  94. void display (void) {
  95.    
  96.     glClearDepth(1);
  97.    
  98.     glClearColor (0.0,0.0,0.0,1.0);
  99.    
  100.     glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  101.    
  102.     glLoadIdentity();
  103.    
  104.  
  105.    
  106.     glTranslatef(0,0,-10);
  107.    
  108.  
  109.    
  110.     glRotatef((angle/2),0,1,0);
  111.    
  112.     DisplaySphere(5, texture[0]);
  113.    
  114.  
  115.    
  116.     glutSwapBuffers();
  117.    
  118.     angle ++;
  119. }
  120.  
  121. void init (void) {
  122.    
  123. glEnable(GL_DEPTH_TEST);
  124.    
  125. glEnable( GL_TEXTURE_2D );
  126.    
  127. glDepthFunc(GL_LEQUAL);
  128.    
  129. glCullFace(GL_BACK);
  130.    
  131. glFrontFace(GL_CCW);
  132.    
  133. glEnable(GL_CULL_FACE);
  134.    
  135. texture[0] = LoadTextureRAW( "earth.raw" );
  136.    
  137. CreateSphere(70,0,0,0);
  138. }
  139. void reshape (int w, int h) {
  140.    
  141.     glViewport (0, 0, (GLsizei)w, (GLsizei)h);
  142.    
  143.     glMatrixMode (GL_PROJECTION);
  144.    
  145.     glLoadIdentity ();
  146.    
  147.     gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 100.0);
  148.    
  149.     glMatrixMode (GL_MODELVIEW);
  150. }
  151.  
  152. int main (int argc, char **argv) {
  153.    
  154.     glutInit (&argc, argv);
  155.    
  156.     glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH);
  157.    
  158.     glutInitWindowSize (500, 500);
  159.    
  160.     glutInitWindowPosition (100, 100);
  161.    
  162.     glutCreateWindow ("A basic OpenGL Window");
  163.    
  164.     init();
  165.    
  166.     glutDisplayFunc (display);
  167.    
  168.     glutIdleFunc (display);
  169.    
  170.     glutReshapeFunc (reshape);
  171.    
  172.     glutMainLoop ();
  173.    
  174.     return 0;
  175. }
  176.  
  177. GLuint LoadTextureRAW( const char * filename )
  178. {
  179.    
  180.   GLuint texture;
  181.    
  182.   int width, height;
  183.    
  184.   unsigned char * data;
  185.    
  186.   FILE * file;
  187.    
  188.  
  189.    
  190.   file = fopen( filename, "rb" );
  191.    
  192.   if ( file == NULL ) return 0;
  193.    
  194.  
  195.    
  196.   width = 1024;
  197.    
  198.   height = 512;
  199.    
  200.   data = (unsigned char *)malloc( width * height * 3 );
  201.    
  202.  
  203.    
  204.   fread( data, width * height * 3, 1, file );
  205.    
  206.   fclose( file );
  207.    
  208.  
  209.    
  210.   glGenTextures( 1, &texture );
  211.    
  212.  
  213.    
  214.   glBindTexture( GL_TEXTURE_2D, texture );
  215.    
  216.  
  217.    
  218.   glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
  219.    
  220. GL_MODULATE );
  221.    
  222.  
  223.    
  224.   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  225.    
  226.  GL_LINEAR_MIPMAP_NEAREST );
  227.    
  228.  
  229.    
  230.   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  231.    
  232.  GL_LINEAR );
  233.    
  234.  
  235.    
  236.   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,
  237.    
  238. GL_REPEAT );
  239.    
  240.   glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,
  241.    
  242. GL_REPEAT );
  243.    
  244.  
  245.    
  246.   gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height,
  247.    
  248. GL_RGB, GL_UNSIGNED_BYTE, data );
  249.    
  250.  
  251.    
  252.   free( data );
  253.    
  254.  
  255.    
  256.   return texture;
  257.    
  258.  
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement