1 //
2 // Copyright 2021 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 CubeMapActivity.java from The Android Open Source Project ApiDemos
8 // https://android.googlesource.com/platform/development/+/refs/heads/master/samples/ApiDemos/src/com/example/android/apis/graphics/CubeMapActivity.java
9
10 #include "SampleApplication.h"
11 #include "torus.h"
12
13 class GLES1TorusLightingSample : public SampleApplication
14 {
15 public:
GLES1TorusLightingSample(int argc,char ** argv)16 GLES1TorusLightingSample(int argc, char **argv)
17 : SampleApplication("GLES1 Torus Lighting", argc, argv, 1, 0)
18 {}
19
initialize()20 bool initialize() override
21 {
22 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
23
24 glShadeModel(GL_SMOOTH);
25
26 GLfloat light_model_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
27 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
28 glEnable(GL_LIGHTING);
29 glEnable(GL_LIGHT0);
30
31 GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);
32
33 return true;
34 }
35
destroy()36 void destroy() override
37 {
38 glDeleteBuffers(1, &mVertexBuffer);
39 glDeleteBuffers(1, &mIndexBuffer);
40 }
41
draw()42 void draw() override
43 {
44 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
45 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
46
47 float ratio = (float)getWindow()->getWidth() / (float)getWindow()->getHeight();
48 glMatrixMode(GL_PROJECTION);
49 glLoadIdentity();
50 glFrustumf(-ratio, ratio, -1, 1, 1.0f, 20.0f);
51
52 glEnable(GL_DEPTH_TEST);
53 glMatrixMode(GL_MODELVIEW);
54 glLoadIdentity();
55
56 glPushMatrix();
57
58 GLfloat lightDir[] = {0.0f, 0.0f, 1.0f, 0.0f};
59 glLightfv(GL_LIGHT0, GL_POSITION, lightDir);
60 glPopMatrix();
61
62 glTranslatef(0, 0, -5);
63
64 glRotatef(mAngle, 0, 1, 0);
65 glRotatef(mAngle * 0.25f, 1, 0, 0);
66
67 glEnableClientState(GL_VERTEX_ARRAY);
68
69 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
70 glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), nullptr);
71
72 glEnableClientState(GL_NORMAL_ARRAY);
73 glNormalPointer(GL_FLOAT, 6 * sizeof(GLfloat),
74 reinterpret_cast<const void *>(3 * sizeof(GLfloat)));
75
76 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
77 glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
78 glDisableClientState(GL_VERTEX_ARRAY);
79 glDisableClientState(GL_NORMAL_ARRAY);
80 glBindBuffer(GL_ARRAY_BUFFER, 0);
81 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
82
83 mAngle++;
84 }
85
86 private:
87 GLuint mVertexBuffer;
88 GLuint mIndexBuffer;
89 GLsizei mIndexCount;
90 float mAngle = 0;
91 };
92
main(int argc,char ** argv)93 int main(int argc, char **argv)
94 {
95 GLES1TorusLightingSample app(argc, argv);
96 return app.run();
97 }
98