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