Path: blob/main_old/samples/post_sub_buffer/PostSubBuffer.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_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"16#include "texture_utils.h"17#include "util/Matrix.h"18#include "util/geometry_utils.h"19#include "util/shader_utils.h"2021#include <cmath>22#include <iostream>2324class PostSubBufferSample : public SampleApplication25{26public:27PostSubBufferSample(int argc, char **argv) : SampleApplication("PostSubBuffer", argc, argv) {}2829bool initialize() override30{31constexpr char kVS[] = R"(uniform mat4 u_mvpMatrix;32attribute vec4 a_position;33attribute vec2 a_texcoord;34varying vec2 v_texcoord;35void main()36{37gl_Position = u_mvpMatrix * a_position;38v_texcoord = a_texcoord;39})";4041constexpr char kFS[] = R"(precision mediump float;42varying vec2 v_texcoord;43void main()44{45gl_FragColor = vec4(v_texcoord.x, v_texcoord.y, 1.0, 1.0);46})";4748mProgram = CompileProgram(kVS, kFS);49if (!mProgram)50{51return false;52}5354// Get the attribute locations55mPositionLoc = glGetAttribLocation(mProgram, "a_position");56mTexcoordLoc = glGetAttribLocation(mProgram, "a_texcoord");5758// Get the uniform locations59mMVPMatrixLoc = glGetUniformLocation(mProgram, "u_mvpMatrix");6061// Generate the geometry data62GenerateCubeGeometry(0.5f, &mCube);6364// Set an initial rotation65mRotation = 45.0f;6667// Clear the whole window surface to blue.68glClearColor(0.0f, 0.0f, 1.0f, 0.0f);69glClear(GL_COLOR_BUFFER_BIT);70SampleApplication::swap();7172glClearColor(0.0f, 0.0f, 0.0f, 0.0f);73glCullFace(GL_BACK);74glEnable(GL_CULL_FACE);7576return true;77}7879void destroy() override { glDeleteProgram(mProgram); }8081void step(float dt, double totalTime) override82{83mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);8485Matrix4 perspectiveMatrix = Matrix4::perspective(8660.0f, float(getWindow()->getWidth()) / getWindow()->getHeight(), 1.0f, 20.0f);8788Matrix4 modelMatrix = Matrix4::translate(angle::Vector3(0.0f, 0.0f, -2.0f)) *89Matrix4::rotate(mRotation, angle::Vector3(1.0f, 0.0f, 1.0f));9091Matrix4 viewMatrix = Matrix4::identity();9293Matrix4 mvpMatrix = perspectiveMatrix * viewMatrix * modelMatrix;9495// Load the matrices96glUniformMatrix4fv(mMVPMatrixLoc, 1, GL_FALSE, mvpMatrix.data);97}9899void draw() override100{101// Set the viewport102glViewport(0, 0, getWindow()->getWidth(), getWindow()->getHeight());103104// Clear the color buffer105glClear(GL_COLOR_BUFFER_BIT);106107// Use the program object108glUseProgram(mProgram);109110// Load the vertex position111glVertexAttribPointer(mPositionLoc, 3, GL_FLOAT, GL_FALSE, 0, mCube.positions.data());112glEnableVertexAttribArray(mPositionLoc);113114// Load the texcoord data115glVertexAttribPointer(mTexcoordLoc, 2, GL_FLOAT, GL_FALSE, 0, mCube.texcoords.data());116glEnableVertexAttribArray(mTexcoordLoc);117118// Draw the cube119glDrawElements(GL_TRIANGLES, static_cast<GLsizei>(mCube.indices.size()), GL_UNSIGNED_SHORT,120mCube.indices.data());121}122123void swap() override124{125// Instead of letting the application call eglSwapBuffers, call eglPostSubBufferNV here126// instead127EGLint windowWidth = static_cast<EGLint>(getWindow()->getWidth());128EGLint windowHeight = static_cast<EGLint>(getWindow()->getHeight());129EGLDisplay display = getDisplay();130EGLSurface surface = getSurface();131eglPostSubBufferNV(display, surface, 60, 60, windowWidth - 120, windowHeight - 120);132}133134private:135// Handle to a program object136GLuint mProgram;137138// Attribute locations139GLint mPositionLoc;140GLint mTexcoordLoc;141142// Uniform locations143GLuint mMVPMatrixLoc;144145// Current rotation146float mRotation;147148// Geometry data149CubeGeometry mCube;150};151152int main(int argc, char **argv)153{154PostSubBufferSample app(argc, argv);155return app.run();156}157158159