xref: /aosp_15_r20/external/angle/samples/gles1/SimpleTexture2D.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
1 //
2 // Copyright 2018 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/gles_loader_autogen.h"
20 #include "util/shader_utils.h"
21 
22 class GLES1SimpleTexture2DSample : public SampleApplication
23 {
24   public:
GLES1SimpleTexture2DSample(int argc,char ** argv)25     GLES1SimpleTexture2DSample(int argc, char **argv)
26         : SampleApplication("GLES1SimpleTexture2D", argc, argv, ClientType::ES1)
27     {}
28 
initialize()29     bool initialize() override
30     {
31         // Load the texture
32         mTexture = CreateSimpleTexture2D();
33 
34         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
35         glEnable(GL_TEXTURE_2D);
36 
37         return true;
38     }
39 
destroy()40     void destroy() override { glDeleteTextures(1, &mTexture); }
41 
draw()42     void draw() override
43     {
44         GLfloat vertices[] = {
45             -0.5f, 0.5f,  0.0f,  // Position 0
46             0.0f,  0.0f,         // TexCoord 0
47             -0.5f, -0.5f, 0.0f,  // Position 1
48             0.0f,  1.0f,         // TexCoord 1
49             0.5f,  -0.5f, 0.0f,  // Position 2
50             1.0f,  1.0f,         // TexCoord 2
51             0.5f,  0.5f,  0.0f,  // Position 3
52             1.0f,  0.0f          // TexCoord 3
53         };
54         GLushort indices[] = {0, 1, 2, 0, 2, 3};
55 
56         // Set the viewport
57         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
58 
59         // Clear the color buffer
60         glClear(GL_COLOR_BUFFER_BIT);
61 
62         // Load the vertex position
63         glVertexPointer(3, GL_FLOAT, 5 * sizeof(GLfloat), vertices);
64         // Load the texture coordinate
65         glTexCoordPointer(2, GL_FLOAT, 5 * sizeof(GLfloat), vertices + 3);
66 
67         glEnableClientState(GL_VERTEX_ARRAY);
68         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
69 
70         // Bind the texture
71         glActiveTexture(GL_TEXTURE0);
72         glBindTexture(GL_TEXTURE_2D, mTexture);
73 
74         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);
75     }
76 
77   private:
78     // Texture handle
79     GLuint mTexture = 0;
80 };
81 
main(int argc,char ** argv)82 int main(int argc, char **argv)
83 {
84     GLES1SimpleTexture2DSample app(argc, argv);
85     return app.run();
86 }
87