Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/angle
Path: blob/main_old/samples/texture_wrap/TextureWrap.cpp
2573 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 TextureWrap.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 TextureWrapSample : public SampleApplication
22
{
23
public:
24
TextureWrapSample(int argc, char **argv) : SampleApplication("TextureWrap", argc, argv) {}
25
26
bool initialize() override
27
{
28
constexpr char kVS[] = R"(uniform float u_offset;
29
attribute vec4 a_position;
30
attribute vec2 a_texCoord;
31
varying vec2 v_texCoord;
32
void main()
33
{
34
gl_Position = a_position;
35
gl_Position.x += u_offset;
36
v_texCoord = a_texCoord;
37
})";
38
39
constexpr char kFS[] = R"(precision mediump float;
40
varying vec2 v_texCoord;
41
uniform sampler2D s_texture;
42
void main()
43
{
44
gl_FragColor = texture2D(s_texture, v_texCoord);
45
})";
46
47
mProgram = CompileProgram(kVS, kFS);
48
if (!mProgram)
49
{
50
return false;
51
}
52
53
// Get the attribute locations
54
mPositionLoc = glGetAttribLocation(mProgram, "a_position");
55
mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord");
56
57
// Get the sampler location
58
mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");
59
60
// Get the offset location
61
mOffsetLoc = glGetUniformLocation(mProgram, "u_offset");
62
63
// Load the texture
64
mTexture = CreateMipMappedTexture2D();
65
66
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
67
68
return true;
69
}
70
71
void destroy() override { glDeleteProgram(mProgram); }
72
73
void draw() override
74
{
75
GLfloat vertices[] = {
76
-0.3f, 0.3f, 0.0f, 1.0f, // Position 0
77
-1.0f, -1.0f, // TexCoord 0
78
-0.3f, -0.3f, 0.0f, 1.0f, // Position 1
79
-1.0f, 2.0f, // TexCoord 1
80
0.3f, -0.3f, 0.0f, 1.0f, // Position 2
81
2.0f, 2.0f, // TexCoord 2
82
0.3f, 0.3f, 0.0f, 1.0f, // Position 3
83
2.0f, -1.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, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices);
98
glEnableVertexAttribArray(mPositionLoc);
99
100
// Load the texture coordinate
101
glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),
102
vertices + 4);
103
glEnableVertexAttribArray(mTexCoordLoc);
104
105
// Set the sampler texture unit to 0
106
glUniform1i(mSamplerLoc, 0);
107
108
// Draw quad with repeat wrap mode
109
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
110
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
111
glUniform1f(mOffsetLoc, -0.7f);
112
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
113
114
// Draw quad with clamp to edge wrap mode
115
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
116
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
117
glUniform1f(mOffsetLoc, 0.0f);
118
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
119
120
// Draw quad with mirrored repeat
121
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);
122
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);
123
glUniform1f(mOffsetLoc, 0.7f);
124
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
125
}
126
127
private:
128
// Handle to a program object
129
GLuint mProgram;
130
131
// Attribute locations
132
GLint mPositionLoc;
133
GLint mTexCoordLoc;
134
135
// Sampler location
136
GLint mSamplerLoc;
137
138
// Offset location
139
GLint mOffsetLoc;
140
141
// Texture handle
142
GLuint mTexture;
143
};
144
145
int main(int argc, char **argv)
146
{
147
TextureWrapSample app(argc, argv);
148
return app.run();
149
}
150
151