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 const float kDegreesPerSecond = 90.0f;
14
15 class GLES1TorusLightingSample : public SampleApplication
16 {
17 public:
GLES1TorusLightingSample(int argc,char ** argv)18 GLES1TorusLightingSample(int argc, char **argv)
19 : SampleApplication("GLES1 Torus Lighting", argc, argv, ClientType::ES1)
20 {}
21
initialize()22 bool initialize() override
23 {
24 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
25 glEnable(GL_DEPTH_TEST);
26 glShadeModel(GL_SMOOTH);
27
28 glEnable(GL_LIGHTING);
29 GLfloat light_model_ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
30 glLightModelfv(GL_LIGHT_MODEL_AMBIENT, light_model_ambient);
31
32 glEnable(GL_LIGHT0);
33 GLfloat lightDir[] = {0.0f, 0.0f, 1.0f, 0.0f};
34 glLightfv(GL_LIGHT0, GL_POSITION, lightDir);
35
36 GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);
37
38 glEnableClientState(GL_VERTEX_ARRAY);
39
40 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
41 glVertexPointer(3, GL_FLOAT, 6 * sizeof(GLfloat), nullptr);
42
43 glEnableClientState(GL_NORMAL_ARRAY);
44 glNormalPointer(GL_FLOAT, 6 * sizeof(GLfloat),
45 reinterpret_cast<const void *>(3 * sizeof(GLfloat)));
46
47 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
48
49 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
50
51 float ratio = static_cast<float>(getWindow()->getWidth()) /
52 static_cast<float>(getWindow()->getHeight());
53 glMatrixMode(GL_PROJECTION);
54 glFrustumf(-ratio, ratio, -1, 1, 1.0f, 20.0f);
55
56 return true;
57 }
58
destroy()59 void destroy() override
60 {
61 glDisableClientState(GL_VERTEX_ARRAY);
62 glDisableClientState(GL_NORMAL_ARRAY);
63 glBindBuffer(GL_ARRAY_BUFFER, 0);
64 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
65 glDeleteBuffers(1, &mVertexBuffer);
66 glDeleteBuffers(1, &mIndexBuffer);
67 }
68
step(float dt,double totalTime)69 void step(float dt, double totalTime) override { mAngle += kDegreesPerSecond * dt; }
70
draw()71 void draw() override
72 {
73 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
74
75 glMatrixMode(GL_MODELVIEW);
76 glLoadIdentity();
77 glTranslatef(0, 0, -5);
78 glRotatef(mAngle, 0, 1, 0);
79 glRotatef(mAngle * 0.25f, 1, 0, 0);
80
81 glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
82 }
83
84 private:
85 GLuint mVertexBuffer = 0;
86 GLuint mIndexBuffer = 0;
87 GLsizei mIndexCount = 0;
88 float mAngle = 0;
89 };
90
main(int argc,char ** argv)91 int main(int argc, char **argv)
92 {
93 GLES1TorusLightingSample app(argc, argv);
94 return app.run();
95 }
96