Compare commits

...

2 Commits

Author SHA1 Message Date
e313656757 fix 2023-03-28 17:11:29 +02:00
cc0b6e8f32 fix 2023-03-28 17:05:20 +02:00
9 changed files with 524 additions and 506 deletions

View File

@ -1,8 +1,47 @@
#ifndef CORE_HPP
#define CORE_HPP
#define SCR_HEIGHT 600
#define SCR_WIDTH 800
#include <set>
#include <map>
#include <array>
#include <tuple>
#include <queue>
#include <vector>
#include <string>
#include <bitset>
#include <stdio.h>
#include <memory>
#include <cassert>
#include <sstream>
#include <fstream>
#include <iostream>
#include <typeinfo>
#include <algorithm>
#include <functional>
#include <filesystem>
#include <unordered_map>
#include <stdlib.h>
/* GLEW */
#include <GL/glew.h>
/* GLFW */
#include <GLFW/glfw3.h>
/* stb */
#include "../vendor/stb_image.h"
/* GLM */
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
/* imgui */
#include "../vendor/imgui.h"
#include "../vendor/imgui_impl_glfw.h"
#include "../vendor/imgui_impl_opengl3.h"
#endif // CORE_HPP

View File

@ -1,185 +1,191 @@
#include "engine.hpp"
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
namespace b3d{
void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); }
void Engine::init() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
void Engine::init() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Blocking3D", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, camera.mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (glewInit() != GLEW_OK) {
std::cout << "GLEW init failed" << std::endl;
return;
}
glEnable(GL_DEPTH_TEST);
Shader chunk_shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
float vertices[] = {
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
0.1f, -0.1f, -0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 1.0f,
0.1f, 0.1f, 0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, 0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, -0.1f, 1.0f, 1.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f
};
//32x32x32 chunk | ONLY FOR TEST THIS IS'NT EVEN A CHUNK
glm::vec3 cubePositions[1024];
for(int i = 0; i < 1024; i++){
cubePositions[i] = glm::vec3(0.2f * (i % 32), 0.2f * (i / 32), 0.2f * (i / 32));
}
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("res/textures/gold.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
chunk_shader.bind();
chunk_shader.setuniform("texture1", 0);
while (!glfwWindowShouldClose(window))
{
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessMovement(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessMovement(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessMovement(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessMovement(RIGHT, deltaTime);
camera.Update();
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
// activate shader
chunk_shader.bind();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
chunk_shader.setuniform("projection", projection);
glm::mat4 view = camera.GetViewMatrix();
chunk_shader.setuniform("view", view);
glBindVertexArray(VAO);
for (unsigned int i = 0; i < (sizeof(cubePositions)/sizeof(*cubePositions)) ; i++)
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Blocking3D", NULL, NULL);
if (window == NULL)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
chunk_shader.setuniform("model", model);
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, camera.mouse_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glDrawArrays(GL_TRIANGLES, 0, 36);
if (glewInit() != GLEW_OK) {
std::cout << "GLEW init failed" << std::endl;
return;
}
glfwSwapBuffers(window);
glfwPollEvents();
glEnable(GL_DEPTH_TEST);
Shader chunk_shader("shaders/chunk_vertex.glsl", "shaders/chunk_fragment.glsl");
float vertices[] = {
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
0.1f, -0.1f, -0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 0.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 1.0f,
0.1f, 0.1f, 0.1f, 1.0f, 1.0f,
-0.1f, 0.1f, 0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
0.1f, -0.1f, -0.1f, 1.0f, 1.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
0.1f, -0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, -0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, -0.1f, -0.1f, 0.0f, 1.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f,
0.1f, 0.1f, -0.1f, 1.0f, 1.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
0.1f, 0.1f, 0.1f, 1.0f, 0.0f,
-0.1f, 0.1f, 0.1f, 0.0f, 0.0f,
-0.1f, 0.1f, -0.1f, 0.0f, 1.0f
};
//32x32x32 chunk | ONLY FOR TEST THIS IS'NT EVEN A CHUNK
glm::vec3 cubePositions[1024];
for(int i = 0; i < 1024; i++){
cubePositions[i] = glm::vec3(0.2f * (i % 32), 0.2f * (i / 32), 0.2f * (i / 32));
}
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
unsigned int texture1;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
int width, height, nrChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char *data = stbi_load("res/textures/gold.png", &width, &height, &nrChannels, 0);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
chunk_shader.bind();
chunk_shader.setuniform("texture1", 0);
while (!glfwWindowShouldClose(window))
{
float currentFrame = static_cast<float>(glfwGetTime());
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
// input
// -----
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessMovement(FORWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
camera.ProcessMovement(BACKWARD, deltaTime);
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
camera.ProcessMovement(LEFT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
camera.ProcessMovement(RIGHT, deltaTime);
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
camera.ProcessMovement(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.ProcessMovement(DOWN, deltaTime);
camera.Update();
// render
// ------
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// bind textures on corresponding texture units
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture1);
// activate shader
chunk_shader.bind();
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
chunk_shader.setuniform("projection", projection);
glm::mat4 view = camera.GetViewMatrix();
chunk_shader.setuniform("view", view);
glBindVertexArray(VAO);
for (unsigned int i = 0; i < (sizeof(cubePositions)/sizeof(*cubePositions)) ; i++)
{
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, cubePositions[i]);
float angle = 20.0f * i;
chunk_shader.setuniform("model", model);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glfwTerminate();
}
}

View File

@ -1,60 +1,33 @@
#ifndef ENGINE_HPP
#define ENGINE_HPP
#include "core.hpp"
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "../vendor/stb_image.h"
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "shader.hpp"
#include "player_camera.hpp"
#include <iostream>
namespace b3d{
#include "../vendor/imgui.h"
#include "../vendor/imgui_impl_glfw.h"
#include "../vendor/imgui_impl_opengl3.h"
class Engine {
private:
GLFWwindow* window;
// settings
//const unsigned int SCR_WIDTH = 800;
//const unsigned int SCR_HEIGHT = 600;
// camera
Camera camera;
/*
struct Node {
std::string name;
std::string texture;
bool spriteNode = false;
};
// timing
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
Node nodes[] = {
{"cobble stone", "res/textures/cobble.png"}
{"iron block", "res/textures/iron.png"}
};*/
class Engine {
private:
GLFWwindow* window;
// settings
//const unsigned int SCR_WIDTH = 800;
//const unsigned int SCR_HEIGHT = 600;
// camera
Camera camera;
// timing
float deltaTime = 0.0f; // time between current frame and last frame
float lastFrame = 0.0f;
public:
Engine() = default;
~Engine() = default;
void init();
void render();
void update();
};
public:
Engine() = default;
~Engine() = default;
void init();
void render();
void update();
};
}
#endif // ENGINE_HPP

View File

@ -1,7 +0,0 @@
#include "../vendor/imgui.h"
void DebugInfo() {
ImGui::Begin("Debug Info");
ImGui::Text("Hello, world!");
ImGui::End();
}

21
src/game/nodes.hpp Normal file
View File

@ -0,0 +1,21 @@
#ifndef NODES_HPP
#define NODES_HPP
#include "core.hpp"
namespace b3d {
/*
struct Node {
std::string name;
std::string texture;
bool spriteNode = false;
};
Node nodes[] = {
{"cobble stone", "res/textures/cobble.png"}
{"iron block", "res/textures/iron.png"}
};
*/
}
#endif // NODES_HPP

View File

@ -6,159 +6,148 @@
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <vector>
namespace b3d{
enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT,
UP,
DOWN
};
enum Camera_Movement {
FORWARD,
BACKWARD,
LEFT,
RIGHT
};
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
const float YAW = -90.0f;
const float PITCH = 0.0f;
const float SPEED = 2.5f;
const float SENSITIVITY = 0.1f;
const float ZOOM = 45.0f;
static float mx,my;
static float mx,my;
class Camera
{
public:
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;
float Yaw;
float Pitch;
float MovementSpeed;
float MouseSensitivity;
float Zoom;
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
class Camera
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
public:
glm::vec3 Position;
glm::vec3 Front;
glm::vec3 Up;
glm::vec3 Right;
glm::vec3 WorldUp;
float Yaw;
float Pitch;
float MovementSpeed;
float MouseSensitivity;
float Zoom;
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
glm::mat4 GetViewMatrix()
{
return glm::lookAt(Position, Position + Front, Up);
}
void Update(){
if (firstMouse)
Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), float yaw = YAW, float pitch = PITCH) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = position;
WorldUp = up;
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
Camera(float posX, float posY, float posZ, float upX, float upY, float upZ, float yaw, float pitch) : Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVITY), Zoom(ZOOM)
{
Position = glm::vec3(posX, posY, posZ);
WorldUp = glm::vec3(upX, upY, upZ);
Yaw = yaw;
Pitch = pitch;
updateCameraVectors();
}
glm::mat4 GetViewMatrix()
{
return glm::lookAt(Position, Position + Front, Up);
}
void Update(){
if (firstMouse)
{
lastX = mx;
lastY = my;
firstMouse = false;
}
float xoffset = mx - lastX;
float yoffset = lastY - my;
lastX = mx;
lastY = my;
firstMouse = false;
ProcessMouseMovement(xoffset, yoffset);
}
float xoffset = mx - lastX;
float yoffset = lastY - my;
lastX = mx;
lastY = my;
ProcessMouseMovement(xoffset, yoffset);
}
void ProcessMovement(Camera_Movement direction, float deltaTime)
{
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
}
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
{
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw += xoffset;
Pitch += yoffset;
if (constrainPitch)
void ProcessMovement(Camera_Movement direction, float deltaTime)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
float velocity = MovementSpeed * deltaTime;
if (direction == FORWARD)
Position += Front * velocity;
if (direction == BACKWARD)
Position -= Front * velocity;
if (direction == LEFT)
Position -= Right * velocity;
if (direction == RIGHT)
Position += Right * velocity;
if (direction == UP)
Position += Up * velocity;
if (direction == DOWN)
Position -= Up * velocity;
}
updateCameraVectors();
}
void ProcessMouseScroll(float yoffset)
{
Zoom -= (float)yoffset;
if (Zoom < 1.0f)
Zoom = 1.0f;
if (Zoom > 45.0f)
Zoom = 45.0f;
}
static void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
{/*
float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn);
if (firstMouse)
void ProcessMouseMovement(float xoffset, float yoffset, GLboolean constrainPitch = true)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
xoffset *= MouseSensitivity;
yoffset *= MouseSensitivity;
Yaw += xoffset;
Pitch += yoffset;
if (constrainPitch)
{
if (Pitch > 89.0f)
Pitch = 89.0f;
if (Pitch < -89.0f)
Pitch = -89.0f;
}
updateCameraVectors();
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos;
lastX = xpos;
lastY = ypos;
ProcessMouseMovement(xoffset, yoffset);*/
mx = static_cast<float>(xposIn);
my = static_cast<float>(yposIn);
}
void ProcessMouseScroll(float yoffset)
{
Zoom -= (float)yoffset;
if (Zoom < 1.0f)
Zoom = 1.0f;
if (Zoom > 45.0f)
Zoom = 45.0f;
}
private:
void updateCameraVectors()
{
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
static void mouse_callback(GLFWwindow* window, double xposIn, double yposIn)
{
mx = static_cast<float>(xposIn);
my = static_cast<float>(yposIn);
}
private:
void updateCameraVectors()
{
glm::vec3 front;
front.x = cos(glm::radians(Yaw)) * cos(glm::radians(Pitch));
front.y = sin(glm::radians(Pitch));
front.z = sin(glm::radians(Yaw)) * cos(glm::radians(Pitch));
Front = glm::normalize(front);
Right = glm::normalize(glm::cross(Front, WorldUp));
Up = glm::normalize(glm::cross(Right, Front));
}
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
};
}
float lastX = SCR_WIDTH / 2.0f;
float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true;
};
#endif

View File

@ -1,104 +1,102 @@
#include "shader.hpp"
#include <iostream>
#include <fstream>
#include <sstream>
namespace b3d{
Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
unsigned int vertex, fragment;
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
checkCompileErrors(program, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
Shader::Shader(const char* vertexPath, const char* fragmentPath)
{
std::string vertexCode;
std::string fragmentCode;
std::ifstream vShaderFile;
std::ifstream fShaderFile;
vShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
fShaderFile.exceptions (std::ifstream::failbit | std::ifstream::badbit);
try
{
vShaderFile.open(vertexPath);
fShaderFile.open(fragmentPath);
std::stringstream vShaderStream, fShaderStream;
vShaderStream << vShaderFile.rdbuf();
fShaderStream << fShaderFile.rdbuf();
vShaderFile.close();
fShaderFile.close();
vertexCode = vShaderStream.str();
fragmentCode = fShaderStream.str();
}
catch (std::ifstream::failure& e)
{
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ: " << e.what() << std::endl;
}
const char* vShaderCode = vertexCode.c_str();
const char * fShaderCode = fragmentCode.c_str();
unsigned int vertex, fragment;
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vShaderCode, NULL);
glCompileShader(vertex);
checkCompileErrors(vertex, "VERTEX");
fragment = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragment, 1, &fShaderCode, NULL);
glCompileShader(fragment);
checkCompileErrors(fragment, "FRAGMENT");
program = glCreateProgram();
glAttachShader(program, vertex);
glAttachShader(program, fragment);
glLinkProgram(program);
checkCompileErrors(program, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(fragment);
}
}
void Shader::bind(){
glUseProgram(program);
}
void Shader::bind(){
glUseProgram(program);
}
void Shader::unbind(){
glUseProgram(0);
}
void Shader::unbind(){
glUseProgram(0);
}
void Shader::setuniform(const GLchar* uName, unsigned int value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::setuniform(const GLchar* uName, unsigned int value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::setuniform(const GLchar* uName, int value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::setuniform(const GLchar* uName, int value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::setuniform(const GLchar* uName, GLfloat value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::setuniform(const GLchar* uName, GLfloat value){
glUniform1i(glGetUniformLocation(program, uName), value);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y){
glUniform2f(glGetUniformLocation(program, uName), x, y);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y){
glUniform2f(glGetUniformLocation(program, uName), x, y);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z){
glUniform3f(glGetUniformLocation(program, uName), x, y, z);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z){
glUniform3f(glGetUniformLocation(program, uName), x, y, z);
}
void Shader::setuniform(const GLchar* uName, glm::vec3 vector){
glUniform3f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z);
}
void Shader::setuniform(const GLchar* uName, glm::vec3 vector){
glUniform3f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w){
glUniform4f(glGetUniformLocation(program, uName), x, y, z, w);
}
void Shader::setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w){
glUniform4f(glGetUniformLocation(program, uName), x, y, z, w);
}
void Shader::setuniform(const GLchar* name, const glm::mat4 &mat)
{
glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, &mat[0][0]);
}
void Shader::setuniform(const GLchar* name, const glm::mat4 &mat)
{
glUniformMatrix4fv(glGetUniformLocation(program, name), 1, GL_FALSE, &mat[0][0]);
}
void Shader::setuniform(const GLchar* uName, glm::vec2 vector){
glUniform2f(glGetUniformLocation(program, uName), vector.x, vector.y);
}
void Shader::setuniform(const GLchar* uName, glm::vec2 vector){
glUniform2f(glGetUniformLocation(program, uName), vector.x, vector.y);
}
void Shader::setuniform(const GLchar* uName, glm::vec4 vector){
glUniform4f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z, vector.w);
}
void Shader::setuniform(const GLchar* uName, GLuint tex2d, GLint unit){ // sample 2d
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tex2d);
glUniform1i(glGetUniformLocation(program, uName), unit);
void Shader::setuniform(const GLchar* uName, glm::vec4 vector){
glUniform4f(glGetUniformLocation(program, uName), vector.x, vector.y, vector.z, vector.w);
}
void Shader::setuniform(const GLchar* uName, GLuint tex2d, GLint unit){ // sample 2d
glActiveTexture(GL_TEXTURE0 + unit);
glBindTexture(GL_TEXTURE_2D, tex2d);
glUniform1i(glGetUniformLocation(program, uName), unit);
}
}

View File

@ -1,62 +1,61 @@
#ifndef SHADER_H
#define SHADER_H
#include <GL/glew.h>
#include <glm/glm.hpp>
#include "core.hpp"
#include <iostream>
namespace b3d{
class Shader {
public:
Shader() : program(0) {}
Shader(const GLuint id) : program(id) {}
Shader(const char* vertexPath, const char* fragmentPath);
~Shader() { glDeleteProgram(program); }
void bind();
void unbind();
void setuniform(const GLchar* uName, unsigned int value);
void setuniform(const GLchar* uName, int value);
void setuniform(const GLchar* uName, GLfloat value);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y);
void setuniform(const GLchar* uName, glm::vec2 vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z);
void setuniform(const GLchar* uName, glm::vec3 vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void setuniform(const GLchar* uName, glm::vec4 vector);
void setuniform(const GLchar* uName, const glm::mat4& mtx);
void setuniform(const GLchar* uName, GLuint tex2d, GLint unit); // sample 2d
GLuint getuniform(const char* name);
GLuint GetProgram() { return program; }
class Shader {
public:
Shader() : program(0) {}
Shader(const GLuint id) : program(id) {}
Shader(const char* vertexPath, const char* fragmentPath);
~Shader() { glDeleteProgram(program); }
void bind();
void unbind();
void setuniform(const GLchar* uName, unsigned int value);
void setuniform(const GLchar* uName, int value);
void setuniform(const GLchar* uName, GLfloat value);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y);
void setuniform(const GLchar* uName, glm::vec2 vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z);
void setuniform(const GLchar* uName, glm::vec3 vector);
void setuniform(const GLchar* uName, GLfloat x, GLfloat y, GLfloat z, GLfloat w);
void setuniform(const GLchar* uName, glm::vec4 vector);
void setuniform(const GLchar* uName, const glm::mat4& mtx);
void setuniform(const GLchar* uName, GLuint tex2d, GLint unit); // sample 2d
GLuint getuniform(const char* name);
GLuint GetProgram() { return program; }
private:
void checkCompileErrors(GLuint shader, std::string type)
{
GLint success;
GLchar infoLog[1024];
if(type != "PROGRAM")
private:
void checkCompileErrors(GLuint shader, std::string type)
{
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success)
GLint success;
GLchar infoLog[1024];
if(type != "PROGRAM")
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if(!success)
{
glGetShaderInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
else
{
glGetProgramiv(shader, GL_LINK_STATUS, &success);
if(!success)
{
glGetProgramInfoLog(shader, 1024, NULL, infoLog);
std::cout << "ERROR::PROGRAM_LINKING_ERROR of type: " << type << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
}
}
}
GLuint program;
};
GLuint program;
};
}
#endif

View File

@ -2,7 +2,7 @@
#include "game/engine.hpp"
int main() {
Engine engine;
b3d::Engine engine;
engine.init();
return 0;
}