commit 7c0fc63202b735d4e0e097ea377077ac8b687385 Author: wuyize Date: Wed Jul 20 18:31:32 2022 +0800 万物基于三角形 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..1ff0c42 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,63 @@ +############################################################################### +# Set default behavior to automatically normalize line endings. +############################################################################### +* text=auto + +############################################################################### +# Set default behavior for command prompt diff. +# +# This is need for earlier builds of msysgit that does not have it on by +# default for csharp files. +# Note: This is only used by command line +############################################################################### +#*.cs diff=csharp + +############################################################################### +# Set the merge driver for project and solution files +# +# Merging from the command prompt will add diff markers to the files if there +# are conflicts (Merging from VS is not affected by the settings below, in VS +# the diff markers are never inserted). Diff markers may cause the following +# file extensions to fail to load in VS. An alternative would be to treat +# these files as binary and thus will always conflict and require user +# intervention with every merge. To do so, just uncomment the entries below +############################################################################### +#*.sln merge=binary +#*.csproj merge=binary +#*.vbproj merge=binary +#*.vcxproj merge=binary +#*.vcproj merge=binary +#*.dbproj merge=binary +#*.fsproj merge=binary +#*.lsproj merge=binary +#*.wixproj merge=binary +#*.modelproj merge=binary +#*.sqlproj merge=binary +#*.wwaproj merge=binary + +############################################################################### +# behavior for image files +# +# image files are treated as binary by default. +############################################################################### +#*.jpg binary +#*.png binary +#*.gif binary + +############################################################################### +# diff behavior for common document formats +# +# Convert binary document formats to text before diffing them. This feature +# is only available from the command line. Turn it on by uncommenting the +# entries below. +############################################################################### +#*.doc diff=astextplain +#*.DOC diff=astextplain +#*.docx diff=astextplain +#*.DOCX diff=astextplain +#*.dot diff=astextplain +#*.DOT diff=astextplain +#*.pdf diff=astextplain +#*.PDF diff=astextplain +#*.rtf diff=astextplain +#*.RTF diff=astextplain diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9491a2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,363 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Oo]ut/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd \ No newline at end of file diff --git a/ArchitectureColoredPainting.sln b/ArchitectureColoredPainting.sln new file mode 100644 index 0000000..1f4fb23 --- /dev/null +++ b/ArchitectureColoredPainting.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.2.32519.379 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ArchitectureColoredPainting", "ArchitectureColoredPainting\ArchitectureColoredPainting.vcxproj", "{3FE96A33-2BB7-4686-A710-3EB8E3BBD709}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Release|x64 = Release|x64 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3FE96A33-2BB7-4686-A710-3EB8E3BBD709}.Debug|x64.ActiveCfg = Debug|x64 + {3FE96A33-2BB7-4686-A710-3EB8E3BBD709}.Debug|x64.Build.0 = Debug|x64 + {3FE96A33-2BB7-4686-A710-3EB8E3BBD709}.Release|x64.ActiveCfg = Release|x64 + {3FE96A33-2BB7-4686-A710-3EB8E3BBD709}.Release|x64.Build.0 = Release|x64 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2B75CEEF-BAB8-4E59-A9B4-A063DAFA8526} + EndGlobalSection +EndGlobal diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj new file mode 100644 index 0000000..1cc7b04 --- /dev/null +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj @@ -0,0 +1,112 @@ + + + + + Debug + x64 + + + Release + x64 + + + + {3FE96A33-2BB7-4686-A710-3EB8E3BBD709} + QtVS_v304 + 10.0.19041.0 + 10.0.19041.0 + $(MSBuildProjectDirectory)\QtMsBuild + + + + Application + v143 + + + Application + v143 + + + + + + + 5.15.2_msvc2019_64 + core;gui;widgets + debug + + + 5.15.2_msvc2019_64 + core;gui;widgets + release + + + + + + + + + + + + + + + + + + + + + + true + true + ProgramDatabase + Disabled + MultiThreadedDebugDLL + + + Console + true + + + + + true + true + None + MaxSpeed + MultiThreadedDLL + + + Windows + false + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters new file mode 100644 index 0000000..0efd154 --- /dev/null +++ b/ArchitectureColoredPainting/ArchitectureColoredPainting.vcxproj.filters @@ -0,0 +1,71 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + qml;cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + qrc;rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + {99349809-55BA-4b9d-BF79-8FDBB0286EB3} + ui + + + {639EADAA-A684-42e4-A9AD-28FC9BCB8F7C} + ts + + + {60515177-3da7-420f-8f5b-27c16bb2b77b} + + + + + Resource Files + + + Form Files + + + Header Files + + + Source Files + + + + + Source Files + + + Source Files + + + Source Files + + + + + Header Files + + + + + Shaders + + + Shaders + + + + + Header Files + + + \ No newline at end of file diff --git a/ArchitectureColoredPainting/Camera.cpp b/ArchitectureColoredPainting/Camera.cpp new file mode 100644 index 0000000..1504611 --- /dev/null +++ b/ArchitectureColoredPainting/Camera.cpp @@ -0,0 +1,96 @@ +#include "Camera.h" + + + +// constructor with vectors +Camera::Camera(QVector3D position, QVector3D up, float yaw, float pitch) : Front(QVector3D(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM) +{ + Position = position; + WorldUp = up; + Yaw = yaw; + Pitch = pitch; + updateCameraVectors(); +} +// constructor with scalar values +Camera::Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(QVector3D(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM) +{ + Position = QVector3D(posX, posY, posZ); + WorldUp = QVector3D(upX, upY, upZ); + Yaw = yaw; + Pitch = pitch; + updateCameraVectors(); +} + +// returns the view matrix calculated using Euler Angles and the LookAt Matrix +QMatrix4x4 Camera::GetViewMatrix() +{ + QMatrix4x4 viewMatrix; + viewMatrix.lookAt(Position, Position + Front, Up); + return viewMatrix; +} + +// processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) +void Camera::ProcessKeyboard(Camera_Movement direction, float deltaTime) +{ + float velocity = MovementSpeed * deltaTime; + if (direction == FORWARD) + Position += PositionFront * velocity; + if (direction == BACKWARD) + Position -= PositionFront * velocity; + if (direction == LEFT) + Position -= PositionRight * velocity; + if (direction == RIGHT) + Position += PositionRight * velocity; + if (direction == UP) + Position.setY(Position.y() + velocity); + if (direction == DOWN) + Position.setY(Position.y() - velocity); +} + +// processes input received from a mouse input system. Expects the offset value in both the x and y direction. +void Camera::ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch) +{ + xoffset *= MouseSensitivity; + yoffset *= MouseSensitivity; + + Yaw += xoffset; + Pitch += yoffset; + + // make sure that when pitch is out of bounds, screen doesn't get flipped + if (constrainPitch) + { + if (Pitch > 89.99f) + Pitch = 89.99f; + if (Pitch < -89.99f) + Pitch = -89.99f; + } + + // update Front, Right and Up Vectors using the updated Euler angles + updateCameraVectors(); +} + +// processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis +void Camera::ProcessMouseScroll(float yoffset) +{ + Zoom -= (float)yoffset; + if (Zoom < 1.0f) + Zoom = 1.0f; + if (Zoom > 45.0f) + Zoom = 45.0f; +} + +// calculates the front vector from the Camera's (updated) Euler Angles +void Camera::updateCameraVectors() +{ + // calculate the new Front vector + QVector3D front; + front.setX(cos(qDegreesToRadians(Yaw)) * cos(qDegreesToRadians(Pitch))); + front.setY(sin(qDegreesToRadians(Pitch))); + front.setZ(sin(qDegreesToRadians(Yaw)) * cos(qDegreesToRadians(Pitch))); + Front = front.normalized(); + // also re-calculate the Right and Up vector + Right = QVector3D::crossProduct(Front, WorldUp).normalized(); // normalize the vectors, because their length gets closer to 0 the more you look up or down which results in slower movement. + Up = QVector3D::crossProduct(Right, Front).normalized(); + PositionFront = QVector3D(front.x(), 0.0f, front.z()).normalized(); + PositionRight = QVector3D::crossProduct(PositionFront, WorldUp).normalized(); +} \ No newline at end of file diff --git a/ArchitectureColoredPainting/Camera.h b/ArchitectureColoredPainting/Camera.h new file mode 100644 index 0000000..80e9a00 --- /dev/null +++ b/ArchitectureColoredPainting/Camera.h @@ -0,0 +1,67 @@ +#pragma once + +#include +#include +#include +#include + +// Default camera values +const float YAW = -90.0f; +const float PITCH = 0.0f; +const float SPEED = 2.5f; +const float SENSITIVITY = 0.1f; +const float ZOOM = 45.0f; + +// Defines several possible options for camera movement. Used as abstraction to stay away from window-system specific input methods +enum Camera_Movement { + FORWARD, + BACKWARD, + LEFT, + RIGHT, + UP, + DOWN +}; + +class Camera +{ + +public: + // camera Attributes + QVector3D Position; + QVector3D Front; + QVector3D Up; + QVector3D Right; + QVector3D WorldUp; + QVector3D PositionFront; + QVector3D PositionRight; + // euler Angles, ½Ç¶ÈÖÆ + float Yaw; + float Pitch; + // camera options + float MovementSpeed; + float MouseSensitivity; + float Zoom; + + // constructor with vectors + Camera(QVector3D position = QVector3D(0.0f, 0.0f, 0.0f), QVector3D up = QVector3D(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH); + + // constructor with scalar values + Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch); + + // returns the view matrix calculated using Euler Angles and the LookAt Matrix + QMatrix4x4 GetViewMatrix(); + + // processes input received from any keyboard-like input system. Accepts input parameter in the form of camera defined ENUM (to abstract it from windowing systems) + void ProcessKeyboard(Camera_Movement direction, float deltaTime); + + // processes input received from a mouse input system. Expects the offset value in both the x and y direction. + void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true); + + // processes input received from a mouse scroll-wheel event. Only requires input on the vertical wheel-axis + void ProcessMouseScroll(float yoffset); + +private: + // calculates the front vector from the Camera's (updated) Euler Angles + void updateCameraVectors(); +}; + diff --git a/ArchitectureColoredPainting/MainWindow.cpp b/ArchitectureColoredPainting/MainWindow.cpp new file mode 100644 index 0000000..9b97b34 --- /dev/null +++ b/ArchitectureColoredPainting/MainWindow.cpp @@ -0,0 +1,10 @@ +#include "MainWindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) +{ + ui.setupUi(this); +} + +MainWindow::~MainWindow() +{} diff --git a/ArchitectureColoredPainting/MainWindow.h b/ArchitectureColoredPainting/MainWindow.h new file mode 100644 index 0000000..337ecec --- /dev/null +++ b/ArchitectureColoredPainting/MainWindow.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include "ui_MainWindow.h" + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindowClass ui; +}; diff --git a/ArchitectureColoredPainting/MainWindow.qrc b/ArchitectureColoredPainting/MainWindow.qrc new file mode 100644 index 0000000..68043e7 --- /dev/null +++ b/ArchitectureColoredPainting/MainWindow.qrc @@ -0,0 +1,4 @@ + + + + diff --git a/ArchitectureColoredPainting/MainWindow.ui b/ArchitectureColoredPainting/MainWindow.ui new file mode 100644 index 0000000..cdb031a --- /dev/null +++ b/ArchitectureColoredPainting/MainWindow.ui @@ -0,0 +1,36 @@ + + + MainWindowClass + + + + 0 + 0 + 600 + 400 + + + + MainWindow + + + + + + + + + + + + + RendererWidget + QOpenGLWidget +
RendererWidget.h
+
+
+ + + + +
diff --git a/ArchitectureColoredPainting/RendererWidget.cpp b/ArchitectureColoredPainting/RendererWidget.cpp new file mode 100644 index 0000000..18cec3f --- /dev/null +++ b/ArchitectureColoredPainting/RendererWidget.cpp @@ -0,0 +1,182 @@ +#include "RendererWidget.h" +#include +#include +#include +#include +//#include +//#include +RendererWidget::RendererWidget(QWidget* parent) + : QOpenGLWidget(parent), camera(QVector3D(0.0f, 0.0f, 3.0f)) +{ + startTimer(1000 / 120); + lastFrame = std::clock(); + setFocusPolicy(Qt::StrongFocus); + setMouseTracking(true); + setCursor(Qt::BlankCursor); +} + +RendererWidget::~RendererWidget() +{ + if (m_program == nullptr) + return; + makeCurrent(); + m_vbo.destroy(); + delete m_program; + m_program = nullptr; + doneCurrent(); +} + + +void RendererWidget::initializeGL() +{ + + initializeOpenGLFunctions(); + glEnable(GL_DEPTH_TEST); + glClearColor(0, 0, 0, 1); + std::string vertexCode; + std::string fragmentCode; + std::ifstream vShaderFile; + std::ifstream fShaderFile; + vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit); + try + { + vShaderFile.open("Shaders/shader.vert"); + fShaderFile.open("Shaders/shader.frag"); + std::stringstream vShaderStream, fShaderStream; + + vShaderStream << vShaderFile.rdbuf(); + fShaderStream << fShaderFile.rdbuf(); + + vShaderFile.close(); + fShaderFile.close(); + + vertexCode = vShaderStream.str(); + fragmentCode = fShaderStream.str(); + } + catch (std::ifstream::failure& e) + { + std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl; + } + + m_program = new QOpenGLShaderProgram; + m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexCode.c_str()); + m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentCode.c_str()); + m_program->link(); + m_program->bind(); + + m_vao.create(); + QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao); + + m_vbo.create(); + m_vbo.bind(); + + + + //Assimp::Importer importer; + + GLfloat vertex[] = { + -1,-1, 0, 0,0, + 1,-1, 0, 1,0, + -1, 1, 0, 0,1, + }; + + m_vbo.allocate(vertex, sizeof(vertex)); + m_vbo.bind(); + QOpenGLFunctions_4_5_Compatibility* f = QOpenGLContext::currentContext()->versionFunctions(); + f->glEnableVertexAttribArray(0); + f->glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), + nullptr); + f->glEnableVertexAttribArray(1); + f->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), + reinterpret_cast(3 * sizeof(GLfloat))); + m_vbo.release(); +} + +void RendererWidget::paintGL() +{ + //std::cout << (double)CLOCKS_PER_SEC/(std::clock() -lastFrame) << std::endl; + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao); + m_program->bind(); + // camera/view transformation + QMatrix4x4 view = camera.GetViewMatrix(); + m_program->setUniformValue(m_program->uniformLocation("view"), view); + glDrawArrays(GL_TRIANGLES, 0, 3); + + m_program->release(); +} + +void RendererWidget::resizeGL(int width, int height) +{ + m_program->bind(); + // pass projection matrix to shader (note that in this case it could change every frame) + QMatrix4x4 projection; + projection.perspective(camera.Zoom, (float)width / (float)height, 0.1f, 100.0f); + m_program->setUniformValue(m_program->uniformLocation("projection"), projection); + +} + +void RendererWidget::timerEvent(QTimerEvent* event) +{ + clock_t currentFrame = std::clock(); + deltaTime = (float)(std::clock() - lastFrame) / CLOCKS_PER_SEC; + lastFrame = currentFrame; + if (pressedKeys.contains(Qt::Key_Escape)) { + close(); + } + if (pressedKeys.contains(Qt::Key_W)) { + camera.ProcessKeyboard(FORWARD, deltaTime); + } + if (pressedKeys.contains(Qt::Key_S)) { + camera.ProcessKeyboard(BACKWARD, deltaTime); + } + if (pressedKeys.contains(Qt::Key_A)) { + camera.ProcessKeyboard(LEFT, deltaTime); + } + if (pressedKeys.contains(Qt::Key_D)) { + camera.ProcessKeyboard(RIGHT, deltaTime); + } + if (pressedKeys.contains(Qt::Key_Shift)) { + camera.ProcessKeyboard(DOWN, deltaTime); + } + if (pressedKeys.contains(Qt::Key_Space)) { + camera.ProcessKeyboard(UP, deltaTime); + } + repaint(); +} + +void RendererWidget::keyPressEvent(QKeyEvent* event) +{ + if (!event->isAutoRepeat()) + pressedKeys.insert(event->key()); + QOpenGLWidget::keyPressEvent(event); +} + +void RendererWidget::keyReleaseEvent(QKeyEvent* event) +{ + if (!event->isAutoRepeat()) + pressedKeys.remove(event->key()); + QOpenGLWidget::keyReleaseEvent(event); +} + +void RendererWidget::mouseMoveEvent(QMouseEvent* event) +{ + static bool firstMouse = true; + if (firstMouse) + { + firstMouse = false; + } + else + { + float xoffset = event->pos().x() - width() / 2; + float yoffset = height() / 2 - event->pos().y(); + camera.ProcessMouseMovement(xoffset, yoffset); + } + + cursor().setPos(mapToGlobal(QPoint(width() / 2, height() / 2))); + QOpenGLWidget::mouseMoveEvent(event); +} + diff --git a/ArchitectureColoredPainting/RendererWidget.h b/ArchitectureColoredPainting/RendererWidget.h new file mode 100644 index 0000000..94fbf09 --- /dev/null +++ b/ArchitectureColoredPainting/RendererWidget.h @@ -0,0 +1,35 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include "Camera.h" + +class RendererWidget : public QOpenGLWidget, protected QOpenGLFunctions_4_5_Compatibility +{ + Q_OBJECT +public: + RendererWidget(QWidget* parent = nullptr); + ~RendererWidget(); + +protected: + void initializeGL() override; + void paintGL() override; + void resizeGL(int width, int height) override; + void timerEvent(QTimerEvent* event) override; + void keyPressEvent(QKeyEvent* event) override; + void keyReleaseEvent(QKeyEvent* event) override; + void mouseMoveEvent(QMouseEvent* event) override; +private: + QSet pressedKeys; + Camera camera; + clock_t lastFrame; + float deltaTime; + QOpenGLShaderProgram* m_program = nullptr; + QOpenGLBuffer m_vbo; + QOpenGLVertexArrayObject m_vao; +}; + diff --git a/ArchitectureColoredPainting/Shaders/shader.frag b/ArchitectureColoredPainting/Shaders/shader.frag new file mode 100644 index 0000000..5c2d54f --- /dev/null +++ b/ArchitectureColoredPainting/Shaders/shader.frag @@ -0,0 +1,8 @@ +#version 330 core +out vec4 FragColor; +in vec2 texCoord; + +void main() +{ + FragColor = vec4(texCoord,1,1); +} diff --git a/ArchitectureColoredPainting/Shaders/shader.vert b/ArchitectureColoredPainting/Shaders/shader.vert new file mode 100644 index 0000000..2b96d45 --- /dev/null +++ b/ArchitectureColoredPainting/Shaders/shader.vert @@ -0,0 +1,11 @@ +#version 330 core +uniform mat4 view; +uniform mat4 projection; +layout (location = 0) in vec3 aPos; +layout (location = 1) in vec2 aTexCoord; +out vec2 texCoord; +void main() +{ + gl_Position = projection * view * vec4(aPos, 1.0f); + texCoord = aTexCoord; +} \ No newline at end of file diff --git a/ArchitectureColoredPainting/main.cpp b/ArchitectureColoredPainting/main.cpp new file mode 100644 index 0000000..2336890 --- /dev/null +++ b/ArchitectureColoredPainting/main.cpp @@ -0,0 +1,12 @@ +#include "MainWindow.h" +#include + +int main(int argc, char *argv[]) +{ + QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough); + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..f3f8f22 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +# ArchitectureColoredPainting \ No newline at end of file