performance test

This commit is contained in:
2025-01-04 13:09:10 -07:00
parent 5bd42f50bb
commit bd3bb970f8
6 changed files with 117 additions and 56 deletions

View File

@ -131,9 +131,12 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
<ClCompile Include="Square.cpp" />
<ClCompile Include="Window.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="deff.h" />
<ClInclude Include="Square.h" />
<ClInclude Include="Window.h" />
</ItemGroup>
<ItemGroup>

View File

@ -21,10 +21,19 @@
<ClCompile Include="Window.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="Square.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="Window.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="Square.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="deff.h">
<Filter>Source Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

60
2dGameProject/Square.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "Square.h"
#include <glad/glad.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "deff.h"
Square::Square(glm::vec3 position, glm::vec3 scale, glm::vec4 color) {
//Initialize Member Variables
this->position = position;
this->scale = scale;
this->color = color;
model_transform = glm::mat4(1.0f);
model_transform = glm::translate(model_transform, position);
model_transform = glm::scale(model_transform, scale);
//Generate Resources
glGenVertexArrays(1, &VAO);
unsigned int buffers[2];
glGenBuffers(2, buffers);
VBO = buffers[0];
EBO = buffers[1];
//Initialize Resources
float vertexData[] = {
0.0f, 0.0f, 0.0f, color.r, color.g, color.b, color.a,
1.0f, 0.0f, 0.0f, color.r, color.g, color.b, color.a,
1.0f, 1.0f, 0.0f, color.r, color.g, color.b, color.a,
0.0f, 1.0f, 0.0f, color.r, color.g, color.b, color.a
};
unsigned int indices[]{
0, 1, 2,
0, 2, 3
};
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)0);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 7 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindVertexArray(NULL);
}
Square::~Square() {
unsigned int buffers[] = { VBO, EBO };
glDeleteBuffers(2, buffers);
glDeleteVertexArrays(1, &VAO);
}
void Square::Draw() {
glUniformMatrix4fv(UNIFORM_MODEL_MAT, 1, GL_FALSE, glm::value_ptr(model_transform));
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(NULL);
}

18
2dGameProject/Square.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#include <glm/glm.hpp>
class Square {
private:
unsigned int VAO, VBO, EBO;
glm::vec3 position;
glm::vec3 scale;
glm::vec4 color;
glm::mat4 model_transform;
public:
Square(glm::vec3 position, glm::vec3 scale, glm::vec4 color);
~Square();
void Draw();
};

4
2dGameProject/deff.h Normal file
View File

@ -0,0 +1,4 @@
#pragma once
#define UNIFORM_PROJ_MAT 0
#define UNIFORM_MODEL_MAT 1

View File

@ -1,5 +1,4 @@
#define GLFW_INCLUDE_NONE
#include <iostream>
#include <GLFW/glfw3.h>
#include "Window.h"
@ -12,6 +11,9 @@
#include <spdlog/sinks/basic_file_sink.h>
#include <memory>
#include <chrono>
#include "Square.h"
#include "deff.h"
#include <array>
int OpenGLVersion;
spdlog::logger logger("none");
@ -36,10 +38,11 @@ float squareColors[] = {
};
const char* vertexShaderSource = "#version 330 core\n"
"#extension GL_ARB_explicit_uniform_location: enable\n"
"layout (location = 0) in vec3 aPos;\n"
"layout (location = 1) in vec4 color;\n"
"uniform mat4 projection;\n"
"uniform mat4 model;\n"
"layout (location = 0) uniform mat4 projection;\n"
"layout (location = 1) uniform mat4 model;\n"
"out vec4 vertexColor;\n"
"void main()\n"
"{\n"
@ -93,19 +96,11 @@ int main() {
init();
Window* window = new Window(640, 480, "Test Window");
//window->setWindowedFullscreen();
window->beginRender();
glfwSwapInterval(0);
openGLContextInit();
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(square), square, GL_STATIC_DRAW);
unsigned int vertexShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
@ -149,58 +144,30 @@ int main() {
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
unsigned int colorBuffer;
glGenBuffers(1, &colorBuffer);
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(squareColors), squareColors, GL_STATIC_DRAW);
glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(squareIndices), squareIndices, GL_STATIC_DRAW);
glBindVertexArray(NULL);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, NULL);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
bool up = false;
float interval = 0.001;
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
unsigned int projMatLocation = glGetUniformLocation(shaderProgram, "projection");
glUniformMatrix4fv(projMatLocation, 1, GL_FALSE, glm::value_ptr(glm::ortho(0.0f, 800.0f, 0.0f, 600.0f)));
unsigned int modelMatLocation = glGetUniformLocation(shaderProgram, "model");
glUniformMatrix4fv(modelMatLocation, 1, GL_FALSE, glm::value_ptr(glm::scale(glm::translate(glm::mat4(1.0), glm::vec3(400, 300, 0)), glm::vec3(100, 100, 1))));
glUniformMatrix4fv(UNIFORM_PROJ_MAT, 1, GL_FALSE, glm::value_ptr(glm::ortho(0.0f, 800.0f, 600.0f, 0.0f)));
std::array<Square*, 100*100> squares;
for (int i = 0; i < 100 * 100; i++) {
int x = i % 100;
int y = i / 100;
squares[i] = new Square(glm::vec3(x*6, y*6, 0.0f), glm::vec3(6.0f, 6.0f, 1.0f), glm::vec4(0.0f, 1.0f, 0.0f, 1.0f));
}
//Square square({ 0, 0, 0 }, { 6, 6, 0 }, {0.0f, 1.0f, 0.0f, 1.0f});
while (!window->isClosing()) {
if (up) {
squareColors[0] += interval;
if (squareColors[0] >= 1.0) {
squareColors[0] = 1.0;
up = false;
}
} else {
squareColors[0] -= interval;
if (squareColors[0] <= 0.0) {
squareColors[0] = 0.0;
up = true;
}
//square.Draw();
for (auto square : squares) {
square->Draw();
}
glBindBuffer(GL_ARRAY_BUFFER, colorBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(squareColors), squareColors, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, NULL);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(NULL);
window->endRender();
glfwPollEvents();
}
for (auto square : squares) {
delete square;
}
deinit();
return 0;