Path: blob/main_old/samples/texture_wrap/TextureWrap.cpp
2573 views
//1// Copyright 2014 The ANGLE Project Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file.4//56// Based on TextureWrap.c from7// Book: OpenGL(R) ES 2.0 Programming Guide8// Authors: Aaftab Munshi, Dan Ginsburg, Dave Shreiner9// ISBN-10: 032150279510// ISBN-13: 978032150279711// Publisher: Addison-Wesley Professional12// URLs: http://safari.informit.com/978032156383513// http://www.opengles-book.com1415#include "SampleApplication.h"1617#include "texture_utils.h"18#include "util/shader_utils.h"1920class TextureWrapSample : public SampleApplication21{22public:23TextureWrapSample(int argc, char **argv) : SampleApplication("TextureWrap", argc, argv) {}2425bool initialize() override26{27constexpr char kVS[] = R"(uniform float u_offset;28attribute vec4 a_position;29attribute vec2 a_texCoord;30varying vec2 v_texCoord;31void main()32{33gl_Position = a_position;34gl_Position.x += u_offset;35v_texCoord = a_texCoord;36})";3738constexpr char kFS[] = R"(precision mediump float;39varying vec2 v_texCoord;40uniform sampler2D s_texture;41void main()42{43gl_FragColor = texture2D(s_texture, v_texCoord);44})";4546mProgram = CompileProgram(kVS, kFS);47if (!mProgram)48{49return false;50}5152// Get the attribute locations53mPositionLoc = glGetAttribLocation(mProgram, "a_position");54mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord");5556// Get the sampler location57mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");5859// Get the offset location60mOffsetLoc = glGetUniformLocation(mProgram, "u_offset");6162// Load the texture63mTexture = CreateMipMappedTexture2D();6465glClearColor(0.0f, 0.0f, 0.0f, 0.0f);6667return true;68}6970void destroy() override { glDeleteProgram(mProgram); }7172void draw() override73{74GLfloat vertices[] = {75-0.3f, 0.3f, 0.0f, 1.0f, // Position 076-1.0f, -1.0f, // TexCoord 077-0.3f, -0.3f, 0.0f, 1.0f, // Position 178-1.0f, 2.0f, // TexCoord 1790.3f, -0.3f, 0.0f, 1.0f, // Position 2802.0f, 2.0f, // TexCoord 2810.3f, 0.3f, 0.0f, 1.0f, // Position 3822.0f, -1.0f // TexCoord 383};84GLushort indices[] = {0, 1, 2, 0, 2, 3};8586// Set the viewport87glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());8889// Clear the color buffer90glClear(GL_COLOR_BUFFER_BIT);9192// Use the program object93glUseProgram(mProgram);9495// Load the vertex position96glVertexAttribPointer(mPositionLoc, 4, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), vertices);97glEnableVertexAttribArray(mPositionLoc);9899// Load the texture coordinate100glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat),101vertices + 4);102glEnableVertexAttribArray(mTexCoordLoc);103104// Set the sampler texture unit to 0105glUniform1i(mSamplerLoc, 0);106107// Draw quad with repeat wrap mode108glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);109glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);110glUniform1f(mOffsetLoc, -0.7f);111glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);112113// Draw quad with clamp to edge wrap mode114glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);115glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);116glUniform1f(mOffsetLoc, 0.0f);117glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);118119// Draw quad with mirrored repeat120glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT);121glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT);122glUniform1f(mOffsetLoc, 0.7f);123glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);124}125126private:127// Handle to a program object128GLuint mProgram;129130// Attribute locations131GLint mPositionLoc;132GLint mTexCoordLoc;133134// Sampler location135GLint mSamplerLoc;136137// Offset location138GLint mOffsetLoc;139140// Texture handle141GLuint mTexture;142};143144int main(int argc, char **argv)145{146TextureWrapSample app(argc, argv);147return app.run();148}149150151