Compare commits

..

3 Commits

Author SHA1 Message Date
Gorbunov f071a02057 Added visual camera upd 2024-05-15 12:56:54 +03:00
Gorbunov 960a4ce7ed Added textures comp 2024-05-15 12:56:11 +03:00
Gorbunov 1521d9f0b3 Added block obj 2024-05-15 12:54:06 +03:00
6 changed files with 444 additions and 0 deletions

71
include/block.h Normal file
View File

@ -0,0 +1,71 @@
#pragma once
#include <glm/vec2.hpp>
#include <iostream>
enum BLOCKS
{
air = 0,
grass,
dirt,
stone,
ice,
BLOCKS_COUNT
};
class Block
{
public:
Block() {};
Block(int type):type(type){};
Block(Block &other) { this->type = other.type; }
uint8_t &getType() { return type; }
uint8_t getType() const { return type; }
bool isTransparent() const
{
return !isOpaque();
}
bool isTranslucent() const
{
if(type == ice)
{
return true;
}
return false;
}
bool isOpaque() const
{
if(type == air || type == ice)
{
return false;
}
return true;
}
bool isAir() const
{
return type == air;
}
const char *getBlockName() const;
glm::ivec2 getPositionInAtlas(int face);
friend std::ostream &operator<<(std::ostream &os, const Block &block);
Block & operator=(const Block &other)
{
this->type = other.type;
return *this;
}
private:
unsigned char type = 0;
};

94
include/camera.h Normal file
View File

@ -0,0 +1,94 @@
#pragma once
#include <glm/glm.hpp>
class Camera
{
public:
glm::vec3 &getPosition() { return position; }
glm::ivec3 getPositionInWorld()
{
glm::ivec3 p = position;
if (position.x < 0)
{
p.x--;
}
if(position.y < 0)
{
p.y--;
}
if(position.z < 0)
{
p.z--;
}
return p;
}
glm::vec3 getViewDirection();
//full view transform matrix
glm::mat4 getProjectionViewMatrix();
//view matrix
glm::mat4 getViewMatrix();
//just the projection matrix
glm::mat4 getProjectionMatrix();
void rotateCamera(glm::vec2 v) { rotateCamera(v.x, v.y); }
void rotateCamera(float x, float y);
void updateAspectRatio(float w, float h)
{
aspectRatio = w / h;
}
virtual void move(glm::vec3 direction) = 0;
protected:
glm::vec3 position = {};
glm::vec3 upVector = { 0,1,0 };
glm::vec2 viewAngle = {};
float fovRadians = glm::radians(100.f);
float closePlane = 0.1f;
float farPlane = 100.f;
float aspectRatio = 1.f;
};
//just moves in the direction without taking into acount it's orientation
class KinematicCamera : public Camera
{
public:
virtual void move(glm::vec3 direction) override { position += direction; };
};
//moves in the direction of the orientation but stays at the same height
class CreativeModeCamera: public Camera
{
public:
virtual void move(glm::vec3 direction) override;
};
//just moves in the direction of the orientation
class FlyCamera: public Camera
{
public:
virtual void move(glm::vec3 direction) override;
};

19
include/texture.h Normal file
View File

@ -0,0 +1,19 @@
#pragma once
#include <string>
#include <glad/glad.h>
class Texture
{
public:
Texture() = default;
Texture(std::string path) { load(path); };
void bind(int slot);
void load(std::string path);
void clear();
private:
GLuint id = 0;
};

94
src/block.cpp Normal file
View File

