Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <GL/glut.h>
- #include <Windows.h>
- #include <stdio.h>
- #include <iostream>
- #include "stdafx.h"
- #define MAXZ 8.0
- #define MINZ -8.0
- #define ZINC 0.02
- float solidZ = MAXZ;
- float transparentZ = MINZ;
- GLuint sphereList, cubeList;
- float mat2_dif[] = { 0.0f, 0.0f, 0.8f };
- float mat2_amb[] = { 0.2f, 0.2f, 0.2f };
- float mat2_spec[] = { 0.6f, 0.6f, 0.6f };
- float mat2_shininess = 0.7f * 128;
- float mat3_dif[] = { 0.0f, 0.0f, 0.8f };
- float mat3_amb[] = { 0.2f, 0.2f, 0.2f };
- float mat3_spec[] = { 0.6f, 0.6f, 0.6f };
- float mat3_shininess = 0.7f * 128;
- int spin = 0;
- void init(void)
- {
- GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 0.15 };
- GLfloat mat_shininess[] = { 100.0 };
- GLfloat position[] = { 0.5, 0.5, 1.0, 0.0 };
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
- GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat light_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
- GLfloat light_specular[] = { 1.0, 0.0, 0.0, 1.0 };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- GLfloat light1_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat light1_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat light1_specular[] = { 1.0, 1.0, 1.0, 1.0 };
- GLfloat light1_position[] = { -1.0, -1.0, 1.0, 0.0 };
- glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
- glLightfv(GL_LIGHT1, GL_SPECULAR, light1_specular);
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT1);
- glEnable(GL_DEPTH_TEST);
- GLfloat light2_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat light2_diffuse[] = { 0.0, 0.0, 1.0, 1.0 };
- GLfloat light2_specular[] = { 0.0, 0.0, 1.0, 1.0 };
- GLfloat light2_position[] = { 1.0, -1.0, -1.0, 0.0 };
- glLightfv(GL_LIGHT2, GL_AMBIENT, light2_ambient);
- glLightfv(GL_LIGHT2, GL_DIFFUSE, light2_diffuse);
- glLightfv(GL_LIGHT2, GL_SPECULAR, light2_specular);
- glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT2);
- glEnable(GL_DEPTH_TEST);
- sphereList = glGenLists(1);
- glNewList(sphereList, GL_COMPILE);
- glutSolidSphere(0.4, 16, 16);
- glEndList();
- cubeList = glGenLists(1);
- glNewList(cubeList, GL_COMPILE);
- glutSolidCube(0.6);
- glEndList();
- }
- void display(void)
- {
- GLfloat mat_solid[] = { 0.75, 0.75, 0.0, 1.0 };
- GLfloat mat_zero[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat mat_transparent[] = { 0.0, 0.8, 0.8, 0.6 };
- GLfloat mat_emission[] = { 0.0, 0.3, 0.3, 0.6 };
- GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
- GLfloat light1_position[] = { -1.0, -1.0, 1.0, 0.0 };
- GLfloat light2_position[] = { 1.0, -1.0, -1.0, 0.0 };
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
- glPushMatrix();
- glTranslatef(-0.15, -0.15, solidZ);
- glRotated(spin, 1.0, 0.0, 0.0);
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_zero);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_solid);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glLightfv(GL_LIGHT1, GL_POSITION, light1_position);
- glLightfv(GL_LIGHT2, GL_POSITION, light2_position);
- glPopMatrix();
- glCallList(sphereList);
- glPopMatrix();
- glFlush();
- glPushMatrix();
- glTranslatef(0.15, 0.15, transparentZ);
- glRotatef(15.0, 1.0, 1.0, 0.0);
- glRotatef(30.0, 0.0, 1.0, 0.0);
- glMaterialfv(GL_FRONT, GL_EMISSION, mat_emission);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_transparent);
- glEnable(GL_BLEND);
- glDepthMask(GL_FALSE);
- glBlendFunc(GL_SRC_ALPHA, GL_ONE);
- glCallList(cubeList);
- glDepthMask(GL_TRUE);
- glDisable(GL_BLEND);
- glPopMatrix();
- glutSwapBuffers();
- }
- void reshape(int w, int h)
- {
- glViewport(0, 0, (GLsizei)w, (GLsizei)h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- if (w <= h)
- glOrtho(-1.5, 1.5, -0.5*(GLfloat)h / (GLfloat)w, 1.5*(GLfloat)h / (GLfloat)w, -
- 10.0, 10.0);
- else
- glOrtho(-1.5*(GLfloat)w / (GLfloat)h, 1.5*(GLfloat)w / (GLfloat)h, -1.5, 1.5, -
- 10.0, 10.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void animate()
- {
- if (solidZ <= MINZ || transparentZ >= MAXZ)
- glutIdleFunc(NULL);
- else
- {
- solidZ -= ZINC;
- transparentZ += ZINC;
- glutPostRedisplay();
- }
- }
- void keyboard(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 'a':
- case 'A':
- solidZ = MAXZ;
- transparentZ = MINZ;
- glutIdleFunc(animate);
- break;
- case 'r':
- case 'R':
- solidZ = MAXZ;
- transparentZ = MINZ;
- glutPostRedisplay();
- break;
- case 27:
- exit(0);
- }
- }
- void light_moving(void)
- {
- spin = (spin + 1) % 360;
- glutPostRedisplay();
- }
- void mouse(int button, int state, int x, int y)
- {
- switch (button)
- {
- case GLUT_LEFT_BUTTON:
- if (state == GLUT_DOWN)
- glutIdleFunc(light_moving);
- else
- glutIdleFunc(NULL);
- break;
- }
- }
- int main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(500, 500);
- glutCreateWindow("LABBAL4");
- init();
- glutDisplayFunc(display);
- glutReshapeFunc(reshape);
- glutKeyboardFunc(keyboard);
- glutMouseFunc(mouse);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement