Path: blob/main_old/samples/simple_texture_cubemap/SimpleTextureCubemap.cpp
2586 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_TextureCubemap.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/geometry_utils.h"19#include "util/shader_utils.h"2021class SimpleTextureCubemapSample : public SampleApplication22{23public:24SimpleTextureCubemapSample(int argc, char **argv)25: SampleApplication("SimpleTextureCubemap", argc, argv)26{}2728bool initialize() override29{30constexpr char kVS[] = R"(attribute vec4 a_position;31attribute vec3 a_normal;32varying vec3 v_normal;33void main()34{35gl_Position = a_position;36v_normal = a_normal;37})";3839constexpr char kFS[] = R"(precision mediump float;40varying vec3 v_normal;41uniform samplerCube s_texture;42void main()43{44gl_FragColor = textureCube(s_texture, v_normal);45})";4647mProgram = CompileProgram(kVS, kFS);48if (!mProgram)49{50return false;51}5253// Get the attribute locations54mPositionLoc = glGetAttribLocation(mProgram, "a_position");55mNormalLoc = glGetAttribLocation(mProgram, "a_normal");5657// Get the sampler locations58mSamplerLoc = glGetUniformLocation(mProgram, "s_texture");5960// Load the texture61mTexture = CreateSimpleTextureCubemap();6263// Generate the geometry data64CreateSphereGeometry(128, 0.75f, &mSphere);6566glClearColor(0.0f, 0.0f, 0.0f, 0.0f);67glCullFace(GL_BACK);68glEnable(GL_CULL_FACE);6970return true;71}7273void destroy() override74{75glDeleteProgram(mProgram);76glDeleteTextures(1, &mTexture);77}7879void draw() override80{81// Set the viewport82glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());8384// Clear the color buffer85glClear(GL_COLOR_BUFFER_BIT);8687// Use the program object88glUseProgram(mProgram);8990// Load the vertex position91glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mSphere.positions.data());92glEnableVertexAttribArray(mPositionLoc);9394// Load the normal95glVertexAttribPointer(mNormalLoc, 3, GL_FLOAT, GL_FALSE, 0, mSphere.normals.data());96glEnableVertexAttribArray(mNormalLoc);9798// Bind the texture99glActiveTexture(GL_TEXTURE0);100glBindTexture(GL_TEXTURE_CUBE_MAP, mTexture);101102// Set the texture sampler to texture unit to 0103glUniform1i(mSamplerLoc, 0);104105glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mSphere.indices.size()),106GL_UNSIGNED_SHORT, mSphere.indices.data());107}108109private:110// Handle to a program object111GLuint mProgram;112113// Attribute locations114GLint mPositionLoc;115GLint mNormalLoc;116117// Sampler location118GLint mSamplerLoc;119120// Texture handle121GLuint mTexture;122123// Geometry data124SphereGeometry mSphere;125};126127int main(int argc, char **argv)128{129SimpleTextureCubemapSample app(argc, argv);130return app.run();131}132133134