@ -0,0 +1,94 @@
#include "block.h"
glm::ivec2 frontFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 backFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 topFaces[BLOCKS_COUNT] =
{
{0,0},
{0, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 bottomFaces[BLOCKS_COUNT] =
{
{0,0},
{2, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 leftFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
glm::ivec2 rightFaces[BLOCKS_COUNT] =
{
{0,0},
{3, 15}, //grass
{2, 15}, // dirt
{1, 15}, //stone
{3, 11}, //ice
};
static glm::ivec2 *faces[6] =
{
frontFaces, backFaces, topFaces, bottomFaces, leftFaces, rightFaces
};
glm::ivec2 Block::getPositionInAtlas(int face)
{
return faces[face][type];
}
static const char *blockNames[BLOCKS_COUNT + 1]
{
"air",
"grass block",
"dirt block",
"stone block",
"ice"
""
};
const char *Block::getBlockName() const
{
return blockNames[type];
}
std::ostream &operator<<(std::ostream &os, const Block &block)
{
os << block.getBlockName();
return os;
}

104
src/camera.cpp Normal file
View File

@ -0,0 +1,104 @@
#include "camera.h"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/gtc/matrix_transform.hpp"
#include <glm/gtx/transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/glm.hpp>
glm::mat4 Camera::getProjectionViewMatrix()
{
return getProjectionMatrix() * getViewMatrix();
}
glm::mat4 Camera::getViewMatrix()
{
return glm::lookAt({0,0,0}, getViewDirection(), upVector);
}
glm::mat4 Camera::getProjectionMatrix()
{
return glm::perspective(fovRadians,
aspectRatio, closePlane, farPlane);
}
glm::vec3 Camera::getViewDirection()
{
glm::vec3 viewDirection = {0,0,-1};
//now we rotate by y vector
viewDirection = glm::mat3(glm::rotate(viewAngle.x, upVector)) * viewDirection;
glm::vec3 vectorToTheRight = glm::cross(viewDirection, upVector);
//now we rotate by x vector
viewDirection = glm::mat3(glm::rotate(viewAngle.y, vectorToTheRight)) * viewDirection;
viewDirection = glm::normalize(viewDirection);
return viewDirection;
}
void Camera::rotateCamera(float x, float y)
{
if(!x && !y)
{
return;
}
x *= -1;
y *= -1;
//float speed = glm::radians(10.f);
float speed = 0.10;
viewAngle += glm::vec2{x, y} *speed;
viewAngle.y = glm::clamp(viewAngle.y, glm::radians(-89.f), glm::radians(89.f));
}
void CreativeModeCamera::move(glm::vec3 direction)
{
glm::vec3 m = {};
m.y = direction.y;
glm::vec3 moveFront = direction;
if (direction.x || direction.z)
{
glm::vec3 v = getViewDirection();
v.y = 0;
m -= glm::normalize(v) * direction.z;
m += glm::normalize(glm::cross(v, upVector)) * direction.x;
}
position += m;
}
void FlyCamera::move(glm::vec3 direction)
{
glm::vec3 m = {};
m.y = direction.y;
glm::vec3 moveFront = direction;
if (direction.x || direction.z || direction.y)
{
glm::vec3 v = getViewDirection();
glm::vec3 vectorToTheRight = glm::cross(v, upVector);
m -= glm::normalize(v) * direction.z;
m += glm::normalize(glm::cross(v, upVector)) * direction.x;
m -= glm::normalize(glm::cross(v, vectorToTheRight)) * direction.y;
}
position += m;
}

62
src/texture.cpp Normal file
View File

@ -0,0 +1,62 @@
#include "texture.h"
#include "stb_image/stb_image.h"
#include <fstream>
#include <iostream>
void Texture::bind(int slot)
{
glBindTexture(GL_TEXTURE_2D, id);
glActiveTexture(GL_TEXTURE0 + slot);
}
void Texture::load(std::string path)
{
std::ifstream f(path, std::ios::binary);
if (!f.is_open())
{
throw(std::string( "err loading ") + path + "\n");
}
//most vexing parse here yay love cpp
std::string ret{ std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>() };
stbi_set_flip_vertically_on_load(true);
int width = 0;
int height = 0;
int channels = 0;
const unsigned char *decodedImage = stbi_load_from_memory((unsigned char*)ret.c_str(),
ret.size(), &width, &height, &channels, 4);
glGenTextures(1, &id);
glBindTexture(GL_TEXTURE_2D, id);
glActiveTexture(GL_TEXTURE0);
//minecraft is a pixel game so free optimizations, no bilinear filtey yay
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, decodedImage);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LOD, 4);
this->id = id;
stbi_image_free((void*)decodedImage);
}
void Texture::clear()
{
glDeleteTextures(1, &id);
id = 0;
}