Skip to content
Snippets Groups Projects
Commit d3be0ee6 authored by Armin Co's avatar Armin Co
Browse files

Version 1.0

parent 90b6ceb2
Branches push_to_gitlab
No related tags found
No related merge requests found
Showing with 735 additions and 83 deletions
......@@ -3,6 +3,7 @@ project(QPong VERSION 0.0.1 LANGUAGES C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "-O3 -Wall -Wextra")
# Check for PkgConfig
find_package(PkgConfig REQUIRED)
......
......@@ -4,7 +4,6 @@
#include <GLFW/glfw3.h>
#include "Application.hpp"
#include "Log.hpp"
#include "ApplicationEvent.hpp"
......@@ -65,10 +64,13 @@ namespace GuiCore
Timestep timeStep = time - m_lastFrameTime;
m_lastFrameTime = time;
if (!m_minimzed)
{
for (auto layer : m_layerStack)
{
layer->onUpdate(timeStep);
}
}
m_guiLayer->begin();
for (auto layer: m_layerStack)
......@@ -89,6 +91,10 @@ namespace GuiCore
bool Application::onWindowResize(Event &event)
{
WindowResizeEvent *rsEvent = static_cast<WindowResizeEvent*>(&event);
Log::get()->trace("Window minimized, {0}, {1}", rsEvent->getWidth(), rsEvent->getHeight());
glViewport(0,0, rsEvent->getWidth(), rsEvent->getHeight());
return false;
}
......
......@@ -12,6 +12,7 @@
#include "IWindow.hpp"
#include "LayerStack.hpp"
#include "ImGuiLayer.hpp"
#include "ApplicationEvent.hpp"
namespace GuiCore
{
......@@ -34,6 +35,7 @@ namespace GuiCore
private:
std::unique_ptr<IWindow> m_window;
bool m_isRunnig = true;
bool m_minimzed = false;
LayerStack m_layerStack;
float m_lastFrameTime = 0.0f;
std::shared_ptr<ImGuiLayer> m_guiLayer;
......
find_package(Freetype REQUIRED)
# GuiCore library
add_library(GuiCore
Application.cpp
......@@ -6,17 +8,20 @@ add_library(GuiCore
LayerStack.cpp
Log.cpp
Timestep.cpp
Glyphs.cpp
)
target_link_libraries(GuiCore
spdlog
)
target_include_directories(GuiCore PRIVATE
target_include_directories(GuiCore PUBLIC
../libs/spdlog/include/
${FREETYPE_INCLUDE_DIRS}
)
target_link_libraries(GuiCore
spdlog
${FREETYPE_LIBRARIES}
)
# ImGui is a dependency of GuiCore
add_library(ImGui
Shader.cpp
......@@ -29,8 +34,11 @@ add_library(ImGui
../libs/imgui/backends/imgui_impl_opengl3.cpp
../libs/imgui/examples/libs/gl3w/GL/gl3w.c
)
# target_include_directories(ImGui PRIVATE
# ${FREETYPE_INCLUDE_DIRS}
# )
target_include_directories(ImGui PRIVATE
target_include_directories(ImGui PUBLIC
../libs
../libs/imgui # for glfw
../libs/imgui/backends
......@@ -40,6 +48,7 @@ target_include_directories(ImGui PRIVATE
target_link_libraries(GuiCore
ImGui
# ${FREETYPE_LIBRARIES}
)
# Check for GLFW3 and link
......
/// @file Glyphs.cpp
/// @author Armin Co
///
#include "Glyphs.hpp"
#include <GLFW/glfw3.h>
#include "Log.hpp"
#include <glm/gtc/type_ptr.hpp>
extern "C" {
#include <ft2build.h>
#include FT_FREETYPE_H
}
std::map<char, GuiCore::Character> GuiCore::Glyphs::s_characters;
GuiCore::Shader* GuiCore::Glyphs::m_shader;
GLuint GuiCore::Glyphs::m_vertexArray;
GLuint GuiCore::Glyphs::m_vertexBuffer;
glm::mat4 GuiCore::Glyphs::projection = glm::ortho(-3.2f, 3.2f, -1.8f, 1.8f, -1.0f, 1.0f);
void GuiCore::Glyphs::setup()
{
m_shader = GuiCore::Shader::fromGLSLTextFiles(
"assets/shaders/glyph.vert.glsl",
"assets/shaders/glyph.frag.glsl"
);
glGenVertexArrays(1, &m_vertexArray);
glGenBuffers(1, &m_vertexBuffer);
glBindVertexArray(m_vertexArray);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(float) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(float), 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
FT_Library ft;
if (FT_Init_FreeType(&ft))
{
GuiCore::Log::get()->error("ERROR::FREETYPE: Could not init FreeType Library");
}
FT_Face face;
if (FT_New_Face(ft, "assets/arial.ttf", 0, &face))
{
GuiCore::Log::get()->error("ERROR::FREETYPE: Failed to load font");
}
FT_Set_Pixel_Sizes(face, 0, 48);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
for (unsigned char c = 0; c < 128; c++)
{
// load character glyph
auto error = FT_Load_Char(face, c, FT_LOAD_RENDER);
if (error)
{
GuiCore::Log::get()->error("ERROR::FREETYTPE: Failed to load Glyph {0} {1}", c, error);
continue;
}
// generate texture
unsigned int texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RED,
face->glyph->bitmap.width,
face->glyph->bitmap.rows,
0,
GL_RED,
GL_UNSIGNED_BYTE,
face->glyph->bitmap.buffer
);
// set texture options
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// now store character for later use
Character character = {
texture,
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
face->glyph->advance.x
};
s_characters.insert(std::pair<char, Character>(c, character));
}
}
void GuiCore::Glyphs::renderText(std::string text, float x, float y, float scale, glm::vec3 color)
{
glUseProgram(m_shader->getRendererID());
glUniformMatrix4fv(glGetUniformLocation(m_shader->getRendererID(), "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3f(glGetUniformLocation(m_shader->getRendererID(), "textColor"), color.r, color.g, color.b);
glActiveTexture(GL_TEXTURE0);
glBindVertexArray(m_vertexArray);
std::string::const_iterator c;
for (c = text.begin(); c != text.end(); c++)
{
Character ch = s_characters[*c];
float xPos = x + ch.bearing.x * scale;
float yPos = y - (ch.size.y - ch.bearing.y) * scale;
float w = ch.size.x * scale;
float h = ch.size.y * scale;
float vertices[6][4] = {
{ xPos, yPos + h, 0.0f, 0.0f },
{ xPos, yPos, 0.0f, 1.0f },
{ xPos + w, yPos, 1.0f, 1.0f },
{ xPos, yPos + h, 0.0f, 0.0f },
{ xPos + w, yPos, 1.0f, 1.0f },
{ xPos + w, yPos + h, 1.0f, 0.0f }
};
glBindTexture(GL_TEXTURE_2D, ch.textureId);
glBindBuffer(GL_ARRAY_BUFFER, m_vertexBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertices), vertices);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (ch.advance >> 6) * scale;
}
glBindVertexArray(0);
glBindTexture(GL_TEXTURE_2D, 0);
}
\ No newline at end of file
/// @file Glyphs.hpp
/// @author Armin Co
///
#ifndef GUI_CORE_GLYPHS_HPP
#define GUI_CORE_GLYPHS_HPP
#include <map>
#include <glm/glm.hpp>
#include "Shader.hpp"
namespace GuiCore
{
struct Character {
unsigned int textureId; // ID handle of the glyph texture
glm::ivec2 size; // Size of glyph
glm::ivec2 bearing; // Offset from baseline to left/top of glyph
unsigned int advance; // Offset to advance to next glyph
};
class Glyphs
{
public:
static void setup();
static void renderText(std::string text, float x, float y, float scale, glm::vec3 color);
private:
static std::map<char, Character> s_characters;
static Shader *m_shader;
static GLuint m_vertexArray;
static GLuint m_vertexBuffer;
static glm::mat4 projection;
};
}
#endif
\ No newline at end of file
......@@ -8,6 +8,8 @@
#include "Shader.hpp"
#include "Log.hpp"
namespace GuiCore
{
static std::string readFileAsString(const std::string &filepath)
......
......@@ -8,7 +8,6 @@
#include <string>
#include <GL/gl3w.h>
#include <GLFW/glfw3.h>
namespace GuiCore
{
class Shader
......
This diff is collapsed.
......@@ -29,11 +29,37 @@ public:
{
glm::vec3 position;
glm::vec3 momentum;
double smoothEdge;
double pointUnsharpness;
double startValue;
double hbar;
};
struct Player
{
double currentlyScored;
int totalScore;
};
class Bat
{
public:
Bat(double x, double y, int keyUp, int keyDown);
double getX() const;
double getY() const;
void onUpdate(GuiCore::Timestep ts);
double getPotential(double x, double y) const;
private:
double m_x;
double m_y;
double m_vx;
double m_vy;
double m_height;
double m_width;
int m_keyUp;
int m_keyDown;
static double s_speedStepSize;
};
GameLayer(Properties props);
virtual ~GameLayer(){};
virtual void onAttach() override;
......@@ -43,18 +69,25 @@ public:
virtual void onEvent(GuiCore::Event &event) override;
private:
void reset();
void calculatePointPositions(float *points);
void calculateMomentumViewPositions(float *points);
void calculateTriangleIndices(uint32_t *indices);
void calculateBorderPotential(float *potential);
void initialiseParticleMomentum();
void propagateStep(double dt);
void applyPotentials(int indexBegin, int indexEnd, double dt);
void moveForward(int indexBegin, int indexEnd, double dt);
void updateMomentumView(int indexBegin, int indexEnd);
double absorbParticle(int i, double c);
// positions at array index
inline const double posAt(int pos) const;
inline const double xAtIndex(int i) const;
inline const double yAtIndex(int i) const;
inline const double potentialAt(int pos) const;
inline const double pxAtIndex(int i) const;
inline const double pyAtIndex(int i) const;
inline double posAt(int pos) const;
inline double xAtIndex(int i) const;
inline double yAtIndex(int i) const;
inline double potentialAt(int pos) const;
inline double pxAtIndex(int i) const;
inline double pyAtIndex(int i) const;
GuiCore::Shader *m_shader;
std::string m_vertexShaderPath;
......@@ -68,22 +101,39 @@ private:
GLuint m_vertexArray; ///< OpenGL object that "knows" all buffers.
GLuint m_pointsBuffer; ///< Buffer with the location for each element in the particle array
GLuint m_indicesBuffer; ///< Indicies to draw trinagles from the points to fill the "canvas"
GLuint m_particleBuffer; ///< Reference for OpenGL to load the paritcle data to the GPU
GLuint m_gameObjectsBuffer; ///< Buffer for game objects like borders and bats.
GLuint m_colorBuffer; ///< Reference for OpenGL to load the paritcle data to the GPU
GLuint m_potentialBuffer; ///< Borders and bats
float *m_particleViewPositions;
float *m_momentumViewPositions;
float *m_staticBorderPotential;
float *m_dynamicPotential;
glm::mat4 m_viewPojection;
glm::mat4 m_viewProjectionPosition;
int m_viewProjectionLocation;
GLuint m_showPotentialsLocation;
ParticleProps m_initialParticleProperties;
double m_particleAbsouluteSumStart;
fftw_plan m_planForward;
fftw_plan m_planBackwards;
Bat m_leftBat;
Bat m_rightBat;
Player m_playerOne;
Player m_playerTwo;
bool m_gameCounts;
bool m_restart;
bool m_propagate;
bool m_renderMomentum;
bool m_removeParticleEnabled;
bool m_potentialsEnabled;
int m_renderingThreadsNumber {8};
size_t m_memorySize {0};
float m_speedScale {1.0f};
std::complex<double> *m_psi;
std::complex<double> *m_momentum;
};
}
#endif
\ No newline at end of file
......@@ -23,6 +23,8 @@ void MainMenuLayer::onAttach()
{
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_backgroundColor = {0.2f, 0.2f, 0.2f};
createNewGame();
}
......
......@@ -75,8 +75,6 @@ void Window::setup(const GuiCore::WindowProperties &properties)
WindowCloseEvent event;
data.eventCallback(event);
});
/// @todo add key callback ...
}
void Window::shutdown()
......
File added
#version 450 core
in vec2 textureCoordinates;
out vec4 color;
uniform sampler2D text;
uniform vec3 textColor;
void main()
{
color = vec4(textColor, texture(text, textureCoordinates).r) ;
}
\ No newline at end of file
#version 450 core
layout (location = 0) in vec4 vertex;
out vec2 textureCoordinates;
uniform mat4 projection;
void main()
{
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);
textureCoordinates = vertex.zw;
}
\ No newline at end of file
#version 450 core
in vec3 v_fragmentColor;
out vec3 pixelColor;
in vec3 fragmentColor;
out vec4 pixelColor;
void main()
{
pixelColor = v_fragmentColor;
pixelColor = vec4(fragmentColor, 1.0);
}
\ No newline at end of file
#version 450 core
layout (location = 0) in vec3 v_position;
layout (location = 1) in vec2 v_particle_complex;
layout (location = 2) in float potential;
uniform mat4 u_viewProjection;
uniform int u_showPotential;
out vec3 fragmentColor;
void main()
{
float pot = potential * 120.0;
if (u_showPotential==0)
{
pot = 0.0;
}
gl_Position = u_viewProjection * vec4(v_position, 1.0f);
float real = v_particle_complex[0];
float imag = v_particle_complex[1];
float real2 = real * real;
float imag2 = imag * imag;
fragmentColor = vec3(
sqrt(real2) * 20.0,
sqrt(real2) * 15.0 + sqrt(imag2) * 15.0,
sqrt(imag2) * 20.0);
sqrt(real2) * 5.0 + pot,
sqrt(real2) * 5.0 + sqrt(imag2) * 5.0 + pot / 2.0,
sqrt(imag2) * 5.0);
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment