xref: /aosp_15_r20/external/angle/samples/gles1/DrawTexture.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 #include "texture_utils.h"
18 #include "util/shader_utils.h"
19 #include "util/test_utils.h"
20 
21 #include <GLES/gl.h>
22 #include <GLES/glext.h>
23 
24 class GLES1DrawTextureSample : public SampleApplication
25 {
26   public:
GLES1DrawTextureSample(int argc,char ** argv)27     GLES1DrawTextureSample(int argc, char **argv)
28         : SampleApplication("GLES1DrawTexture", argc, argv, ClientType::ES1, 1280, 800)
29     {}
30 
initialize()31     bool initialize() override
32     {
33         // Load the texture
34         mTexture = CreateSimpleTexture2D();
35 
36         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
37         glActiveTexture(GL_TEXTURE0);
38         glEnable(GL_TEXTURE_2D);
39 
40         glActiveTexture(GL_TEXTURE0);
41         glEnable(GL_TEXTURE_2D);
42         glBindTexture(GL_TEXTURE_2D, mTexture);
43 
44         GLint crop[4] = {0, 0, 2, 2};
45         glTexParameteriv(GL_TEXTURE_2D, GL_TEXTURE_CROP_RECT_OES, crop);
46         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
47 
48         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
49 
50         return true;
51     }
52 
destroy()53     void destroy() override { glDeleteTextures(1, &mTexture); }
54 
draw()55     void draw() override
56     {
57         glClear(GL_COLOR_BUFFER_BIT);
58 
59         GLint windowWidth  = getWindow()->getWidth();
60         GLint windowHeight = getWindow()->getHeight();
61 
62         glDrawTexiOES(mX, mY, 0, mWidth, mHeight);
63         glDrawTexiOES(windowWidth - mX, mY, 0, mWidth, mHeight);
64         glDrawTexiOES(mX, windowHeight - mY, 0, mWidth, mHeight);
65         glDrawTexiOES(windowWidth - mX, windowHeight - mY, 0, mWidth, mHeight);
66 
67         mX += mReverseX ? -1 : 1;
68         mY += mReverseY ? -1 : 1;
69 
70         if (mX + mWidth >= windowWidth)
71             mReverseX = true;
72         if (mX < 0)
73             mReverseX = false;
74 
75         if (mY + mHeight >= windowHeight)
76             mReverseY = true;
77         if (mY < 0)
78             mReverseY = false;
79 
80         ++mWidth;
81         ++mHeight;
82         if (mWidth >= windowWidth)
83             mWidth = 0;
84         if (mHeight >= windowHeight)
85             mHeight = 0;
86 
87         angle::Sleep(16);
88     }
89 
90   private:
91     // Texture handle
92     GLuint mTexture = 0;
93 
94     // Draw texture coordinates and dimensions to loop through
95     GLint mX      = 0;
96     GLint mY      = 0;
97     GLint mWidth  = 0;
98     GLint mHeight = 0;
99 
100     bool mReverseX = false;
101     bool mReverseY = false;
102 };
103 
main(int argc,char ** argv)104 int main(int argc, char **argv)
105 {
106     GLES1DrawTextureSample app(argc, argv);
107     return app.run();
108 }
109