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 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 "shader_utils.h" 18#include "texture_utils.h" 19 20class SimpleTexture2DSample : public SampleApplication 21{ 22 public: 23 SimpleTexture2DSample::SimpleTexture2DSample() 24 : SampleApplication("SimpleTexture2D", 1280, 720) 25 { 26 } 27 28 virtual bool initialize() 29 { 30 const std::string vs = SHADER_SOURCE 31 ( 32 attribute vec4 a_position; 33 attribute vec2 a_texCoord; 34 varying vec2 v_texCoord; 35 void main() 36 { 37 gl_Position = a_position; 38 v_texCoord = a_texCoord; 39 } 40 ); 41 42 const std::string fs = SHADER_SOURCE 43 ( 44 precision mediump float; 45 varying vec2 v_texCoord; 46 uniform sampler2D s_texture; 47 void main() 48 { 49 gl_FragColor = texture2D(s_texture, v_texCoord); 50 } 51 ); 52 53 mProgram = CompileProgram(vs, fs); 54 if (!mProgram) 55 { 56 return false; 57 } 58 59 // Get the attribute locations 60 mPositionLoc = glGetAttribLocation(mProgram, "a_position"); 61 mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord"); 62 63 // Get the sampler location 64 mSamplerLoc = glGetUniformLocation(mProgram, "s_texture"); 65 66 // Load the texture 67 mTexture = CreateSimpleTexture2D(); 68 69 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 70 71 return true; 72 } 73 74 virtual void destroy() 75 { 76 glDeleteProgram(mProgram); 77 glDeleteTextures(1, &mTexture); 78 } 79 80 virtual void draw() 81 { 82 GLfloat vertices[] = 83 { 84 -0.5f, 0.5f, 0.0f, // Position 0 85 0.0f, 0.0f, // TexCoord 0 86 -0.5f, -0.5f, 0.0f, // Position 1 87 0.0f, 1.0f, // TexCoord 1 88 0.5f, -0.5f, 0.0f, // Position 2 89 1.0f, 1.0f, // TexCoord 2 90 0.5f, 0.5f, 0.0f, // Position 3 91 1.0f, 0.0f // TexCoord 3 92 }; 93 GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; 94 95 // Set the viewport 96 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight()); 97 98 // Clear the color buffer 99 glClear(GL_COLOR_BUFFER_BIT); 100 101 // Use the program object 102 glUseProgram(mProgram); 103 104 // Load the vertex position 105 glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices); 106 // Load the texture coordinate 107 glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices + 3); 108 109 glEnableVertexAttribArray(mPositionLoc); 110 glEnableVertexAttribArray(mTexCoordLoc); 111 112 // Bind the texture 113 glActiveTexture(GL_TEXTURE0); 114 glBindTexture(GL_TEXTURE_2D, mTexture); 115 116 // Set the texture sampler to texture unit to 0 117 glUniform1i(mSamplerLoc, 0); 118 119 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); 120 } 121 122 private: 123 // Handle to a program object 124 GLuint mProgram; 125 126 // Attribute locations 127 GLint mPositionLoc; 128 GLint mTexCoordLoc; 129 130 // Sampler location 131 GLint mSamplerLoc; 132 133 // Texture handle 134 GLuint mTexture; 135}; 136 137int main(int argc, char **argv) 138{ 139 SimpleTexture2DSample app; 140 return app.run(); 141} 142