SHARE
TWEET

Untitled

Danilqaman Nov 15th, 2017 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdlib.h>
  2. #include <GL/glut.h>
  3. #include <Windows.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6. #include "stdafx.h"
  7.  
  8. #define MAXZ 8.0
  9. #define MINZ -8.0
  10. #define ZINC 0.02
  11.  
  12. float solidZ = MAXZ;
  13. float transparentZ = MINZ;
  14.  
  15. GLuint sphereList, cubeList;
  16.  
  17. float mat2_dif[] = { 0.0f, 0.0f, 0.8f };
  18. float mat2_amb[] = { 0.2f, 0.2f, 0.2f };
  19. float mat2_spec[] = { 0.6f, 0.6f, 0.6f };
  20. float mat2_shininess = 0.7f * 128;
  21.  
  22. float mat3_dif[] = { 0.0f, 0.0f, 0.8f };
  23. float mat3_amb[] = { 0.2f, 0.2f, 0.2f };
  24. float mat3_spec[] = { 0.6f, 0.6f, 0.6f };
  25. float mat3_shininess = 0.7f * 128;
  26.  
  27. int spin = 0;
  28.  
  29. void init(void)
  30. {
  31.     GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
  32.     GLfloat mat_shininess[] = { 100.0 };
  33.     GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
  34.  
  35.     glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  36.     glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  37.  
  38.     GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  39.     GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
  40.     GLfloat light_specular[] = { 1.0, 0.0, 0.0, 1.0 };
  41.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  42.  
  43.     glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  44.     glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  45.     glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  46.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  47.  
  48.     glEnable(GL_LIGHTING);
  49.     glEnable(GL_LIGHT0);
  50.  
  51.     glEnable(GL_DEPTH_TEST);
  52.  
  53.     GLfloat light1_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  54.     GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  55.     GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  56.     GLfloat light1_position[] = { -1.0, -1.0, 1.0, 0.0 };
  57.  
  58.     glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
  59.     glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
  60.     glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
  61.     glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  62.  
  63.     glEnable(GL_LIGHTING);
  64.     glEnable(GL_LIGHT1);
  65.  
  66.     glEnable(GL_DEPTH_TEST);
  67.  
  68.     GLfloat light2_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
  69.     GLfloat light2_diffuse[] = { 0.0, 0.0, 1.0, 1.0 };
  70.     GLfloat light2_specular[] = { 0.0, 0.0, 1.0, 1.0 };
  71.     GLfloat light2_position[] = { 1.0, -1.0, -1.0, 0.0 };
  72.  
  73.     glLightfv(GL_LIGHT2, GL_AMBIENT, light2_ambient);
  74.     glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
  75.     glLightfv(GL_LIGHT2, GL_SPECULAR, light2_specular);
  76.     glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
  77.  
  78.     glEnable(GL_LIGHTING);
  79.     glEnable(GL_LIGHT2);
  80.  
  81.     glEnable(GL_DEPTH_TEST);
  82.  
  83.     sphereList = glGenLists(1);
  84.     glNewList(sphereList, GL_COMPILE);
  85.     glutSolidSphere(0.4, 16, 16);
  86.     glEndList();
  87.  
  88.     cubeList = glGenLists(1);
  89.     glNewList(cubeList, GL_COMPILE);
  90.     glutSolidCube(0.6);
  91.     glEndList();
  92. }
  93.  
  94. void display(void)
  95. {
  96.     GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
  97.     GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
  98.     GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
  99.     GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
  100.  
  101.     GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
  102.     GLfloat light1_position[] = { -1.0, -1.0, 1.0, 0.0 };
  103.     GLfloat light2_position[] = { 1.0, -1.0, -1.0, 0.0 };
  104.  
  105.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  106.     glPushMatrix();
  107.     gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
  108.     glPushMatrix();
  109.     glTranslatef(-0.15, -0.15, solidZ);
  110.     glRotated(spin, 1.0, 0.0, 0.0);
  111.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
  112.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
  113.     glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  114.     glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
  115.     glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
  116.     glPopMatrix();
  117.     glCallList(sphereList);
  118.     glPopMatrix();
  119.     glFlush();
  120.  
  121.     glPushMatrix();
  122.     glTranslatef(0.15, 0.15, transparentZ);
  123.     glRotatef(15.0, 1.0, 1.0, 0.0);
  124.     glRotatef(30.0, 0.0, 1.0, 0.0);
  125.     glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
  126.     glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
  127.     glEnable(GL_BLEND);
  128.     glDepthMask(GL_FALSE);
  129.     glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  130.     glCallList(cubeList);
  131.     glDepthMask(GL_TRUE);
  132.     glDisable(GL_BLEND);
  133.     glPopMatrix();
  134.     glutSwapBuffers();
  135. }
  136.  
  137. void reshape(int w, int h)
  138. {
  139.     glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  140.     glMatrixMode(GL_PROJECTION);
  141.     glLoadIdentity();
  142.     if (w <= h)
  143.         glOrtho(-1.5, 1.5, -0.5*(GLfloat)h / (GLfloat)w, 1.5*(GLfloat)h / (GLfloat)w, -
  144.             10.0, 10.0);
  145.     else
  146.         glOrtho(-1.5*(GLfloat)w / (GLfloat)h, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5, -
  147.             10.0, 10.0);
  148.     glMatrixMode(GL_MODELVIEW);
  149.     glLoadIdentity();
  150. }
  151.  
  152. void animate()
  153. {
  154.     if (solidZ <= MINZ || transparentZ >= MAXZ)
  155.         glutIdleFunc(NULL);
  156.     else
  157.     {
  158.         solidZ -= ZINC;
  159.         transparentZ += ZINC;
  160.         glutPostRedisplay();
  161.     }
  162. }
  163.  
  164. void keyboard(unsigned char key, int x, int y)
  165. {
  166.     switch (key)
  167.     {
  168.     case 'a':
  169.     case 'A':
  170.         solidZ = MAXZ;
  171.         transparentZ = MINZ;
  172.         glutIdleFunc(animate);
  173.         break;
  174.     case 'r':
  175.     case 'R':
  176.         solidZ = MAXZ;
  177.         transparentZ = MINZ;
  178.         glutPostRedisplay();
  179.         break;
  180.     case 27:
  181.         exit(0);
  182.     }
  183. }
  184.  
  185.  
  186. void light_moving(void)
  187. {
  188.     spin = (spin + 1) % 360;
  189.     glutPostRedisplay();
  190. }
  191.  
  192. void mouse(int button, int state, int x, int y)
  193. {
  194.     switch (button)
  195.     {
  196.     case GLUT_LEFT_BUTTON:
  197.         if (state == GLUT_DOWN)
  198.             glutIdleFunc(light_moving);
  199.         else
  200.             glutIdleFunc(NULL);
  201.         break;
  202.     }
  203. }
  204.  
  205. int main(int argc, char** argv)
  206. {
  207.     glutInit(&argc, argv);
  208.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  209.     glutInitWindowSize(500, 500);
  210.     glutCreateWindow("LABBAL4");
  211.     init();
  212.     glutDisplayFunc(display);
  213.     glutReshapeFunc(reshape);
  214.     glutKeyboardFunc(keyboard);
  215.     glutMouseFunc(mouse);
  216.     glutMainLoop();
  217.     return 0;
  218. }
RAW Paste Data
Top