-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathexample6.cpp
238 lines (186 loc) · 6.33 KB
/
example6.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// fragment shading of sphere model
#include "Angel.h"
const int NumTimesToSubdivide = 5;
const int NumTriangles = 4096; // (4 faces)^(NumTimesToSubdivide + 1)
const int NumVertices = 3 * NumTriangles;
typedef Angel::vec4 point4;
typedef Angel::vec4 color4;
point4 points[NumVertices];
vec3 normals[NumVertices];
// Model-view and projection matrices uniform location
GLuint ModelView, Projection;
//----------------------------------------------------------------------------
int Index = 0;
void
triangle( const point4& a, const point4& b, const point4& c )
{
vec3 normal = normalize( cross(b - a, c - b) );
normals[Index] = normal; points[Index] = a; Index++;
normals[Index] = normal; points[Index] = b; Index++;
normals[Index] = normal; points[Index] = c; Index++;
}
//----------------------------------------------------------------------------
point4
unit( const point4& p )
{
float len = p.x*p.x + p.y*p.y + p.z*p.z;
point4 t;
if ( len > DivideByZeroTolerance ) {
t = p / sqrt(len);
t.w = 1.0;
}
return t;
}
void
divide_triangle( const point4& a, const point4& b,
const point4& c, int count )
{
if ( count > 0 ) {
point4 v1 = unit( a + b );
point4 v2 = unit( a + c );
point4 v3 = unit( b + c );
divide_triangle( a, v1, v2, count - 1 );
divide_triangle( c, v2, v3, count - 1 );
divide_triangle( b, v3, v1, count - 1 );
divide_triangle( v1, v3, v2, count - 1 );
}
else {
triangle( a, b, c );
}
}
void
tetrahedron( int count )
{
point4 v[4] = {
vec4( 0.0, 0.0, 1.0, 1.0 ),
vec4( 0.0, 0.942809, -0.333333, 1.0 ),
vec4( -0.816497, -0.471405, -0.333333, 1.0 ),
vec4( 0.816497, -0.471405, -0.333333, 1.0 )
};
divide_triangle( v[0], v[1], v[2], count );
divide_triangle( v[3], v[2], v[1], count );
divide_triangle( v[0], v[3], v[1], count );
divide_triangle( v[0], v[2], v[3], count );
}
//----------------------------------------------------------------------------
// OpenGL initialization
void
init()
{
// Subdivide a tetrahedron into a sphere
tetrahedron( NumTimesToSubdivide );
// Create a vertex array object
GLuint vao;
glGenVertexArrays( 1, &vao );
glBindVertexArray( vao );
// Create and initialize a buffer object
GLuint buffer;
glGenBuffers( 1, &buffer );
glBindBuffer( GL_ARRAY_BUFFER, buffer );
glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals),
NULL, GL_STATIC_DRAW );
glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points );
glBufferSubData( GL_ARRAY_BUFFER, sizeof(points),
sizeof(normals), normals );
// Load shaders and use the resulting shader program
GLuint program = InitShader( "vshader56.glsl", "fshader56.glsl" );
glUseProgram( program );
// set up vertex arrays
GLuint vPosition = glGetAttribLocation( program, "vPosition" );
glEnableVertexAttribArray( vPosition );
glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(0) );
GLuint vNormal = glGetAttribLocation( program, "vNormal" );
glEnableVertexAttribArray( vNormal );
glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0,
BUFFER_OFFSET(sizeof(points)) );
// Initialize shader lighting parameters
point4 light_position( 0.0, 0.0, 2.0, 0.0 );
color4 light_ambient( 0.2, 0.2, 0.2, 1.0 );
color4 light_diffuse( 1.0, 1.0, 1.0, 1.0 );
color4 light_specular( 1.0, 1.0, 1.0, 1.0 );
color4 material_ambient( 1.0, 0.0, 1.0, 1.0 );
color4 material_diffuse( 1.0, 0.8, 0.0, 1.0 );
color4 material_specular( 1.0, 0.0, 1.0, 1.0 );
float material_shininess = 5.0;
color4 ambient_product = light_ambient * material_ambient;
color4 diffuse_product = light_diffuse * material_diffuse;
color4 specular_product = light_specular * material_specular;
glUniform4fv( glGetUniformLocation(program, "AmbientProduct"),
1, ambient_product );
glUniform4fv( glGetUniformLocation(program, "DiffuseProduct"),
1, diffuse_product );
glUniform4fv( glGetUniformLocation(program, "SpecularProduct"),
1, specular_product );
glUniform4fv( glGetUniformLocation(program, "LightPosition"),
1, light_position );
glUniform1f( glGetUniformLocation(program, "Shininess"),
material_shininess );
// Retrieve transformation uniform variable locations
ModelView = glGetUniformLocation( program, "ModelView" );
Projection = glGetUniformLocation( program, "Projection" );
glEnable( GL_DEPTH_TEST );
glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */
}
//----------------------------------------------------------------------------
void
display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
point4 at( 0.0, 0.0, 0.0, 1.0 );
point4 eye( 0.0, 0.0, 2.0, 1.0 );
vec4 up( 0.0, 1.0, 0.0, 0.0 );
mat4 model_view = LookAt( eye, at, up );
glUniformMatrix4fv( ModelView, 1, GL_TRUE, model_view );
glDrawArrays( GL_TRIANGLES, 0, NumVertices );
glutSwapBuffers();
}
//----------------------------------------------------------------------------
void
keyboard( unsigned char key, int x, int y )
{
switch( key ) {
case 033: // Escape Key
case 'q': case 'Q':
exit( EXIT_SUCCESS );
break;
}
}
//----------------------------------------------------------------------------
void
reshape( int width, int height )
{
glViewport( 0, 0, width, height );
GLfloat left = -2.0, right = 2.0;
GLfloat top = 2.0, bottom = -2.0;
GLfloat zNear = -20.0, zFar = 20.0;
GLfloat aspect = GLfloat(width)/height;
if ( aspect > 1.0 ) {
left *= aspect;
right *= aspect;
}
else {
top /= aspect;
bottom /= aspect;
}
mat4 projection = Ortho( left, right, bottom, top, zNear, zFar );
glUniformMatrix4fv( Projection, 1, GL_TRUE, projection );
}
//----------------------------------------------------------------------------
int
main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH );
glutInitWindowSize( 512, 512 );
glutInitContextVersion( 3, 2 );
glutInitContextProfile( GLUT_CORE_PROFILE );
glutCreateWindow( "Sphere" );
glewInit();
init();
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMainLoop();
return 0;
}