1 //
2 // Copyright 2018 The ANGLE Project Authors. All rights reserved.
3 // Use of this source code is governed by a BSD-style license that can be
4 // found in the LICENSE file.
5 //
6
7 // Based on Hello_Triangle.c from
8 // Book: OpenGL(R) ES 2.0 Programming Guide
9 // Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner
10 // ISBN-10: 0321502795
11 // ISBN-13: 9780321502797
12 // Publisher: Addison-Wesley Professional
13 // URLs: http://safari.informit.com/9780321563835
14 // http://www.opengles-book.com
15
16 #include "SampleApplication.h"
17
18 #include <algorithm>
19
20 #include "util/gles_loader_autogen.h"
21
22 class SimpleLightingSample : public SampleApplication
23 {
24 public:
SimpleLightingSample(int argc,char ** argv)25 SimpleLightingSample(int argc, char **argv)
26 : SampleApplication("SimpleLightingSample", argc, argv, 1, 0)
27 {}
28
initialize()29 bool initialize() override
30 {
31 glClearColor(0.4f, 0.3f, 0.2f, 1.0f);
32 mRotDeg = 0.0f;
33
34 return true;
35 }
36
destroy()37 void destroy() override {}
38
draw()39 void draw() override
40 {
41
42 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
43 glColor4f(0.2f, 0.6f, 0.8f, 1.0f);
44
45 GLfloat mat_ambient[] = {0.7f, 0.4f, 0.2f, 1.0f};
46 GLfloat mat_specular[] = {0.5f, 0.5f, 0.5f, 1.0f};
47 GLfloat mat_diffuse[] = {0.3f, 0.4f, 0.6f, 1.0f};
48 GLfloat lightpos[] = {0.0f, 1.0f, 0.0f, 0.0f};
49
50 GLfloat normals[] = {
51 -0.4f, 0.4f, -0.4f, -0.4f, -0.4f, -0.4f, 0.2f, 0.0f, -0.4f,
52
53 -0.4f, 0.4f, 0.4f, -0.4f, -0.4f, 0.4f, 0.2f, 0.0f, 0.4f,
54 };
55
56 GLfloat vertices[] = {
57 -0.5f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, 0.0f, 0.0f,
58
59 -0.5f, 0.5f, 0.3f, -0.5f, -0.5f, 0.3f, 0.5f, 0.0f, 0.3f,
60 };
61
62 GLuint indices[] = {
63 0, 1, 2, 3, 4, 5,
64
65 0, 4, 3, 4, 0, 1,
66
67 4, 1, 2, 2, 5, 4,
68
69 5, 2, 3, 3, 2, 0,
70 };
71
72 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
73 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
74 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
75
76 glEnable(GL_DEPTH_TEST);
77 glEnable(GL_LIGHTING);
78 glEnable(GL_LIGHT0);
79 glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
80
81 glEnableClientState(GL_VERTEX_ARRAY);
82 glEnableClientState(GL_NORMAL_ARRAY);
83 glVertexPointer(3, GL_FLOAT, 0, vertices);
84 glNormalPointer(GL_FLOAT, 0, normals);
85
86 for (int i = 0; i < 3; i++)
87 {
88 for (int j = 0; j < 3; j++)
89 {
90 glPushMatrix();
91
92 glTranslatef(-0.6f + i * 0.6f, -0.6f + j * 0.6f, 0.0f);
93
94 glRotatef(mRotDeg + (10.0f * (3.0f * i + j)), 0.0f, 1.0f, 0.0f);
95 glRotatef(20.0f + (20.0f * (3.0f * i + j)), 1.0f, 0.0f, 0.0f);
96 GLfloat scale = 0.5;
97 glScalef(scale, scale, scale);
98 glDrawElements(GL_TRIANGLES, 3 * 8, GL_UNSIGNED_INT, indices);
99
100 glPopMatrix();
101 }
102 }
103
104 mRotDeg += 0.03f;
105 }
106
107 private:
108 float mRotDeg;
109 };
110
main(int argc,char ** argv)111 int main(int argc, char **argv)
112 {
113 SimpleLightingSample app(argc, argv);
114 return app.run();
115 }
116