Path: blob/main_old/samples/simple_vertex_shader/SimpleVertexShader.cpp
2585 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_VertexShader.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/Matrix.h"19#include "util/geometry_utils.h"20#include "util/shader_utils.h"2122#include <cmath>2324class SimpleVertexShaderSample : public SampleApplication25{26public:27SimpleVertexShaderSample(int argc, char **argv)28: SampleApplication("SimpleVertexShader", argc, argv)29{}3031bool initialize() override32{33constexpr char kVS[] = R"(uniform mat4 u_mvpMatrix;34attribute vec4 a_position;35attribute vec2 a_texcoord;36varying vec2 v_texcoord;37void main()38{39gl_Position = u_mvpMatrix * a_position;40v_texcoord = a_texcoord;41})";4243constexpr char kFS[] = R"(precision mediump float;44varying vec2 v_texcoord;45void main()46{47gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);48})";4950mProgram = CompileProgram(kVS, kFS);51if (!mProgram)52{53return false;54}5556// Get the attribute locations57mPositionLoc = glGetAttribLocation(mProgram, "a_position");58mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");5960// Get the uniform locations61mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");6263// Generate the geometry data64GenerateCubeGeometry(0.5f, &mCube);6566// Set an initial rotation67mRotation = 45.0f;6869glClearColor(0.0f, 0.0f, 0.0f, 0.0f);70glCullFace(GL_BACK);71glEnable(GL_CULL_FACE);7273return true;74}7576void destroy() override { glDeleteProgram(mProgram); }7778void step(float dt, double totalTime) override79{80mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);8182Matrix4 perspectiveMatrix = Matrix4::perspective(8360.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(), 1.0f, 20.0f);8485Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0.0f, 0.0f, -2.0f)) *86Matrix4::rotate(mRotation, angle::Vector3(1.0f, 0.0f, 1.0f));8788Matrix4 viewMatrix = Matrix4::identity();8990Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;9192// Load the matrices93glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);94}9596void draw() override97{98// Set the viewport99glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());100101// Clear the color buffer102glClear(GL_COLOR_BUFFER_BIT);103104// Use the program object105glUseProgram(mProgram);106107// Load the vertex position108glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());109glEnableVertexAttribArray(mPositionLoc);110111// Load the texcoord data112glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());113glEnableVertexAttribArray(mTexcoordLoc);114115// Draw the cube116glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mCube.indices.size()), GL_UNSIGNED_SHORT,117mCube.indices.data());118}119120private:121// Handle to a program object122GLuint mProgram;123124// Attribute locations125GLint mPositionLoc;126GLint mTexcoordLoc;127128// Uniform locations129GLuint mMVPMatrixLoc;130131// Current rotation132float mRotation;133134// Geometry data135CubeGeometry mCube;136};137138int main(int argc, char **argv)139{140SimpleVertexShaderSample app(argc, argv);141return app.run();142}143144145