Home | History | Annotate | Download | only in hello_triangle
      1 //
      2 // Copyright (c) 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 #include "shader_utils.h"
     18 
     19 class HelloTriangleSample : public SampleApplication
     20 {
     21   public:
     22     HelloTriangleSample::HelloTriangleSample()
     23         : SampleApplication("HelloTriangle", 1280, 720)
     24     {
     25     }
     26 
     27     virtual bool initialize()
     28     {
     29         const std::string vs = SHADER_SOURCE
     30         (
     31             attribute vec4 vPosition;
     32             void main()
     33             {
     34                 gl_Position = vPosition;
     35             }
     36         );
     37 
     38         const std::string fs = SHADER_SOURCE
     39         (
     40             precision mediump float;
     41             void main()
     42             {
     43                 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
     44             }
     45         );
     46 
     47         mProgram = CompileProgram(vs, fs);
     48         if (!mProgram)
     49         {
     50             return false;
     51         }
     52 
     53         glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     54 
     55         return true;
     56     }
     57 
     58     virtual void destroy()
     59     {
     60         glDeleteProgram(mProgram);
     61     }
     62 
     63     virtual void draw()
     64     {
     65         GLfloat vertices[] =
     66         {
     67              0.0f,  0.5f, 0.0f,
     68             -0.5f, -0.5f, 0.0f,
     69              0.5f, -0.5f, 0.0f,
     70         };
     71 
     72         // Set the viewport
     73         glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());
     74 
     75         // Clear the color buffer
     76         glClear(GL_COLOR_BUFFER_BIT);
     77 
     78         // Use the program object
     79         glUseProgram(mProgram);
     80 
     81         // Load the vertex data
     82         glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices);
     83         glEnableVertexAttribArray(0);
     84 
     85         glDrawArrays(GL_TRIANGLES, 0, 3);
     86     }
     87 
     88   private:
     89     GLuint mProgram;
     90 };
     91 
     92 int main(int argc, char **argv)
     93 {
     94     HelloTriangleSample app;
     95     return app.run();
     96 }
     97