Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <GL/glut.h>
- void drawScene(void),
- setMatrix(void),
- initLightAndMaterial(void),
- animation(void),
- resize(int w, int h),
- menu(int choice),
- keyboard(unsigned char c, int x, int y);
- bool isLighting = true;
- float ax, ay, az;
- GLUquadricObj *quadObj;
- static float lmodel_twoside[] = {GL_TRUE};
- static float lmodel_oneside[] = {GL_FALSE};
- int main (int argc, char **argv)
- {
- glutInit(&argc, argv);
- quadObj = gluNewQuadric();
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("Oswietlenie dwustronne");
- ax = 10.0;
- ay = -10.0;
- az = 0.0;
- initLightAndMaterial();
- glutDisplayFunc(drawScene);
- glutReshapeFunc(resize);
- glutCreateMenu(menu);
- glutAddMenuEntry("Motion", 3);
- glutAddMenuEntry("Two-sided lighting", 1);
- glutAddMenuEntry("One-sided lighting", 2);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
- void drawScene(void)
- {
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- gluQuadricDrawStyle(quadObj, GLU_FILL);
- glColor3f(1.0, 1.0, 1.0);
- glRotatef(ax, 1.0, 0.0, 0.0);
- glRotatef(-ay, 0.0, 1.0, 0.0);
- gluCylinder(quadObj, 10.0, 7.0, 15.0, 45, 9); //
- glPopMatrix();
- glutSwapBuffers();
- }
- void setMatrix(void)
- {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-15.0, 15.0, -15.0, 15.0, -10.0, 10.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int count = 0;
- void animation(void)
- {
- ax += 5.0;
- ay -= 2.0;
- az += 5.0;
- if (ax >= 360)
- ax = 0.0;
- if (ay <= -360)
- ay = 0.0;
- if (az >= 360)
- az = 0.0;
- drawScene();
- count++;
- if (count >= 60)
- glutIdleFunc(NULL);
- }
- void keyboard(unsigned char c, int x, int y)
- {
- switch (c)
- {
- case 27:
- exit(0);
- break;
- case 108:
- if (isLighting)
- {
- glDisable(GL_LIGHTING);
- glDisable(GL_LIGHTING);
- }
- else
- {
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHTING);
- }
- isLighting = !isLighting;
- glEnable(GL_LIGHT0);
- default:
- break;
- }
- }
- void menu(int choice)
- {
- switch (choice)
- {
- case 3:
- count = 0;
- glutIdleFunc(animation);
- break;
- case 2:
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_oneside);
- glutSetWindowTitle("Jednostronne oswietlenie");
- glutPostRedisplay();
- break;
- case 1:
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
- glutSetWindowTitle("Dwustronne oswietlenie");
- glutPostRedisplay();
- break;
- }
- }
- void resize(int w, int h)
- {
- glViewport(0, 0, w, h);
- setMatrix();
- }
- void initLightAndMaterial(void)
- {
- static float ambient[] = {0.25, 0.25, 0.25, 1.0};
- static float diffuse[] = {0.5, 1.0, 1.0, 1.0};
- static float position[] = {90.0, 90.0, 150.0, 0.0};
- static float front_mat_shininess[] = {76.8};
- static float front_mat_specular[] {0.77, 0.77, 0.77, 1.0};
- static float front_mat_diffuse[] {0.4, 0.4, 0.4, 1.0};
- static float back_mat_shininess[] = {90.0};
- static float back_mat_specular[] = {0.5, 0.5, 0.5, 1.0};
- static float back_mat_diffuse[] = {1.0, 0.9, 0.9, 1.0};
- static float lmodel_ambient[] = {1.0, 1.0, 1.0, 1.0};
- setMatrix();
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, position);
- glMaterialfv(GL_FRONT, GL_SHININESS, front_mat_shininess);
- glMaterialfv(GL_FRONT, GL_SPECULAR, front_mat_specular);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, front_mat_diffuse);
- glMaterialfv(GL_BACK, GL_SHININESS, back_mat_shininess);
- glMaterialfv(GL_BACK, GL_SPECULAR, back_mat_specular);
- glMaterialfv(GL_BACK, GL_DIFFUSE, back_mat_diffuse);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
- glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glShadeModel(GL_SMOOTH);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement