Format code

This commit is contained in:
MrLetsplay 2023-09-13 20:42:01 +02:00
parent d47eb8c943
commit 74367e5fb0
Signed by: mr
SSH Key Fingerprint: SHA256:92jBH80vpXyaZHjaIl47pjRq+Yt7XGTArqQg1V7hSqg
43 changed files with 365 additions and 360 deletions

View File

@ -1,11 +1,11 @@
#include "defaultplayercontroller.h"
#include "input.h" #include "input.h"
#include "internal.h"
#include "internal/physics.h"
#include "internal/ui.h"
#include "noclipplayercontroller.h"
#include "ui.h" #include "ui.h"
#include "uielements.h" #include "uielements.h"
#include "defaultplayercontroller.h"
#include "noclipplayercontroller.h"
#include "internal.h"
#include "internal/ui.h"
#include "internal/physics.h"
namespace kek::Defaults { namespace kek::Defaults {
@ -88,7 +88,7 @@ static void defaultKeyCallback(GLFWwindow *window, int key, int scancode, int ac
double x, y; double x, y;
glfwGetCursorPos(kekData.window, &x, &y); glfwGetCursorPos(kekData.window, &x, &y);
doUIMouseHover(x, y); doUIMouseHover(x, y);
}else { } else {
Input::setCursorMode(GLFWCursorMode::CAPTURE); Input::setCursorMode(GLFWCursorMode::CAPTURE);
for(auto e : UI::getElements()) { for(auto e : UI::getElements()) {
@ -102,7 +102,7 @@ static void defaultKeyCallback(GLFWwindow *window, int key, int scancode, int ac
kekData.player->controller = kekData.player->noclip ? (PlayerController *) noclipController : (PlayerController *) defaultController; kekData.player->controller = kekData.player->noclip ? (PlayerController *) noclipController : (PlayerController *) defaultController;
if(kekData.player->noclip) { if(kekData.player->noclip) {
kekData.physics->world->removeRigidBody(kekData.player->physics->body); kekData.physics->world->removeRigidBody(kekData.player->physics->body);
}else { } else {
kekData.physics->world->addRigidBody(kekData.player->physics->body); kekData.physics->world->addRigidBody(kekData.player->physics->body);
} }
} }
@ -118,8 +118,7 @@ static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *d
} }
switch(Input::getCursorMode()) { switch(Input::getCursorMode()) {
case GLFWCursorMode::CAPTURE: case GLFWCursorMode::CAPTURE: {
{
float xoff = lastX - x; float xoff = lastX - x;
float yoff = lastY - y; float yoff = lastY - y;
@ -131,8 +130,7 @@ static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *d
break; break;
} }
case GLFWCursorMode::FREE: case GLFWCursorMode::FREE:
case GLFWCursorMode::HIDDEN: case GLFWCursorMode::HIDDEN: {
{
doUIMouseHover(x, y); doUIMouseHover(x, y);
break; break;
} }
@ -142,21 +140,18 @@ static void defaultMouseCallback(GLFWwindow *window, double x, double y, void *d
lastY = y; lastY = y;
} }
static void defaultMouseButtonCallback(GLFWwindow* window, int button, int action, int mods, void *data) { static void defaultMouseButtonCallback(GLFWwindow *window, int button, int action, int mods, void *data) {
switch(Input::getCursorMode()) { switch(Input::getCursorMode()) {
case GLFWCursorMode::CAPTURE: case GLFWCursorMode::CAPTURE: {
{
// TODO // TODO
break; break;
} }
case GLFWCursorMode::FREE: case GLFWCursorMode::FREE:
case GLFWCursorMode::HIDDEN: case GLFWCursorMode::HIDDEN: {
{
switch(action) { switch(action) {
case GLFW_PRESS: case GLFW_PRESS:
break; break;
case GLFW_RELEASE: case GLFW_RELEASE: {
{
double x, y; double x, y;
glfwGetCursorPos(window, &x, &y); glfwGetCursorPos(window, &x, &y);
doUIMouseClick(x, y, (GLFWMouseButton) button); doUIMouseClick(x, y, (GLFWMouseButton) button);
@ -188,7 +183,7 @@ void init() {
Input::addMouseButtonListener(MouseButtonCallback(defaultMouseButtonCallback, nullptr)); Input::addMouseButtonListener(MouseButtonCallback(defaultMouseButtonCallback, nullptr));
options = new ButtonElement(uiPx(0), uiPx(100), uiPx(100), uiPx(50)); options = new ButtonElement(uiPx(0), uiPx(100), uiPx(100), uiPx(50));
//UI::addElement(options); // UI::addElement(options);
kekData.player = new Player(); kekData.player = new Player();
btVector3 positions[2]; btVector3 positions[2];
@ -204,23 +199,22 @@ void init() {
btTransform from; btTransform from;
from.setIdentity(); from.setIdentity();
from.setOrigin(btVector3(0,0,0)); from.setOrigin(btVector3(0, 0, 0));
btTransform to; btTransform to;
to.setIdentity(); to.setIdentity();
to.setOrigin(btVector3(1,1,1)); to.setOrigin(btVector3(1, 1, 1));
btCollisionWorld::ClosestConvexResultCallback cb = btCollisionWorld::ClosestConvexResultCallback(from.getOrigin(), to.getOrigin()); btCollisionWorld::ClosestConvexResultCallback cb = btCollisionWorld::ClosestConvexResultCallback(from.getOrigin(), to.getOrigin());
kekData.physics->world->convexSweepTest((btConvexShape *) shape, from, to, cb); kekData.physics->world->convexSweepTest((btConvexShape *) shape, from, to, cb);
kekData.player->physics = new PlayerPhysicsObjectData(); kekData.player->physics = new PlayerPhysicsObjectData();
kekData.player->physics->body = body; kekData.player->physics->body = body;
kekData.player->moveTo(glm::vec3(5,10,0)); kekData.player->moveTo(glm::vec3(5, 10, 0));
kekData.player->controller = defaultController; kekData.player->controller = defaultController;
} }
void destroy() { void destroy() {
} }
} }

View File

@ -1,33 +1,33 @@
#include "engine.h" #include "engine.h"
#include <iostream> #include <chrono>
#include <cstring> #include <cstring>
#include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <chrono>
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION #define STB_IMAGE_WRITE_IMPLEMENTATION
#include <stb_image.h> #include <stb_image.h>
#include <stb_image_write.h> #include <stb_image_write.h>
#include "errordialog.h"
#include "objparser.h"
#include "resource.h"
#include "camera.h" #include "camera.h"
#include "constants.h" #include "constants.h"
#include "gameobject.h"
#include "scene.h"
#include "defaults.h" #include "defaults.h"
#include "errordialog.h"
#include "gameobject.h"
#include "objparser.h"
#include "physics.h"
#include "resource.h"
#include "scene.h"
#include "ui.h" #include "ui.h"
#include "uielements.h" #include "uielements.h"
#include "physics.h"
#include "internal.h" #include "internal.h"
#include "internal/ui.h"
#include "internal/input.h" #include "internal/input.h"
#include "internal/ui.h"
kek::KekData kek::kekData; kek::KekData kek::kekData;
@ -42,19 +42,19 @@ static void framebufferSizeCallback(GLFWwindow *window, int w, int h) {
} }
static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char *message, const void *userParam) { static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum severity, GLsizei length, const char *message, const void *userParam) {
//if(id == 0x20071) return; // if(id == 0x20071) return;
if(severity == GL_DEBUG_SEVERITY_NOTIFICATION) return; if(severity == GL_DEBUG_SEVERITY_NOTIFICATION) return;
std::cout << "OpenGL Debug (" << id << "): " << message << std::endl; std::cout << "OpenGL Debug (" << id << "): " << message << std::endl;
} }
int init() { int init() {
// Init GLFW // Init GLFW
if (glfwInit() != GL_TRUE) { if(glfwInit() != GL_TRUE) {
const char *errorMsg; const char *errorMsg;
int code = glfwGetError(&errorMsg); int code = glfwGetError(&errorMsg);
if(code != GLFW_NO_ERROR) { if(code != GLFW_NO_ERROR) {
ErrorDialog::showError("Failed to initialize GLFW: " + std::string(errorMsg) + std::string(" (") + std::to_string(code) + std::string(")")); ErrorDialog::showError("Failed to initialize GLFW: " + std::string(errorMsg) + std::string(" (") + std::to_string(code) + std::string(")"));
}else { } else {
ErrorDialog::showError("Failed to initialize GLFW: Unknown error"); ErrorDialog::showError("Failed to initialize GLFW: Unknown error");
} }
@ -79,7 +79,7 @@ int init() {
int code = glfwGetError(&errorMsg); int code = glfwGetError(&errorMsg);
if(code != GLFW_NO_ERROR) { if(code != GLFW_NO_ERROR) {
ErrorDialog::showError("Failed to create window: " + std::string(errorMsg) + " (" + std::to_string(code) + ")"); ErrorDialog::showError("Failed to create window: " + std::string(errorMsg) + " (" + std::to_string(code) + ")");
}else { } else {
ErrorDialog::showError("Failed to create window: Unknown error"); ErrorDialog::showError("Failed to create window: Unknown error");
} }
@ -93,7 +93,7 @@ int init() {
// Init GLEW // Init GLEW
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) { if(glewInit() != GLEW_OK) {
ErrorDialog::showError("Failed to initialize GLEW"); ErrorDialog::showError("Failed to initialize GLEW");
glfwTerminate(); glfwTerminate();
return KEK_ERROR; return KEK_ERROR;
@ -105,7 +105,7 @@ int init() {
int flags; int flags;
glGetIntegerv(GL_CONTEXT_FLAGS, &flags); glGetIntegerv(GL_CONTEXT_FLAGS, &flags);
if (flags & GL_CONTEXT_FLAG_DEBUG_BIT) { if(flags & GL_CONTEXT_FLAG_DEBUG_BIT) {
glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageCallback(glDebugOutput, nullptr); glDebugMessageCallback(glDebugOutput, nullptr);
@ -122,7 +122,7 @@ int init() {
glCullFace(GL_BACK); glCullFace(GL_BACK);
glClearColor(0.1f, 0.3f, 0.1f, 0.0f); glClearColor(0.1f, 0.3f, 0.1f, 0.0f);
//glfwSwapInterval(0); // glfwSwapInterval(0);
stbi_set_flip_vertically_on_load(true); stbi_set_flip_vertically_on_load(true);
@ -228,23 +228,20 @@ int start() {
ShaderLight shLight; ShaderLight shLight;
memcpy(shLight.color, glm::value_ptr(light->color), sizeof(shLight.color)); memcpy(shLight.color, glm::value_ptr(light->color), sizeof(shLight.color));
switch(light->getType()) { switch(light->getType()) {
case LightType::POINT: case LightType::POINT: {
{
PointLight *l = (PointLight *) light; PointLight *l = (PointLight *) light;
memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position)); memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position));
memcpy(&shLight.attenuation, glm::value_ptr(glm::vec3(l->constant, l->linear, l->quadratic)), sizeof(shLight.attenuation)); memcpy(&shLight.attenuation, glm::value_ptr(glm::vec3(l->constant, l->linear, l->quadratic)), sizeof(shLight.attenuation));
numPointLights++; numPointLights++;
break; break;
} }
case LightType::DIRECTIONAL: case LightType::DIRECTIONAL: {
{
DirectionalLight *l = (DirectionalLight *) light; DirectionalLight *l = (DirectionalLight *) light;
memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction)); memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction));
numDirectionalLights++; numDirectionalLights++;
break; break;
} }
case LightType::SPOT: case LightType::SPOT: {
{
SpotLight *l = (SpotLight *) light; SpotLight *l = (SpotLight *) light;
memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position)); memcpy(&shLight.position, glm::value_ptr(l->getPosition()), sizeof(shLight.position));
memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction)); memcpy(&shLight.direction, glm::value_ptr(l->direction), sizeof(shLight.direction));

View File

@ -1,7 +1,7 @@
#include "input.h" #include "input.h"
#include <map>
#include <iostream> #include <iostream>
#include <map>
#include "internal.h" #include "internal.h"
#include "internal/input.h" #include "internal/input.h"
@ -55,7 +55,7 @@ void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint) {
} }
} }
void onMouseButtonCallback(GLFWwindow* window, int button, int action, int mods) { void onMouseButtonCallback(GLFWwindow *window, int button, int action, int mods) {
for(auto cb : kekData.input->mouseButtonCallbacks) { for(auto cb : kekData.input->mouseButtonCallbacks) {
cb.second(window, button, action, mods); cb.second(window, button, action, mods);
} }
@ -157,8 +157,7 @@ KeyboardCapture captureKeyboardInput(KeyCharCallback charCallback, KeyCallback k
id, id,
charCallback, charCallback,
keyCallback, keyCallback,
uncaptureCallback uncaptureCallback};
};
return id; return id;
} }
@ -169,8 +168,7 @@ bool uncaptureKeyboardInput(KeyboardCapture capture) {
KEK_INVALID_ID, KEK_INVALID_ID,
KeyCharCallback(), KeyCharCallback(),
KeyCallback(), KeyCallback(),
Callable() Callable()};
};
return true; return true;
} }

View File

@ -34,7 +34,7 @@ void GameObject::draw(Shader *shader) {
void GameObject::addPhysics(btCollisionShape *shape, float mass, int collisionFlags) { void GameObject::addPhysics(btCollisionShape *shape, float mass, int collisionFlags) {
this->physics = new PhysicsObjectData(); this->physics = new PhysicsObjectData();
btVector3 inertia = btVector3(0,0,0); btVector3 inertia = btVector3(0, 0, 0);
shape->calculateLocalInertia(mass, inertia); shape->calculateLocalInertia(mass, inertia);
btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape, inertia); btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape, inertia);
body->setFriction(1); body->setFriction(1);

View File

@ -1,7 +1,7 @@
#include <vector> #include <vector>
#include "light.h"
#include "constants.h" #include "constants.h"
#include "light.h"
namespace kek { namespace kek {
@ -10,40 +10,40 @@ Light::Light(glm::vec3 color) {
} }
Light::~Light() { Light::~Light() {
} }
PointLight::PointLight(glm::vec3 color, float constant, float linear, float quadratic): Light(color) { PointLight::PointLight(glm::vec3 color, float constant, float linear, float quadratic)
: Light(color) {
this->constant = constant; this->constant = constant;
this->linear = linear; this->linear = linear;
this->quadratic = quadratic; this->quadratic = quadratic;
} }
PointLight::~PointLight() { PointLight::~PointLight() {
} }
LightType PointLight::getType() { LightType PointLight::getType() {
return LightType::POINT; return LightType::POINT;
} }
DirectionalLight::DirectionalLight(glm::vec3 color, glm::vec3 direction): Light(color) { DirectionalLight::DirectionalLight(glm::vec3 color, glm::vec3 direction)
: Light(color) {
this->direction = direction; this->direction = direction;
} }
DirectionalLight::~DirectionalLight() { DirectionalLight::~DirectionalLight() {
} }
LightType DirectionalLight::getType() { LightType DirectionalLight::getType() {
return LightType::DIRECTIONAL; return LightType::DIRECTIONAL;
} }
void DirectionalLight::lookAt(glm::vec3 direction){ void DirectionalLight::lookAt(glm::vec3 direction) {
this->direction = direction; this->direction = direction;
} }
SpotLight::SpotLight(glm::vec3 color, glm::vec3 direction, float constant, float linear, float quadratic, float innerCutoff, float outerCutoff): Light(color) { SpotLight::SpotLight(glm::vec3 color, glm::vec3 direction, float constant, float linear, float quadratic, float innerCutoff, float outerCutoff)
: Light(color) {
this->direction = direction; this->direction = direction;
this->constant = constant; this->constant = constant;
this->linear = linear; this->linear = linear;
@ -53,7 +53,6 @@ SpotLight::SpotLight(glm::vec3 color, glm::vec3 direction, float constant, float
} }
SpotLight::~SpotLight() { SpotLight::~SpotLight() {
} }
LightType SpotLight::getType() { LightType SpotLight::getType() {
@ -65,14 +64,19 @@ void SpotLight::lookAt(glm::vec3 direction) {
} }
LightList::LightList() { LightList::LightList() {
} }
void LightList::add(Light *light) { void LightList::add(Light *light) {
switch(light->getType()) { switch(light->getType()) {
case LightType::POINT: point.push_back((PointLight *) light); break; case LightType::POINT:
case LightType::DIRECTIONAL: directional.push_back((DirectionalLight *) light); break; point.push_back((PointLight *) light);
case LightType::SPOT: spot.push_back((SpotLight *) light); break; break;
case LightType::DIRECTIONAL:
directional.push_back((DirectionalLight *) light);
break;
case LightType::SPOT:
spot.push_back((SpotLight *) light);
break;
} }
} }

View File

@ -3,8 +3,8 @@
#include "internal.h" #include "internal.h"
#include "internal/physics.h" #include "internal/physics.h"
#include <glm/gtx/vector_angle.hpp>
#include <BulletCollision/CollisionDispatch/btCollisionWorld.h> #include <BulletCollision/CollisionDispatch/btCollisionWorld.h>
#include <glm/gtx/vector_angle.hpp>
namespace kek { namespace kek {
@ -31,7 +31,7 @@ static bool castPlayer(glm::vec3 pos, glm::vec3 delta, const btCollisionObject *
} }
static bool checkPlayerGrounded(float groundDistance, float maxWalkAngle, const btCollisionObject **outGround, glm::vec3 *outNormal) { static bool checkPlayerGrounded(float groundDistance, float maxWalkAngle, const btCollisionObject **outGround, glm::vec3 *outNormal) {
glm::vec3 groundDir = glm::vec3(0,-groundDistance,0); glm::vec3 groundDir = glm::vec3(0, -groundDistance, 0);
glm::vec3 point; glm::vec3 point;
glm::vec3 normal; glm::vec3 normal;
@ -49,11 +49,9 @@ static bool checkPlayerGrounded(float groundDistance, float maxWalkAngle, const
} }
DefaultPlayerController::DefaultPlayerController() { DefaultPlayerController::DefaultPlayerController() {
} }
DefaultPlayerController::~DefaultPlayerController() { DefaultPlayerController::~DefaultPlayerController() {
} }
glm::vec3 DefaultPlayerController::move(glm::vec3 movement) { glm::vec3 DefaultPlayerController::move(glm::vec3 movement) {
@ -61,7 +59,7 @@ glm::vec3 DefaultPlayerController::move(glm::vec3 movement) {
glm::vec3 totalMovement = glm::vec3(0); glm::vec3 totalMovement = glm::vec3(0);
int count = 0; int count = 0;
while (count < 3) { while(count < 3) {
float frac; float frac;
glm::vec3 normal; glm::vec3 normal;
bool collision = castPlayer(pos + totalMovement, movement, nullptr, &frac, nullptr, &normal); bool collision = castPlayer(pos + totalMovement, movement, nullptr, &frac, nullptr, &normal);
@ -96,7 +94,7 @@ void DefaultPlayerController::update() {
if(!onGround) { if(!onGround) {
velocity += gravity * kekData.lastFrameTime; velocity += gravity * kekData.lastFrameTime;
}else{ } else {
velocity = glm::vec3(0); velocity = glm::vec3(0);
} }

View File

@ -5,11 +5,9 @@
namespace kek { namespace kek {
NoclipPlayerController::NoclipPlayerController() { NoclipPlayerController::NoclipPlayerController() {
} }
NoclipPlayerController::~NoclipPlayerController() { NoclipPlayerController::~NoclipPlayerController() {
} }
glm::vec3 NoclipPlayerController::move(glm::vec3 movement) { glm::vec3 NoclipPlayerController::move(glm::vec3 movement) {

View File

@ -5,8 +5,8 @@
namespace kek { namespace kek {
Player::Player(): RotateableObject() { Player::Player()
: RotateableObject() {
} }
void Player::rotateTo(glm::quat rotation) { void Player::rotateTo(glm::quat rotation) {

View File

@ -3,7 +3,6 @@
namespace kek { namespace kek {
PlayerController::~PlayerController() { PlayerController::~PlayerController() {
} }
} }

View File

@ -5,8 +5,8 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/euler_angles.hpp> #include <glm/gtx/euler_angles.hpp>
#include <math.h>
#include <glm/gtx/matrix_decompose.hpp> #include <glm/gtx/matrix_decompose.hpp>
#include <math.h>
#include "object.h" #include "object.h"
#include "utils.h" #include "utils.h"
@ -64,7 +64,7 @@ glm::mat4 Camera::transformationMatrix() {
float x = sin(rollRad); float x = sin(rollRad);
float y = cos(rollRad); float y = cos(rollRad);
glm::vec3 up = glm::vec3(x, y, 0.0f); glm::vec3 up = glm::vec3(x, y, 0.0f);
//glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f); // glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 right = glm::normalize(glm::cross(direction, up)); glm::vec3 right = glm::normalize(glm::cross(direction, up));
glm::vec3 cameraUp = glm::normalize(glm::cross(right, direction)); glm::vec3 cameraUp = glm::normalize(glm::cross(right, direction));
@ -103,7 +103,7 @@ glm::vec3 Camera::eulerAngles() {
float yawDegrees; float yawDegrees;
if(direction.x == 0 && direction.z == 0) { if(direction.x == 0 && direction.z == 0) {
yawDegrees = 0; yawDegrees = 0;
}else { } else {
glm::vec2 xz = glm::normalize(glm::vec2(direction.x, direction.z)); // Normalized XZ coordinates glm::vec2 xz = glm::normalize(glm::vec2(direction.x, direction.z)); // Normalized XZ coordinates
yawDegrees = glm::degrees(atan2(-xz.y, xz.x)) + 90.0f; // -z because OpenGL yawDegrees = glm::degrees(atan2(-xz.y, xz.x)) + 90.0f; // -z because OpenGL
if(yawDegrees > 180.0f) { if(yawDegrees > 180.0f) {

View File

@ -1,15 +1,15 @@
#include "fonts.h" #include "fonts.h"
#include <stb_image_write.h>
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream>
#include <glm/gtc/type_ptr.hpp> #include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <stb_image_write.h>
#include "shader.h"
#include "engine.h"
#include "constants.h" #include "constants.h"
#include "engine.h"
#include "internal.h" #include "internal.h"
#include "shader.h"
#include "unicode.h" #include "unicode.h"
namespace kek { namespace kek {
@ -37,7 +37,8 @@ void TextObject::destroy() {
blocks.clear(); blocks.clear();
} }
TextObject::TextObject(TextObject &&other): blocks(other.blocks) { TextObject::TextObject(TextObject &&other)
: blocks(other.blocks) {
other.blocks.clear(); other.blocks.clear();
} }
@ -74,13 +75,12 @@ struct RenderChar {
RenderChar(float xPos, float yPos, float w, float h, float texL, float texR, float texU, float texD) { RenderChar(float xPos, float yPos, float w, float h, float texL, float texR, float texU, float texD) {
float coords[] = { float coords[] = {
xPos, yPos, texL, texU, xPos, yPos, texL, texU,
xPos, yPos + h, texL, texD, xPos, yPos + h, texL, texD,
xPos + w, yPos, texR, texU, xPos + w, yPos, texR, texU,
xPos + w, yPos, texR, texU, xPos + w, yPos, texR, texU,
xPos, yPos + h, texL, texD, xPos, yPos + h, texL, texD,
xPos + w, yPos + h, texR, texD xPos + w, yPos + h, texR, texD};
};
for(int i = 0; i < 24; i++) data[i] = coords[i]; for(int i = 0; i < 24; i++) data[i] = coords[i];
} }
}; };
@ -142,7 +142,7 @@ void TextObject::loadChars() {
auto charsForBlock = chars.find(charBlock); auto charsForBlock = chars.find(charBlock);
if(charsForBlock != chars.end()) { if(charsForBlock != chars.end()) {
charsForBlock->second.push_back(rCh); charsForBlock->second.push_back(rCh);
}else { } else {
std::vector<RenderChar> chs; std::vector<RenderChar> chs;
chs.push_back(rCh); chs.push_back(rCh);
chars[charBlock] = chs; chars[charBlock] = chs;
@ -175,7 +175,7 @@ void TextObject::loadChars() {
oldBlock->length = textLength; oldBlock->length = textLength;
glNamedBufferSubData(oldBlock->vbo, 0, textLength * sizeof(RenderChar), &it->second[0]); glNamedBufferSubData(oldBlock->vbo, 0, textLength * sizeof(RenderChar), &it->second[0]);
}else { } else {
// Allocate Buffer // Allocate Buffer
TextBlock block; TextBlock block;
allocateBuffer(&block, textLength); allocateBuffer(&block, textLength);
@ -192,7 +192,7 @@ void TextObject::loadChars() {
while(it2 != blocks.end()) { while(it2 != blocks.end()) {
if(chars.find(it2->first) == chars.end()) { if(chars.find(it2->first) == chars.end()) {
it2 = blocks.erase(it2); it2 = blocks.erase(it2);
}else { } else {
it2++; it2++;
} }
} }

View File

@ -28,7 +28,7 @@ Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices, Material
glCreateBuffers(1, &vbo); glCreateBuffers(1, &vbo);
glCreateBuffers(1, &ebo); glCreateBuffers(1, &ebo);
//glBindVertexArray(vao); // glBindVertexArray(vao);
glNamedBufferData(vbo, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW); glNamedBufferData(vbo, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glNamedBufferData(ebo, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW); glNamedBufferData(ebo, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);

View File

@ -1,7 +1,7 @@
#include "shader.h" #include "shader.h"
#include "types.h"
#include "resource.h"
#include "constants.h" #include "constants.h"
#include "resource.h"
#include "types.h"
#include <GL/glew.h> #include <GL/glew.h>
@ -27,7 +27,7 @@ static GLuint compileShader(GLenum type, std::string path) {
} }
stream << includeBuf; stream << includeBuf;
delete includeBuf; delete includeBuf;
}else { } else {
stream << line << '\n'; stream << line << '\n';
} }
} }
@ -47,7 +47,8 @@ static GLuint compileShader(GLenum type, std::string path) {
if(!success) { if(!success) {
char log[512]; char log[512];
glGetShaderInfoLog(shaderID, 512, nullptr, log); glGetShaderInfoLog(shaderID, 512, nullptr, log);
std::cout << "Failed to compile shader \"" << path << "\":\n" << log << std::endl; std::cout << "Failed to compile shader \"" << path << "\":\n"
<< log << std::endl;
throw std::exception(); throw std::exception();
} }
@ -76,7 +77,8 @@ static GLuint compileProgram(unsigned int n, GLenum *types, std::string *paths)
if(!success) { if(!success) {
char log[512]; char log[512];
glGetProgramInfoLog(id, 512, nullptr, log); glGetProgramInfoLog(id, 512, nullptr, log);
std::cout << "Failed to link program:\n" << log << std::endl; std::cout << "Failed to link program:\n"
<< log << std::endl;
throw std::exception(); throw std::exception();
} }

View File

@ -3,8 +3,8 @@
#include <stb_image.h> #include <stb_image.h>
#include <stb_image_write.h> #include <stb_image_write.h>
#include "resource.h"
#include "internal.h" #include "internal.h"
#include "resource.h"
namespace kek { namespace kek {
@ -18,7 +18,7 @@ static GLuint allocTexture(unsigned char *data, int width, int height, float bor
glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTextureParameteri(id, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTextureStorage2D(id, 1, GL_RGB8, width, height); glTextureStorage2D(id, 1, GL_RGB8, width, height);
glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); glTextureSubImage2D(id, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
//glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); // glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateTextureMipmap(id); glGenerateTextureMipmap(id);
return id; return id;
} }
@ -36,7 +36,7 @@ Texture::Texture(std::string texturePath) {
unsigned char *data = stbi_load_from_memory((stbi_uc *) buf->buffer, buf->length, &width, &height, &nrChannels, 0); unsigned char *data = stbi_load_from_memory((stbi_uc *) buf->buffer, buf->length, &width, &height, &nrChannels, 0);
delete buf; delete buf;
float borderColor[] = { 1.0f, 0.0f, 1.0f, 1.0f }; float borderColor[] = {1.0f, 0.0f, 1.0f, 1.0f};
id = allocTexture(data, width, height, borderColor); id = allocTexture(data, width, height, borderColor);
stbi_image_free(data); stbi_image_free(data);
@ -74,7 +74,7 @@ std::shared_ptr<Texture> Texture::generateColor(glm::vec3 color) {
data[0] = (unsigned char) (color.x * 255); data[0] = (unsigned char) (color.x * 255);
data[1] = (unsigned char) (color.y * 255); data[1] = (unsigned char) (color.y * 255);
data[2] = (unsigned char) (color.z * 255); data[2] = (unsigned char) (color.z * 255);
float borderColor[4] = { color.x, color.y, color.z, 1.0f }; float borderColor[4] = {color.x, color.y, color.z, 1.0f};
GLuint tex = allocTexture(data, 1, 1, borderColor); GLuint tex = allocTexture(data, 1, 1, borderColor);
free(data); free(data);
return std::make_shared<Texture>(tex); return std::make_shared<Texture>(tex);

View File

@ -7,12 +7,12 @@
namespace kek { namespace kek {
UIElement::UIElement(UIValue x, UIValue y): x(x), y(y) { UIElement::UIElement(UIValue x, UIValue y)
: x(x),
y(y) {
} }
UIElement::~UIElement() { UIElement::~UIElement() {
} }
// Returns the element's position relative to its parent in pixels // Returns the element's position relative to its parent in pixels
@ -170,7 +170,7 @@ void UIElement::clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button)
if(clickedChild != nullptr) { if(clickedChild != nullptr) {
UIPoint childPos = clickedChild->getPosition(); UIPoint childPos = clickedChild->getPosition();
clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button); clickedChild->clickAll(UIPoint(pos.x - childPos.x, pos.y - childPos.y), screenPos, button);
}else { } else {
click(pos, screenPos, button); click(pos, screenPos, button);
} }
} }
@ -196,7 +196,7 @@ bool UIElement::focusEnterAll(UIPoint pos, UIPoint screenPos) {
} }
if(focusable) { if(focusable) {
//UI::focusElement(this); FIXME // UI::focusElement(this); FIXME
return true; return true;
} }

View File

@ -7,7 +7,8 @@
namespace kek { namespace kek {
TextElement::TextElement(UIValue x, UIValue y, Font *font): UIElement(x, y) { TextElement::TextElement(UIValue x, UIValue y, Font *font)
: UIElement(x, y) {
this->text = new TextObject(font, "Text"); this->text = new TextObject(font, "Text");
this->sizePixels = KEK_DEFAULT_FONT_SIZE_PIXELS; this->sizePixels = KEK_DEFAULT_FONT_SIZE_PIXELS;
this->color = Colors::WHITE; this->color = Colors::WHITE;
@ -15,7 +16,8 @@ TextElement::TextElement(UIValue x, UIValue y, Font *font): UIElement(x, y) {
this->textBounds = TextBounds::SMALLEST; this->textBounds = TextBounds::SMALLEST;
} }
TextElement::TextElement(UIValue x, UIValue y): TextElement(x, y, kekData.ui->defaultFont) {} TextElement::TextElement(UIValue x, UIValue y)
: TextElement(x, y, kekData.ui->defaultFont) {}
TextElement::~TextElement() { TextElement::~TextElement() {
delete text; delete text;
@ -32,7 +34,7 @@ UIBounds TextElement::getBounds() {
switch(textBounds) { switch(textBounds) {
case TextBounds::SMALLEST: case TextBounds::SMALLEST:
default: default:
h = metrics.height; h = metrics.height;
offsetY = metrics.offsetY; offsetY = metrics.offsetY;
break; break;
case TextBounds::LINE: case TextBounds::LINE:
@ -49,7 +51,6 @@ UIBounds TextElement::getBounds() {
default: default:
return offsetUIBounds(w, h, origin); return offsetUIBounds(w, h, origin);
} }
} }
void TextElement::setText(std::string text) { void TextElement::setText(std::string text) {
@ -84,12 +85,12 @@ void TextElement::draw(UIPoint screenPos, glm::mat4 projection) {
} }
} }
UIWindow::UIWindow(UIValue x, UIValue y): UIElement(x, y) { UIWindow::UIWindow(UIValue x, UIValue y)
: UIElement(x, y) {
addChild(new TextElement(x, y)); addChild(new TextElement(x, y));
} }
UIWindow::~UIWindow() { UIWindow::~UIWindow() {
} }
UIElementType UIWindow::getType() { UIElementType UIWindow::getType() {
@ -97,19 +98,25 @@ UIElementType UIWindow::getType() {
} }
void UIWindow::draw(UIPoint screenPos, glm::mat4 projection) { void UIWindow::draw(UIPoint screenPos, glm::mat4 projection) {
} }
static float rectangleVerts[] = { static float rectangleVerts[] = {
0.0f, 1.0f, 0.0f,
1.0f, 1.0f, 1.0f,
1.0f, 0.0f, 1.0f,
1.0f, 0.0f, 1.0f,
0.0f, 0.0f, 1.0f,
0.0f, 1.0f, 0.0f,
1.0f,
0.0f,
0.0f,
0.0f,
0.0f,
1.0f,
}; };
RectangleElement::RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h): UIElement(x, y) { RectangleElement::RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h)
: UIElement(x, y) {
this->w = w; this->w = w;
this->h = h; this->h = h;
color = Colors::BLACK; color = Colors::BLACK;
@ -153,7 +160,8 @@ void RectangleElement::draw(UIPoint screenPos, glm::mat4 projection) {
glDrawArrays(GL_TRIANGLES, 0, 6); glDrawArrays(GL_TRIANGLES, 0, 6);
} }
ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h): RectangleElement(x, y, w, h) { ButtonElement::ButtonElement(UIValue x, UIValue y, UIValue w, UIValue h)
: RectangleElement(x, y, w, h) {
clickable = true; clickable = true;
color = Colors::CYAN; color = Colors::CYAN;

View File

@ -44,7 +44,8 @@ static void show(GtkMessageType type, GtkButtonsType buttons, const char *title,
gtk_dialog_run(GTK_DIALOG(d)); gtk_dialog_run(GTK_DIALOG(d));
gtk_widget_destroy(w); gtk_widget_destroy(w);
gtk_widget_destroy(d); gtk_widget_destroy(d);
while(g_main_context_iteration(nullptr, false)); while(g_main_context_iteration(nullptr, false))
;
} }
void showInfo(std::string message) { void showInfo(std::string message) {

View File

@ -1,12 +1,12 @@
#include "objparser.h" #include "objparser.h"
#include <iostream>
#include <string>
#include <sstream>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <iostream>
#include <sstream>
#include <string>
#include "types.h"
#include "resource.h" #include "resource.h"
#include "types.h"
namespace kek::ObjParser { namespace kek::ObjParser {
@ -51,15 +51,15 @@ Mesh *parseMesh(MemoryBuffer *buf, std::string folderPath) {
float x, y, z; float x, y, z;
str >> x >> y >> z; str >> x >> y >> z;
vertexPositions.push_back(glm::vec3(x, y, z)); vertexPositions.push_back(glm::vec3(x, y, z));
}else if(cmd == "vn") { } else if(cmd == "vn") {
float x, y, z; float x, y, z;
str >> x >> y >> z; str >> x >> y >> z;
vertexNormals.push_back(glm::normalize(glm::vec3(x, y, z))); vertexNormals.push_back(glm::normalize(glm::vec3(x, y, z)));
}else if(cmd == "vt") { } else if(cmd == "vt") {
float u, v = 1.0f; float u, v = 1.0f;
str >> u >> v; str >> u >> v;
vertexTexCoords.push_back(glm::vec2(u, v)); vertexTexCoords.push_back(glm::vec2(u, v));
}else if(cmd == "f") { } else if(cmd == "f") {
uint32_t v1, v1T, v1N; uint32_t v1, v1T, v1N;
readFaceVertex(str, &v1, &v1T, &v1N); readFaceVertex(str, &v1, &v1T, &v1N);
uint32_t v2, v2T, v2N; uint32_t v2, v2T, v2N;
@ -73,14 +73,14 @@ Mesh *parseMesh(MemoryBuffer *buf, std::string folderPath) {
indices.push_back(indices.size()); indices.push_back(indices.size());
vertices.push_back(Vertex(vertexPositions[v3 - 1], vertexNormals[v3N - 1], vertexTexCoords[v3T - 1])); vertices.push_back(Vertex(vertexPositions[v3 - 1], vertexNormals[v3N - 1], vertexTexCoords[v3T - 1]));
indices.push_back(indices.size()); indices.push_back(indices.size());
}else if(cmd == "mtllib") { } else if(cmd == "mtllib") {
std::string name; std::string name;
str >> name; str >> name;
std::string mtllibPath = folderPath + name; std::string mtllibPath = folderPath + name;
std::map<std::string, Material *> mats = loadMaterials(mtllibPath); std::map<std::string, Material *> mats = loadMaterials(mtllibPath);
materials.insert(mats.begin(), mats.end()); materials.insert(mats.begin(), mats.end());
}else if(cmd == "usemtl") { } else if(cmd == "usemtl") {
std::string mtlName; std::string mtlName;
str >> mtlName; str >> mtlName;
auto it = materials.find(mtlName); auto it = materials.find(mtlName);
@ -90,7 +90,7 @@ Mesh *parseMesh(MemoryBuffer *buf, std::string folderPath) {
} }
material = it->second; material = it->second;
}else { } else {
std::cout << "Ignoring unknown/unsupported OBJ command: " << cmd << std::endl; std::cout << "Ignoring unknown/unsupported OBJ command: " << cmd << std::endl;
} }
} }
@ -146,23 +146,23 @@ std::map<std::string, Material *> parseMaterials(MemoryBuffer *buf, std::string
if(name.length() > 0) mats.emplace(name, makeMaterial(ambient, diffuse, specular, shininess)); if(name.length() > 0) mats.emplace(name, makeMaterial(ambient, diffuse, specular, shininess));
str >> name; str >> name;
}else if(cmd == "Ka") { } else if(cmd == "Ka") {
float r, g, b; float r, g, b;
str >> r >> g >> b; str >> r >> g >> b;
ambient = Texture::generateColor(glm::vec3(r, g, b)); ambient = Texture::generateColor(glm::vec3(r, g, b));
}else if(cmd == "Kd") { } else if(cmd == "Kd") {
float r, g, b; float r, g, b;
str >> r >> g >> b; str >> r >> g >> b;
diffuse = Texture::generateColor(glm::vec3(r, g, b)); diffuse = Texture::generateColor(glm::vec3(r, g, b));
}else if(cmd == "Ks") { } else if(cmd == "Ks") {
float r, g, b; float r, g, b;
str >> r >> g >> b; str >> r >> g >> b;
specular = Texture::generateColor(glm::vec3(r, g, b)); specular = Texture::generateColor(glm::vec3(r, g, b));
}else if(cmd == "Ns") { } else if(cmd == "Ns") {
float s; float s;
str >> s; str >> s;
shininess = s; shininess = s;
}else { } else {
std::cout << "Ignoring unknown/unsupported MTL command: " << cmd << std::endl; std::cout << "Ignoring unknown/unsupported MTL command: " << cmd << std::endl;
} }
} }

View File

@ -48,53 +48,53 @@ glm::vec3 faceNormal(CubeFace face) {
} }
float cubeTriangles[] = { float cubeTriangles[] = {
// Front // Front
-0.5f, -0.5f, 0.5f, // 0 -0.5f, -0.5f, 0.5f, // 0
0.5f, -0.5f, 0.5f, // 1 0.5f, -0.5f, 0.5f, // 1
0.5f, 0.5f, 0.5f, // 2 0.5f, 0.5f, 0.5f, // 2
0.5f, 0.5f, 0.5f, // 2 0.5f, 0.5f, 0.5f, // 2
-0.5f, 0.5f, 0.5f, // 3 -0.5f, 0.5f, 0.5f, // 3
-0.5f, -0.5f, 0.5f, // 0 -0.5f, -0.5f, 0.5f, // 0
// Back // Back
0.5f, -0.5f, -0.5f, // 5 0.5f, -0.5f, -0.5f, // 5
-0.5f, -0.5f, -0.5f, // 4 -0.5f, -0.5f, -0.5f, // 4
-0.5f, 0.5f, -0.5f, // 7 -0.5f, 0.5f, -0.5f, // 7
-0.5f, 0.5f, -0.5f, // 7 -0.5f, 0.5f, -0.5f, // 7
0.5f, 0.5f, -0.5f, // 6 0.5f, 0.5f, -0.5f, // 6
0.5f, -0.5f, -0.5f, // 5 0.5f, -0.5f, -0.5f, // 5
// Up // Up
-0.5f, 0.5f, 0.5f, // 3 -0.5f, 0.5f, 0.5f, // 3
0.5f, 0.5f, 0.5f, // 2 0.5f, 0.5f, 0.5f, // 2
0.5f, 0.5f, -0.5f, // 6 0.5f, 0.5f, -0.5f, // 6
0.5f, 0.5f, -0.5f, // 6 0.5f, 0.5f, -0.5f, // 6
-0.5f, 0.5f, -0.5f, // 7 -0.5f, 0.5f, -0.5f, // 7
-0.5f, 0.5f, 0.5f, // 3 -0.5f, 0.5f, 0.5f, // 3
// Down // Down
0.5f, -0.5f, -0.5f, // 5 0.5f, -0.5f, -0.5f, // 5
0.5f, -0.5f, 0.5f, // 1 0.5f, -0.5f, 0.5f, // 1
-0.5f, -0.5f, 0.5f, // 0 -0.5f, -0.5f, 0.5f, // 0
-0.5f, -0.5f, 0.5f, // 0 -0.5f, -0.5f, 0.5f, // 0
-0.5f, -0.5f, -0.5f, // 4 -0.5f, -0.5f, -0.5f, // 4
0.5f, -0.5f, -0.5f, // 5 0.5f, -0.5f, -0.5f, // 5
// Left // Left
-0.5f, -0.5f, -0.5f, // 4 -0.5f, -0.5f, -0.5f, // 4
-0.5f, -0.5f, 0.5f, // 0 -0.5f, -0.5f, 0.5f, // 0
-0.5f, 0.5f, 0.5f, // 3 -0.5f, 0.5f, 0.5f, // 3
-0.5f, 0.5f, 0.5f, // 3 -0.5f, 0.5f, 0.5f, // 3
-0.5f, 0.5f, -0.5f, // 7 -0.5f, 0.5f, -0.5f, // 7
-0.5f, -0.5f, -0.5f, // 4 -0.5f, -0.5f, -0.5f, // 4
// Right // Right
0.5f, -0.5f, 0.5f, // 1 0.5f, -0.5f, 0.5f, // 1
0.5f, -0.5f, -0.5f, // 5 0.5f, -0.5f, -0.5f, // 5
0.5f, 0.5f, -0.5f, // 6 0.5f, 0.5f, -0.5f, // 6
0.5f, 0.5f, -0.5f, // 6 0.5f, 0.5f, -0.5f, // 6
0.5f, 0.5f, 0.5f, // 2 0.5f, 0.5f, 0.5f, // 2
0.5f, -0.5f, 0.5f, // 1 0.5f, -0.5f, 0.5f, // 1
}; };
float textureCoordinates[] = { float textureCoordinates[] = {

View File

@ -8,7 +8,7 @@ namespace kek {
class Camera: public DefaultObject { class Camera: public DefaultObject {
public: public:
glm::vec3 direction; glm::vec3 direction;
float roll; float roll;
@ -38,7 +38,6 @@ public:
glm::vec3 eulerAngles(); glm::vec3 eulerAngles();
void applyEuler(glm::vec3 euler); void applyEuler(glm::vec3 euler);
}; };
} }

View File

@ -5,11 +5,17 @@ namespace kek {
struct Color { struct Color {
float r, g, b, a; float r, g, b, a;
constexpr Color(float r, float g, float b, float a): r(r), g(g), b(b), a(a) {} constexpr Color(float r, float g, float b, float a)
: r(r),
g(g),
b(b),
a(a) {}
constexpr Color(float r, float g, float b): Color(r, g, b, 1.0) {} constexpr Color(float r, float g, float b)
: Color(r, g, b, 1.0) {}
constexpr Color(): Color(0,0,0,1) {} constexpr Color()
: Color(0, 0, 0, 1) {}
float *valuePointer() { float *valuePointer() {
return &r; return &r;
@ -22,24 +28,23 @@ struct Color {
constexpr Color brighter(float factor = 0.7) const { constexpr Color brighter(float factor = 0.7) const {
return Color(r / factor, g / factor, b / factor, a); return Color(r / factor, g / factor, b / factor, a);
} }
}; };
class Colors { class Colors {
public: public:
static constexpr Color RED = Color(1.0, 0.0, 0.0); static constexpr Color RED = Color(1.0, 0.0, 0.0);
static constexpr Color ORANGE = Color(1.0, 0.5, 0.0); static constexpr Color ORANGE = Color(1.0, 0.5, 0.0);
static constexpr Color YELLOW = Color(1.0, 1.0, 0.0); static constexpr Color YELLOW = Color(1.0, 1.0, 0.0);
static constexpr Color GREEN = Color(0.0, 1.0, 0.0); static constexpr Color GREEN = Color(0.0, 1.0, 0.0);
static constexpr Color CYAN = Color(0.0, 1.0, 1.0); static constexpr Color CYAN = Color(0.0, 1.0, 1.0);
static constexpr Color BLUE = Color(0.0, 0.0, 1.0); static constexpr Color BLUE = Color(0.0, 0.0, 1.0);
static constexpr Color PURPLE = Color(0.5, 0.0, 0.5); static constexpr Color PURPLE = Color(0.5, 0.0, 0.5);
static constexpr Color MAGENTA = Color(1.0, 0.0, 1.0); static constexpr Color MAGENTA = Color(1.0, 0.0, 1.0);
static constexpr Color GRAY = Color(0.5, 0.5, 0.5); static constexpr Color GRAY = Color(0.5, 0.5, 0.5);
static constexpr Color WHITE = Color(1.0, 1.0, 1.0); static constexpr Color WHITE = Color(1.0, 1.0, 1.0);
static constexpr Color BLACK = Color(0.0, 0.0, 0.0); static constexpr Color BLACK = Color(0.0, 0.0, 0.0);
static constexpr Color TRANSPARENT = Color(0.0, 0.0, 0.0, 0.0); static constexpr Color TRANSPARENT = Color(0.0, 0.0, 0.0, 0.0);
Colors() = delete; Colors() = delete;
}; };

View File

@ -1,14 +1,14 @@
#pragma once #pragma once
#include "playercontroller.h"
#include "input.h" #include "input.h"
#include "playercontroller.h"
namespace kek { namespace kek {
class DefaultPlayerController: public PlayerController { class DefaultPlayerController: public PlayerController {
public: public:
glm::vec3 gravity = glm::vec3(0,-9.81,0); glm::vec3 gravity = glm::vec3(0, -9.81, 0);
float minSlideAngle = 5; float minSlideAngle = 5;
float maxWalkAngle = 60; float maxWalkAngle = 60;
float jumpVelocity = 3; float jumpVelocity = 3;
@ -28,7 +28,6 @@ public:
virtual glm::vec3 move(glm::vec3 movement); virtual glm::vec3 move(glm::vec3 movement);
virtual void update(); virtual void update();
}; };
} }

View File

@ -4,8 +4,8 @@
#include FT_FREETYPE_H #include FT_FREETYPE_H
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <string>
#include <map> #include <map>
#include <string>
#include "color.h" #include "color.h"
#include "resource.h" #include "resource.h"
@ -64,7 +64,7 @@ struct TextBlock {
class TextObject { class TextObject {
private: private:
Font *font; Font *font;
std::string text; std::string text;
@ -73,7 +73,7 @@ private:
int width; int width;
int height; int height;
public: public:
std::map<unsigned int, TextBlock> blocks; std::map<unsigned int, TextBlock> blocks;
TextObject(Font *font, std::string text); TextObject(Font *font, std::string text);
@ -96,18 +96,17 @@ public:
TextMetrics getMetrics(int sizePixels); TextMetrics getMetrics(int sizePixels);
private: private:
void destroy(); void destroy();
void allocateBuffer(TextBlock *block, int numChars); void allocateBuffer(TextBlock *block, int numChars);
void loadChars(); void loadChars();
}; };
class Font { class Font {
private: private:
int lineHeight; int lineHeight;
int ascender; int ascender;
int descender; int descender;
@ -117,8 +116,7 @@ private:
std::map<unsigned int, CharacterBlock> characterBlocks; std::map<unsigned int, CharacterBlock> characterBlocks;
public: public:
Font(std::string path); Font(std::string path);
~Font(); ~Font();
@ -151,9 +149,8 @@ public:
// Draws text centered around (x,y) // Draws text centered around (x,y)
void drawTextCentered(TextObject *textObject, glm::mat4 projection, int x, int y, int sizePixels, Color color); void drawTextCentered(TextObject *textObject, glm::mat4 projection, int x, int y, int sizePixels, Color color);
private: private:
CharacterBlock generateCharacterBlock(unsigned int block); CharacterBlock generateCharacterBlock(unsigned int block);
}; };
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "object.h"
#include "mesh.h" #include "mesh.h"
#include "object.h"
#include "physics.h" #include "physics.h"
#include <BulletCollision/CollisionShapes/btCollisionShape.h> #include <BulletCollision/CollisionShapes/btCollisionShape.h>
@ -10,10 +10,10 @@ namespace kek {
class GameObject: public RotateableObject { class GameObject: public RotateableObject {
protected: protected:
std::vector<Mesh *> meshes; std::vector<Mesh *> meshes;
public: public:
PhysicsObjectData *physics; PhysicsObjectData *physics;
GameObject(); GameObject();
@ -31,7 +31,7 @@ public:
virtual void rotateTo(glm::quat rotation); virtual void rotateTo(glm::quat rotation);
virtual void lookAt(glm::vec3 direction) { rotateTo(glm::quatLookAt(glm::normalize(direction), glm::vec3(0,1,0))); }; virtual void lookAt(glm::vec3 direction) { rotateTo(glm::quatLookAt(glm::normalize(direction), glm::vec3(0, 1, 0))); };
virtual glm::quat getRotation(); virtual glm::quat getRotation();
@ -40,7 +40,6 @@ public:
virtual void moveTo(glm::vec3 position); virtual void moveTo(glm::vec3 position);
virtual glm::vec3 getPosition(); virtual glm::vec3 getPosition();
}; };
} }

View File

@ -4,13 +4,13 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "input.h"
#include "camera.h" #include "camera.h"
#include "fonts.h"
#include "input.h"
#include "player.h"
#include "scene.h" #include "scene.h"
#include "texture.h" #include "texture.h"
#include "fonts.h"
#include "ui.h" #include "ui.h"
#include "player.h"
namespace kek { namespace kek {

View File

@ -1,5 +1,9 @@
#pragma once #pragma once
#include <map>
#include "../input.h"
namespace kek { namespace kek {
struct ActiveKeyboardCapture { struct ActiveKeyboardCapture {
@ -26,7 +30,7 @@ namespace Input {
void onCursorPosCallback(GLFWwindow *window, double x, double y); void onCursorPosCallback(GLFWwindow *window, double x, double y);
void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods); void onKeyCallback(GLFWwindow *window, int key, int scancode, int action, int mods);
void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint); void onKeyCharCallback(GLFWwindow *window, unsigned int codepoint);
void onMouseButtonCallback(GLFWwindow* window, int button, int action, int mods); void onMouseButtonCallback(GLFWwindow *window, int button, int action, int mods);
} }

View File

@ -1,7 +1,10 @@
#pragma once #pragma once
#include <btBulletDynamicsCommon.h>
#include <BulletCollision/CollisionDispatch/btGhostObject.h> #include <BulletCollision/CollisionDispatch/btGhostObject.h>
#include <btBulletDynamicsCommon.h>
#include <glm/glm.hpp>
#include "../physics.h"
namespace kek { namespace kek {
@ -17,29 +20,29 @@ struct PlayerPhysicsObjectData: public PhysicsObjectData {
btGhostObject *jumpCollider; btGhostObject *jumpCollider;
}; };
struct ClosestNotSelfConvexResultCallback : public btCollisionWorld::ConvexResultCallback { struct ClosestNotSelfConvexResultCallback: public btCollisionWorld::ConvexResultCallback {
ClosestNotSelfConvexResultCallback(const btVector3& convexFromWorld, const btVector3& convexToWorld, btCollisionObject *self) : ClosestNotSelfConvexResultCallback(const btVector3 &convexFromWorld, const btVector3 &convexToWorld, btCollisionObject *self)
m_self(self), : m_self(self),
m_convexFromWorld(convexFromWorld), m_convexFromWorld(convexFromWorld),
m_convexToWorld(convexToWorld), m_convexToWorld(convexToWorld),
m_hitCollisionObject(0){} m_hitCollisionObject(0) {}
btCollisionObject *m_self; btCollisionObject *m_self;
btVector3 m_convexFromWorld; //used to calculate hitPointWorld from hitFraction btVector3 m_convexFromWorld; // used to calculate hitPointWorld from hitFraction
btVector3 m_convexToWorld; btVector3 m_convexToWorld;
btVector3 m_hitNormalWorld; btVector3 m_hitNormalWorld;
btVector3 m_hitPointWorld; btVector3 m_hitPointWorld;
const btCollisionObject* m_hitCollisionObject; const btCollisionObject *m_hitCollisionObject;
virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult& convexResult, bool normalInWorldSpace) { virtual btScalar addSingleResult(btCollisionWorld::LocalConvexResult &convexResult, bool normalInWorldSpace) {
if(convexResult.m_hitCollisionObject == m_self) return 1.0f; if(convexResult.m_hitCollisionObject == m_self) return 1.0f;
m_closestHitFraction = convexResult.m_hitFraction; m_closestHitFraction = convexResult.m_hitFraction;
m_hitCollisionObject = convexResult.m_hitCollisionObject; m_hitCollisionObject = convexResult.m_hitCollisionObject;
if (normalInWorldSpace) { if(normalInWorldSpace) {
m_hitNormalWorld = convexResult.m_hitNormalLocal; m_hitNormalWorld = convexResult.m_hitNormalLocal;
}else{ } else {
///need to transform normal into worldspace /// need to transform normal into worldspace
m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis() * convexResult.m_hitNormalLocal; m_hitNormalWorld = m_hitCollisionObject->getWorldTransform().getBasis() * convexResult.m_hitNormalLocal;
} }
m_hitPointWorld = convexResult.m_hitPointLocal; m_hitPointWorld = convexResult.m_hitPointLocal;

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
#include "../ui.h"
namespace kek { namespace kek {
struct UIData { struct UIData {

View File

@ -15,7 +15,7 @@ enum class LightType {
class Light { class Light {
public: public:
glm::vec3 color; glm::vec3 color;
Light(glm::vec3 color); Light(glm::vec3 color);
@ -23,12 +23,11 @@ public:
virtual ~Light(); virtual ~Light();
virtual LightType getType() = 0; virtual LightType getType() = 0;
}; };
class PointLight: public Light, public DefaultObject { class PointLight: public Light, public DefaultObject {
public: public:
float constant, linear, quadratic; float constant, linear, quadratic;
PointLight(glm::vec3 color, float constant, float linear, float quadratic); PointLight(glm::vec3 color, float constant, float linear, float quadratic);
@ -36,12 +35,11 @@ public:
~PointLight(); ~PointLight();
LightType getType(); LightType getType();
}; };
class DirectionalLight: public Light { class DirectionalLight: public Light {
public: public:
glm::vec3 direction; glm::vec3 direction;
DirectionalLight(glm::vec3 color, glm::vec3 direction); DirectionalLight(glm::vec3 color, glm::vec3 direction);
@ -51,12 +49,11 @@ public:
LightType getType(); LightType getType();
void lookAt(glm::vec3 direction); void lookAt(glm::vec3 direction);
}; };
class SpotLight: public Light, public DefaultObject { class SpotLight: public Light, public DefaultObject {
public: public:
glm::vec3 direction; glm::vec3 direction;
float constant, linear, quadratic; float constant, linear, quadratic;
float innerCutoff, outerCutoff; float innerCutoff, outerCutoff;
@ -68,12 +65,11 @@ public:
LightType getType(); LightType getType();
void lookAt(glm::vec3 direction); void lookAt(glm::vec3 direction);
}; };
class LightList { class LightList {
public: public:
std::vector<PointLight *> point; std::vector<PointLight *> point;
std::vector<DirectionalLight *> directional; std::vector<DirectionalLight *> directional;
std::vector<SpotLight *> spot; std::vector<SpotLight *> spot;
@ -81,7 +77,6 @@ public:
LightList(); LightList();
void add(Light *light); void add(Light *light);
}; };
} }

View File

@ -1,10 +1,10 @@
#pragma once #pragma once
#include <glm/glm.hpp>
#include <vector>
#include <cstdint> #include <cstdint>
#include <glm/glm.hpp>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <vector>
#include "shader.h" #include "shader.h"
#include "texture.h" #include "texture.h"
@ -17,24 +17,22 @@ struct Vertex {
glm::vec2 texCoords; glm::vec2 texCoords;
Vertex(glm::vec3 pos, glm::vec3 normal, glm::vec2 texCoords); Vertex(glm::vec3 pos, glm::vec3 normal, glm::vec2 texCoords);
}; };
struct Material { struct Material {
public: public:
std::shared_ptr<Texture> ambient; std::shared_ptr<Texture> ambient;
std::shared_ptr<Texture> diffuse; std::shared_ptr<Texture> diffuse;
std::shared_ptr<Texture> specular; std::shared_ptr<Texture> specular;
float shininess; float shininess;
Material(std::shared_ptr<Texture> ambient, std::shared_ptr<Texture> diffuse, std::shared_ptr<Texture> specular, float shininess); Material(std::shared_ptr<Texture> ambient, std::shared_ptr<Texture> diffuse, std::shared_ptr<Texture> specular, float shininess);
}; };
class Mesh { class Mesh {
public: public:
std::vector<Vertex> vertices; std::vector<Vertex> vertices;
std::vector<uint32_t> indices; std::vector<uint32_t> indices;
Material *material; Material *material;
@ -46,7 +44,6 @@ public:
~Mesh(); ~Mesh();
void draw(Shader *shader); void draw(Shader *shader);
}; };
} }

View File

@ -1,13 +1,13 @@
#pragma once #pragma once
#include "playercontroller.h"
#include "input.h" #include "input.h"
#include "playercontroller.h"
namespace kek { namespace kek {
class NoclipPlayerController: public PlayerController { class NoclipPlayerController: public PlayerController {
public: public:
float noclipSpeed = 6; float noclipSpeed = 6;
KeyBinding keyForward = KEK_INVALID_ID; KeyBinding keyForward = KEK_INVALID_ID;
@ -24,7 +24,6 @@ public:
virtual glm::vec3 move(glm::vec3 movement); virtual glm::vec3 move(glm::vec3 movement);
virtual void update(); virtual void update();
}; };
} }

View File

@ -7,8 +7,8 @@ namespace kek {
class Object { class Object {
public: public:
virtual ~Object() {}; virtual ~Object(){};
virtual void translate(glm::vec3 delta) = 0; virtual void translate(glm::vec3 delta) = 0;
@ -21,31 +21,29 @@ public:
virtual void moveTo(glm::vec3 position) = 0; virtual void moveTo(glm::vec3 position) = 0;
virtual glm::vec3 getPosition() = 0; virtual glm::vec3 getPosition() = 0;
}; };
class DefaultObject: public Object { class DefaultObject: public Object {
protected: protected:
glm::vec3 position; glm::vec3 position;
public: public:
DefaultObject(); DefaultObject();
virtual ~DefaultObject() {}; virtual ~DefaultObject(){};
virtual void translate(glm::vec3 delta); virtual void translate(glm::vec3 delta);
virtual void moveTo(glm::vec3 position); virtual void moveTo(glm::vec3 position);
virtual glm::vec3 getPosition(); virtual glm::vec3 getPosition();
}; };
class RotateableObject: public Object { class RotateableObject: public Object {
public: public:
virtual ~RotateableObject() {}; virtual ~RotateableObject(){};
virtual void rotate(float angle, glm::vec3 axis) = 0; virtual void rotate(float angle, glm::vec3 axis) = 0;
@ -56,18 +54,17 @@ public:
virtual void lookAtPos(glm::vec3 position); virtual void lookAtPos(glm::vec3 position);
virtual glm::quat getRotation() = 0; virtual glm::quat getRotation() = 0;
}; };
class DefaultRotateableObject: public RotateableObject, public DefaultObject { class DefaultRotateableObject: public RotateableObject, public DefaultObject {
protected: protected:
glm::quat rotation; glm::quat rotation;
public: public:
DefaultRotateableObject(); DefaultRotateableObject();
virtual ~DefaultRotateableObject() {}; virtual ~DefaultRotateableObject(){};
virtual void rotate(float angle, glm::vec3 axis); virtual void rotate(float angle, glm::vec3 axis);
@ -82,7 +79,6 @@ public:
virtual void moveTo(glm::vec3 position) { DefaultObject::moveTo(position); }; virtual void moveTo(glm::vec3 position) { DefaultObject::moveTo(position); };
virtual glm::vec3 getPosition() { return DefaultObject::getPosition(); }; virtual glm::vec3 getPosition() { return DefaultObject::getPosition(); };
}; };
} }

View File

@ -2,8 +2,8 @@
#include <map> #include <map>
#include "types.h"
#include "mesh.h" #include "mesh.h"
#include "types.h"
namespace kek::ObjParser { namespace kek::ObjParser {

View File

@ -7,7 +7,7 @@ namespace kek {
class Player: public RotateableObject { class Player: public RotateableObject {
public: public:
PlayerPhysicsObjectData *physics; PlayerPhysicsObjectData *physics;
PlayerController *controller; PlayerController *controller;
bool noclip = false; bool noclip = false;
@ -19,7 +19,7 @@ public:
virtual void rotateTo(glm::quat rotation); virtual void rotateTo(glm::quat rotation);
virtual void lookAt(glm::vec3 direction) { rotateTo(glm::quatLookAt(glm::normalize(direction), glm::vec3(0,1,0))); }; virtual void lookAt(glm::vec3 direction) { rotateTo(glm::quatLookAt(glm::normalize(direction), glm::vec3(0, 1, 0))); };
virtual glm::quat getRotation(); virtual glm::quat getRotation();
@ -32,7 +32,6 @@ public:
virtual glm::vec3 getEyePosition() { return getPosition() + glm::vec3(0, KEK_PLAYER_EYE_OFFSET, 0); } virtual glm::vec3 getEyePosition() { return getPosition() + glm::vec3(0, KEK_PLAYER_EYE_OFFSET, 0); }
virtual glm::vec3 getFootPosition() { return getPosition() - glm::vec3(0, KEK_PLAYER_HEIGHT / 2, 0); }; virtual glm::vec3 getFootPosition() { return getPosition() - glm::vec3(0, KEK_PLAYER_HEIGHT / 2, 0); };
}; };
} }

View File

@ -6,12 +6,11 @@ namespace kek {
class PlayerController { class PlayerController {
public: public:
virtual ~PlayerController() = 0; virtual ~PlayerController() = 0;
virtual glm::vec3 move(glm::vec3 movement) = 0; virtual glm::vec3 move(glm::vec3 movement) = 0;
virtual void update() = 0; virtual void update() = 0;
}; };
} }

View File

@ -9,7 +9,7 @@ namespace kek {
class Scene { class Scene {
public: public:
std::vector<GameObject *> objects; std::vector<GameObject *> objects;
LightList *lights; LightList *lights;
@ -25,7 +25,6 @@ public:
// Draws the scene // Draws the scene
void draw(Shader *shader); void draw(Shader *shader);
}; };
} }

View File

@ -1,13 +1,14 @@
#pragma once #pragma once
#include <string>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <string>
#include "constants.h" #include "constants.h"
namespace kek { namespace kek {
#pragma pack(push, 1) #pragma pack(push, 1)
struct ShaderLight { struct ShaderLight {
float color[3]; float color[3];
float pad; float pad;
@ -18,9 +19,10 @@ struct ShaderLight {
float attenuation[3]; // constant, linear, quadratic float attenuation[3]; // constant, linear, quadratic
float pad4; float pad4;
float cutoff[2]; // inner, outer float cutoff[2]; // inner, outer
//float lightSpaceMatrix[16]; // mat4 // float lightSpaceMatrix[16]; // mat4
float padding[2]; float padding[2];
}; };
#pragma pack(pop) #pragma pack(pop)
// Lighting data for shader // Lighting data for shader
@ -33,7 +35,7 @@ struct LightingData {
class Shader { class Shader {
public: public:
unsigned int id; unsigned int id;
LightingData *lighting; LightingData *lighting;
@ -46,7 +48,6 @@ public:
void initLighting(); void initLighting();
void use(); void use();
}; };
} }

View File

@ -4,15 +4,15 @@
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <vector>
#include <string>
#include <memory> #include <memory>
#include <string>
#include <vector>
namespace kek { namespace kek {
class Texture { class Texture {
public: public:
GLuint id; GLuint id;
std::string path; std::string path;
@ -27,7 +27,6 @@ public:
static std::shared_ptr<Texture> load(std::string texturePath); static std::shared_ptr<Texture> load(std::string texturePath);
static std::shared_ptr<Texture> generateColor(glm::vec3 color); static std::shared_ptr<Texture> generateColor(glm::vec3 color);
}; };
} }

View File

@ -1,15 +1,15 @@
#pragma once #pragma once
#include <glm/glm.hpp>
#include <vector>
#include <cstdint> #include <cstdint>
#include <glm/glm.hpp>
#include <iostream> #include <iostream>
#include <vector>
namespace kek { namespace kek {
class MemoryBuffer: public std::streambuf { class MemoryBuffer: public std::streambuf {
public: public:
char *buffer; char *buffer;
size_t length; size_t length;
@ -22,7 +22,6 @@ public:
~MemoryBuffer() { ~MemoryBuffer() {
free(buffer); free(buffer);
} }
}; };
} }

View File

@ -16,13 +16,20 @@ namespace kek {
struct UIPoint { struct UIPoint {
int x, y; int x, y;
UIPoint(int x, int y): x(x), y(y) {}
UIPoint(int x, int y)
: x(x),
y(y) {}
}; };
struct UIBounds { struct UIBounds {
int x, y, w, h; int x, y, w, h;
UIBounds(int x, int y, int w, int h): x(x), y(y), w(w), h(h) {} UIBounds(int x, int y, int w, int h)
: x(x),
y(y),
w(w),
h(h) {}
inline bool contains(UIPoint pos) { inline bool contains(UIPoint pos) {
return pos.x > x && pos.y > y && pos.x < x + w && pos.y < y + h; return pos.x > x && pos.y > y && pos.x < x + w && pos.y < y + h;
@ -115,16 +122,15 @@ struct UIValue {
constexpr friend UIValue operator-(const UIValue &lhs, const UIValue &rhs) { constexpr friend UIValue operator-(const UIValue &lhs, const UIValue &rhs) {
return UIValue(lhs) -= rhs; return UIValue(lhs) -= rhs;
} }
}; };
class UIElement { class UIElement {
protected: protected:
UIElement *parent = nullptr; UIElement *parent = nullptr;
std::vector<UIElement *> children; std::vector<UIElement *> children;
public: public:
UIValue x, y; UIValue x, y;
Origin origin = Origin::TOP_LEFT; Origin origin = Origin::TOP_LEFT;
bool clickable = false; bool clickable = false;
@ -152,13 +158,13 @@ public:
void addChild(UIElement *child); void addChild(UIElement *child);
void removeChild(UIElement *child); void removeChild(UIElement *child);
protected: protected:
int uiToScreen(UIValue val); int uiToScreen(UIValue val);
static int offsetX(int w, Origin origin); static int offsetX(int w, Origin origin);
static int offsetY(int h, Origin origin); static int offsetY(int h, Origin origin);
static UIBounds offsetUIBounds(int w, int h, Origin origin); static UIBounds offsetUIBounds(int w, int h, Origin origin);
public: public:
// Returns the bounds of the element relative to its origin // Returns the bounds of the element relative to its origin
virtual UIBounds getBounds() = 0; virtual UIBounds getBounds() = 0;
@ -167,22 +173,21 @@ public:
void hoverAll(UIPoint pos, UIPoint screenPos); void hoverAll(UIPoint pos, UIPoint screenPos);
void hoverExitAll(); void hoverExitAll();
virtual void hoverEnter(UIPoint pos, UIPoint screenPos) {}; virtual void hoverEnter(UIPoint pos, UIPoint screenPos){};
virtual void hover(UIPoint pos, UIPoint screenPos) {}; virtual void hover(UIPoint pos, UIPoint screenPos){};
virtual void hoverExit() {}; virtual void hoverExit(){};
void clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button); void clickAll(UIPoint pos, UIPoint screenPos, GLFWMouseButton button);
virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button) {}; virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button){};
bool focusEnterAll(UIPoint pos, UIPoint screenPos); bool focusEnterAll(UIPoint pos, UIPoint screenPos);
virtual void focusEnter(UIPoint pos, UIPoint screenPos) {}; virtual void focusEnter(UIPoint pos, UIPoint screenPos){};
virtual void focusExit() {}; virtual void focusExit(){};
UIElement *dragEnterAll(UIPoint pos, UIPoint screenPos); UIElement *dragEnterAll(UIPoint pos, UIPoint screenPos);
virtual void dragEnter(UIPoint pos, UIPoint screenPos) {}; virtual void dragEnter(UIPoint pos, UIPoint screenPos){};
virtual void drag(UIPoint pos, UIPoint screenPos) {}; virtual void drag(UIPoint pos, UIPoint screenPos){};
virtual void dragExit(UIPoint pos, UIPoint screenPos) {}; virtual void dragExit(UIPoint pos, UIPoint screenPos){};
}; };
namespace UI { namespace UI {

View File

@ -13,7 +13,6 @@ class UIWindow: public UIElement {
virtual UIElementType getType(); virtual UIElementType getType();
virtual void draw(UIPoint screenPos, glm::mat4 projection); virtual void draw(UIPoint screenPos, glm::mat4 projection);
}; };
enum class TextMode { enum class TextMode {
@ -30,10 +29,10 @@ enum class TextBounds {
class TextElement: public UIElement { class TextElement: public UIElement {
protected: protected:
TextObject *text; TextObject *text;
public: public:
int sizePixels; int sizePixels;
Color color; Color color;
TextMode textMode; TextMode textMode;
@ -54,18 +53,17 @@ public:
std::string getText(); std::string getText();
virtual void draw(UIPoint screenPos, glm::mat4 projection); virtual void draw(UIPoint screenPos, glm::mat4 projection);
}; };
class RectangleElement: public UIElement { class RectangleElement: public UIElement {
protected: protected:
UIValue w, h; UIValue w, h;
unsigned int vao; unsigned int vao;
unsigned int vbo; unsigned int vbo;
public: public:
Color color; Color color;
RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h); RectangleElement(UIValue x, UIValue y, UIValue w, UIValue h);
@ -77,12 +75,11 @@ public:
virtual UIBounds getBounds(); virtual UIBounds getBounds();
virtual void draw(UIPoint screenPos, glm::mat4 projection); virtual void draw(UIPoint screenPos, glm::mat4 projection);
}; };
class ButtonElement: public RectangleElement { class ButtonElement: public RectangleElement {
public: public:
TextElement *text; TextElement *text;
Color color; Color color;
Color hoverColor; Color hoverColor;
@ -97,7 +94,6 @@ public:
virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button); virtual void click(UIPoint pos, UIPoint screenPos, GLFWMouseButton button);
virtual void draw(UIPoint screenPos, glm::mat4 projection); virtual void draw(UIPoint screenPos, glm::mat4 projection);
}; };
} }

View File

@ -8,19 +8,25 @@
namespace kek { namespace kek {
template<typename ...Args> template <typename... Args>
using GenericFunction = void(*)(Args... args); using GenericFunction = void (*)(Args... args);
template<typename ...Args> template <typename... Args>
struct GenericCallable { struct GenericCallable {
GenericFunction<Args..., void *> function; GenericFunction<Args..., void *> function;
void *data; void *data;
GenericCallable(): function(nullptr), data(nullptr) {} GenericCallable()
: function(nullptr),
data(nullptr) {}
GenericCallable(GenericFunction<Args..., void *> function): function(function), data(nullptr) {}; GenericCallable(GenericFunction<Args..., void *> function)
: function(function),
data(nullptr){};
GenericCallable(GenericFunction<Args..., void *> function, void *data): function(function), data(data) {} GenericCallable(GenericFunction<Args..., void *> function, void *data)
: function(function),
data(data) {}
void operator()(Args... args) { void operator()(Args... args) {
if(function == nullptr) return; if(function == nullptr) return;
@ -39,7 +45,12 @@ float clampCyclic(int value, int min, int max);
float clamp(int value, int min, int max); float clamp(int value, int min, int max);
enum class CubeFace { enum class CubeFace {
FRONT, BACK, UP, DOWN, LEFT, RIGHT FRONT,
BACK,
UP,
DOWN,
LEFT,
RIGHT
}; };
glm::vec3 faceNormal(CubeFace face); glm::vec3 faceNormal(CubeFace face);

View File

@ -16,37 +16,40 @@ using namespace kek;
static ButtonElement *button; static ButtonElement *button;
static KeyboardCapture capture = KEK_INVALID_ID; static KeyboardCapture capture = KEK_INVALID_ID;
void periodicCallback(GLFWwindow *window, void *data){ void periodicCallback(GLFWwindow *window, void *data) {
} }
void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data){ void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
} }
void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, void *data){ void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods, void *data) {
} }
void onButtonClick(void *data) { void onButtonClick(void *data) {
button->color = Colors::RED; button->color = Colors::RED;
button->hoverColor = Colors::ORANGE; button->hoverColor = Colors::ORANGE;
capture = Input::captureKeyboardInput(KeyCharCallback([](GLFWwindow *window, unsigned int codepoint, void *data) { capture = Input::captureKeyboardInput(
std::u32string str = Unicode::convertStdToU32(button->text->getText()); KeyCharCallback([](GLFWwindow *window, unsigned int codepoint, void *data) {
if(codepoint == KEK_INPUT_DELETE) { std::u32string str = Unicode::convertStdToU32(button->text->getText());
str = str.substr(0, str.length() - 1); if(codepoint == KEK_INPUT_DELETE) {
}else { str = str.substr(0, str.length() - 1);
str.push_back(codepoint); } else {
} str.push_back(codepoint);
button->text->setText(Unicode::convertU32ToStd(str)); }
}, nullptr), KeyCallback([](GLFWwindow *window, int key, int scancode, int action, int mods, void *data) { button->text->setText(Unicode::convertU32ToStd(str));
if(key == GLFW_KEY_ENTER && action == GLFW_PRESS) { },
Input::uncaptureKeyboardInput(capture); nullptr),
} KeyCallback([](GLFWwindow *window, int key, int scancode, int action, int mods, void *data) {
}, nullptr), Callable([](void *data) { if(key == GLFW_KEY_ENTER && action == GLFW_PRESS) {
button->color = Colors::WHITE; Input::uncaptureKeyboardInput(capture);
button->hoverColor = Colors::GRAY; }
}, nullptr)); },
nullptr),
Callable([](void *data) {
button->color = Colors::WHITE;
button->hoverColor = Colors::GRAY;
},
nullptr));
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
@ -58,7 +61,7 @@ int main(int argc, char **argv) {
{ {
std::shared_ptr<Texture> tex = Texture::load("image/white.png"); std::shared_ptr<Texture> tex = Texture::load("image/white.png");
floor->addMesh(genCubeMesh(100, 1, 100, tex, tex, tex)); floor->addMesh(genCubeMesh(100, 1, 100, tex, tex, tex));
btCollisionShape *shape = new btBoxShape(btVector3(50,0.5,50)); btCollisionShape *shape = new btBoxShape(btVector3(50, 0.5, 50));
floor->addPhysics(shape, 0, btCollisionObject::CF_STATIC_OBJECT); floor->addPhysics(shape, 0, btCollisionObject::CF_STATIC_OBJECT);
scene->addObject(floor); scene->addObject(floor);
} }
@ -67,10 +70,10 @@ int main(int argc, char **argv) {
{ {
std::shared_ptr<Texture> tex = Texture::load("image/white.png"); std::shared_ptr<Texture> tex = Texture::load("image/white.png");
wall->addMesh(genCubeMesh(1, 10, 50, tex, tex, tex)); wall->addMesh(genCubeMesh(1, 10, 50, tex, tex, tex));
btCollisionShape *shape = new btBoxShape(btVector3(0.5,5,25)); btCollisionShape *shape = new btBoxShape(btVector3(0.5, 5, 25));
wall->addPhysics(shape, 0, btCollisionObject::CF_STATIC_OBJECT); wall->addPhysics(shape, 0, btCollisionObject::CF_STATIC_OBJECT);
wall->moveTo(glm::vec3(0,2.5,0)); wall->moveTo(glm::vec3(0, 2.5, 0));
wall->rotate(M_PI / 4, glm::vec3(0,0,1)); wall->rotate(M_PI / 4, glm::vec3(0, 0, 1));
scene->addObject(wall); scene->addObject(wall);
} }
@ -84,7 +87,7 @@ int main(int argc, char **argv) {
Mesh *mesh2 = ObjParser::loadMesh("object/cube_colored/Cube.obj"); Mesh *mesh2 = ObjParser::loadMesh("object/cube_colored/Cube.obj");
GameObject *test3 = new GameObject(); GameObject *test3 = new GameObject();
btCollisionShape *shape2 = new btBoxShape(btVector3(1,1,1)); btCollisionShape *shape2 = new btBoxShape(btVector3(1, 1, 1));
test3->addPhysics(shape2, 0, btCollisionObject::CF_STATIC_OBJECT); test3->addPhysics(shape2, 0, btCollisionObject::CF_STATIC_OBJECT);
test3->addMesh(mesh2); test3->addMesh(mesh2);
test3->moveTo(glm::vec3(2, 1, 2)); test3->moveTo(glm::vec3(2, 1, 2));
@ -92,7 +95,7 @@ int main(int argc, char **argv) {
for(int i = 0; i < 10; i++) { for(int i = 0; i < 10; i++) {
GameObject *test2 = new GameObject(); GameObject *test2 = new GameObject();
btCollisionShape *shape = new btBoxShape(btVector3(1,1,1)); btCollisionShape *shape = new btBoxShape(btVector3(1, 1, 1));
test2->addPhysics(shape, 10); test2->addPhysics(shape, 10);
test2->addMesh(ObjParser::loadMesh("object/cube_colored/Cube.obj")); test2->addMesh(ObjParser::loadMesh("object/cube_colored/Cube.obj"));
test2->moveTo(glm::vec3(1.0f, 5.0f, 3 * i)); test2->moveTo(glm::vec3(1.0f, 5.0f, 3 * i));
@ -100,15 +103,15 @@ int main(int argc, char **argv) {
} }
PointLight *light = new PointLight(glm::vec3(1), 1, 0, 0.1); PointLight *light = new PointLight(glm::vec3(1), 1, 0, 0.1);
light->moveTo(glm::vec3(0,1,0)); light->moveTo(glm::vec3(0, 1, 0));
scene->lights->add(light); scene->lights->add(light);
//DirectionalLight *l = new DirectionalLight(glm::vec3(1), glm::vec3(1, -1, 1)); // DirectionalLight *l = new DirectionalLight(glm::vec3(1), glm::vec3(1, -1, 1));
//scene->lights->add(l); // scene->lights->add(l);
//SpotLight *spot = new SpotLight(glm::vec3(1), glm::vec3(0, 0, -1), 1, 0, 1, glm::radians(8.0f), glm::radians(15.0f)); // SpotLight *spot = new SpotLight(glm::vec3(1), glm::vec3(0, 0, -1), 1, 0, 1, glm::radians(8.0f), glm::radians(15.0f));
//spot->moveTo(glm::vec3(0, 0, 5)); // spot->moveTo(glm::vec3(0, 0, 5));
//scene->lights->add(spot); // scene->lights->add(spot);
Engine::setActiveScene(scene); Engine::setActiveScene(scene);