Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/samples/simple_texture_2d/SimpleTexture2D.cpp
1694 views
1
//
2
// Copyright 2014 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 Simple_Texture2D.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 "texture_utils.h"
19
#include "util/shader_utils.h"
20
21
class SimpleTexture2DSample : public SampleApplication
22
{
23
public:
24
SimpleTexture2DSample(int argc, char **argv) : SampleApplication("SimpleTexture2D", argc, argv)
25
{}
26
27
bool initialize() override
28
{
29
constexpr char kVS[] = R"(attribute vec4 a_position;
30
attribute vec2 a_texCoord;
31
varying vec2 v_texCoord;
32
void main()
33
{
34
gl_Position = a_position;
35
v_texCoord = a_texCoord;
36
})";
37
38
constexpr char kFS[] = R"(precision mediump float;
39
varying vec2 v_texCoord;
40
uniform sampler2D s_texture;
41
void main()
42
{
43
gl_FragColor = texture2D(s_texture, v_texCoord);
44
})";
45
46
mProgram = CompileProgram(kVS, kFS);
47
if (!mProgram)
48
{
49
return false;
50
}
51
52
// Get the attribute locations
53
mPositionLoc = glGetAttribLocation(mProgram, "a_position");
54
mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord");
55
56
// Get the sampler location
57
mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");
58
59
// Load the texture
60
mTexture = CreateSimpleTexture2D();
61
62
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
63
64
return true;
65
}
66
67
void destroy() override
68
{
69
glDeleteProgram(mProgram);
70
glDeleteTextures(1, &mTexture);
71
}
72
73
void draw() override
74
{
75
GLfloat vertices[] = {
76
-0.5f, 0.5f, 0.0f, // Position 0
77
0.0f, 0.0f, // TexCoord 0
78
-0.5f, -0.5f, 0.0f, // Position 1
79
0.0f, 1.0f, // TexCoord 1
80
0.5f, -0.5f, 0.0f, // Position 2
81
1.0f, 1.0f, // TexCoord 2
82
0.5f, 0.5f, 0.0f, // Position 3
83
1.0f, 0.0f // TexCoord 3
84
};
85
GLushort indices[] = {0, 1, 2, 0, 2, 3};
86
87
// Set the viewport
88
glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
89
90
// Clear the color buffer
91
glClear(GL_COLOR_BUFFER_BIT);
92
93
// Use the program object
94
glUseProgram(mProgram);
95
96
// Load the vertex position
97
glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices);
98
// Load the texture coordinate
99
glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
100
vertices + 3);
101
102
glEnableVertexAttribArray(mPositionLoc);
103
glEnableVertexAttribArray(mTexCoordLoc);
104
105
// Bind the texture
106
glActiveTexture(GL_TEXTURE0);
107
glBindTexture(GL_TEXTURE_2D, mTexture);
108
109
// Set the texture sampler to texture unit to 0
110
glUniform1i(mSamplerLoc, 0);
111
112
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
113
}
114
115
private:
116
// Handle to a program object
117
GLuint mProgram;
118
119
// Attribute locations
120
GLint mPositionLoc;
121
GLint mTexCoordLoc;
122
123
// Sampler location
124
GLint mSamplerLoc;
125
126
// Texture handle
127
GLuint mTexture;
128
};
129
130
int main(int argc, char **argv)
131
{
132
SimpleTexture2DSample app(argc, argv);
133
return app.run();
134
}
135
136