include stdlib include GL glut include Windows include iostream using

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include <stdlib.h>
#include <GL/glut.h>
#include <Windows.h>
#include <iostream>
using namespace std;
static int window;
int LightType = 1;
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_LIGHTING);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
glEnable(GL_NORMALIZE); // автоматическое приведение нормалей к единичной длине
}
void reshape(int width, int height)
{
glViewport(0, 0, width, height); // двухмерное окно вывода
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width <= height) glOrtho(-1.5, 1.5, -1.5*(GLfloat)height/(GLfloat)width, 1.5*(GLfloat)height/(GLfloat)width, -10.0, 10.0);
else glOrtho(-1.5*(GLfloat)width/(GLfloat)height, 1.5*(GLfloat)width/(GLfloat)height, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display(void)
{
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
if (LightType == 1) //направленный источник света
{
GLfloat Light0_diffuse[] = { 1.0, 0.0, 1.0, 1.0 };
GLfloat Light0_position[] = {- 1.0, 1.0, 1.0, 1.0 };
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, Light0_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, Light0_position);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
if (LightType == 2) //точечный источник света
{
GLfloat Light1_diffuse[] = {0.0, 1.0, 1.0, 1.0 };
GLfloat Light1_position[] = { -1.0, 1.0, 1.0, 1.0 };
glEnable(GL_LIGHT1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, Light1_diffuse);
glLightfv(GL_LIGHT1, GL_POSITION, Light1_position);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
if (LightType == 3) //точечный источник света с убыванием интенсивности
{
GLfloat Light2_diffuse[] = {0.0, 1.0, 1.0, 1.0 };
GLfloat Light2_position[] = { -1.0, 1.0, 1.0, 1.0 };
glEnable(GL_LIGHT2);
glLightfv(GL_LIGHT2, GL_DIFFUSE, Light2_diffuse);
glLightfv(GL_LIGHT2, GL_POSITION, Light2_position);
glLightf(GL_LIGHT2, GL_CONSTANT_ATTENUATION, 0.8);
glLightf(GL_LIGHT2, GL_LINEAR_ATTENUATION, 0.5);
glLightf(GL_LIGHT2, GL_QUADRATIC_ATTENUATION, 1.0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
if (LightType == 4) //несколько источников света
{
GLfloat Light3_diffuse[] = { 1.0, 0.0, 0.0, 1.0 };
GLfloat Light3_position[] = { 1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT3, GL_DIFFUSE, Light3_diffuse);
glLightfv(GL_LIGHT3, GL_POSITION, Light3_position);
glLightf(GL_LIGHT3, GL_CONSTANT_ATTENUATION, 0.0);
glLightf(GL_LIGHT3, GL_LINEAR_ATTENUATION, 0.4);
glLightf(GL_LIGHT3, GL_QUADRATIC_ATTENUATION, 0.8);
glEnable(GL_LIGHT3);
GLfloat Light5_diffuse[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat Light5_position[] = { -1.0, 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT5, GL_DIFFUSE, Light5_diffuse);
glLightfv(GL_LIGHT5, GL_POSITION, Light5_position);
glLightf(GL_LIGHT5, GL_CONSTANT_ATTENUATION, 0.0);
glLightf(GL_LIGHT5, GL_LINEAR_ATTENUATION, 0.4);
glLightf(GL_LIGHT5, GL_QUADRATIC_ATTENUATION, 0.8);
glEnable(GL_LIGHT5);
GLfloat Light4_diffuse[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat Light4_position[] = { -1.0, -1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT4, GL_DIFFUSE, Light4_diffuse);
glLightfv(GL_LIGHT4, GL_POSITION, Light4_position);
glLightf(GL_LIGHT4, GL_CONSTANT_ATTENUATION, 0.0);
glLightf(GL_LIGHT4, GL_LINEAR_ATTENUATION, 0.4);
glLightf(GL_LIGHT4, GL_QUADRATIC_ATTENUATION, 0.8);
glEnable(GL_LIGHT4);
GLfloat Light6_diffuse[] = {0.0, 1.0, 1.0, 1.0 };
GLfloat Light6_position[] = { 1.0, -1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT6, GL_DIFFUSE, Light6_diffuse);
glLightfv(GL_LIGHT6, GL_POSITION, Light6_position);
glLightf(GL_LIGHT6, GL_CONSTANT_ATTENUATION, 0.0);
glLightf(GL_LIGHT6, GL_LINEAR_ATTENUATION, 0.4);
glLightf(GL_LIGHT6, GL_QUADRATIC_ATTENUATION, 0.8);
glEnable(GL_LIGHT6);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
}
if (LightType == 5) //рассеянный источник света
{
GLfloat Light7_ambient[] = { 0.0, 0.0, 1.0, 1.0 };
GLfloat Light7_position[] = { 1.0, 1.0, 1.0, 1.0 };
glEnable(GL_LIGHT7);
glLightfv(GL_LIGHT7, GL_DIFFUSE, Light7_ambient);
glLightfv(GL_LIGHT7, GL_AMBIENT, Light7_position);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSolidSphere(1.0, 20, 16);
glFlush();
glDisable(GL_LIGHT0);
glDisable(GL_LIGHT1);
glDisable(GL_LIGHT2);
glDisable(GL_LIGHT3);
glDisable(GL_LIGHT4);
glDisable(GL_LIGHT5);
glDisable(GL_LIGHT6);
glDisable(GL_LIGHT7);
glutSwapBuffers();
}
void MenuCheck (int v )
{
if(v == 0)
{
glutDestroyWindow(window);
exit(0);
}
else
{
LightType = v;
}
glutPostRedisplay();
}
void SubMenuCheck (int v)
{
if(v == 0)
{
glutDestroyWindow(window);
exit(0);
}
else
{
LightType = v;
}
glutPostRedisplay();
}
void MenuInit()
{
int M=glutCreateMenu(MenuCheck);
int Msub=glutCreateMenu(SubMenuCheck);
glutSetMenu(M);
glutAddMenuEntry ("Включить направленный источник света",1);
glutAddSubMenu("Точечный источник", Msub);
glutSetMenu(Msub);
glutAddMenuEntry ("Включит точечный источник света",2);
glutAddMenuEntry ("Точечный источник с убыванием интенсивности",3);
glutSetMenu(M);
glutAddMenuEntry ("Включить несколько источников света",4);
glutAddMenuEntry ("Включить рассеянный источник света",5);
glutAttachMenu(GLUT_LEFT_BUTTON);
}
int main(int argc, char** argv)
{
setlocale (LC_ALL, "rus");
cout <<"Нажмите левую кнопку мышки";
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (200, 200);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
MenuInit();
glutMainLoop();
return 0;
}