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 TextureWrap.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 TextureWrapSample : public SampleApplication 21{ 22 public: 23 TextureWrapSample::TextureWrapSample() 24 : SampleApplication("TextureWrap", 1280, 720) 25 { 26 } 27 28 virtual bool initialize() 29 { 30 const std::string vs = SHADER_SOURCE 31 ( 32 uniform float u_offset; 33 attribute vec4 a_position; 34 attribute vec2 a_texCoord; 35 varying vec2 v_texCoord; 36 void main() 37 { 38 gl_Position = a_position; 39 gl_Position.x += u_offset; 40 v_texCoord = a_texCoord; 41 } 42 ); 43 44 const std::string fs = SHADER_SOURCE 45 ( 46 precision mediump float; 47 varying vec2 v_texCoord; 48 uniform sampler2D s_texture; 49 void main() 50 { 51 gl_FragColor = texture2D(s_texture, v_texCoord); 52 } 53 ); 54 55 mProgram = CompileProgram(vs, fs); 56 if (!mProgram) 57 { 58 return false; 59 } 60 61 // Get the attribute locations 62 mPositionLoc = glGetAttribLocation(mProgram, "a_position"); 63 mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord"); 64 65 // Get the sampler location 66 mSamplerLoc = glGetUniformLocation(mProgram, "s_texture"); 67 68 // Get the offset location 69 mOffsetLoc = glGetUniformLocation(mProgram, "u_offset"); 70 71 // Load the texture 72 mTexture = CreateMipMappedTexture2D(); 73 74 glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 75 76 return true; 77 } 78 79 virtual void destroy() 80 { 81 glDeleteProgram(mProgram); 82 } 83 84 virtual void draw() 85 { 86 GLfloat vertices[] = 87 { 88 -0.3f, 0.3f, 0.0f, 1.0f, // Position 0 89 -1.0f, -1.0f, // TexCoord 0 90 -0.3f, -0.3f, 0.0f, 1.0f, // Position 1 91 -1.0f, 2.0f, // TexCoord 1 92 0.3f, -0.3f, 0.0f, 1.0f, // Position 2 93 2.0f, 2.0f, // TexCoord 2 94 0.3f, 0.3f, 0.0f, 1.0f, // Position 3 95 2.0f, -1.0f // TexCoord 3 96 }; 97 GLushort indices[] = { 0, 1, 2, 0, 2, 3 }; 98 99 // Set the viewport 100 glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight()); 101 102 // Clear the color buffer 103 glClear(GL_COLOR_BUFFER_BIT); 104 105 // Use the program object 106 glUseProgram(mProgram); 107 108 // Load the vertex position 109 glVertexAttribPointer(mPositionLoc, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices); 110 glEnableVertexAttribArray(mPositionLoc); 111 112 // Load the texture coordinate 113 glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices + 4); 114 glEnableVertexAttribArray(mTexCoordLoc); 115 116 // Set the sampler texture unit to 0 117 glUniform1i(mSamplerLoc, 0); 118 119 // Draw quad with repeat wrap mode 120 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 121 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 122 glUniform1f(mOffsetLoc, -0.7f); 123 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); 124 125 // Draw quad with clamp to edge wrap mode 126 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 127 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 128 glUniform1f(mOffsetLoc, 0.0f); 129 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); 130 131 // Draw quad with mirrored repeat 132 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT); 133 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT); 134 glUniform1f(mOffsetLoc, 0.7f); 135 glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices); 136 } 137 138private: 139 // Handle to a program object 140 GLuint mProgram; 141 142 // Attribute locations 143 GLint mPositionLoc; 144 GLint mTexCoordLoc; 145 146 // Sampler location 147 GLint mSamplerLoc; 148 149 // Offset location 150 GLint mOffsetLoc; 151 152 // Texture handle 153 GLuint mTexture; 154}; 155 156int main(int argc, char **argv) 157{ 158 TextureWrapSample app; 159 return app.run(); 160} 161