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