Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <math.h>
  5. #include <GL/glut.h>
  6.  
  7. static float lightHeight = 10;
  8. GLfloat angle = -150;   /* in degrees */
  9. GLfloat angle2 = 30;   /* in degrees */
  10.  
  11. int moving, startx, starty;
  12. static GLfloat lightPosition[4];
  13.  
  14. static void makeFloorTexture(void)
  15. {
  16. #define TEXSIZE 256
  17.     GLubyte floorTexture[TEXSIZE][TEXSIZE][3];
  18.     int s, t;
  19.  
  20.     /* Setup RGB image for the texture. */
  21.     for (t = 0; t < TEXSIZE; t++)
  22.     {
  23.         for (s = 0; s < TEXSIZE; s++)
  24.         {
  25.             int red   = (t * (256/TEXSIZE)) % 256;
  26.             int green = (s * (256/TEXSIZE)) % 256;
  27.             int blue  = (int)(((t*s) * (1./TEXSIZE)) + 0.5) % 256;
  28.             floorTexture[t][s][0] = red;
  29.             floorTexture[t][s][1] = blue;
  30.             floorTexture[t][s][2] = green;
  31.         }
  32.     }
  33.  
  34.     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  35.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
  36.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
  37.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  38.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  39.     glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TEXSIZE, TEXSIZE, 0, GL_RGB, GL_UNSIGNED_BYTE, floorTexture);
  40. }
  41.  
  42. static GLfloat floorVertices[4][3] =
  43. {
  44.     { -20.0, 0.0,  20.0 },
  45.     {  20.0, 0.0,  20.0 },
  46.     {  20.0, 0.0, -20.0 },
  47.     { -20.0, 0.0, -20.0 },
  48. };
  49.  
  50. static void glVertex3fvScale(GLfloat x, GLfloat y, GLfloat radius)
  51. {
  52.     GLfloat out[3];
  53.     out[0] = x*radius*2 - radius;
  54.     out[1] = 0;
  55.     out[2] = y*radius*2 - radius;
  56.     glVertex3fv(out);
  57. }
  58.  
  59. static void drawFloor(void)
  60. {
  61.     GLfloat radius = 20;
  62. #define FLOOR_PARTS 128
  63.     glDisable(GL_TEXTURE_2D);
  64.     //glEnable(GL_TEXTURE_2D);
  65.     for(int y = 0; y < FLOOR_PARTS; y++)
  66.         for(int x = 0; x < FLOOR_PARTS; x++)
  67.         {
  68.             GLfloat startx = (GLfloat)(x+0) / FLOOR_PARTS;
  69.             GLfloat   endx = (GLfloat)(x+1) / FLOOR_PARTS;
  70.             GLfloat starty = (GLfloat)(y+0) / FLOOR_PARTS;
  71.             GLfloat   endy = (GLfloat)(y+1) / FLOOR_PARTS;
  72.             glBegin(GL_QUADS);
  73.             glTexCoord2f     (startx*8.0, starty*8.0);
  74.             glNormal3f(0,1,0);
  75.             glVertex3fvScale(startx    , starty, radius);
  76.             glTexCoord2f     (startx*8.0,   endy*8.0);
  77.             glNormal3f(0,1,0);
  78.             glVertex3fvScale(startx    ,   endy, radius);
  79.             glTexCoord2f     (  endx*8.0,   endy*8.0);
  80.             glNormal3f(0,1,0);
  81.             glVertex3fvScale(  endx    ,   endy, radius);
  82.             glTexCoord2f     (  endx*8.0, starty*8.0);
  83.             glNormal3f(0,1,0);
  84.             glVertex3fvScale(  endx    , starty, radius);
  85.             glEnd();
  86.         }
  87.     //glDisable(GL_TEXTURE_2D);
  88. }
  89.  
  90. static void redraw(void)
  91. {
  92.     GLfloat white[]  = {1,1,1,1};
  93.     GLfloat blue[] = {0,0,1,1};
  94.     GLfloat red[] = {1,0,0,1};
  95.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  96.  
  97.     lightPosition[0] = 0;
  98.     lightPosition[1] = lightHeight;
  99.     lightPosition[2] = 0;
  100.     lightPosition[3] = 0;
  101.     glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  102.     glLightfv(GL_LIGHT0, GL_SPECULAR, white);
  103.     glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, 1.0);
  104.     glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 0.5);
  105.     glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, 0.5);
  106.     glEnable(GL_LIGHT0);
  107.  
  108.     glPushMatrix();
  109.     /* Perform scene rotations based on user mouse input. */
  110.     glRotatef(angle2, 1.0, 0.0, 0.0);
  111.     glRotatef(angle, 0.0, 1.0, 0.0);
  112.  
  113.     glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
  114.     glPushMatrix();
  115.     glTranslatef(lightPosition[0],lightPosition[1],lightPosition[2]);
  116.     glutSolidSphere(1,30,30);
  117.     glPopMatrix();
  118.  
  119.     glMaterialfv(GL_FRONT, GL_DIFFUSE, red);
  120.     glMaterialfv(GL_FRONT, GL_SPECULAR, white);
  121.     glMateriali (GL_FRONT, GL_SHININESS, 50);
  122.     glMaterialfv(GL_BACK, GL_DIFFUSE, blue);
  123.     glMaterialfv(GL_BACK, GL_SPECULAR, white);
  124.     glMateriali (GL_BACK, GL_SHININESS, 50);
  125.     glColor3f(1.0, 1.0, 1.0);
  126.     drawFloor();
  127.  
  128.     glPopMatrix();
  129.     glutSwapBuffers();
  130. }
  131.  
  132. static void mouse(int button, int state, int x, int y)
  133. {
  134.     if (button == GLUT_LEFT_BUTTON)
  135.     {
  136.         if (state == GLUT_DOWN)
  137.         {
  138.             moving = 1;
  139.             startx = x;
  140.             starty = y;
  141.         }
  142.         if (state == GLUT_UP)
  143.             moving = 0;
  144.     }
  145. }
  146.  
  147. static void motion(int x, int y)
  148. {
  149.     if (moving)
  150.     {
  151.         angle = angle + (x - startx);
  152.         angle2 = angle2 + (y - starty);
  153.         startx = x;
  154.         starty = y;
  155.         glutPostRedisplay();
  156.     }
  157. }
  158.  
  159. /* Advance time varying state when idle callback registered. */
  160. static void idle(void)
  161. {
  162.     glutPostRedisplay();
  163. }
  164.  
  165. /* When not visible, stop animating.  Restart when visible again. */
  166. static void visible(int vis)
  167. {
  168.     glutIdleFunc(vis == GLUT_VISIBLE ? idle : NULL);
  169. }
  170.  
  171. static void key(unsigned char c, int x, int y)
  172. {
  173. }
  174.  
  175. static void special(int k, int x, int y)
  176. {
  177. }
  178.  
  179. int main(int argc, char **argv)
  180. {
  181.     glutInit(&argc, argv);
  182.     glutInitWindowSize(800,800);
  183.  
  184.     glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH | GLUT_MULTISAMPLE);
  185.  
  186.     glutCreateWindow("OpenGL Robot");
  187.  
  188.     /* Register GLUT callbacks. */
  189.     glutDisplayFunc(redraw);
  190.     glutMouseFunc(mouse);
  191.     glutMotionFunc(motion);
  192.     glutVisibilityFunc(visible);
  193.     glutKeyboardFunc(key);
  194.     glutSpecialFunc(special);
  195.     glEnable(GL_DEPTH_TEST);
  196.     glEnable(GL_TEXTURE_2D);
  197.  
  198.     glMatrixMode(GL_PROJECTION);
  199.     gluPerspective(40.0, 1.0, 10.0, 100.0);
  200.     glMatrixMode(GL_MODELVIEW);
  201.     gluLookAt(  0.0, 8.0, 60.0,  /* eye is at (0,8,60) */
  202.                 0.0, 8.0, 0.0,   /* center is at (0,8,0) */
  203.                 0.0, 1.0, 0.);   /* up is in postivie Y direction */
  204.  
  205.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  206.     glEnable(GL_LIGHTING);
  207.  
  208.     makeFloorTexture();
  209.     glutMainLoop();
  210.     return 0;
  211. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement