Basic rendering

This commit is contained in:
MrLetsplay 2022-10-08 00:14:45 +02:00
parent 118f63b910
commit 396e5611c4
21 changed files with 3371 additions and 41 deletions

View File

@ -0,0 +1,128 @@
#include "camera.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/euler_angles.hpp>
#include <math.h>
#include <glm/gtx/matrix_decompose.hpp>
#include "object.h"
#include "utils.h"
namespace kek {
Camera::Camera() {
this->position = glm::vec3(0.0f, 0.0f, 0.0f);
this->direction = glm::vec3(0.0f, 0.0f, -1.0f);
this->roll = 0;
}
void Camera::lookAt(glm::vec3 direction) {
this->direction = glm::normalize(direction);
}
void Camera::lookAtPos(glm::vec3 pos) {
this->direction = glm::normalize(pos - position);
}
void Camera::setYaw(float degrees) {
glm::vec3 euler = eulerAngles();
euler.x = degrees;
applyEuler(euler);
}
void Camera::rotateYaw(float degrees) {
glm::vec3 euler = eulerAngles();
euler.x += degrees;
applyEuler(euler);
}
void Camera::setPitch(float degrees) {
glm::vec3 euler = eulerAngles();
euler.y = degrees;
applyEuler(euler);
}
void Camera::rotatePitch(float degrees) {
glm::vec3 euler = eulerAngles();
euler.y += degrees;
applyEuler(euler);
}
void Camera::setRoll(float degrees) {
roll = degrees;
}
void Camera::rotateRoll(float degrees) {
roll += degrees;
}
glm::mat4 Camera::transformationMatrix() {
float rollRad = glm::radians(roll);
float x = sin(rollRad);
float y = cos(rollRad);
glm::vec3 up = glm::vec3(x, y, 0.0f);
//glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f);
glm::vec3 right = glm::normalize(glm::cross(direction, up));
glm::vec3 cameraUp = glm::normalize(glm::cross(right, direction));
glm::mat4 transform = glm::mat4(1.0f);
transform[0][0] = right.x;
transform[1][0] = right.y;
transform[2][0] = right.z;
transform[0][1] = cameraUp.x;
transform[1][1] = cameraUp.y;
transform[2][1] = cameraUp.z;
transform[0][2] = -direction.x;
transform[1][2] = -direction.y;
transform[2][2] = -direction.z;
transform[3][0] = -glm::dot(right, position);
transform[3][1] = -glm::dot(cameraUp, position);
transform[3][2] = glm::dot(direction, position);
return transform;
}
void Camera::lookAtMatrix(glm::mat4 mat) {
glm::quat q;
float root, trace = mat[0].x + mat[1].y + mat[2].z;
root = sqrt(trace + 1.0f);
q.w = 0.5f * root;
root = 0.5f / root;
q.x = root * (mat[1].z - mat[2].y);
q.y = root * (mat[2].x - mat[0].z);
q.z = root * (mat[0].y - mat[1].x);
this->direction = glm::vec3(0.0, 0.0, -1.0) * q;
}
// x = yaw, y = pitch, z = roll
glm::vec3 Camera::eulerAngles() {
// Yaw
float yawDegrees;
if(direction.x == 0 && direction.z == 0) {
yawDegrees = 0;
}else {
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
if(yawDegrees > 180.0f) {
yawDegrees -= 360.0f;
}
}
// Pitch
float pitchDegrees = glm::degrees(atan(direction.y / sqrt(direction.x * direction.x + direction.z * direction.z)));
return glm::vec3(yawDegrees, pitchDegrees, roll);
}
void Camera::applyEuler(glm::vec3 euler) {
euler.y = clamp(euler.y, -89.0f, 89.0f);
direction.x = sin(glm::radians(euler.x)) * cos(glm::radians(euler.y)); // sin(yaw) * cos(pitch)
direction.y = sin(glm::radians(euler.y)); // sin(pitch)
direction.z = cos(glm::radians(euler.x)) * cos(glm::radians(euler.y)); // cos(yaw) * cos(pitch)
roll = euler.z;
}
}

View File

@ -8,10 +8,12 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <glm/gtc/type_ptr.hpp>
namespace kek {
static GLFWwindow *window;
static Camera *cam;
static int screenWidth = 800.0f;
static int screenHeight = 600.0f;
@ -28,6 +30,27 @@ static void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum se
std::cout << "OpenGL Debug (" << id << "): " << message << std::endl;
}
void onCursorPosCallback(GLFWwindow *window, double x, double y) {
static bool firstMouse = true;
static double lastX = 0, lastY = 0;
if(firstMouse) {
lastX = x;
lastY = y;
firstMouse = false;
}
float xoff = lastX - x;
float yoff = lastY - y;
lastX = x;
lastY = y;
xoff *= 0.1f;
yoff *= 0.1f;
cam->rotateYaw(xoff);
cam->rotatePitch(yoff);
}
int init() {
// Init GLFW
if (glfwInit() != GL_TRUE) {
@ -53,7 +76,7 @@ int init() {
glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
/*window = glfwCreateWindow(screenWidth, screenHeight, "MrLetsplay's weird OpenGL stuff", NULL, NULL);
window = glfwCreateWindow(screenWidth, screenHeight, "KekEngine", NULL, NULL);
if(window == NULL) {
const char *errorMsg;
int code = glfwGetError(&errorMsg);
@ -92,16 +115,79 @@ int init() {
glViewport(0, 0, screenWidth, screenHeight);
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);*/
glfwSetFramebufferSizeCallback(window, framebufferSizeCallback);
//char *file = (char *) "# Blender 3.3.0\n# www.blender.org\nmtllib Cube.mtl\no Cube\nv 1.000000 1.000000 -1.000000\nv 1.000000 -1.000000 -1.000000\nv 1.000000 1.000000 1.000000\nv 1.000000 -1.000000 1.000000\nv -1.000000 1.000000 -1.000000\nv -1.000000 -1.000000 -1.000000\nv -1.000000 1.000000 1.000000\nv -1.000000 -1.000000 1.000000\nvn -0.0000 1.0000 -0.0000\nvn -0.0000 -0.0000 1.0000\nvn -1.0000 -0.0000 -0.0000\nvn -0.0000 -1.0000 -0.0000\nvn 1.0000 -0.0000 -0.0000\nvn -0.0000 -0.0000 -1.0000\nvt 0.625000 0.500000\nvt 0.375000 0.500000\nvt 0.625000 0.750000\nvt 0.375000 0.750000\nvt 0.875000 0.500000\nvt 0.625000 0.250000\nvt 0.125000 0.500000\nvt 0.375000 0.250000\nvt 0.875000 0.750000\nvt 0.625000 1.000000\nvt 0.625000 0.000000\nvt 0.375000 1.000000\nvt 0.375000 0.000000\nvt 0.125000 0.750000\ns 0\nusemtl Material\nf 1/1/1 5/5/1 7/9/1 3/3/1\nf 4/4/2 3/3/2 7/10/2 8/12/2\nf 8/13/3 7/11/3 5/6/3 6/8/3\nf 6/7/4 2/2/4 4/4/4 8/14/4\nf 2/2/5 1/1/5 3/3/5 4/4/5\nf 6/8/6 5/6/6 1/1/6 2/2/6";
//MemoryBuffer *fileBuf = new MemoryBuffer(file, strlen(file));
//Mesh *mesh = ObjParser::parse(fileBuf);
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
MemoryBuffer *buf = kek::Resource::loadResource("../src/res/shader/mesh/fragment.glsl");
if(buf) {
std::cout << buf << std::endl;
delete buf;
glfwSetCursorPosCallback(window, onCursorPosCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glClearColor(0.1f, 0.3f, 0.1f, 0.0f);
cam = new Camera();
kek::MemoryBuffer *buf = kek::Resource::loadResource("../src/res/object/sphere/Sphere.obj");
if(!buf) std::cout << "err buf" << std::endl;
kek::Mesh *mesh = kek::ObjParser::parse(buf);
kek::Shader *shader = new kek::Shader("../src/res/shader/mesh/vertex.glsl", "../src/res/shader/mesh/fragment.glsl");
while(1) {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, screenWidth, screenHeight);
if(glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cam->translate(cam->direction * 0.1f);
}
if(glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cam->translate(cam->direction * -0.1f);
}
if(glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
glm::vec3 camRight = glm::normalize(glm::cross(cam->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
cam->translate(-camRight * 0.1f);
}
if(glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
glm::vec3 camRight = glm::normalize(glm::cross(cam->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
cam->translate(camRight * 0.1f);
}
if(glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
cam->translateY(0.1f);
}
if(glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS) {
cam->translateY(-0.1f);
}
if(glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
break;
}
shader->use();
glm::mat4 view = cam->transformationMatrix();
glm::mat4 projection;
projection = glm::perspective(glm::radians(90.0f), screenWidth / (float) screenHeight, KEK_CAMERA_NEAR, KEK_CAMERA_FAR);
glm::vec3 position = cam->getPosition();
glUniformMatrix4fv(glGetUniformLocation(shader->id, "view"), 1, GL_FALSE, glm::value_ptr(view));
glUniformMatrix4fv(glGetUniformLocation(shader->id, "projection"), 1, GL_FALSE, glm::value_ptr(projection));
glUniform3fv(glGetUniformLocation(shader->id, "cameraPos"), 1, glm::value_ptr(position));
glm::mat4 model = glm::mat4(1.0f);
glUniformMatrix4fv(glGetUniformLocation(shader->id, "model"), 1, GL_FALSE, glm::value_ptr(model));
mesh->draw(shader);
// Swap buffers and poll window events
glfwSwapBuffers(window);
glfwPollEvents();
}
return KEK_SUCCESS;

View File

@ -0,0 +1,78 @@
#include "mesh.h"
#include "constants.h"
#include <GL/glew.h>
namespace kek {
Vertex::Vertex(glm::vec3 pos, glm::vec3 normal, glm::vec2 texCoords) {
this->pos = pos;
this->normal = normal;
this->texCoords = texCoords;
}
Mesh::Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices) {
this->vertices = vertices;
this->indices = indices;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
//glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glVertexAttribPointer(KEK_VERTEX_SHADER_IN_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), 0);
glEnableVertexAttribArray(KEK_VERTEX_SHADER_IN_POSITION);
glVertexAttribPointer(KEK_VERTEX_SHADER_IN_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, normal));
glEnableVertexAttribArray(KEK_VERTEX_SHADER_IN_NORMAL);
glVertexAttribPointer(KEK_VERTEX_SHADER_IN_TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *) offsetof(Vertex, texCoords));
glEnableVertexAttribArray(KEK_VERTEX_SHADER_IN_TEXCOORD);
glBindVertexArray(0);
GLenum err = glGetError();
if(err != GL_NO_ERROR) {
std::cout << "OpenGL error while loading mesh: " << err << std::endl;
exit(1);
}
}
Mesh::~Mesh() {
glDeleteBuffers(1, &ebo);
glDeleteBuffers(1, &vbo);
glDeleteVertexArrays(1, &vao);
}
void Mesh::draw(Shader *shader) {
glBindVertexArray(vao);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
/*if(!light) {
material->diffuse->use(GL_TEXTURE0);
glUniform1i(glGetUniformLocation(shader->id, "material.diffuse"), 0);
material->specular->use(GL_TEXTURE1);
glUniform1i(glGetUniformLocation(shader->id, "material.specular"), 1);
glUniform1f(glGetUniformLocation(shader->id, "material.shininess"), material->shininess);
}*/
// Bind vertices + texture coordinates
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}
}

View File

@ -0,0 +1,56 @@
#include "object.h"
namespace kek {
void Object::translateX(float delta) {
translate(glm::vec3(delta, 0, 0));
}
void Object::translateY(float delta) {
translate(glm::vec3(0, delta, 0));
}
void Object::translateZ(float delta) {
translate(glm::vec3(0, 0, delta));
}
DefaultObject::DefaultObject() {
this->position = glm::vec3(0.0f, 0.0f, 0.0f);
}
void DefaultObject::translate(glm::vec3 delta) {
position += delta;
}
void DefaultObject::moveTo(glm::vec3 position) {
//this->position = glm::vec3(position.x, position.y, position.z);
this->position.x = position.x;
this->position.y = position.y;
this->position.z = position.z;
}
glm::vec3 DefaultObject::getPosition() {
return this->position;
}
void RotateableObject::lookAtPos(glm::vec3 position) {
lookAt(position - this->getPosition());
}
DefaultRotateableObject::DefaultRotateableObject() {
this->rotation = glm::quat(0.0f, 0.0f, 1.0f, 0.0f);
}
void DefaultRotateableObject::rotate(float angle, glm::vec3 axis) {
this->rotation = glm::rotate(rotation, glm::radians(angle), axis);
}
void DefaultRotateableObject::lookAt(glm::vec3 direction) {
this->rotation = glm::quatLookAt(glm::normalize(direction), glm::vec3(0.0f, 1.0f, 0.0f));
}
glm::quat DefaultRotateableObject::getRotation() {
return this->rotation;
}
}

View File

@ -4,6 +4,7 @@
#include <string>
#include <sstream>
#include <glm/glm.hpp>
#include <memory>
#include "types.h"
@ -62,11 +63,11 @@ Mesh *parse(MemoryBuffer *buf) {
uint32_t v3, v3T, v3N;
readFaceVertex(str, &v3, &v3T, &v3N);
vertices.push_back(Vertex(vertexPositions[v1], vertexNormals[v1N], vertexTexCoords[v1T]));
vertices.push_back(Vertex(vertexPositions[v1 - 1], vertexNormals[v1N - 1], vertexTexCoords[v1T - 1]));
indices.push_back(indices.size());
vertices.push_back(Vertex(vertexPositions[v2], vertexNormals[v2N], vertexTexCoords[v2T]));
vertices.push_back(Vertex(vertexPositions[v2 - 1], vertexNormals[v2N - 1], vertexTexCoords[v2T - 1]));
indices.push_back(indices.size());
vertices.push_back(Vertex(vertexPositions[v3], vertexNormals[v3N], vertexTexCoords[v3T]));
vertices.push_back(Vertex(vertexPositions[v3 - 1], vertexNormals[v3N - 1], vertexTexCoords[v3T - 1]));
indices.push_back(indices.size());
}else {
std::cout << "Ignoring unknown OBJ command: " << cmd << std::endl;

View File

@ -23,18 +23,19 @@ static GLuint compileShader(GLenum type, std::string path) {
GLint success;
GLuint vertex = glCreateShader(type);
glShaderSource(vertex, 1, &src, NULL);
glCompileShader(vertex);
GLuint shaderID = glCreateShader(type);
glShaderSource(shaderID, 1, &src, NULL);
glCompileShader(shaderID);
glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
glGetShaderiv(shaderID, GL_COMPILE_STATUS, &success);
if(!success) {
char log[512];
glGetShaderInfoLog(vertex, 512, NULL, log);
std::cout << "Failed to compile vertex shader \"" << path << "\":\n" << log << std::endl;
glGetShaderInfoLog(shaderID, 512, NULL, log);
std::cout << "Failed to compile shader \"" << path << "\":\n" << log << std::endl;
throw std::exception();
}
return vertex;
return shaderID;
}
static GLuint compileProgram(unsigned int n, GLenum *types, std::string *paths) {
@ -60,6 +61,7 @@ static GLuint compileProgram(unsigned int n, GLenum *types, std::string *paths)
char log[512];
glGetProgramInfoLog(id, 512, NULL, log);
std::cout << "Failed to link program:\n" << log << std::endl;
throw std::exception();
}
// Delete shaders
@ -71,19 +73,37 @@ static GLuint compileProgram(unsigned int n, GLenum *types, std::string *paths)
}
Shader::Shader(std::string vertexPath, std::string fragmentPath) {
GLenum types[2];
types[0] = GL_VERTEX_SHADER;
types[1] = GL_FRAGMENT_SHADER;
std::string paths[2];
paths[0] = vertexPath;
paths[1] = fragmentPath;
id = compileProgram(2, types, paths);
}
Shader::Shader(std::string vertexPath, std::string geometryPath, std::string fragmentPath) {
GLenum types[3];
types[0] = GL_VERTEX_SHADER;
types[1] = GL_GEOMETRY_SHADER;
types[2] = GL_FRAGMENT_SHADER;
std::string paths[3];
paths[0] = vertexPath;
paths[1] = geometryPath;
paths[2] = fragmentPath;
id = compileProgram(3, types, paths);
}
Shader::~Shader() {
glDeleteProgram(id);
}
void Shader::use() {
glUseProgram(id);
}
}

174
src/kekengine/cpp/utils.cpp Normal file
View File

@ -0,0 +1,174 @@
#include "utils.h"
#include <vector>
namespace kek {
float clampCyclic(float value, float min, float max) {
while(value > max) value -= (max - min);
while(value < min) value += (max - min);
return value;
}
float clamp(float value, float min, float max) {
if(value > max) value = max;
if(value < min) value = min;
return value;
}
float clampCyclic(int value, int min, int max) {
while(value > max) value -= (max - min);
while(value < min) value += (max - min);
return value;
}
float clamp(int value, int min, int max) {
if(value > max) value = max;
if(value < min) value = min;
return value;
}
glm::vec3 faceNormal(CubeFace face) {
switch(face) {
case CubeFace::FRONT:
return glm::vec3(0.0f, 0.0f, 1.0f);
case CubeFace::BACK:
return glm::vec3(0.0f, 0.0f, -1.0f);
case CubeFace::UP:
return glm::vec3(0.0f, 1.0f, 0.0f);
case CubeFace::DOWN:
return glm::vec3(0.0f, -1.0f, 0.0f);
case CubeFace::LEFT:
return glm::vec3(-1.0f, 0.0f, 0.0f);
case CubeFace::RIGHT:
return glm::vec3(1.0f, 0.0f, 0.0f);
}
return glm::vec3(0.0f, 0.0f, 0.0f);
}
float cubeTriangles[] = {
// Front
-0.5f, -0.5f, 0.5f, // 0
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, // 3
-0.5f, -0.5f, 0.5f, // 0
// Back
0.5f, -0.5f, -0.5f, // 5
-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, // 6
0.5f, -0.5f, -0.5f, // 5
// Up
-0.5f, 0.5f, 0.5f, // 3
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, // 7
-0.5f, 0.5f, 0.5f, // 3
// Down
0.5f, -0.5f, -0.5f, // 5
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, // 4
0.5f, -0.5f, -0.5f, // 5
// Left
-0.5f, -0.5f, -0.5f, // 4
-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, // 7
-0.5f, -0.5f, -0.5f, // 4
// Right
0.5f, -0.5f, 0.5f, // 1
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, // 2
0.5f, -0.5f, 0.5f, // 1
};
float textureCoordinates[] = {
// Front
0.0f, 0.0f, // 0
1.0f, 0.0f, // 1
1.0f, 1.0f, // 2
1.0f, 1.0f, // 2
0.0f, 1.0f, // 3
0.0f, 0.0f, // 0
// Back
0.0f, 0.0f, // 5
1.0f, 0.0f, // 4
1.0f, 1.0f, // 7
1.0f, 1.0f, // 7
0.0f, 1.0f, // 6
0.0f, 0.0f, // 5
// Up
0.0f, 0.0f, // 3
1.0f, 0.0f, // 2
1.0f, 1.0f, // 6
1.0f, 1.0f, // 6
0.0f, 1.0f, // 7
0.0f, 0.0f, // 3
// Down
1.0f, 1.0f, // 5
1.0f, 0.0f, // 1
0.0f, 0.0f, // 0
0.0f, 0.0f, // 0
0.0f, 1.0f, // 4
1.0f, 1.0f, // 5
// Left
0.0f, 0.0f, // 4
1.0f, 0.0f, // 0
1.0f, 1.0f, // 3
1.0f, 1.0f, // 3
0.0f, 1.0f, // 7
0.0f, 0.0f, // 4
// Right
0.0f, 0.0f, // 1
1.0f, 0.0f, // 5
1.0f, 1.0f, // 6
1.0f, 1.0f, // 6
0.0f, 1.0f, // 2
0.0f, 0.0f, // 1
};
std::vector<kek::Vertex> genCubeVertices(float w, float h, float d, bool texCoords) {
std::vector<kek::Vertex> vertices;
for(int i = 0; i < 6; i++) {
CubeFace f = (CubeFace) i;
glm::vec3 normal = faceNormal(f);
for(int v = 0; v < 6; v++) {
kek::Vertex vertex(
glm::vec3(cubeTriangles[i * 18 + v * 3] * w, cubeTriangles[i * 18 + v * 3 + 1] * h, cubeTriangles[i * 18 + v * 3 + 2] * d),
normal,
texCoords ? glm::vec2(textureCoordinates[i * 12 + v * 2], textureCoordinates[i * 12 + v * 2 + 1]) : glm::vec2());
vertices.push_back(vertex);
}
}
return vertices;
}
std::string toString(glm::vec3 vector) {
return std::to_string(vector.x) + " " + std::to_string(vector.y) + " " + std::to_string(vector.z);
}
}

View File

@ -0,0 +1,44 @@
#pragma once
#include <glm/glm.hpp>
#include "object.h"
namespace kek {
class Camera: public DefaultObject {
public:
glm::vec3 direction;
float roll;
Camera();
void lookAt(glm::vec3 direction);
void lookAtPos(glm::vec3 pos);
void setYaw(float degrees);
void rotateYaw(float degrees);
void setPitch(float degrees);
void rotatePitch(float degrees);
void setRoll(float degrees);
void rotateRoll(float degrees);
glm::mat4 transformationMatrix();
void lookAtMatrix(glm::mat4 matrix);
// x = yaw, y = pitch, z = roll
glm::vec3 eulerAngles();
void applyEuler(glm::vec3 euler);
};
}

View File

@ -2,3 +2,10 @@
#define KEK_SUCCESS 0
#define KEK_ERROR 1
#define KEK_VERTEX_SHADER_IN_POSITION 0
#define KEK_VERTEX_SHADER_IN_NORMAL 1
#define KEK_VERTEX_SHADER_IN_TEXCOORD 2
#define KEK_CAMERA_NEAR 0.1f
#define KEK_CAMERA_FAR 100.0f

View File

@ -1,6 +1,15 @@
#pragma once
#include "camera.h"
#include "constants.h"
#include "errordialog.h"
#include "mesh.h"
#include "object.h"
#include "objparser.h"
#include "resource.h"
#include "shader.h"
#include "types.h"
#include "utils.h"
namespace kek {

View File

@ -1,5 +1,7 @@
#pragma once
#include "shader.h"
#include <glm/glm.hpp>
#include <vector>
#include <cstdint>
@ -12,11 +14,8 @@ struct Vertex {
glm::vec3 normal;
glm::vec2 texCoords;
Vertex(glm::vec3 pos, glm::vec3 normal, glm::vec2 texCoords) {
this->pos = pos;
this->normal = normal;
this->texCoords = texCoords;
}
Vertex(glm::vec3 pos, glm::vec3 normal, glm::vec2 texCoords);
};
class Mesh {
@ -25,13 +24,14 @@ public:
std::vector<Vertex> vertices;
std::vector<uint32_t> indices;
Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices) {
this->vertices = vertices;
this->indices = indices;
}
unsigned int vao, vbo, ebo;
Mesh(std::vector<Vertex> vertices, std::vector<uint32_t> indices);
~Mesh();
void draw(Shader *shader);
};
}

View File

@ -1 +1,84 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
namespace kek {
class Object {
public:
virtual ~Object() {};
virtual void translate(glm::vec3 delta) = 0;
virtual void translateX(float delta);
virtual void translateY(float delta);
virtual void translateZ(float delta);
virtual void moveTo(glm::vec3 position) = 0;
virtual glm::vec3 getPosition() = 0;
};
class DefaultObject: public Object {
protected:
glm::vec3 position;
public:
DefaultObject();
virtual ~DefaultObject() {};
virtual void translate(glm::vec3 delta);
virtual void moveTo(glm::vec3 position);
virtual glm::vec3 getPosition();
};
class RotateableObject: public Object {
public:
virtual ~RotateableObject() {};
virtual void rotate(float angle, glm::vec3 axis) = 0;
virtual void lookAt(glm::vec3 direction) = 0;
virtual void lookAtPos(glm::vec3 position);
virtual glm::quat getRotation() = 0;
};
class DefaultRotateableObject: public RotateableObject, public DefaultObject {
protected:
glm::quat rotation;
public:
DefaultRotateableObject();
virtual ~DefaultRotateableObject() {};
virtual void rotate(float angle, glm::vec3 axis);
virtual void lookAt(glm::vec3 direction);
virtual glm::quat getRotation();
virtual void translate(glm::vec3 delta) { DefaultObject::translate(delta); }
virtual void moveTo(glm::vec3 position) { DefaultObject::moveTo(position); };
virtual glm::vec3 getPosition() { return DefaultObject::getPosition(); };
};
}

View File

@ -0,0 +1,46 @@
#pragma once
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <iostream>
#include "mesh.h"
namespace kek {
template<typename ...Args>
using generic_function_t = void(*)(Args... args);
template<typename ...Args>
struct generic_callable_t {
generic_function_t<Args..., void *> function;
void *data;
void operator()(Args... args) {
function(args..., data);
}
};
typedef generic_callable_t<> callable_t;
float clampCyclic(float value, float min, float max);
float clamp(float value, float min, float max);
float clampCyclic(int value, int min, int max);
float clamp(int value, int min, int max);
enum class CubeFace {
FRONT, BACK, UP, DOWN, LEFT, RIGHT
};
glm::vec3 faceNormal(CubeFace face);
void genCubeVertices(float w, float h, float d, bool texCoords, float **outVerts, size_t *outSize, size_t *vertexCount);
std::vector<Vertex> genCubeVertices(float w, float h, float d, bool texCoords);
std::string toString(glm::vec3 vector);
}

View File

@ -1,6 +1,8 @@
#include <iostream>
#include "kekengine.h"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
int main(int argc, char **argv) {
kek::init();

View File

@ -0,0 +1,12 @@
# Blender 3.3.0 MTL File: 'None'
# www.blender.org
newmtl Material
Ns 250.000000
Ka 1.000000 1.000000 1.000000
Kd 0.800000 0.800000 0.800000
Ks 0.500000 0.500000 0.500000
Ke 0.000000 0.000000 0.000000
Ni 1.450000
d 1.000000
illum 2

View File

@ -0,0 +1,46 @@
# Blender 3.3.0
# www.blender.org
mtllib Cube.mtl
o Cube
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vn -0.0000 1.0000 -0.0000
vn -0.0000 -0.0000 1.0000
vn -1.0000 -0.0000 -0.0000
vn -0.0000 -1.0000 -0.0000
vn 1.0000 -0.0000 -0.0000
vn -0.0000 -0.0000 -1.0000
vt 0.625000 0.500000
vt 0.375000 0.500000
vt 0.625000 0.750000
vt 0.375000 0.750000
vt 0.875000 0.500000
vt 0.625000 0.250000
vt 0.125000 0.500000
vt 0.375000 0.250000
vt 0.875000 0.750000
vt 0.625000 1.000000
vt 0.625000 0.000000
vt 0.375000 0.000000
vt 0.375000 1.000000
vt 0.125000 0.750000
s 0
usemtl Material
f 5/5/1 3/3/1 1/1/1
f 3/3/2 8/13/2 4/4/2
f 7/11/3 6/8/3 8/12/3
f 2/2/4 8/14/4 6/7/4
f 1/1/5 4/4/5 2/2/5
f 5/6/6 2/2/6 6/8/6
f 5/5/1 7/9/1 3/3/1
f 3/3/2 7/10/2 8/13/2
f 7/11/3 5/6/3 6/8/3
f 2/2/4 4/4/4 8/14/4
f 1/1/5 3/3/5 4/4/5
f 5/6/6 1/1/6 2/2/6

View File

@ -0,0 +1,2 @@
# Blender 3.3.0 MTL File: 'None'
# www.blender.org

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,9 @@
#version 330 core
in VS_OUT {
vec3 fragmentPosition;
vec2 textureCoordinate;
vec3 normal;
vec3 fragmentPosition;
} fs_in;
uniform vec3 cameraPos;
@ -11,7 +11,7 @@ uniform vec3 cameraPos;
out vec4 color;
void main() {
vec3 norm = normalize(fs_in.normal);
vec3 viewDir = normalize(cameraPos - fs_in.fragmentPosition);
color = vec4(1.0);
//vec3 norm = normalize(fs_in.normal);
//vec3 viewDir = normalize(cameraPos - fs_in.fragmentPosition);
color = length(fs_in.fragmentPosition) / 2 * vec4(1.0);
}