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
12 #include "torus.h"
13 #include "util/Matrix.h"
14 #include "util/shader_utils.h"
15
16 const float kDegreesPerSecond = 90.0f;
17
18 class GLES2TorusLightingSample : public SampleApplication
19 {
20 public:
GLES2TorusLightingSample(int argc,char ** argv)21 GLES2TorusLightingSample(int argc, char **argv)
22 : SampleApplication("GLES2 Torus Lighting", argc, argv)
23 {}
24
initialize()25 bool initialize() override
26 {
27 constexpr char kVS[] = R"(uniform mat4 mv;
28 uniform mat4 mvp;
29
30 attribute vec4 position;
31 attribute vec3 normal;
32
33 varying vec3 normal_view;
34
35 void main()
36 {
37 normal_view = vec3(mv * vec4(normal, 0.0));
38 gl_Position = mvp * position;
39 })";
40
41 constexpr char kFS[] = R"(precision mediump float;
42
43 varying vec3 normal_view;
44
45 void main() {
46 gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0) * dot(vec3(0.0, 0, 1.0), normalize(normal_view));
47 })";
48
49 mProgram = CompileProgram(kVS, kFS);
50 if (!mProgram)
51 {
52 return false;
53 }
54
55 mPositionLoc = glGetAttribLocation(mProgram, "position");
56 mNormalLoc = glGetAttribLocation(mProgram, "normal");
57
58 mMVPMatrixLoc = glGetUniformLocation(mProgram, "mvp");
59 mMVMatrixLoc = glGetUniformLocation(mProgram, "mv");
60
61 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
62 glEnable(GL_DEPTH_TEST);
63
64 GenerateTorus(&mVertexBuffer, &mIndexBuffer, &mIndexCount);
65
66 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
67 glUseProgram(mProgram);
68 glEnableVertexAttribArray(mPositionLoc);
69 glBindBuffer(GL_ARRAY_BUFFER, mVertexBuffer);
70 glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, false, 6 * sizeof(GLfloat), nullptr);
71
72 glVertexAttribPointer(mNormalLoc, 3, GL_FLOAT, false, 6 * sizeof(GLfloat),
73 reinterpret_cast<const void *>(3 * sizeof(GLfloat)));
74 glEnableVertexAttribArray(mNormalLoc);
75
76 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mIndexBuffer);
77
78 float ratio = static_cast<float>(getWindow()->getWidth()) /
79 static_cast<float>(getWindow()->getHeight());
80 mPerspectiveMatrix = Matrix4::frustum(-ratio, ratio, -1, 1, 1.0f, 20.0f);
81 mTranslationMatrix = Matrix4::translate(angle::Vector3(0, 0, -5));
82
83 return true;
84 }
85
destroy()86 void destroy() override
87 {
88 glDisableVertexAttribArray(mPositionLoc);
89 glDisableVertexAttribArray(mNormalLoc);
90 glBindBuffer(GL_ARRAY_BUFFER, 0);
91 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
92 glDeleteProgram(mProgram);
93 glDeleteBuffers(1, &mVertexBuffer);
94 glDeleteBuffers(1, &mIndexBuffer);
95 }
96
step(float dt,double totalTime)97 void step(float dt, double totalTime) override { mAngle += kDegreesPerSecond * dt; }
98
draw()99 void draw() override
100 {
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
102
103 Matrix4 modelMatrix = mTranslationMatrix * Matrix4::rotate(mAngle, mYUnitVec) *
104 Matrix4::rotate(mAngle * 0.25f, mXUnitVec);
105
106 Matrix4 mvpMatrix = mPerspectiveMatrix * modelMatrix;
107
108 glUniformMatrix4fv(mMVMatrixLoc, 1, GL_FALSE, modelMatrix.data);
109 glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);
110
111 glDrawElements(GL_TRIANGLES, mIndexCount, GL_UNSIGNED_SHORT, 0);
112 }
113
114 private:
115 GLuint mProgram = 0;
116
117 GLint mPositionLoc = 0;
118 GLint mNormalLoc = 0;
119
120 GLuint mMVPMatrixLoc = 0;
121 GLuint mMVMatrixLoc = 0;
122
123 GLuint mVertexBuffer = 0;
124 GLuint mIndexBuffer = 0;
125 GLsizei mIndexCount = 0;
126
127 float mAngle = 0;
128
129 Matrix4 mPerspectiveMatrix;
130 Matrix4 mTranslationMatrix;
131
132 const angle::Vector3 mYUnitVec{0.0f, 1.0f, 0.0f};
133 const angle::Vector3 mXUnitVec{1.0f, 0.0f, 0.0f};
134 };
135
main(int argc,char ** argv)136 int main(int argc, char **argv)
137 {
138 GLES2TorusLightingSample app(argc, argv);
139 return app.run();
140 }
141