Extra whitespaces stripped.
This commit is contained in:
parent
f2e9f10735
commit
810a3b5ed3
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Application.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 05:09:11
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -27,25 +27,25 @@ class Application {
|
||||
public:
|
||||
Application();
|
||||
~Application();
|
||||
|
||||
|
||||
void initGL();
|
||||
|
||||
|
||||
void handleEvents();
|
||||
|
||||
|
||||
void run();
|
||||
|
||||
|
||||
Window &window() { return m_window; }
|
||||
|
||||
|
||||
static Application &getInstance() {
|
||||
static Application instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
Window m_window;
|
||||
|
||||
|
||||
GameClock m_clock;
|
||||
|
||||
|
||||
ApplicationStateStack *m_applicationStateStack;
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: ApplicationStateStack.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:48:48
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -27,13 +27,13 @@ class ApplicationStateStack {
|
||||
public:
|
||||
ApplicationStateStack();
|
||||
~ApplicationStateStack();
|
||||
|
||||
ApplicationState &top() { return *m_stack.top().get(); }
|
||||
|
||||
ApplicationState &top() { return *m_stack.top().get(); }
|
||||
void push(ApplicationState *state) { m_stack.push(std::unique_ptr<ApplicationState>(state)); }
|
||||
void pop() { m_stack.pop(); }
|
||||
|
||||
|
||||
static ApplicationStateStack &getInstance();
|
||||
|
||||
|
||||
private:
|
||||
std::stack<std::unique_ptr<ApplicationState>> m_stack;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Config.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:45:14
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Keyboard.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 00:52:04
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -31,26 +31,26 @@ class Keyboard {
|
||||
W,
|
||||
X,
|
||||
Z,
|
||||
|
||||
|
||||
Left,
|
||||
Right,
|
||||
Up,
|
||||
Down,
|
||||
|
||||
|
||||
BackSpace,
|
||||
LeftShift,
|
||||
Space,
|
||||
Return,
|
||||
RightShift
|
||||
};
|
||||
|
||||
|
||||
static bool isKeyPressed(Key key);
|
||||
static bool isKeyPressedOnce(Key key);
|
||||
static bool isKeyPressedWithDelay(Key key, u16 delay);
|
||||
|
||||
|
||||
private:
|
||||
static const u8 *getState();
|
||||
|
||||
|
||||
static std::map<Key, bool> pressed;
|
||||
static std::map<Key, u32> lastTimePressed;
|
||||
static std::map<Key, u32> keysCode;
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Mouse.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 15:38:51
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -24,17 +24,17 @@ class Mouse {
|
||||
public:
|
||||
static void reset();
|
||||
static void update(s32 x, s32 y, s32 dx, s32 dy);
|
||||
|
||||
|
||||
static s32 getX() { return x; }
|
||||
static s32 getY() { return y; }
|
||||
|
||||
|
||||
static s32 getDX() { return dx; }
|
||||
static s32 getDY() { return dy; }
|
||||
|
||||
|
||||
private:
|
||||
static s32 x;
|
||||
static s32 y;
|
||||
|
||||
|
||||
static s32 dx;
|
||||
static s32 dy;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: SDLHeaders.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 01:18:31
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: SDLManager.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 01:24:24
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -22,7 +22,7 @@ class SDLManager {
|
||||
public:
|
||||
static void init();
|
||||
static void free();
|
||||
|
||||
|
||||
private:
|
||||
static bool sdlInitialized;
|
||||
static bool imgInitialized;
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Window.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 00:16:51
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -25,30 +25,30 @@ class Window {
|
||||
public:
|
||||
Window();
|
||||
~Window();
|
||||
|
||||
|
||||
void open(std::string caption, u16 width, u16 height);
|
||||
|
||||
|
||||
void clear();
|
||||
void display();
|
||||
|
||||
|
||||
void setMouseCursorGrabbed(bool grabbed);
|
||||
void setMouseCursorVisible(bool visible);
|
||||
|
||||
|
||||
void setVerticalSyncEnabled(bool enabled);
|
||||
|
||||
|
||||
u16 width() const { return m_width; }
|
||||
u16 height() const { return m_height; }
|
||||
|
||||
|
||||
void close() { m_isOpen = false; }
|
||||
bool isOpen() const { return m_isOpen; }
|
||||
|
||||
|
||||
private:
|
||||
SDL_Window *m_window;
|
||||
SDL_GLContext m_glContext;
|
||||
|
||||
|
||||
u16 m_width;
|
||||
u16 m_height;
|
||||
|
||||
|
||||
bool m_isOpen;
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Skybox.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 25/12/2014 23:43:07
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -25,9 +25,9 @@ class Skybox {
|
||||
public:
|
||||
Skybox();
|
||||
~Skybox();
|
||||
|
||||
|
||||
void draw(Shader &shader);
|
||||
|
||||
|
||||
private:
|
||||
VertexBuffer m_vbo;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Camera.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 16/12/2014 12:21:03
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -32,38 +32,38 @@ class Camera {
|
||||
public:
|
||||
Camera();
|
||||
~Camera();
|
||||
|
||||
|
||||
void turnH(float angle);
|
||||
void turnV(float angle);
|
||||
|
||||
|
||||
void move(float direction);
|
||||
|
||||
|
||||
glm::mat4 processInputs();
|
||||
glm::mat4 update();
|
||||
|
||||
|
||||
float pointTargetedX() const { return m_x + cos(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES); }
|
||||
float pointTargetedY() const { return m_y + sin(m_angleV * RADIANS_PER_DEGREES); }
|
||||
float pointTargetedZ() const { return m_z + sin(m_angleH * RADIANS_PER_DEGREES) * cos(m_angleV * RADIANS_PER_DEGREES) - 0.00001; }
|
||||
|
||||
|
||||
static Camera &getInstance() {
|
||||
static Camera instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
||||
float x() const { return m_x; }
|
||||
float y() const { return m_y; }
|
||||
float z() const { return m_z; }
|
||||
|
||||
|
||||
private:
|
||||
glm::mat4 m_viewMatrix;
|
||||
|
||||
|
||||
float m_x;
|
||||
float m_y;
|
||||
float m_z;
|
||||
|
||||
|
||||
float m_angleH;
|
||||
float m_angleV;
|
||||
|
||||
|
||||
float m_vx;
|
||||
float m_vz;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: OpenGL.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 04:18:34
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Shader.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 16:30:20
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -30,27 +30,27 @@ class Shader {
|
||||
Shader();
|
||||
Shader(const char *vertexFilename, const char *fragementFilename);
|
||||
~Shader();
|
||||
|
||||
|
||||
void loadFromFile(const char *vertexFilename, const char *fragementFilename);
|
||||
|
||||
|
||||
void createProgram();
|
||||
void linkProgram();
|
||||
|
||||
|
||||
void addShader(GLenum type, const char *filename);
|
||||
|
||||
|
||||
GLint attrib(std::string name);
|
||||
GLint uniform(std::string name);
|
||||
|
||||
|
||||
void enableVertexAttribArray(std::string name);
|
||||
void disableVertexAttribArray(std::string name);
|
||||
|
||||
|
||||
void setUniform(std::string name, int n);
|
||||
void setUniform(std::string name, const glm::mat4 &matrix);
|
||||
|
||||
|
||||
static void bind(const Shader *shader);
|
||||
|
||||
|
||||
GLint program() const { return m_program; }
|
||||
|
||||
|
||||
private:
|
||||
std::vector<GLuint> m_vertexShaders;
|
||||
std::vector<GLuint> m_fragmentShaders;
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Texture.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 01:15:27
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -28,20 +28,20 @@ class Texture {
|
||||
Texture();
|
||||
Texture(const std::string &filename);
|
||||
~Texture();
|
||||
|
||||
|
||||
void load(const std::string &filename);
|
||||
|
||||
|
||||
static void bind(const Texture *texture);
|
||||
|
||||
|
||||
u16 width() const { return m_width; }
|
||||
u16 height() const { return m_height; }
|
||||
|
||||
|
||||
private:
|
||||
std::string m_filename;
|
||||
|
||||
|
||||
u16 m_width;
|
||||
u16 m_height;
|
||||
|
||||
|
||||
GLuint m_texture;
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: VertexBuffer.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 17:09:58
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -24,12 +24,12 @@ class VertexBuffer {
|
||||
public:
|
||||
VertexBuffer();
|
||||
~VertexBuffer();
|
||||
|
||||
|
||||
void setData(GLsizeiptr size, const GLvoid *data, GLenum usage);
|
||||
void updateData(GLintptr offset, GLsizeiptr size, const GLvoid *data);
|
||||
|
||||
|
||||
static void bind(const VertexBuffer *vertexBuffer);
|
||||
|
||||
|
||||
private:
|
||||
GLuint m_id;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: ApplicationState.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:49:55
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -22,11 +22,11 @@ class ApplicationState {
|
||||
public:
|
||||
ApplicationState();
|
||||
virtual ~ApplicationState();
|
||||
|
||||
|
||||
virtual void handleEvents();
|
||||
|
||||
|
||||
virtual void update() = 0;
|
||||
|
||||
|
||||
virtual void draw() = 0;
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: GameState.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 03:51:32
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -29,19 +29,19 @@ class GameState : public ApplicationState {
|
||||
public:
|
||||
GameState();
|
||||
~GameState();
|
||||
|
||||
|
||||
void update();
|
||||
|
||||
|
||||
void draw();
|
||||
|
||||
|
||||
private:
|
||||
glm::mat4 m_projectionMatrix;
|
||||
glm::mat4 m_viewMatrix;
|
||||
|
||||
|
||||
Camera &m_camera;
|
||||
|
||||
|
||||
Shader m_shader;
|
||||
|
||||
|
||||
Skybox m_skybox;
|
||||
World m_world;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Debug.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 05:02:30
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -42,7 +42,7 @@ namespace Debug {
|
||||
Red = 31,
|
||||
Blue = 36
|
||||
};
|
||||
|
||||
|
||||
inline std::string textColor(u8 color = TextColor::White, bool bold = false) {
|
||||
#ifdef DEBUG_COLOR
|
||||
return std::string("\33[0;") + ((color < 10) ? "0" : "") + std::to_string(color) + ";0" + ((bold) ? "1" : "0") + "m";
|
||||
@ -50,23 +50,23 @@ namespace Debug {
|
||||
return std::string("");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
template<typename T>
|
||||
std::string makeString(std::stringstream &stream, T value) {
|
||||
stream << value;
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
|
||||
template<typename T, typename... Args>
|
||||
std::string makeString(std::stringstream &stream, T value, Args... args) {
|
||||
stream << value << " ";
|
||||
return makeString(stream, args...);
|
||||
}
|
||||
|
||||
|
||||
template<typename... Args>
|
||||
void print(Args... args) {
|
||||
std::stringstream stream;
|
||||
|
||||
|
||||
std::cout << textColor(0, true) << makeString(stream, args...) << textColor() << std::endl;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Exception.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 05:02:53
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -32,22 +32,22 @@ class Exception {
|
||||
Exception(u16 line, std::string filename, Args... args) throw() {
|
||||
m_line = line;
|
||||
m_filename = filename;
|
||||
|
||||
|
||||
std::stringstream stream;
|
||||
m_errorMsg = Debug::makeString(stream, args...);
|
||||
}
|
||||
|
||||
|
||||
~Exception() throw() {
|
||||
}
|
||||
|
||||
|
||||
virtual std::string what() const throw() {
|
||||
return Debug::textColor(Debug::TextColor::Red, true) + "at " + m_filename + ":" + std::to_string(m_line) + ": " + Debug::textColor(0, true) + m_errorMsg.c_str() + Debug::textColor();
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
u16 m_line;
|
||||
std::string m_filename;
|
||||
|
||||
|
||||
std::string m_errorMsg;
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: GameClock.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:42:17
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -26,20 +26,20 @@ class GameClock {
|
||||
public:
|
||||
GameClock();
|
||||
~GameClock();
|
||||
|
||||
|
||||
static u32 getTicks(bool realTime = false);
|
||||
|
||||
|
||||
void measureLastFrameDuration();
|
||||
|
||||
|
||||
void updateGame(std::function<void(void)> updateFunc);
|
||||
|
||||
|
||||
void drawGame(std::function<void(void)> drawFunc);
|
||||
|
||||
|
||||
u8 timestep() const { return m_timestep; }
|
||||
|
||||
|
||||
private:
|
||||
static u32 ticks;
|
||||
|
||||
|
||||
u32 m_lastFrameDate;
|
||||
u32 m_lag;
|
||||
u32 m_timeDropped;
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Types.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 05:05:33
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Block.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 29/12/2014 04:56:02
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -26,11 +26,11 @@ class Block {
|
||||
public:
|
||||
Block(u16 id);
|
||||
~Block();
|
||||
|
||||
|
||||
glm::vec4 getTexCoords();
|
||||
|
||||
|
||||
u16 id() const { return m_id; }
|
||||
|
||||
|
||||
private:
|
||||
u16 m_id;
|
||||
};
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Chunk.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 17:31:17
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -31,65 +31,65 @@ class Chunk {
|
||||
public:
|
||||
Chunk(s32 x, s32 y, s32 z, Texture &texture);
|
||||
~Chunk();
|
||||
|
||||
|
||||
void generate();
|
||||
void update();
|
||||
|
||||
|
||||
void draw(Shader &shader);
|
||||
|
||||
|
||||
Block *getBlock(s8 x, s8 y, s8 z);
|
||||
|
||||
|
||||
u32 getCoordID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate);
|
||||
|
||||
|
||||
u32 getVertexID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate);
|
||||
u32 getTexCoordID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate);
|
||||
|
||||
|
||||
bool vertexExists(u8 x, u8 y, u8 z, u8 i, u8 j);
|
||||
|
||||
|
||||
static float noise2d(float x, float y, int seed, int octaves, float persistence);
|
||||
static float noise3d_abs(float x, float y, float z, int seed, int octaves, float persistence);
|
||||
|
||||
|
||||
s32 x() const { return m_x; }
|
||||
s32 y() const { return m_y; }
|
||||
s32 z() const { return m_z; }
|
||||
|
||||
|
||||
bool initialized() const { return m_initialized; }
|
||||
void setInitialized(bool initialized) { m_initialized = initialized; }
|
||||
|
||||
|
||||
Chunk *left() const { return m_surroundingChunks[0]; }
|
||||
Chunk *right() const { return m_surroundingChunks[1]; }
|
||||
Chunk *front() const { return m_surroundingChunks[2]; }
|
||||
Chunk *back() const { return m_surroundingChunks[3]; }
|
||||
|
||||
|
||||
static const u8 width = 16;
|
||||
static const u8 height = 32;
|
||||
static const u8 depth = 16;
|
||||
|
||||
|
||||
void setLeft(Chunk *left) { m_surroundingChunks[0] = left; }
|
||||
void setRight(Chunk *right) { m_surroundingChunks[1] = right; }
|
||||
void setFront(Chunk *front) { m_surroundingChunks[2] = front; }
|
||||
void setBack(Chunk *back) { m_surroundingChunks[3] = back; }
|
||||
|
||||
|
||||
private:
|
||||
s32 m_x;
|
||||
s32 m_y;
|
||||
s32 m_z;
|
||||
|
||||
|
||||
Texture m_texture;
|
||||
|
||||
|
||||
std::vector<std::unique_ptr<Block>> m_data;
|
||||
|
||||
|
||||
std::vector<float> m_vertices;
|
||||
std::vector<float> m_normals;
|
||||
std::vector<float> m_texCoords;
|
||||
|
||||
|
||||
std::map<u32, u32> m_verticesID;
|
||||
std::map<u32, u32> m_extendedFaces;
|
||||
|
||||
|
||||
VertexBuffer m_vbo;
|
||||
|
||||
|
||||
Chunk *m_surroundingChunks[4];
|
||||
|
||||
|
||||
bool m_changed;
|
||||
bool m_initialized;
|
||||
bool m_generated;
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: World.hpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 16/12/2014 15:28:02
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -26,20 +26,20 @@ class World {
|
||||
public:
|
||||
World();
|
||||
~World();
|
||||
|
||||
|
||||
void draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::mat4 &viewMatrix);
|
||||
|
||||
|
||||
Chunk *getChunk(s32 x, s32 z);
|
||||
|
||||
|
||||
// Render distance in chunks
|
||||
static const u16 renderDistance = 8;
|
||||
|
||||
|
||||
private:
|
||||
s32 m_width;
|
||||
s32 m_depth;
|
||||
|
||||
|
||||
Texture m_texture;
|
||||
|
||||
|
||||
std::vector<std::unique_ptr<Chunk>> m_chunks;
|
||||
};
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Application.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 05:09:21
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -27,14 +27,14 @@
|
||||
|
||||
Application::Application() {
|
||||
srand(time(NULL));
|
||||
|
||||
|
||||
m_window.open(APP_NAME, SCREEN_WIDTH, SCREEN_HEIGHT);
|
||||
m_window.setMouseCursorGrabbed(true);
|
||||
m_window.setMouseCursorVisible(false);
|
||||
m_window.setVerticalSyncEnabled(true);
|
||||
|
||||
|
||||
initGL();
|
||||
|
||||
|
||||
//ResourceHandler::getInstance().loadResources();
|
||||
m_applicationStateStack = &ApplicationStateStack::getInstance();
|
||||
}
|
||||
@ -48,15 +48,15 @@ void Application::initGL() {
|
||||
throw EXCEPTION("glew init failed");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
glEnable(GL_BLEND);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
|
||||
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glEnable(GL_CULL_FACE);
|
||||
|
||||
|
||||
glHint(GL_GENERATE_MIPMAP_HINT, GL_NICEST);
|
||||
|
||||
|
||||
glClearColor(0.196078, 0.6, 0.8, 1.0); // Skyblue
|
||||
//glClearColor(0.0, 0.0, 0.0, 1.0); // Skyblue
|
||||
}
|
||||
@ -64,18 +64,18 @@ void Application::initGL() {
|
||||
void Application::run() {
|
||||
while(m_window.isOpen()) {
|
||||
m_clock.measureLastFrameDuration();
|
||||
|
||||
|
||||
m_applicationStateStack->top().handleEvents();
|
||||
|
||||
|
||||
m_clock.updateGame([&]{
|
||||
m_applicationStateStack->top().update();
|
||||
});
|
||||
|
||||
|
||||
m_clock.drawGame([&]{
|
||||
m_window.clear();
|
||||
|
||||
|
||||
m_applicationStateStack->top().draw();
|
||||
|
||||
|
||||
m_window.display();
|
||||
});
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: ApplicationStateStack.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:49:05
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Keyboard.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 00:52:10
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -26,12 +26,12 @@ std::map<Keyboard::Key, bool> Keyboard::pressed = {
|
||||
{Key::W, false},
|
||||
{Key::X, false},
|
||||
{Key::Z, false},
|
||||
|
||||
|
||||
{Key::Left, false},
|
||||
{Key::Right, false},
|
||||
{Key::Up, false},
|
||||
{Key::Down, false},
|
||||
|
||||
|
||||
{Key::BackSpace, false},
|
||||
{Key::LeftShift, false},
|
||||
{Key::RightShift, false},
|
||||
@ -46,12 +46,12 @@ std::map<Keyboard::Key, u32> Keyboard::lastTimePressed = {
|
||||
{Key::W, 0},
|
||||
{Key::X, 0},
|
||||
{Key::Z, 0},
|
||||
|
||||
|
||||
{Key::Left, 0},
|
||||
{Key::Right, 0},
|
||||
{Key::Up, 0},
|
||||
{Key::Down, 0},
|
||||
|
||||
|
||||
{Key::BackSpace, 0},
|
||||
{Key::LeftShift, 0},
|
||||
{Key::Space, 0},
|
||||
@ -66,12 +66,12 @@ std::map<Keyboard::Key, u32> Keyboard::keysCode = {
|
||||
{Key::W, SDLK_w},
|
||||
{Key::X, SDLK_x},
|
||||
{Key::Z, SDLK_z},
|
||||
|
||||
|
||||
{Key::Left, SDLK_LEFT},
|
||||
{Key::Right, SDLK_RIGHT},
|
||||
{Key::Up, SDLK_UP},
|
||||
{Key::Down, SDLK_DOWN},
|
||||
|
||||
|
||||
{Key::BackSpace, SDLK_BACKSPACE},
|
||||
{Key::LeftShift, SDLK_LSHIFT},
|
||||
{Key::Space, SDLK_SPACE},
|
||||
@ -113,7 +113,7 @@ bool Keyboard::isKeyPressedWithDelay(Key key, u16 delay) {
|
||||
if(!Keyboard::isKeyPressed(key)) {
|
||||
lastTimePressed[key] = 0;
|
||||
}
|
||||
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Mouse.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 15:39:17
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -26,7 +26,7 @@ s32 Mouse::dy = 0;
|
||||
void Mouse::reset() {
|
||||
x = 0;
|
||||
y = 0;
|
||||
|
||||
|
||||
dx = 0;
|
||||
dy = 0;
|
||||
}
|
||||
@ -34,7 +34,7 @@ void Mouse::reset() {
|
||||
void Mouse::update(s32 x, s32 y, s32 dx, s32 dy) {
|
||||
Mouse::x = x;
|
||||
Mouse::y = y;
|
||||
|
||||
|
||||
Mouse::dx = dx;
|
||||
Mouse::dy = dy;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: SDLManager.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 01:24:53
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -29,20 +29,20 @@ void SDLManager::init() {
|
||||
} else {
|
||||
sdlInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
int imgFlags = IMG_INIT_PNG;
|
||||
if(!IMG_Init(imgFlags) & imgFlags) {
|
||||
EXCEPTION("SDL image init error:", SDL_GetError());
|
||||
} else {
|
||||
imgInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
if(Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 1024) == -1) {
|
||||
EXCEPTION("SDL image init error:", SDL_GetError());
|
||||
} else {
|
||||
mixInitialized = true;
|
||||
}
|
||||
|
||||
|
||||
Mix_AllocateChannels(32);
|
||||
Mix_VolumeMusic(MIX_MAX_VOLUME / 3);
|
||||
Mix_Volume(-1, MIX_MAX_VOLUME);
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Window.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 00:17:10
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -30,21 +30,21 @@ Window::~Window() {
|
||||
void Window::open(std::string caption, u16 width, u16 height) {
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
|
||||
|
||||
|
||||
m_window = SDL_CreateWindow(caption.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
|
||||
if(!m_window) {
|
||||
throw EXCEPTION("Window initialization failed:", SDL_GetError());
|
||||
}
|
||||
|
||||
|
||||
m_glContext = SDL_GL_CreateContext(m_window);
|
||||
if(!m_glContext) {
|
||||
SDL_DestroyWindow(m_window);
|
||||
throw EXCEPTION("OpenGL context creation failed:", SDL_GetError());
|
||||
}
|
||||
|
||||
|
||||
m_width = width;
|
||||
m_height = height;
|
||||
|
||||
|
||||
m_isOpen = true;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Skybox.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 25/12/2014 23:43:18
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -31,34 +31,34 @@ Skybox::Skybox() {
|
||||
0, 0, 0,
|
||||
0, 1, 0,
|
||||
0, 1, 1,
|
||||
|
||||
|
||||
1, 1, 1,
|
||||
1, 1, 0,
|
||||
1, 0, 0,
|
||||
1, 0, 1,
|
||||
|
||||
|
||||
0, 0, 1,
|
||||
1, 0, 1,
|
||||
1, 0, 0,
|
||||
0, 0, 0,
|
||||
|
||||
|
||||
0, 1, 0,
|
||||
1, 1, 0,
|
||||
1, 1, 1,
|
||||
0, 1, 1,
|
||||
|
||||
|
||||
0, 1, 1,
|
||||
1, 1, 1,
|
||||
1, 0, 1,
|
||||
0, 0, 1,
|
||||
|
||||
|
||||
0, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 1, 0,
|
||||
0, 1, 0,
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
float colors[3 * 4 * 6] = {
|
||||
//0.196078, 0.6, 0.8,
|
||||
//0.196078, 0.6, 0.8,
|
||||
@ -69,17 +69,17 @@ Skybox::Skybox() {
|
||||
0.0, 1.0, 1.0,
|
||||
0.0, 0.0, 1.0
|
||||
};
|
||||
|
||||
|
||||
for(int i = 1 ; i < 6 ; i++) {
|
||||
memcpy(&colors[3 * 4 * i], &colors[0], 3 * 4 * sizeof(float));
|
||||
memcpy(&colors[3 * 4 * i], &colors[0], 3 * 4 * sizeof(float));
|
||||
}
|
||||
|
||||
|
||||
VertexBuffer::bind(&m_vbo);
|
||||
|
||||
|
||||
m_vbo.setData(36 * 16 * 9 * sizeof(float), nullptr, GL_STATIC_DRAW);
|
||||
m_vbo.updateData(0, 6 * 4 * 3 * sizeof(float), cubeCoords);
|
||||
m_vbo.updateData(6 * 4 * 3 * sizeof(float), 6 * 4 * 3 * sizeof(float), colors);
|
||||
|
||||
|
||||
VertexBuffer::bind(nullptr);
|
||||
}
|
||||
|
||||
@ -92,22 +92,22 @@ void Skybox::draw(Shader &shader) {
|
||||
Camera::getInstance().y() - World::renderDistance * Chunk::width / 2,
|
||||
Camera::getInstance().z() - World::renderDistance * Chunk::width / 2));
|
||||
modelMatrix *= glm::scale(glm::mat4(1.0), glm::vec3(World::renderDistance * Chunk::width));
|
||||
|
||||
|
||||
shader.setUniform("u_modelMatrix", modelMatrix);
|
||||
|
||||
|
||||
VertexBuffer::bind(&m_vbo);
|
||||
|
||||
|
||||
shader.enableVertexAttribArray("coord3d");
|
||||
shader.enableVertexAttribArray("color");
|
||||
|
||||
|
||||
glVertexAttribPointer(shader.attrib("coord3d"), 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glVertexAttribPointer(shader.attrib("color"), 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(3 * 4 * 6 * sizeof(float)));
|
||||
|
||||
|
||||
glDrawArrays(GL_QUADS, 0, 4 * 6);
|
||||
|
||||
|
||||
shader.disableVertexAttribArray("color");
|
||||
shader.disableVertexAttribArray("coord3d");
|
||||
|
||||
|
||||
VertexBuffer::bind(nullptr);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Camera.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 16/12/2014 12:21:19
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -26,13 +26,13 @@ Camera::Camera() {
|
||||
m_x = 0;
|
||||
m_y = 16;
|
||||
m_z = -64;
|
||||
|
||||
|
||||
m_angleH = 90.0;
|
||||
m_angleV = 0.0;
|
||||
|
||||
|
||||
m_vx = 0;
|
||||
m_vz = 0;
|
||||
|
||||
|
||||
m_viewMatrix = update();
|
||||
}
|
||||
|
||||
@ -41,7 +41,7 @@ Camera::~Camera() {
|
||||
|
||||
void Camera::turnH(float angle) {
|
||||
m_angleH += angle;
|
||||
|
||||
|
||||
while(m_angleH >= 180.0) {
|
||||
m_angleH -= 360.0;
|
||||
}
|
||||
@ -52,7 +52,7 @@ void Camera::turnH(float angle) {
|
||||
|
||||
void Camera::turnV(float angle) {
|
||||
m_angleV += angle;
|
||||
|
||||
|
||||
if(89.9 < m_angleV) {
|
||||
m_angleV = 89.9;
|
||||
}
|
||||
@ -63,13 +63,13 @@ void Camera::turnV(float angle) {
|
||||
|
||||
void Camera::move(float direction) {
|
||||
direction += m_angleH;
|
||||
|
||||
|
||||
m_vx = 0.04f * cos(direction * M_PI / 180.0);
|
||||
m_vz = 0.04f * sin(direction * M_PI / 180.0);
|
||||
|
||||
|
||||
m_x += m_vx;
|
||||
m_z += m_vz;
|
||||
|
||||
|
||||
m_vx = 0;
|
||||
m_vz = 0;
|
||||
}
|
||||
@ -80,7 +80,7 @@ glm::mat4 Camera::processInputs() {
|
||||
turnV(-Mouse::getDY() * 0.02);
|
||||
m_viewMatrix = update();
|
||||
}
|
||||
|
||||
|
||||
if(Keyboard::isKeyPressed(Keyboard::Space)) {
|
||||
m_y += 0.1;
|
||||
m_viewMatrix = update();
|
||||
@ -89,7 +89,7 @@ glm::mat4 Camera::processInputs() {
|
||||
m_y -= 0.1;
|
||||
m_viewMatrix = update();
|
||||
}
|
||||
|
||||
|
||||
if(Keyboard::isKeyPressed(Keyboard::Z)) {
|
||||
move(0.0f);
|
||||
m_viewMatrix = update();
|
||||
@ -98,7 +98,7 @@ glm::mat4 Camera::processInputs() {
|
||||
move(180.0f);
|
||||
m_viewMatrix = update();
|
||||
}
|
||||
|
||||
|
||||
if(Keyboard::isKeyPressed(Keyboard::Q)) {
|
||||
move(-90.0f);
|
||||
m_viewMatrix = update();
|
||||
@ -107,7 +107,7 @@ glm::mat4 Camera::processInputs() {
|
||||
move(90.0f);
|
||||
m_viewMatrix = update();
|
||||
}
|
||||
|
||||
|
||||
return m_viewMatrix;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Shader.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 16:30:34
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -36,21 +36,21 @@ Shader::~Shader() {
|
||||
glDeleteShader(m_vertexShaders.back());
|
||||
m_vertexShaders.pop_back();
|
||||
}
|
||||
|
||||
|
||||
while(m_fragmentShaders.size() != 0) {
|
||||
glDeleteShader(m_fragmentShaders.back());
|
||||
m_fragmentShaders.pop_back();
|
||||
}
|
||||
|
||||
|
||||
glDeleteProgram(m_program);
|
||||
}
|
||||
|
||||
void Shader::loadFromFile(const char *vertexFilename, const char *fragmentFilename) {
|
||||
createProgram();
|
||||
|
||||
|
||||
addShader(GL_VERTEX_SHADER, vertexFilename);
|
||||
addShader(GL_FRAGMENT_SHADER, fragmentFilename);
|
||||
|
||||
|
||||
linkProgram();
|
||||
}
|
||||
|
||||
@ -60,94 +60,94 @@ void Shader::createProgram() {
|
||||
|
||||
void Shader::linkProgram() {
|
||||
glLinkProgram(m_program);
|
||||
|
||||
|
||||
GLint linkOK = GL_FALSE;
|
||||
glGetProgramiv(m_program, GL_LINK_STATUS, &linkOK);
|
||||
if(!linkOK){
|
||||
GLint errorSize = 0;
|
||||
glGetProgramiv(m_program, GL_INFO_LOG_LENGTH, &errorSize);
|
||||
|
||||
|
||||
char *errorMsg = new char[errorSize + 1];
|
||||
|
||||
|
||||
glGetProgramInfoLog(m_program, errorSize, &errorSize, errorMsg);
|
||||
errorMsg[errorSize] = '\0';
|
||||
|
||||
|
||||
std::string error = std::string(errorMsg);
|
||||
|
||||
|
||||
delete[] errorMsg;
|
||||
glDeleteProgram(m_program);
|
||||
|
||||
|
||||
throw EXCEPTION("Program", m_program, "link failed:", error);
|
||||
}
|
||||
}
|
||||
|
||||
void Shader::addShader(GLenum type, const char *filename) {
|
||||
GLuint shader = glCreateShader(type);
|
||||
|
||||
|
||||
if(type == GL_VERTEX_SHADER) {
|
||||
m_vertexShaders.push_back(shader);
|
||||
} else {
|
||||
m_fragmentShaders.push_back(shader);
|
||||
}
|
||||
|
||||
|
||||
std::ifstream file(filename);
|
||||
if(!file) {
|
||||
glDeleteShader(shader);
|
||||
|
||||
|
||||
throw EXCEPTION("Failed to open", filename);
|
||||
}
|
||||
|
||||
|
||||
std::string line;
|
||||
std::string sourceCode;
|
||||
|
||||
|
||||
while(getline(file, line)) sourceCode += line + '\n';
|
||||
file.close();
|
||||
|
||||
|
||||
const GLchar *sourceCodeString = sourceCode.c_str();
|
||||
|
||||
|
||||
glShaderSource(shader, 1, &sourceCodeString, NULL);
|
||||
|
||||
|
||||
glCompileShader(shader);
|
||||
|
||||
|
||||
GLint compileOK = GL_FALSE;
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileOK);
|
||||
if(!compileOK) {
|
||||
GLint errorSize = 0;
|
||||
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &errorSize);
|
||||
|
||||
|
||||
char *errorMsg = new char[errorSize + 1];
|
||||
|
||||
|
||||
glGetShaderInfoLog(shader, errorSize, &errorSize, errorMsg);
|
||||
errorMsg[errorSize] = '\0';
|
||||
|
||||
|
||||
std::string error = std::string(errorMsg);
|
||||
|
||||
|
||||
delete[] errorMsg;
|
||||
glDeleteShader(shader);
|
||||
|
||||
|
||||
throw EXCEPTION("Shader", filename, "compilation failed:", error);
|
||||
}
|
||||
|
||||
|
||||
glAttachShader(m_program, shader);
|
||||
}
|
||||
|
||||
GLint Shader::attrib(std::string name) {
|
||||
GLint attrib = glGetAttribLocation(m_program, name.c_str());
|
||||
|
||||
|
||||
if(attrib == -1) {
|
||||
DEBUG("Could not bind attribute:", name);
|
||||
}
|
||||
|
||||
|
||||
return attrib;
|
||||
}
|
||||
|
||||
GLint Shader::uniform(std::string name) {
|
||||
GLint uniform = glGetUniformLocation(m_program, name.c_str());
|
||||
|
||||
|
||||
if(uniform == -1) {
|
||||
DEBUG("Could not bind uniform:", name);
|
||||
}
|
||||
|
||||
|
||||
return uniform;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Texture.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 20/12/2014 01:15:43
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -34,27 +34,27 @@ void Texture::load(const std::string &filename) {
|
||||
if(!surface) {
|
||||
throw EXCEPTION("Failed to load texture:", filename);
|
||||
}
|
||||
|
||||
|
||||
m_filename = filename;
|
||||
|
||||
|
||||
m_width = surface->w;
|
||||
m_height = surface->h;
|
||||
|
||||
|
||||
glGenTextures(1, &m_texture);
|
||||
|
||||
|
||||
bind(this);
|
||||
|
||||
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
//glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR);
|
||||
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
|
||||
|
||||
GLenum format = (surface->format->BytesPerPixel == 4) ? GL_RGBA : GL_RGB;
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, format, m_width, m_height, 0, format, GL_UNSIGNED_BYTE, surface->pixels);
|
||||
|
||||
|
||||
//glGenerateMipmap(GL_TEXTURE_2D);
|
||||
|
||||
|
||||
bind(nullptr);
|
||||
|
||||
|
||||
SDL_FreeSurface(surface);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: VertexBuffer.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 17:10:16
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: main.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 13/12/2014 20:49:00
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -25,9 +25,9 @@
|
||||
int main(int argc, char *argv[]) {
|
||||
try {
|
||||
SDLManager::init();
|
||||
|
||||
|
||||
Application &app = Application::getInstance();
|
||||
|
||||
|
||||
app.run();
|
||||
}
|
||||
catch(const Exception &err) {
|
||||
@ -42,9 +42,9 @@ int main(int argc, char *argv[]) {
|
||||
std::cerr << "Fatal error: Unknown error." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
SDLManager::free();
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: ApplicationState.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:50:04
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -21,7 +21,7 @@
|
||||
#include "Mouse.hpp"
|
||||
|
||||
ApplicationState::ApplicationState() {
|
||||
|
||||
|
||||
}
|
||||
|
||||
ApplicationState::~ApplicationState() {
|
||||
@ -29,7 +29,7 @@ ApplicationState::~ApplicationState() {
|
||||
|
||||
void ApplicationState::handleEvents() {
|
||||
Mouse::reset();
|
||||
|
||||
|
||||
SDL_Event event;
|
||||
while(SDL_PollEvent(&event) != 0) {
|
||||
switch(event.type) {
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: GameState.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 03:51:55
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -22,24 +22,24 @@
|
||||
|
||||
GameState::GameState() : m_camera(Camera::getInstance()) {
|
||||
m_shader.createProgram();
|
||||
|
||||
|
||||
m_shader.addShader(GL_VERTEX_SHADER, "shaders/game.v.glsl");
|
||||
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/color.f.glsl");
|
||||
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/light.f.glsl");
|
||||
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/fog.f.glsl");
|
||||
m_shader.addShader(GL_FRAGMENT_SHADER, "shaders/game.f.glsl");
|
||||
|
||||
|
||||
m_shader.linkProgram();
|
||||
|
||||
|
||||
Shader::bind(&m_shader);
|
||||
|
||||
|
||||
m_projectionMatrix = glm::perspective(45.0f, 640.0f / 480.0f, 0.1f, 1000.0f);
|
||||
m_viewMatrix = m_camera.update();
|
||||
|
||||
|
||||
m_shader.setUniform("u_projectionMatrix", m_projectionMatrix);
|
||||
|
||||
|
||||
m_shader.setUniform("u_tex", 0);
|
||||
|
||||
|
||||
Shader::bind(nullptr);
|
||||
}
|
||||
|
||||
@ -52,13 +52,13 @@ void GameState::update() {
|
||||
|
||||
void GameState::draw() {
|
||||
Shader::bind(&m_shader);
|
||||
|
||||
|
||||
m_shader.setUniform("u_viewMatrix", m_viewMatrix);
|
||||
|
||||
|
||||
//m_skybox.draw(m_shader);
|
||||
|
||||
|
||||
m_world.draw(m_shader, m_projectionMatrix, m_viewMatrix);
|
||||
|
||||
|
||||
Shader::bind(nullptr);
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: GameClock.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 14/12/2014 13:42:26
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -46,7 +46,7 @@ void GameClock::measureLastFrameDuration() {
|
||||
m_lastFrameDuration = m_now - m_lastFrameDate;
|
||||
m_lastFrameDate = m_now;
|
||||
m_lag += m_lastFrameDuration;
|
||||
|
||||
|
||||
if(m_lag >= 200) {
|
||||
m_timeDropped += m_lag - m_timestep;
|
||||
m_lag = m_timestep;
|
||||
@ -56,12 +56,12 @@ void GameClock::measureLastFrameDuration() {
|
||||
|
||||
void GameClock::updateGame(std::function<void(void)> updateFunc) {
|
||||
m_numUpdates = 0;
|
||||
|
||||
|
||||
while(m_lag >= m_timestep && m_numUpdates < 10 && Application::getInstance().window().isOpen()) {
|
||||
ticks += m_timestep;
|
||||
|
||||
|
||||
updateFunc();
|
||||
|
||||
|
||||
m_lag -= m_timestep;
|
||||
m_numUpdates++;
|
||||
}
|
||||
@ -71,13 +71,13 @@ void GameClock::drawGame(std::function<void(void)> drawFunc) {
|
||||
if(m_numUpdates > 0) {
|
||||
drawFunc();
|
||||
}
|
||||
|
||||
|
||||
m_lastFrameDuration = getTicks(true) - m_timeDropped - m_lastFrameDate;
|
||||
|
||||
|
||||
if(m_lastFrameDuration < m_timestep) {
|
||||
SDL_Delay(m_timestep - m_lastFrameDuration);
|
||||
}
|
||||
|
||||
|
||||
m_now = 0;
|
||||
m_lastFrameDuration = 0;
|
||||
}
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Block.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 29/12/2014 04:56:17
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -27,10 +27,10 @@ Block::~Block() {
|
||||
glm::vec4 Block::getTexCoords() {
|
||||
u16 textureWidth = 256;
|
||||
u16 textureHeight = 256;
|
||||
|
||||
|
||||
float textureX = m_id % (textureWidth / 16) * 16.0f / textureWidth;
|
||||
float textureY = m_id / (textureWidth / 16) * 16.0f / textureHeight;
|
||||
|
||||
|
||||
return glm::vec4(textureX,
|
||||
textureY,
|
||||
textureX + 16.0f / textureWidth,
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: Chunk.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 15/12/2014 17:31:32
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -27,11 +27,11 @@ Chunk::Chunk(s32 x, s32 y, s32 z, Texture &texture) : m_texture(texture) {
|
||||
m_x = x;
|
||||
m_y = y;
|
||||
m_z = z;
|
||||
|
||||
|
||||
m_changed = false;
|
||||
m_initialized = false;
|
||||
m_generated = false;
|
||||
|
||||
|
||||
m_surroundingChunks[0] = nullptr;
|
||||
m_surroundingChunks[1] = nullptr;
|
||||
m_surroundingChunks[2] = nullptr;
|
||||
@ -44,24 +44,24 @@ Chunk::~Chunk() {
|
||||
void Chunk::generate() {
|
||||
if(m_generated) return;
|
||||
else m_generated = true;
|
||||
|
||||
|
||||
time_t seed = time(NULL);
|
||||
|
||||
|
||||
for(u8 z = 0 ; z < depth ; z++) {
|
||||
for(u8 x = 0 ; x < width ; x++) {
|
||||
float n = noise2d((x + m_x * width) / 256.0, (z + m_z * depth) / 256.0, seed, 5, 0.5) * 4;
|
||||
float h = 10 + n * 2;
|
||||
|
||||
|
||||
for(u8 y = 0 ; y < height ; y++) {
|
||||
if(y + m_y * height < h) {
|
||||
m_data.push_back(std::unique_ptr<Block>(new Block(1)));
|
||||
} else {
|
||||
m_data.push_back(std::unique_ptr<Block>(new Block(0)));
|
||||
}
|
||||
|
||||
|
||||
// Random value used to determine land type
|
||||
/*float r = noise3d_abs((x + m_x * 32) / 16.0, (y + m_y * 32) / 16.0, (z + m_z * CZ) / 16.0, -seed, 2, 1);
|
||||
|
||||
|
||||
// Sand layer
|
||||
if(n + r * 5 < 4) {
|
||||
//m_blocks[x][y][z] = 7;
|
||||
@ -84,7 +84,7 @@ void Chunk::generate() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
m_changed = true;
|
||||
}
|
||||
|
||||
@ -93,64 +93,64 @@ void Chunk::generate() {
|
||||
|
||||
void Chunk::update() {
|
||||
m_changed = false;
|
||||
|
||||
|
||||
static const float cubeCoords[6 * 4 * 3] = {
|
||||
0, 1, 1,
|
||||
0, 1, 0,
|
||||
0, 0, 0,
|
||||
0, 0, 1,
|
||||
|
||||
|
||||
1, 0, 1,
|
||||
1, 0, 0,
|
||||
1, 1, 0,
|
||||
1, 1, 1,
|
||||
|
||||
|
||||
0, 0, 0,
|
||||
1, 0, 0,
|
||||
1, 0, 1,
|
||||
0, 0, 1,
|
||||
|
||||
|
||||
0, 1, 1,
|
||||
1, 1, 1,
|
||||
1, 1, 0,
|
||||
0, 1, 0,
|
||||
|
||||
|
||||
0, 0, 1,
|
||||
1, 0, 1,
|
||||
1, 1, 1,
|
||||
0, 1, 1,
|
||||
|
||||
|
||||
0, 1, 0,
|
||||
1, 1, 0,
|
||||
1, 0, 0,
|
||||
0, 0, 0,
|
||||
};
|
||||
|
||||
|
||||
m_vertices.clear();
|
||||
m_normals.clear();
|
||||
m_texCoords.clear();
|
||||
|
||||
|
||||
m_vertices.reserve(width * height * depth * 6 * 4 * 3);
|
||||
m_normals.reserve(width * height * depth * 6 * 4 * 3);
|
||||
m_texCoords.reserve(width * height * depth * 6 * 4 * 2);
|
||||
|
||||
|
||||
m_verticesID.clear();
|
||||
m_extendedFaces.clear();
|
||||
|
||||
|
||||
// Needed in the loop (avoid a lot of glm::vec3 creation)
|
||||
glm::vec3 a, b, c, v1, v2, normal;
|
||||
|
||||
|
||||
for(u8 z = 0 ; z < depth ; z++) {
|
||||
for(u8 y = 0 ; y < height ; y++) {
|
||||
for(u8 x = 0 ; x < width ; x++) {
|
||||
Block *block = getBlock(x, y, z);
|
||||
|
||||
|
||||
if(!block->id()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
const glm::vec4 &blockTexCoords = block->getTexCoords();
|
||||
|
||||
|
||||
float texCoords[2 * 4 * 6] = {
|
||||
blockTexCoords.x, blockTexCoords.w,
|
||||
blockTexCoords.z, blockTexCoords.w,
|
||||
@ -161,11 +161,11 @@ void Chunk::update() {
|
||||
// 1.0f, 0.0f,
|
||||
// 0.0f, 0.0f
|
||||
};
|
||||
|
||||
|
||||
for(int i = 1 ; i < 6 ; i++) {
|
||||
memcpy(&texCoords[2 * 4 * i], &texCoords[0], 2 * 4 * sizeof(float));
|
||||
memcpy(&texCoords[2 * 4 * i], &texCoords[0], 2 * 4 * sizeof(float));
|
||||
}
|
||||
|
||||
|
||||
for(u8 i = 0 ; i < 6 ; i++) {
|
||||
// Skip hidden faces
|
||||
if((x > 0 && getBlock(x - 1, y, z)->id() && i == 0)
|
||||
@ -181,76 +181,76 @@ void Chunk::update() {
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Merge adjacent faces
|
||||
/*if(x > 0 && getBlock(x - 1, y, z)->id() && (i != 0 || i != 1) && vertexExists(x - 1, y, z, i, 0)) {
|
||||
m_vertices[getVertexID(x - 1, y, z, i, 1, 0)] += 1;
|
||||
m_vertices[getVertexID(x - 1, y, z, i, 2, 0)] += 1;
|
||||
|
||||
|
||||
m_texCoords[getTexCoordID(x - 1, y, z, i, 1, 0)] += 16 / 256 * 2;//1;
|
||||
m_texCoords[getTexCoordID(x - 1, y, z, i, 2, 0)] += 16 / 256 * 2;//1;
|
||||
|
||||
|
||||
m_extendedFaces[getCoordID(x, y, z, i, 0, 0)] = getCoordID(x - 1, y, z, i, 0, 0);
|
||||
|
||||
|
||||
continue;
|
||||
}
|
||||
if(z > 0 && getBlock(x, y, z - 1)->id() && (i == 0 || i == 1) && vertexExists(x, y, z - 1, i, 0)) {
|
||||
m_vertices[getVertexID(x, y, z - 1, i, 0, 2)] += 1;
|
||||
m_vertices[getVertexID(x, y, z - 1, i, 3, 2)] += 1;
|
||||
|
||||
|
||||
m_texCoords[getTexCoordID(x, y, z - 1, i, 1, 0)] += 16 / 256 * 2;//1;
|
||||
m_texCoords[getTexCoordID(x, y, z - 1, i, 2, 0)] += 16 / 256 * 2;//1;
|
||||
|
||||
|
||||
m_extendedFaces[getCoordID(x, y, z, i, 0, 0)] = getCoordID(x, y, z - 1, i, 0, 0);
|
||||
|
||||
|
||||
continue;
|
||||
}*/
|
||||
|
||||
|
||||
/*if(y > 0 && getBlock(x, y - 1, z)->id() && (i == 2 || i == 3) && vertexExists(x, y - 1, z, i, 0)) {
|
||||
m_vertices[getVertexID(x, y - 1, z, i, 0, 1)] += 1;
|
||||
m_vertices[getVertexID(x, y - 1, z, i, 3, 1)] += 1;
|
||||
|
||||
|
||||
m_texCoords[getTexCoordID(x, y - 1, z, i, 1, 1)] += 1;
|
||||
m_texCoords[getTexCoordID(x, y - 1, z, i, 2, 1)] += 1;
|
||||
|
||||
|
||||
m_extendedFaces[getCoordID(x, y, z, i, 0, 0)] = getCoordID(x, y - 1, z, i, 0, 0);
|
||||
|
||||
|
||||
continue;
|
||||
}*/
|
||||
|
||||
|
||||
// Three points of the face
|
||||
a.x = cubeCoords[i * 12 + 0];
|
||||
a.y = cubeCoords[i * 12 + 1];
|
||||
a.z = cubeCoords[i * 12 + 2];
|
||||
|
||||
|
||||
b.x = cubeCoords[i * 12 + 3];
|
||||
b.y = cubeCoords[i * 12 + 4];
|
||||
b.z = cubeCoords[i * 12 + 5];
|
||||
|
||||
|
||||
c.x = cubeCoords[i * 12 + 6];
|
||||
c.y = cubeCoords[i * 12 + 7];
|
||||
c.z = cubeCoords[i * 12 + 8];
|
||||
|
||||
|
||||
// Computing two vectors
|
||||
v1 = b - a;
|
||||
v2 = c - a;
|
||||
|
||||
|
||||
// Computing face normal (already normalized because cubeCoords are normalized)
|
||||
normal = glm::cross(v1, v2);
|
||||
|
||||
|
||||
// Store vertex information
|
||||
for(u8 j = 0 ; j < 4 ; j++) {
|
||||
m_vertices.push_back(x + cubeCoords[i * 12 + j * 3]);
|
||||
m_vertices.push_back(y + cubeCoords[i * 12 + j * 3 + 1]);
|
||||
m_vertices.push_back(z + cubeCoords[i * 12 + j * 3 + 2]);
|
||||
|
||||
|
||||
m_normals.push_back(normal.x);
|
||||
m_normals.push_back(normal.y);
|
||||
m_normals.push_back(normal.z);
|
||||
|
||||
|
||||
m_texCoords.push_back(texCoords[i * 8 + j * 2]);
|
||||
m_texCoords.push_back(texCoords[i * 8 + j * 2 + 1]);
|
||||
|
||||
|
||||
m_verticesID[getCoordID(x, y, z, i, j, 0)] = m_vertices.size() - 3;
|
||||
m_verticesID[getCoordID(x, y, z, i, j, 1)] = m_vertices.size() - 2;
|
||||
m_verticesID[getCoordID(x, y, z, i, j, 2)] = m_vertices.size() - 1;
|
||||
@ -259,50 +259,50 @@ void Chunk::update() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
m_vertices.shrink_to_fit();
|
||||
m_normals.shrink_to_fit();
|
||||
m_texCoords.shrink_to_fit();
|
||||
|
||||
|
||||
VertexBuffer::bind(&m_vbo);
|
||||
|
||||
|
||||
m_vbo.setData((m_vertices.size() + m_normals.size() + m_texCoords.size()) * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
|
||||
m_vbo.updateData(0, m_vertices.size() * sizeof(float), m_vertices.data());
|
||||
m_vbo.updateData(m_vertices.size() * sizeof(float), m_normals.size() * sizeof(float), m_normals.data());
|
||||
m_vbo.updateData((m_vertices.size() + m_normals.size()) * sizeof(float), m_texCoords.size() * sizeof(float), m_texCoords.data());
|
||||
|
||||
|
||||
VertexBuffer::bind(nullptr);
|
||||
}
|
||||
|
||||
void Chunk::draw(Shader &shader) {
|
||||
if(m_changed) update();
|
||||
|
||||
|
||||
if(m_vertices.size() == 0) return;
|
||||
|
||||
|
||||
VertexBuffer::bind(&m_vbo);
|
||||
|
||||
|
||||
shader.enableVertexAttribArray("coord3d");
|
||||
shader.enableVertexAttribArray("normal");
|
||||
shader.enableVertexAttribArray("texCoord");
|
||||
|
||||
|
||||
glVertexAttribPointer(shader.attrib("coord3d"), 3, GL_FLOAT, GL_FALSE, 0, 0);
|
||||
glVertexAttribPointer(shader.attrib("normal"), 3, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(m_vertices.size() * sizeof(float)));
|
||||
glVertexAttribPointer(shader.attrib("texCoord"), 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)((m_vertices.size() + m_normals.size()) * sizeof(float)));
|
||||
|
||||
|
||||
Texture::bind(&m_texture);
|
||||
|
||||
|
||||
glDrawArrays(GL_QUADS, 0, m_vertices.size() / 3);
|
||||
|
||||
|
||||
Texture::bind(nullptr);
|
||||
|
||||
|
||||
/*for(u32 i = 0 ; i < m_vertices.size() / 3 ; i += 4) {
|
||||
glDrawArrays(GL_LINE_LOOP, i, 4);
|
||||
}*/
|
||||
|
||||
|
||||
shader.disableVertexAttribArray("texCoord");
|
||||
shader.disableVertexAttribArray("normal");
|
||||
shader.disableVertexAttribArray("coord3d");
|
||||
|
||||
|
||||
VertexBuffer::bind(nullptr);
|
||||
}
|
||||
|
||||
@ -321,11 +321,11 @@ u32 Chunk::getCoordID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate) {
|
||||
|
||||
u32 Chunk::getVertexID(u8 x, u8 y, u8 z, u8 i, u8 j, u8 coordinate) {
|
||||
u32 id = getCoordID(x, y, z, i, j, coordinate);
|
||||
|
||||
|
||||
while(m_extendedFaces.count(id - j * 3 - coordinate)) {
|
||||
id = m_extendedFaces[id - j * 3 - coordinate] + j * 3 + coordinate;
|
||||
}
|
||||
|
||||
|
||||
return m_verticesID[id];
|
||||
}
|
||||
|
||||
@ -342,13 +342,13 @@ float Chunk::noise2d(float x, float y, int seed, int octaves, float persistence)
|
||||
float sum = 0;
|
||||
float strength = 1.0;
|
||||
float scale = 1.0;
|
||||
|
||||
|
||||
for(int i = 0 ; i < octaves ; i++) {
|
||||
sum += strength * glm::simplex(glm::vec2(x, y) * scale);
|
||||
scale *= 2.0;
|
||||
strength *= persistence;
|
||||
}
|
||||
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
@ -356,13 +356,13 @@ float Chunk::noise3d_abs(float x, float y, float z, int seed, int octaves, float
|
||||
float sum = 0;
|
||||
float strength = 1.0;
|
||||
float scale = 1.0;
|
||||
|
||||
|
||||
for(int i = 0 ; i < octaves ; i++) {
|
||||
sum += strength * fabs(glm::simplex(glm::vec3(x, y, z) * scale));
|
||||
scale *= 2.0;
|
||||
strength *= persistence;
|
||||
}
|
||||
|
||||
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
@ -3,7 +3,7 @@
|
||||
*
|
||||
* Filename: World.cpp
|
||||
*
|
||||
* Description:
|
||||
* Description:
|
||||
*
|
||||
* Version: 1.0
|
||||
* Created: 16/12/2014 15:28:19
|
||||
@ -11,7 +11,7 @@
|
||||
* Compiler: gcc
|
||||
*
|
||||
* Author: Quentin BAZIN, <quent42340@gmail.com>
|
||||
* Company:
|
||||
* Company:
|
||||
*
|
||||
* =====================================================================================
|
||||
*/
|
||||
@ -23,16 +23,16 @@
|
||||
World::World() {
|
||||
m_width = 100;
|
||||
m_depth = 100;
|
||||
|
||||
|
||||
//m_texture.load("textures/cobblestone.bmp");
|
||||
m_texture.load("textures/blocks.png");
|
||||
|
||||
|
||||
for(s32 z = -m_depth / 2 ; z < m_depth / 2 ; z++) {
|
||||
for(s32 x = -m_width / 2 ; x < m_width / 2 ; x++) {
|
||||
m_chunks.push_back(std::unique_ptr<Chunk>(new Chunk(x, 0, z, m_texture)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for(s32 z = -m_depth / 2 ; z < m_depth / 2 ; z++) {
|
||||
for(s32 x = -m_width / 2 ; x < m_width / 2 ; x++) {
|
||||
if(x > -m_width / 2) getChunk(x, z)->setLeft(getChunk(x - 1, z));
|
||||
@ -50,61 +50,61 @@ void World::draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::m
|
||||
float ud = 1000.0;
|
||||
s32 ux = 0;
|
||||
s32 uz = 0;
|
||||
|
||||
|
||||
shader.setUniform("u_renderDistance", renderDistance * Chunk::width);
|
||||
|
||||
|
||||
for(auto &it : m_chunks) {
|
||||
glm::mat4 modelMatrix = glm::translate(glm::mat4(1.0f),
|
||||
glm::vec3(it->x() * Chunk::width,
|
||||
it->y() * Chunk::height,
|
||||
it->z() * Chunk::depth));
|
||||
|
||||
|
||||
// Is the chunk closer enough?
|
||||
glm::vec4 center = viewMatrix * modelMatrix * glm::vec4(Chunk::width / 2,
|
||||
Chunk::height / 2,
|
||||
Chunk::depth / 2, 1);
|
||||
|
||||
|
||||
if(glm::length(center) > (renderDistance + 1) * Chunk::width) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// Is this chunk on the screen?
|
||||
center = projectionMatrix * center;
|
||||
|
||||
|
||||
float d = glm::length(center);
|
||||
center.x /= center.w;
|
||||
center.y /= center.w;
|
||||
|
||||
|
||||
// If it is behind the camera, don't bother drawing it
|
||||
if(center.z < -Chunk::height / 2) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
// It it is outside the screen, don't bother drawing it
|
||||
if(fabsf(center.x) > 1 + fabsf(Chunk::height * 2 / center.w)
|
||||
|| fabsf(center.y) > 1 + fabsf(Chunk::height * 2 / center.w)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
if(!it->initialized()) {
|
||||
if(d < ud) {
|
||||
ud = d;
|
||||
ux = it->x();
|
||||
uz = it->z();
|
||||
}
|
||||
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
|
||||
shader.setUniform("u_modelMatrix", modelMatrix);
|
||||
|
||||
|
||||
it->draw(shader);
|
||||
}
|
||||
|
||||
|
||||
if(ud < 1000) {
|
||||
getChunk(ux, uz)->generate();
|
||||
getChunk(ux, uz)->setInitialized(true);
|
||||
|
||||
|
||||
if(getChunk(ux, uz)->left()) getChunk(ux, uz)->left()->generate();
|
||||
if(getChunk(ux, uz)->right()) getChunk(ux, uz)->right()->generate();
|
||||
if(getChunk(ux, uz)->front()) getChunk(ux, uz)->front()->generate();
|
||||
@ -115,7 +115,7 @@ void World::draw(Shader &shader, const glm::mat4 &projectionMatrix, const glm::m
|
||||
Chunk *World::getChunk(s32 x, s32 z) {
|
||||
x += m_width / 2;
|
||||
z += m_depth / 2;
|
||||
|
||||
|
||||
return m_chunks[x + z * m_width].get();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user