Path: blob/main_old/samples/simple_texture_2d/SimpleTexture2D.cpp
1694 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 Simple_Texture2D.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 SimpleTexture2DSample : public SampleApplication21{22public:23SimpleTexture2DSample(int argc, char **argv) : SampleApplication("SimpleTexture2D", argc, argv)24{}2526bool initialize() override27{28constexpr char kVS[] = R"(attribute vec4 a_position;29attribute vec2 a_texCoord;30varying vec2 v_texCoord;31void main()32{33gl_Position = a_position;34v_texCoord = a_texCoord;35})";3637constexpr char kFS[] = R"(precision mediump float;38varying vec2 v_texCoord;39uniform sampler2D s_texture;40void main()41{42gl_FragColor = texture2D(s_texture, v_texCoord);43})";4445mProgram = CompileProgram(kVS, kFS);46if (!mProgram)47{48return false;49}5051// Get the attribute locations52mPositionLoc = glGetAttribLocation(mProgram, "a_position");53mTexCoordLoc = glGetAttribLocation(mProgram, "a_texCoord");5455// Get the sampler location56mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");5758// Load the texture59mTexture = CreateSimpleTexture2D();6061glClearColor(0.0f, 0.0f, 0.0f, 0.0f);6263return true;64}6566void destroy() override67{68glDeleteProgram(mProgram);69glDeleteTextures(1, &mTexture);70}7172void draw() override73{74GLfloat vertices[] = {75-0.5f, 0.5f, 0.0f, // Position 0760.0f, 0.0f, // TexCoord 077-0.5f, -0.5f, 0.0f, // Position 1780.0f, 1.0f, // TexCoord 1790.5f, -0.5f, 0.0f, // Position 2801.0f, 1.0f, // TexCoord 2810.5f, 0.5f, 0.0f, // Position 3821.0f, 0.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, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), vertices);97// Load the texture coordinate98glVertexAttribPointer(mTexCoordLoc, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),99vertices + 3);100101glEnableVertexAttribArray(mPositionLoc);102glEnableVertexAttribArray(mTexCoordLoc);103104// Bind the texture105glActiveTexture(GL_TEXTURE0);106glBindTexture(GL_TEXTURE_2D, mTexture);107108// Set the texture sampler to texture unit to 0109glUniform1i(mSamplerLoc, 0);110111glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices);112}113114private:115// Handle to a program object116GLuint mProgram;117118// Attribute locations119GLint mPositionLoc;120GLint mTexCoordLoc;121122// Sampler location123GLint mSamplerLoc;124125// Texture handle126GLuint mTexture;127};128129int main(int argc, char **argv)130{131SimpleTexture2DSample app(argc, argv);132return app.run();133}134135136