xref: /aosp_15_r20/external/angle/samples/hello_triangle/HelloTriangle.cpp (revision 8975f5c5ed3d1c378011245431ada316dfb6f244)
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 Hello_Triangle.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 "util/shader_utils.h"
19 
20 class HelloTriangleSample : public SampleApplication
21 {
22   public:
HelloTriangleSample(int argc,char ** argv)23     HelloTriangleSample(int argc, char **argv) : SampleApplication("HelloTriangle", argc, argv) {}
24 
initialize()25     bool initialize() override
26     {
27         constexpr char kVS[] = R"(attribute vec4 vPosition;
28 void main()
29 {
30     gl_Position = vPosition;
31 })";
32 
33         constexpr char kFS[] = R"(precision mediump float;
34 void main()
35 {
36     gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
37 })";
38 
39         mProgram = CompileProgram(kVS, kFS);
40         if (!mProgram)
41         {
42             return false;
43         }
44 
45         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
46 
47         return true;
48     }
49 
destroy()50     void destroy() override { glDeleteProgram(mProgram); }
51 
draw()52     void draw() override
53     {
54         GLfloat vertices[] = {
55             0.0f, 0.5f, 0.0f, -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f,
56         };
57 
58         // Set the viewport
59         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
60 
61         // Clear the color buffer
62         glClear(GL_COLOR_BUFFER_BIT);
63 
64         // Use the program object
65         glUseProgram(mProgram);
66 
67         // Load the vertex data
68         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
69         glEnableVertexAttribArray(0);
70 
71         glDrawArrays(GL_TRIANGLES, 0, 3);
72     }
73 
74   private:
75     GLuint mProgram;
76 };
77 
main(int argc,char ** argv)78 int main(int argc, char **argv)
79 {
80     HelloTriangleSample app(argc, argv);
81     return app.run();
82 }
83