xref: /aosp_15_r20/external/angle/samples/simple_texture_2d/SimpleTexture2D.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1*8975f5c5SAndroid Build Coastguard Worker //
2*8975f5c5SAndroid Build Coastguard Worker // Copyright 2014 The ANGLE Project Authors. All rights reserved.
3*8975f5c5SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
4*8975f5c5SAndroid Build Coastguard Worker // found in the LICENSE file.
5*8975f5c5SAndroid Build Coastguard Worker //
6*8975f5c5SAndroid Build Coastguard Worker 
7*8975f5c5SAndroid Build Coastguard Worker //            Based on Simple_Texture2D.c from
8*8975f5c5SAndroid Build Coastguard Worker // Book:      OpenGL(R) ES 2.0 Programming Guide
9*8975f5c5SAndroid Build Coastguard Worker // Authors:   Aaftab Munshi, Dan Ginsburg, Dave Shreiner
10*8975f5c5SAndroid Build Coastguard Worker // ISBN-10:   0321502795
11*8975f5c5SAndroid Build Coastguard Worker // ISBN-13:   9780321502797
12*8975f5c5SAndroid Build Coastguard Worker // Publisher: Addison-Wesley Professional
13*8975f5c5SAndroid Build Coastguard Worker // URLs:      http://safari.informit.com/9780321563835
14*8975f5c5SAndroid Build Coastguard Worker //            http://www.opengles-book.com
15*8975f5c5SAndroid Build Coastguard Worker 
16*8975f5c5SAndroid Build Coastguard Worker #include "SampleApplication.h"
17*8975f5c5SAndroid Build Coastguard Worker 
18*8975f5c5SAndroid Build Coastguard Worker #include "texture_utils.h"
19*8975f5c5SAndroid Build Coastguard Worker #include "util/shader_utils.h"
20*8975f5c5SAndroid Build Coastguard Worker 
21*8975f5c5SAndroid Build Coastguard Worker class SimpleTexture2DSample : public SampleApplication
22*8975f5c5SAndroid Build Coastguard Worker {
23*8975f5c5SAndroid Build Coastguard Worker   public:
SimpleTexture2DSample(int argc,char ** argv)24*8975f5c5SAndroid Build Coastguard Worker     SimpleTexture2DSample(int argc, char **argv) : SampleApplication("SimpleTexture2D", argc, argv)
25*8975f5c5SAndroid Build Coastguard Worker     {}
26*8975f5c5SAndroid Build Coastguard Worker 
initialize()27*8975f5c5SAndroid Build Coastguard Worker     bool initialize() override
28*8975f5c5SAndroid Build Coastguard Worker     {
29*8975f5c5SAndroid Build Coastguard Worker         constexpr char kVS[] = R"(attribute vec4 a_position;
30*8975f5c5SAndroid Build Coastguard Worker attribute vec2 a_texCoord;
31*8975f5c5SAndroid Build Coastguard Worker varying vec2 v_texCoord;
32*8975f5c5SAndroid Build Coastguard Worker void main()
33*8975f5c5SAndroid Build Coastguard Worker {
34*8975f5c5SAndroid Build Coastguard Worker     gl_Position = a_position;
35*8975f5c5SAndroid Build Coastguard Worker     v_texCoord = a_texCoord;
36*8975f5c5SAndroid Build Coastguard Worker })";
37*8975f5c5SAndroid Build Coastguard Worker 
38*8975f5c5SAndroid Build Coastguard Worker         constexpr char kFS[] = R"(precision mediump float;
39*8975f5c5SAndroid Build Coastguard Worker varying vec2 v_texCoord;
40*8975f5c5SAndroid Build Coastguard Worker uniform sampler2D s_texture;
41*8975f5c5SAndroid Build Coastguard Worker void main()
42*8975f5c5SAndroid Build Coastguard Worker {
43*8975f5c5SAndroid Build Coastguard Worker     gl_FragColor = texture2D(s_texture, v_texCoord);
44*8975f5c5SAndroid Build Coastguard Worker })";
45*8975f5c5SAndroid Build Coastguard Worker 
46*8975f5c5SAndroid Build Coastguard Worker         mProgram = CompileProgram(kVS, kFS);
47*8975f5c5SAndroid Build Coastguard Worker         if (!mProgram)
48*8975f5c5SAndroid Build Coastguard Worker         {
49*8975f5c5SAndroid Build Coastguard Worker             return false;
50*8975f5c5SAndroid Build Coastguard Worker         }
51*8975f5c5SAndroid Build Coastguard Worker 
52*8975f5c5SAndroid Build Coastguard Worker         // Get the attribute locations
53*8975f5c5SAndroid Build Coastguard Worker         mPositionLoc = glGetAttribLocation(mProgram, "a_position");
54*8975f5c5SAndroid Build Coastguard Worker         mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord");
55*8975f5c5SAndroid Build Coastguard Worker 
56*8975f5c5SAndroid Build Coastguard Worker         // Get the sampler location
57*8975f5c5SAndroid Build Coastguard Worker         mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");
58*8975f5c5SAndroid Build Coastguard Worker 
59*8975f5c5SAndroid Build Coastguard Worker         // Load the texture
60*8975f5c5SAndroid Build Coastguard Worker         mTexture = CreateSimpleTexture2D();
61*8975f5c5SAndroid Build Coastguard Worker 
62*8975f5c5SAndroid Build Coastguard Worker         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
63*8975f5c5SAndroid Build Coastguard Worker 
64*8975f5c5SAndroid Build Coastguard Worker         return true;
65*8975f5c5SAndroid Build Coastguard Worker     }
66*8975f5c5SAndroid Build Coastguard Worker 
destroy()67*8975f5c5SAndroid Build Coastguard Worker     void destroy() override
68*8975f5c5SAndroid Build Coastguard Worker     {
69*8975f5c5SAndroid Build Coastguard Worker         glDeleteProgram(mProgram);
70*8975f5c5SAndroid Build Coastguard Worker         glDeleteTextures(1, &mTexture);
71*8975f5c5SAndroid Build Coastguard Worker     }
72*8975f5c5SAndroid Build Coastguard Worker 
draw()73*8975f5c5SAndroid Build Coastguard Worker     void draw() override
74*8975f5c5SAndroid Build Coastguard Worker     {
75*8975f5c5SAndroid Build Coastguard Worker         GLfloat vertices[] = {
76*8975f5c5SAndroid Build Coastguard Worker             -0.5f, 0.5f,  0.0f,  // Position 0
77*8975f5c5SAndroid Build Coastguard Worker             0.0f,  0.0f,         // TexCoord 0
78*8975f5c5SAndroid Build Coastguard Worker             -0.5f, -0.5f, 0.0f,  // Position 1
79*8975f5c5SAndroid Build Coastguard Worker             0.0f,  1.0f,         // TexCoord 1
80*8975f5c5SAndroid Build Coastguard Worker             0.5f,  -0.5f, 0.0f,  // Position 2
81*8975f5c5SAndroid Build Coastguard Worker             1.0f,  1.0f,         // TexCoord 2
82*8975f5c5SAndroid Build Coastguard Worker             0.5f,  0.5f,  0.0f,  // Position 3
83*8975f5c5SAndroid Build Coastguard Worker             1.0f,  0.0f          // TexCoord 3
84*8975f5c5SAndroid Build Coastguard Worker         };
85*8975f5c5SAndroid Build Coastguard Worker         GLushort indices[] = {0, 1, 2, 0, 2, 3};
86*8975f5c5SAndroid Build Coastguard Worker 
87*8975f5c5SAndroid Build Coastguard Worker         // Set the viewport
88*8975f5c5SAndroid Build Coastguard Worker         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
89*8975f5c5SAndroid Build Coastguard Worker 
90*8975f5c5SAndroid Build Coastguard Worker         // Clear the color buffer
91*8975f5c5SAndroid Build Coastguard Worker         glClear(GL_COLOR_BUFFER_BIT);
92*8975f5c5SAndroid Build Coastguard Worker 
93*8975f5c5SAndroid Build Coastguard Worker         // Use the program object
94*8975f5c5SAndroid Build Coastguard Worker         glUseProgram(mProgram);
95*8975f5c5SAndroid Build Coastguard Worker 
96*8975f5c5SAndroid Build Coastguard Worker         // Load the vertex position
97*8975f5c5SAndroid Build Coastguard Worker         glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices);
98*8975f5c5SAndroid Build Coastguard Worker         // Load the texture coordinate
99*8975f5c5SAndroid Build Coastguard Worker         glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
100*8975f5c5SAndroid Build Coastguard Worker                               vertices + 3);
101*8975f5c5SAndroid Build Coastguard Worker 
102*8975f5c5SAndroid Build Coastguard Worker         glEnableVertexAttribArray(mPositionLoc);
103*8975f5c5SAndroid Build Coastguard Worker         glEnableVertexAttribArray(mTexCoordLoc);
104*8975f5c5SAndroid Build Coastguard Worker 
105*8975f5c5SAndroid Build Coastguard Worker         // Bind the texture
106*8975f5c5SAndroid Build Coastguard Worker         glActiveTexture(GL_TEXTURE0);
107*8975f5c5SAndroid Build Coastguard Worker         glBindTexture(GL_TEXTURE_2D, mTexture);
108*8975f5c5SAndroid Build Coastguard Worker 
109*8975f5c5SAndroid Build Coastguard Worker         // Set the texture sampler to texture unit to 0
110*8975f5c5SAndroid Build Coastguard Worker         glUniform1i(mSamplerLoc, 0);
111*8975f5c5SAndroid Build Coastguard Worker 
112*8975f5c5SAndroid Build Coastguard Worker         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
113*8975f5c5SAndroid Build Coastguard Worker     }
114*8975f5c5SAndroid Build Coastguard Worker 
115*8975f5c5SAndroid Build Coastguard Worker   private:
116*8975f5c5SAndroid Build Coastguard Worker     // Handle to a program object
117*8975f5c5SAndroid Build Coastguard Worker     GLuint mProgram;
118*8975f5c5SAndroid Build Coastguard Worker 
119*8975f5c5SAndroid Build Coastguard Worker     // Attribute locations
120*8975f5c5SAndroid Build Coastguard Worker     GLint mPositionLoc;
121*8975f5c5SAndroid Build Coastguard Worker     GLint mTexCoordLoc;
122*8975f5c5SAndroid Build Coastguard Worker 
123*8975f5c5SAndroid Build Coastguard Worker     // Sampler location
124*8975f5c5SAndroid Build Coastguard Worker     GLint mSamplerLoc;
125*8975f5c5SAndroid Build Coastguard Worker 
126*8975f5c5SAndroid Build Coastguard Worker     // Texture handle
127*8975f5c5SAndroid Build Coastguard Worker     GLuint mTexture;
128*8975f5c5SAndroid Build Coastguard Worker };
129*8975f5c5SAndroid Build Coastguard Worker 
main(int argc,char ** argv)130*8975f5c5SAndroid Build Coastguard Worker int main(int argc, char **argv)
131*8975f5c5SAndroid Build Coastguard Worker {
132*8975f5c5SAndroid Build Coastguard Worker     SimpleTexture2DSample app(argc, argv);
133*8975f5c5SAndroid Build Coastguard Worker     return app.run();
134*8975f5c5SAndroid Build Coastguard Worker }
135