Path: blob/main_old/samples/multi_window/MultiWindow.cpp
1693 views
//1// Copyright 2015 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#include "SampleApplication.h"78#include <algorithm>9#include <cmath>10#include <vector>1112#include "util/Matrix.h"13#include "util/random_utils.h"14#include "util/shader_utils.h"1516using namespace angle;1718class MultiWindowSample : public SampleApplication19{20public:21MultiWindowSample(int argc, char **argv)22: SampleApplication("MultiWindow", argc, argv, 2, 0, 256, 256)23{}2425bool initialize() override26{27constexpr char kVS[] = R"(attribute vec4 vPosition;28void main()29{30gl_Position = vPosition;31})";3233constexpr char kFS[] = R"(precision mediump float;34void main()35{36gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);37})";3839mProgram = CompileProgram(kVS, kFS);40if (!mProgram)41{42return false;43}4445// Set an initial rotation46mRotation = 45.0f;4748glClearColor(0.0f, 0.0f, 0.0f, 0.0f);4950window rootWindow;51rootWindow.osWindow = getWindow();52rootWindow.surface = getSurface();53mWindows.push_back(rootWindow);5455const size_t numWindows = 5;56for (size_t i = 1; i < numWindows; i++)57{58window window;5960window.osWindow = OSWindow::New();61if (!window.osWindow->initialize("MultiWindow", 256, 256))62{63return false;64}6566window.surface = eglCreateWindowSurface(getDisplay(), getConfig(),67window.osWindow->getNativeWindow(), nullptr);68if (window.surface == EGL_NO_SURFACE)69{70return false;71}7273window.osWindow->setVisible(true);7475mWindows.push_back(window);76}7778int baseX = rootWindow.osWindow->getX();79int baseY = rootWindow.osWindow->getY();80for (auto &window : mWindows)81{82int x = baseX + mRNG.randomIntBetween(0, 512);83int y = baseY + mRNG.randomIntBetween(0, 512);84int width = mRNG.randomIntBetween(128, 512);85int height = mRNG.randomIntBetween(128, 512);86window.osWindow->setPosition(x, y);87window.osWindow->resize(width, height);88}8990return true;91}9293void destroy() override { glDeleteProgram(mProgram); }9495void step(float dt, double totalTime) override96{97mRotation = fmod(mRotation + (dt * 40.0f), 360.0f);9899for (auto &window : mWindows)100{101window.osWindow->messageLoop();102}103}104105void draw() override106{107OSWindow *rootWindow = mWindows[0].osWindow;108int left = rootWindow->getX();109int right = rootWindow->getX() + rootWindow->getWidth();110int top = rootWindow->getY();111int bottom = rootWindow->getY() + rootWindow->getHeight();112113for (auto &windowRecord : mWindows)114{115OSWindow *window = windowRecord.osWindow;116left = std::min(left, window->getX());117right = std::max(right, window->getX() + window->getWidth());118top = std::min(top, window->getY());119bottom = std::max(bottom, window->getY() + window->getHeight());120}121122float midX = (left + right) * 0.5f;123float midY = (top + bottom) * 0.5f;124125Matrix4 modelMatrix = Matrix4::translate(Vector3(midX, midY, 0.0f)) *126Matrix4::rotate(mRotation, Vector3(0.0f, 0.0f, 1.0f)) *127Matrix4::translate(Vector3(-midX, -midY, 0.0f));128Matrix4 viewMatrix = Matrix4::identity();129130for (auto &windowRecord : mWindows)131{132OSWindow *window = windowRecord.osWindow;133EGLSurface surface = windowRecord.surface;134135eglMakeCurrent(getDisplay(), surface, surface, getContext());136137Matrix4 orthoMatrix =138Matrix4::ortho(static_cast<float>(window->getX()),139static_cast<float>(window->getX() + window->getWidth()),140static_cast<float>(window->getY() + window->getHeight()),141static_cast<float>(window->getY()), 0.0f, 1.0f);142Matrix4 mvpMatrix = orthoMatrix * viewMatrix * modelMatrix;143144Vector3 vertices[] = {145Matrix4::transform(mvpMatrix, Vector4(midX, static_cast<float>(top), 0.0f, 1.0f)),146Matrix4::transform(mvpMatrix, Vector4(static_cast<float>(left),147static_cast<float>(bottom), 0.0f, 1.0f)),148Matrix4::transform(mvpMatrix, Vector4(static_cast<float>(right),149static_cast<float>(bottom), 0.0f, 1.0f)),150};151152// Set the viewport153glViewport(0, 0, window->getWidth(), window->getHeight());154155// Clear the color buffer156glClear(GL_COLOR_BUFFER_BIT);157158// Use the program object159glUseProgram(mProgram);160161// Load the vertex data162glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vertices[0].data());163glEnableVertexAttribArray(0);164165glDrawArrays(GL_TRIANGLES, 0, 3);166167eglSwapBuffers(getDisplay(), surface);168}169}170171// Override swap to do nothing as we already swapped the root172// window in draw() and swapping another time would invalidate173// the content of the default framebuffer.174void swap() override {}175176private:177// Handle to a program object178GLuint mProgram;179180// Current rotation181float mRotation;182183// Window and surface data184struct window185{186OSWindow *osWindow;187EGLSurface surface;188};189std::vector<window> mWindows;190191RNG mRNG;192};193194int main(int argc, char **argv)195{196MultiWindowSample app(argc, argv);197return app.run();198}199200201