mirror of
https://github.com/guezoloic/LearnOpenGL.git
synced 2026-01-25 04:34:14 +00:00
feat: rework few fonctions and change glew to glad
- game files deleted
This commit is contained in:
6
.gitmodules
vendored
6
.gitmodules
vendored
@@ -1,6 +1,3 @@
|
|||||||
[submodule "lib/glew"]
|
|
||||||
path = lib/glew
|
|
||||||
url = https://github.com/edoren/glew
|
|
||||||
[submodule "lib/glm"]
|
[submodule "lib/glm"]
|
||||||
path = lib/glm
|
path = lib/glm
|
||||||
url = https://github.com/g-truc/glm
|
url = https://github.com/g-truc/glm
|
||||||
@@ -10,3 +7,6 @@
|
|||||||
[submodule "lib/stb"]
|
[submodule "lib/stb"]
|
||||||
path = lib/stb
|
path = lib/stb
|
||||||
url = https://github.com/nothings/stb.git
|
url = https://github.com/nothings/stb.git
|
||||||
|
[submodule "lib/glad"]
|
||||||
|
path = lib/glad
|
||||||
|
url = https://github.com/DawidLokiec/glad.git
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
|||||||
|
|
||||||
file(GLOB_RECURSE CPP_SOURCES CONFIGURE_DEPENDS src/*.cpp)
|
file(GLOB_RECURSE CPP_SOURCES CONFIGURE_DEPENDS src/*.cpp)
|
||||||
file(GLOB_RECURSE CPP_HEADERS CONFIGURE_DEPENDS src/*.hpp)
|
file(GLOB_RECURSE CPP_HEADERS CONFIGURE_DEPENDS src/*.hpp)
|
||||||
|
|
||||||
|
list(APPEND CPP_SOURCES lib/glad/src/glad.c)
|
||||||
add_executable(main ${CPP_SOURCES} ${CPP_HEADERS})
|
add_executable(main ${CPP_SOURCES} ${CPP_HEADERS})
|
||||||
|
|
||||||
# GLFW
|
# GLFW
|
||||||
@@ -14,12 +16,8 @@ set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
|
|||||||
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
|
||||||
add_subdirectory(lib/glfw)
|
add_subdirectory(lib/glfw)
|
||||||
|
|
||||||
# GLEW
|
# GLAD
|
||||||
add_definitions(-DGLEW_STATIC)
|
target_include_directories(main PRIVATE lib/glad/include)
|
||||||
include_directories(lib/glew/include)
|
|
||||||
add_library(glew STATIC lib/glew/src/glew.c)
|
|
||||||
target_include_directories(glew PUBLIC lib/glew/include)
|
|
||||||
target_compile_definitions(glew PRIVATE GLEW_STATIC)
|
|
||||||
|
|
||||||
# GLM
|
# GLM
|
||||||
target_include_directories(main PRIVATE lib/glm)
|
target_include_directories(main PRIVATE lib/glm)
|
||||||
@@ -29,7 +27,7 @@ target_include_directories(main PRIVATE lib/stb)
|
|||||||
|
|
||||||
target_include_directories(main PRIVATE inc/)
|
target_include_directories(main PRIVATE inc/)
|
||||||
|
|
||||||
target_link_libraries(main PRIVATE glfw glew)
|
target_link_libraries(main PRIVATE glfw)
|
||||||
|
|
||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
target_link_libraries(main PRIVATE OpenGL::GL)
|
target_link_libraries(main PRIVATE OpenGL::GL)
|
||||||
|
|||||||
19
inc/EBO.hpp
19
inc/EBO.hpp
@@ -1,18 +1,21 @@
|
|||||||
#ifndef EBO_HPP
|
#ifndef EBO_HPP
|
||||||
#define EBO_HPP
|
#define EBO_HPP
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "glad/glad.h"
|
||||||
|
|
||||||
|
|
||||||
class EBO
|
class EBO
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
GLuint id;
|
GLuint id;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
EBO(unsigned int* indices, size_t size);
|
EBO(unsigned int* indices, size_t size);
|
||||||
~EBO();
|
~EBO();
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void unbind();
|
void unbind();
|
||||||
};
|
};
|
||||||
#endif
|
#endif
|
||||||
26
inc/VAO.hpp
26
inc/VAO.hpp
@@ -1,23 +1,27 @@
|
|||||||
#ifndef VAO_HPP
|
#ifndef VAO_HPP
|
||||||
#define VAO_HPP
|
#define VAO_HPP
|
||||||
|
|
||||||
#include <gl/glew.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "glad/glad.h"
|
||||||
|
|
||||||
class VAO
|
class VAO
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VAO();
|
VAO();
|
||||||
~VAO();
|
~VAO();
|
||||||
|
|
||||||
void bind();
|
void bind();
|
||||||
void unbind();
|
void unbind();
|
||||||
|
|
||||||
void setAttributePointer(GLuint index, GLuint size, GLenum type, GLsizei stride, const void* offset);
|
void setAttributePointer(GLuint index, GLuint size, GLenum type,
|
||||||
void drawElement(GLenum mode, GLsizei count, GLenum type, const void *indices);
|
GLsizei stride, const void* offset);
|
||||||
void drawArray(GLenum mode, GLint first, GLsizei count);
|
void drawElement(GLenum mode, GLsizei count, GLenum type,
|
||||||
|
const void* indices);
|
||||||
|
void drawArray(GLenum mode, GLint first, GLsizei count);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
19
inc/VBO.hpp
19
inc/VBO.hpp
@@ -1,20 +1,21 @@
|
|||||||
#ifndef VBO_HPP
|
#ifndef VBO_HPP
|
||||||
#define VBO_HPP
|
#define VBO_HPP
|
||||||
|
|
||||||
#include <gl/glew.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "glad/glad.h"
|
||||||
|
|
||||||
class VBO
|
class VBO
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
unsigned int id;
|
unsigned int id;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
VBO(GLfloat* vertices, size_t size);
|
VBO(GLfloat* vertices, size_t size);
|
||||||
~VBO();
|
~VBO();
|
||||||
|
|
||||||
void bind();
|
|
||||||
void unbind();
|
|
||||||
|
|
||||||
|
void bind();
|
||||||
|
void unbind();
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,44 +1,46 @@
|
|||||||
#ifndef CAMERA_HPP
|
#ifndef CAMERA_HPP
|
||||||
#define CAMERA_HPP
|
#define CAMERA_HPP
|
||||||
|
|
||||||
#include <glfw/glfw3.h>
|
#include "glm/glm.hpp"
|
||||||
#include <glm/glm.hpp>
|
#include "glm/gtc/matrix_transform.hpp"
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
|
|
||||||
class Camera {
|
struct GLFWwindow;
|
||||||
private:
|
|
||||||
int screenWidth;
|
|
||||||
int screenHeight;
|
|
||||||
|
|
||||||
double mousePosX;
|
class Camera
|
||||||
double mousePosY;
|
{
|
||||||
bool firstMouse = true;
|
private:
|
||||||
|
int screenWidth;
|
||||||
|
int screenHeight;
|
||||||
|
|
||||||
float cameraYaw;
|
double mousePosX;
|
||||||
float cameraPitch;
|
double mousePosY;
|
||||||
|
bool firstMouse = true;
|
||||||
|
|
||||||
glm::vec3 cameraFront;
|
float cameraYaw;
|
||||||
glm::vec3 cameraUp;
|
float cameraPitch;
|
||||||
glm::vec3 cameraRight;
|
|
||||||
glm::vec3 worldUp;
|
|
||||||
|
|
||||||
GLFWwindow* window;
|
glm::vec3 cameraFront;
|
||||||
|
glm::vec3 cameraUp;
|
||||||
|
glm::vec3 cameraRight;
|
||||||
|
glm::vec3 worldUp;
|
||||||
|
|
||||||
void processInput(float deltaTime);
|
GLFWwindow* window;
|
||||||
void processMouseMovement();
|
|
||||||
void updateCameraVectors();
|
|
||||||
|
|
||||||
public:
|
void processInput(float deltaTime);
|
||||||
Camera(int width, int height, GLFWwindow* window, float sensitivity);
|
void processMouseMovement();
|
||||||
|
void updateCameraVectors();
|
||||||
|
|
||||||
void update(float deltaTime);
|
public:
|
||||||
glm::mat4 getViewMatrix();
|
Camera(int width, int height, GLFWwindow* window, float sensitivity);
|
||||||
|
|
||||||
float speed;
|
void update(float deltaTime);
|
||||||
float cameraSensitivity;
|
glm::mat4 getViewMatrix();
|
||||||
float fov;
|
|
||||||
|
|
||||||
glm::vec3 cameraPosition;
|
float speed;
|
||||||
|
float cameraSensitivity;
|
||||||
|
float fov;
|
||||||
|
|
||||||
|
glm::vec3 cameraPosition;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
12
inc/cube.hpp
12
inc/cube.hpp
@@ -1,16 +1,18 @@
|
|||||||
#ifndef CUBE_HPP
|
#ifndef CUBE_HPP
|
||||||
#define CUBE_HPP
|
#define CUBE_HPP
|
||||||
|
|
||||||
#include "vbo.hpp"
|
#include <stddef.h>
|
||||||
|
|
||||||
|
#include "camera.hpp"
|
||||||
#include "ebo.hpp"
|
#include "ebo.hpp"
|
||||||
#include "vao.hpp"
|
|
||||||
#include "shader.hpp"
|
#include "shader.hpp"
|
||||||
#include "texture.hpp"
|
#include "texture.hpp"
|
||||||
#include "camera.hpp"
|
#include "vao.hpp"
|
||||||
|
#include "vbo.hpp"
|
||||||
|
|
||||||
class Cube
|
class Cube
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
VBO vbo;
|
VBO vbo;
|
||||||
EBO ebo;
|
EBO ebo;
|
||||||
VAO vao;
|
VAO vao;
|
||||||
@@ -19,7 +21,7 @@ private:
|
|||||||
Shader shader;
|
Shader shader;
|
||||||
Texture texture;
|
Texture texture;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Cube(Camera &camera);
|
Cube(Camera &camera);
|
||||||
void loop(int width, int height);
|
void loop(int width, int height);
|
||||||
};
|
};
|
||||||
|
|||||||
29
inc/game.hpp
29
inc/game.hpp
@@ -1,29 +0,0 @@
|
|||||||
#ifndef GAME_HPP
|
|
||||||
#define GAME_HPP
|
|
||||||
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
class Game
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
int width;
|
|
||||||
int height;
|
|
||||||
const char *name;
|
|
||||||
GLFWwindow *window;
|
|
||||||
|
|
||||||
public:
|
|
||||||
Game(int width, int height, string name);
|
|
||||||
~Game();
|
|
||||||
|
|
||||||
void run(bool (*func)(Game *g));
|
|
||||||
|
|
||||||
GLFWwindow *getWindow();
|
|
||||||
const char *getName();
|
|
||||||
int getWidth();
|
|
||||||
int getHeight();
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
@@ -1,28 +1,25 @@
|
|||||||
#ifndef SHADER_HPP
|
#ifndef SHADER_HPP
|
||||||
#define SHADER_HPP
|
#define SHADER_HPP
|
||||||
|
|
||||||
#include <gl/glew.h>
|
#include "glad/glad.h"
|
||||||
#include <glm/glm.hpp>
|
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
|
||||||
#include <glm/gtc/type_ptr.hpp>
|
|
||||||
|
|
||||||
class Shader
|
class Shader
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
GLuint vertexShader;
|
GLuint vertexShader;
|
||||||
GLuint fragmentShader;
|
GLuint fragmentShader;
|
||||||
GLuint shaderProgram;
|
GLuint shaderProgram;
|
||||||
|
|
||||||
void addVertShader(const char* vertexShaderSource);
|
void addVertShader(const char* vertexShaderSource);
|
||||||
void addFragShader(const char* fragmentShaderSource);
|
void addFragShader(const char* fragmentShaderSource);
|
||||||
void compileInProgram();
|
void compileInProgram();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Shader(const char* vertexShaderSource, const char* fragmentShaderSource);
|
Shader(const char* vertexShaderSource, const char* fragmentShaderSource);
|
||||||
~Shader();
|
~Shader();
|
||||||
|
|
||||||
GLuint getProgram() const;
|
GLuint getProgram() const;
|
||||||
void use() const;
|
void use() const;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
@@ -1,7 +1,7 @@
|
|||||||
#ifndef TEXTUTE_HPP
|
#ifndef TEXTUTE_HPP
|
||||||
#define TEXTUTE_HPP
|
#define TEXTUTE_HPP
|
||||||
|
|
||||||
#include <GL/glew.h>
|
#include "glad/glad.h"
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class Texture
|
class Texture
|
||||||
|
|||||||
1
lib/glad
Submodule
1
lib/glad
Submodule
Submodule lib/glad added at 379a9432ec
1
lib/glew
1
lib/glew
Submodule lib/glew deleted from 5b995cab14
23
src/EBO.cpp
23
src/EBO.cpp
@@ -1,23 +1,14 @@
|
|||||||
#include "EBO.hpp"
|
#include "ebo.hpp"
|
||||||
|
|
||||||
EBO::EBO(unsigned int* indices, size_t size)
|
EBO::EBO(unsigned int* indices, size_t size)
|
||||||
{
|
{
|
||||||
glGenBuffers(1, &id);
|
glGenBuffers(1, &id);
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, size, indices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
EBO::~EBO()
|
EBO::~EBO() { glDeleteBuffers(1, &id); }
|
||||||
{
|
|
||||||
glDeleteBuffers(1, &id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void EBO::bind()
|
void EBO::bind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id); }
|
||||||
{
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void EBO::unbind()
|
void EBO::unbind() { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
|
||||||
{
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
||||||
}
|
|
||||||
50
src/VAO.cpp
50
src/VAO.cpp
@@ -1,40 +1,30 @@
|
|||||||
#include "VAO.hpp"
|
#include "vao.hpp"
|
||||||
|
|
||||||
VAO::VAO()
|
VAO::VAO() { glGenVertexArrays(1, &id); }
|
||||||
|
|
||||||
|
VAO::~VAO() { glDeleteVertexArrays(1, &id); }
|
||||||
|
|
||||||
|
void VAO::bind() { glBindVertexArray(id); }
|
||||||
|
|
||||||
|
void VAO::unbind() { glBindVertexArray(0); }
|
||||||
|
|
||||||
|
void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type,
|
||||||
|
GLsizei stride, const void* offset)
|
||||||
{
|
{
|
||||||
glGenVertexArrays(1, &id);
|
bind();
|
||||||
|
glEnableVertexAttribArray(index);
|
||||||
|
glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset);
|
||||||
}
|
}
|
||||||
|
|
||||||
VAO::~VAO()
|
void VAO::drawElement(GLenum mode, GLsizei count, GLenum type,
|
||||||
|
const void* indices)
|
||||||
{
|
{
|
||||||
glDeleteVertexArrays(1, &id);
|
bind();
|
||||||
}
|
glDrawElements(mode, count, type, indices);
|
||||||
|
|
||||||
void VAO::bind()
|
|
||||||
{
|
|
||||||
glBindVertexArray(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::unbind()
|
|
||||||
{
|
|
||||||
glBindVertexArray(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::setAttributePointer(GLuint index, GLuint size, GLenum type, GLsizei stride, const void* offset)
|
|
||||||
{
|
|
||||||
bind();
|
|
||||||
glEnableVertexAttribArray(index);
|
|
||||||
glVertexAttribPointer(index, size, type, GL_FALSE, stride, offset);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VAO::drawElement(GLenum mode, GLsizei count, GLenum type, const void* indices)
|
|
||||||
{
|
|
||||||
bind();
|
|
||||||
glDrawElements(mode, count, type, indices);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void VAO::drawArray(GLenum mode, GLint first, GLsizei count)
|
void VAO::drawArray(GLenum mode, GLint first, GLsizei count)
|
||||||
{
|
{
|
||||||
bind();
|
bind();
|
||||||
glDrawArrays(mode, first, count);
|
glDrawArrays(mode, first, count);
|
||||||
}
|
}
|
||||||
23
src/VBO.cpp
23
src/VBO.cpp
@@ -1,23 +1,14 @@
|
|||||||
#include "VBO.hpp"
|
#include "vbo.hpp"
|
||||||
|
|
||||||
VBO::VBO(GLfloat* vertices, size_t size)
|
VBO::VBO(GLfloat* vertices, size_t size)
|
||||||
{
|
{
|
||||||
glGenBuffers(1, &id);
|
glGenBuffers(1, &id);
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
glBindBuffer(GL_ARRAY_BUFFER, id);
|
||||||
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
glBufferData(GL_ARRAY_BUFFER, size, vertices, GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
VBO::~VBO()
|
VBO::~VBO() { glDeleteBuffers(1, &id); }
|
||||||
{
|
|
||||||
glDeleteBuffers(1, &id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VBO::bind()
|
void VBO::bind() { glBindBuffer(GL_ARRAY_BUFFER, id); }
|
||||||
{
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void VBO::unbind()
|
void VBO::unbind() { glBindBuffer(GL_ARRAY_BUFFER, 0); }
|
||||||
{
|
|
||||||
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
||||||
}
|
|
||||||
100
src/camera.cpp
100
src/camera.cpp
@@ -1,5 +1,8 @@
|
|||||||
#include "camera.hpp"
|
#include "camera.hpp"
|
||||||
|
|
||||||
|
#include <glad/glad.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
||||||
: screenWidth(width),
|
: screenWidth(width),
|
||||||
screenHeight(height),
|
screenHeight(height),
|
||||||
@@ -14,86 +17,87 @@ Camera::Camera(int width, int height, GLFWwindow* window, float sensitivity)
|
|||||||
worldUp(0.0f, 1.0f, 0.0f),
|
worldUp(0.0f, 1.0f, 0.0f),
|
||||||
firstMouse(true)
|
firstMouse(true)
|
||||||
{
|
{
|
||||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||||
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::update(float deltaTime)
|
void Camera::update(float deltaTime)
|
||||||
{
|
{
|
||||||
processInput(deltaTime);
|
processInput(deltaTime);
|
||||||
processMouseMovement();
|
processMouseMovement();
|
||||||
updateCameraVectors();
|
updateCameraVectors();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::processInput(float deltaTime)
|
void Camera::processInput(float deltaTime)
|
||||||
{
|
{
|
||||||
float velocity = speed * deltaTime;
|
float velocity = speed * deltaTime;
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
||||||
cameraPosition += cameraFront * velocity;
|
cameraPosition += cameraFront * velocity;
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
||||||
cameraPosition -= cameraFront * velocity;
|
cameraPosition -= cameraFront * velocity;
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
||||||
cameraPosition += cameraRight * velocity;
|
cameraPosition += cameraRight * velocity;
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
||||||
cameraPosition -= cameraRight * velocity;
|
cameraPosition -= cameraRight * velocity;
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
||||||
cameraPosition += cameraUp * velocity;
|
cameraPosition += cameraUp * velocity;
|
||||||
|
|
||||||
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
|
if (glfwGetKey(window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS)
|
||||||
cameraPosition -= cameraUp * velocity;
|
cameraPosition -= cameraUp * velocity;
|
||||||
|
|
||||||
// Zoom
|
// Zoom
|
||||||
if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
fov = (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS)
|
||||||
fov = 35.0f;
|
? 35.f
|
||||||
else
|
: 45.f;
|
||||||
fov = 45.0f;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::processMouseMovement()
|
void Camera::processMouseMovement()
|
||||||
{
|
{
|
||||||
double mouseX, mouseY;
|
double mouseX, mouseY;
|
||||||
glfwGetCursorPos(window, &mouseX, &mouseY);
|
glfwGetCursorPos(window, &mouseX, &mouseY);
|
||||||
|
|
||||||
if (firstMouse) {
|
|
||||||
mousePosX = mouseX;
|
|
||||||
mousePosY = mouseY;
|
|
||||||
firstMouse = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
float deltaX = static_cast<float>(mouseX - mousePosX);
|
|
||||||
float deltaY = static_cast<float>(mousePosY - mouseY);
|
|
||||||
|
|
||||||
|
if (firstMouse)
|
||||||
|
{
|
||||||
mousePosX = mouseX;
|
mousePosX = mouseX;
|
||||||
mousePosY = mouseY;
|
mousePosY = mouseY;
|
||||||
|
firstMouse = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
deltaX *= cameraSensitivity;
|
float deltaX = static_cast<float>(mouseX - mousePosX);
|
||||||
deltaY *= cameraSensitivity;
|
float deltaY = static_cast<float>(mousePosY - mouseY);
|
||||||
|
|
||||||
cameraYaw += deltaX;
|
mousePosX = mouseX;
|
||||||
cameraPitch += deltaY;
|
mousePosY = mouseY;
|
||||||
|
|
||||||
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
|
deltaX *= cameraSensitivity;
|
||||||
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
|
deltaY *= cameraSensitivity;
|
||||||
|
|
||||||
|
cameraYaw += deltaX;
|
||||||
|
cameraPitch += deltaY;
|
||||||
|
|
||||||
|
if (cameraPitch > 89.0f) cameraPitch = 89.0f;
|
||||||
|
if (cameraPitch < -89.0f) cameraPitch = -89.0f;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::updateCameraVectors()
|
void Camera::updateCameraVectors()
|
||||||
{
|
{
|
||||||
glm::vec3 front;
|
glm::vec3 front;
|
||||||
front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
front.x = cos(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||||
front.y = sin(glm::radians(cameraPitch));
|
front.y = sin(glm::radians(cameraPitch));
|
||||||
front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
front.z = sin(glm::radians(cameraYaw)) * cos(glm::radians(cameraPitch));
|
||||||
cameraFront = glm::normalize(front);
|
cameraFront = glm::normalize(front);
|
||||||
|
|
||||||
cameraRight = glm::normalize(glm::cross(cameraFront, worldUp));
|
cameraRight = glm::normalize(glm::cross(cameraFront, worldUp));
|
||||||
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
|
cameraUp = glm::normalize(glm::cross(cameraRight, cameraFront));
|
||||||
}
|
}
|
||||||
|
|
||||||
glm::mat4 Camera::getViewMatrix()
|
glm::mat4 Camera::getViewMatrix()
|
||||||
{
|
{
|
||||||
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
|
return glm::lookAt(cameraPosition, cameraPosition + cameraFront, cameraUp);
|
||||||
}
|
}
|
||||||
42
src/cube.cpp
42
src/cube.cpp
@@ -1,5 +1,7 @@
|
|||||||
#include "cube.hpp"
|
#include "cube.hpp"
|
||||||
#include "VAO.hpp"
|
|
||||||
|
#include "glm/gtc/type_ptr.hpp"
|
||||||
|
#include "vao.hpp"
|
||||||
#include "vbo.hpp"
|
#include "vbo.hpp"
|
||||||
|
|
||||||
GLfloat cubeVertices[] = {
|
GLfloat cubeVertices[] = {
|
||||||
@@ -124,21 +126,21 @@ const char* cubeFragShader = R"(
|
|||||||
}
|
}
|
||||||
)";
|
)";
|
||||||
|
|
||||||
Cube::Cube(Camera &camera) :
|
Cube::Cube(Camera& camera)
|
||||||
vbo(cubeVertices, sizeof(cubeVertices)),
|
: vbo(cubeVertices, sizeof(cubeVertices)),
|
||||||
ebo(cubeIndices, sizeof(cubeIndices)),
|
ebo(cubeIndices, sizeof(cubeIndices)),
|
||||||
texture("stone.png"),
|
texture("stone.png"),
|
||||||
camera(camera),
|
camera(camera),
|
||||||
shader(cubeVertexShader, cubeFragShader)
|
shader(cubeVertexShader, cubeFragShader)
|
||||||
{
|
{
|
||||||
vao.bind();
|
vao.bind();
|
||||||
vbo.bind();
|
vbo.bind();
|
||||||
|
|
||||||
GLsizei stride = 8*sizeof(float);
|
GLsizei stride = 8 * sizeof(float);
|
||||||
|
|
||||||
vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0);
|
vao.setAttributePointer(0, 3, GL_FLOAT, stride, (void*)0);
|
||||||
vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3*sizeof(float)));
|
vao.setAttributePointer(1, 3, GL_FLOAT, stride, (void*)(3 * sizeof(float)));
|
||||||
vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6*sizeof(float)));
|
vao.setAttributePointer(2, 2, GL_FLOAT, stride, (void*)(6 * sizeof(float)));
|
||||||
|
|
||||||
ebo.bind();
|
ebo.bind();
|
||||||
}
|
}
|
||||||
@@ -148,13 +150,10 @@ void Cube::loop(int width, int height)
|
|||||||
shader.use();
|
shader.use();
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
|
|
||||||
glm::vec3 coordinate = glm::vec3(0.0f, -1.0f, 0.0f);
|
glm::vec3 coordinate = glm::vec3(0.0f, 0.0f, -1.0f);
|
||||||
glm::mat4 projection = glm::perspective(
|
glm::mat4 projection = glm::perspective(
|
||||||
glm::radians(camera.fov),
|
glm::radians(camera.fov),
|
||||||
static_cast<float>(width) / static_cast<float>(height),
|
static_cast<float>(width) / static_cast<float>(height), 0.1f, 100.0f);
|
||||||
0.1f,
|
|
||||||
100.0f
|
|
||||||
);
|
|
||||||
|
|
||||||
GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse");
|
GLint texLoc = glGetUniformLocation(shader.getProgram(), "material.diffuse");
|
||||||
glUniform1i(texLoc, 0);
|
glUniform1i(texLoc, 0);
|
||||||
@@ -163,16 +162,13 @@ void Cube::loop(int width, int height)
|
|||||||
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
glUniformMatrix4fv(projectionLoc, 1, GL_FALSE, glm::value_ptr(projection));
|
||||||
|
|
||||||
GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view");
|
GLint viewLoc = glGetUniformLocation(shader.getProgram(), "view");
|
||||||
glUniformMatrix4fv(viewLoc, 1, GL_FALSE, glm::value_ptr(camera.getViewMatrix()));
|
glUniformMatrix4fv(viewLoc, 1, GL_FALSE,
|
||||||
|
glm::value_ptr(camera.getViewMatrix()));
|
||||||
|
|
||||||
glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate);
|
glm::mat4 model = glm::translate(glm::mat4(1.0f), coordinate);
|
||||||
GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model");
|
GLint modelLoc = glGetUniformLocation(shader.getProgram(), "model");
|
||||||
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
|
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
|
||||||
|
|
||||||
vao.drawElement(
|
vao.drawElement(GL_TRIANGLES, sizeof(cubeIndices) / sizeof(unsigned int),
|
||||||
GL_TRIANGLES,
|
GL_UNSIGNED_INT, 0);
|
||||||
sizeof(cubeIndices) / sizeof(unsigned int),
|
|
||||||
GL_UNSIGNED_INT,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
87
src/game.cpp
87
src/game.cpp
@@ -1,87 +0,0 @@
|
|||||||
#include "GL/glew.h"
|
|
||||||
#include "game.hpp"
|
|
||||||
#include "GLFW/glfw3.h"
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
|
|
||||||
Game::Game(int width, int height, string name) :
|
|
||||||
width(width),
|
|
||||||
height(height),
|
|
||||||
name(name.c_str())
|
|
||||||
{
|
|
||||||
if(glfwInit() == GLFW_FALSE)
|
|
||||||
std::cerr << "error glfw" << std::endl;
|
|
||||||
|
|
||||||
this->window = glfwCreateWindow(
|
|
||||||
this->width,
|
|
||||||
this->height,
|
|
||||||
this->name,
|
|
||||||
nullptr,
|
|
||||||
nullptr
|
|
||||||
);
|
|
||||||
|
|
||||||
// OpenGL 3.3
|
|
||||||
glfwWindowHint(GLFW_VERSION_MAJOR, 3);
|
|
||||||
glfwWindowHint(GLFW_VERSION_MINOR, 3);
|
|
||||||
|
|
||||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
||||||
|
|
||||||
glfwWindowHint(GLFW_DEPTH_BITS, 24);
|
|
||||||
glfwWindowHint(GLFW_STENCIL_BITS, 8);
|
|
||||||
|
|
||||||
glfwWindowHint(GLFW_SAMPLES, 4);
|
|
||||||
glfwMakeContextCurrent(this->window);
|
|
||||||
|
|
||||||
glewInit();
|
|
||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
|
||||||
glEnable(GL_MULTISAMPLE);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Game::run(bool (*func)(Game *g))
|
|
||||||
{
|
|
||||||
while(func(this))
|
|
||||||
{
|
|
||||||
glfwGetWindowSize(this->window, &this->width, &this->height);
|
|
||||||
glViewport(0, 0, this->width, this->height);
|
|
||||||
|
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
||||||
glClearColor(0.5f, 0.2f, 0.2f, 1.0f);
|
|
||||||
|
|
||||||
glDisable(GL_MULTISAMPLE);
|
|
||||||
glfwPollEvents();
|
|
||||||
glfwSwapBuffers(this->window);
|
|
||||||
|
|
||||||
GLenum error = glGetError();
|
|
||||||
if (error != glGetError())
|
|
||||||
{
|
|
||||||
std::cerr << error << std::endl;
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Game::~Game()
|
|
||||||
{
|
|
||||||
glfwTerminate();
|
|
||||||
}
|
|
||||||
|
|
||||||
GLFWwindow *Game::getWindow()
|
|
||||||
{
|
|
||||||
return this->window;
|
|
||||||
}
|
|
||||||
const char *Game::getName()
|
|
||||||
{
|
|
||||||
return this->name;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Game::getWidth()
|
|
||||||
{
|
|
||||||
return this->height;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Game::getHeight()
|
|
||||||
{
|
|
||||||
return this->width;
|
|
||||||
}
|
|
||||||
100
src/main.cpp
100
src/main.cpp
@@ -1,16 +1,98 @@
|
|||||||
#define STB_IMAGE_IMPLEMENTATION
|
#include <glad/glad.h>
|
||||||
#include "stb_image.h"
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
#include "game.hpp"
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
#include "camera.hpp"
|
||||||
|
#include "cube.hpp"
|
||||||
|
|
||||||
|
static void glfwErrorCallback(int error, const char *description)
|
||||||
|
{
|
||||||
|
std::cerr << "GLFW Error " << error << ": " << description << std::endl;
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
Game game(800, 600, "game");
|
const char *title = "window";
|
||||||
|
int width = 800, height = 600;
|
||||||
|
|
||||||
auto quit = [](Game *g) {
|
glfwSetErrorCallback(glfwErrorCallback);
|
||||||
return !glfwWindowShouldClose(g->getWindow());
|
|
||||||
};
|
|
||||||
|
|
||||||
game.run(quit);
|
if (glfwInit() == GLFW_FALSE)
|
||||||
return 0;
|
{
|
||||||
|
std::cerr << "ERROR(GLFW): initialisation error" << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// OpenGL 3.3
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
||||||
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
||||||
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||||
|
glfwWindowHint(GLFW_DEPTH_BITS, 24);
|
||||||
|
glfwWindowHint(GLFW_STENCIL_BITS, 8);
|
||||||
|
glfwWindowHint(GLFW_SAMPLES, 4);
|
||||||
|
#ifdef __APPLE__
|
||||||
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
||||||
|
#endif
|
||||||
|
|
||||||
|
GLFWwindow *window = glfwCreateWindow(width, height, title, nullptr, nullptr);
|
||||||
|
if (window == nullptr)
|
||||||
|
{
|
||||||
|
std::cerr
|
||||||
|
<< "ERROR: Failed to create GLFW window with requested GL version."
|
||||||
|
<< std::endl;
|
||||||
|
glfwTerminate();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwMakeContextCurrent(window);
|
||||||
|
|
||||||
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||||
|
{
|
||||||
|
std::cerr << "ERROR: Failed to initialize GLAD" << std::endl;
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwSwapInterval(1);
|
||||||
|
|
||||||
|
Camera camera(width, height, window, 0.1f);
|
||||||
|
Cube cube{camera};
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_MULTISAMPLE);
|
||||||
|
|
||||||
|
double deltaTime = 0.f;
|
||||||
|
double lastTime = glfwGetTime();
|
||||||
|
while (!glfwWindowShouldClose(window))
|
||||||
|
{
|
||||||
|
glfwGetFramebufferSize(window, &width, &height);
|
||||||
|
glViewport(0, 0, width, height);
|
||||||
|
|
||||||
|
glClearColor(0.5f, 0.2f, 0.2f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
double currentTime = glfwGetTime();
|
||||||
|
deltaTime = currentTime - lastTime;
|
||||||
|
lastTime = currentTime;
|
||||||
|
|
||||||
|
camera.update(deltaTime);
|
||||||
|
cube.loop(width, height);
|
||||||
|
|
||||||
|
glfwPollEvents();
|
||||||
|
glfwSwapBuffers(window);
|
||||||
|
|
||||||
|
GLenum error = glGetError();
|
||||||
|
if (error != GL_NO_ERROR)
|
||||||
|
{
|
||||||
|
std::cerr << error << std::endl;
|
||||||
|
// exit(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glfwDestroyWindow(window);
|
||||||
|
glfwTerminate();
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -2,51 +2,42 @@
|
|||||||
|
|
||||||
Shader::Shader(const char* vertexShaderSource, const char* fragmentShaderSource)
|
Shader::Shader(const char* vertexShaderSource, const char* fragmentShaderSource)
|
||||||
{
|
{
|
||||||
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
vertexShader = glCreateShader(GL_VERTEX_SHADER);
|
||||||
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
|
||||||
shaderProgram = glCreateProgram();
|
shaderProgram = glCreateProgram();
|
||||||
|
|
||||||
addVertShader(vertexShaderSource);
|
addVertShader(vertexShaderSource);
|
||||||
addFragShader(fragmentShaderSource);
|
addFragShader(fragmentShaderSource);
|
||||||
|
|
||||||
compileInProgram();
|
compileInProgram();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::addVertShader(const char* vertexShaderSource)
|
void Shader::addVertShader(const char* vertexShaderSource)
|
||||||
{
|
{
|
||||||
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
|
glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr);
|
||||||
glCompileShader(vertexShader);
|
glCompileShader(vertexShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::addFragShader(const char* fragmentShaderSource)
|
void Shader::addFragShader(const char* fragmentShaderSource)
|
||||||
{
|
{
|
||||||
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
|
glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr);
|
||||||
glCompileShader(fragmentShader);
|
glCompileShader(fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Shader::compileInProgram()
|
void Shader::compileInProgram()
|
||||||
{
|
{
|
||||||
glAttachShader(shaderProgram, vertexShader);
|
glAttachShader(shaderProgram, vertexShader);
|
||||||
glAttachShader(shaderProgram, fragmentShader);
|
glAttachShader(shaderProgram, fragmentShader);
|
||||||
glLinkProgram(shaderProgram);
|
glLinkProgram(shaderProgram);
|
||||||
|
|
||||||
glDetachShader(shaderProgram, vertexShader);
|
glDetachShader(shaderProgram, vertexShader);
|
||||||
glDetachShader(shaderProgram, fragmentShader);
|
glDetachShader(shaderProgram, fragmentShader);
|
||||||
glDeleteShader(vertexShader);
|
glDeleteShader(vertexShader);
|
||||||
glDeleteShader(fragmentShader);
|
glDeleteShader(fragmentShader);
|
||||||
}
|
}
|
||||||
|
|
||||||
Shader::~Shader()
|
Shader::~Shader() { glDeleteProgram(shaderProgram); }
|
||||||
{
|
|
||||||
glDeleteProgram(shaderProgram);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Shader::use() const
|
void Shader::use() const { glUseProgram(shaderProgram); }
|
||||||
{
|
|
||||||
glUseProgram(shaderProgram);
|
|
||||||
}
|
|
||||||
|
|
||||||
GLuint Shader::getProgram() const
|
GLuint Shader::getProgram() const { return shaderProgram; }
|
||||||
{
|
|
||||||
return shaderProgram;
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
#include "texture.hpp"
|
#include "texture.hpp"
|
||||||
#include <iostream>
|
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
#define STB_IMAGE_IMPLEMENTATION
|
#define STB_IMAGE_IMPLEMENTATION
|
||||||
#include "stb_image.h"
|
#include "stb_image.h"
|
||||||
@@ -9,60 +10,56 @@ namespace fs = std::filesystem;
|
|||||||
|
|
||||||
Texture::Texture(const std::string& filename)
|
Texture::Texture(const std::string& filename)
|
||||||
{
|
{
|
||||||
fs::path path = fs::absolute(fs::current_path() / "res" / filename);
|
fs::path path = fs::absolute(fs::current_path() / "res" / filename);
|
||||||
|
|
||||||
int maxTextureSize;
|
int maxTextureSize;
|
||||||
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
|
glGetIntegerv(GL_MAX_TEXTURE_SIZE, &maxTextureSize);
|
||||||
|
|
||||||
glGenTextures(1, &id);
|
glGenTextures(1, &id);
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
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_WRAP_T, GL_REPEAT);
|
||||||
|
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||||
|
|
||||||
int imgW, imgH, numColCh;
|
int imgW, imgH, numColCh;
|
||||||
stbi_set_flip_vertically_on_load(true);
|
stbi_set_flip_vertically_on_load(true);
|
||||||
unsigned char* data = stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0);
|
unsigned char* data =
|
||||||
|
stbi_load(path.string().c_str(), &imgW, &imgH, &numColCh, 0);
|
||||||
|
|
||||||
if (!data)
|
if (!data)
|
||||||
{
|
{
|
||||||
std::cerr << "Error loading image: " << path << std::endl;
|
std::cerr << "Error loading image: " << path << std::endl;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int loadedTextureSize = imgW * imgH;
|
int loadedTextureSize = imgW * imgH;
|
||||||
|
|
||||||
if (loadedTextureSize >= maxTextureSize)
|
|
||||||
{
|
|
||||||
std::cerr << "Error: max texture size is " << maxTextureSize
|
|
||||||
<< " pixels, but image size = " << loadedTextureSize << std::endl;
|
|
||||||
stbi_image_free(data);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB;
|
|
||||||
|
|
||||||
glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format, GL_UNSIGNED_BYTE, data);
|
|
||||||
glGenerateMipmap(GL_TEXTURE_2D);
|
|
||||||
|
|
||||||
|
if (loadedTextureSize >= maxTextureSize)
|
||||||
|
{
|
||||||
|
std::cerr << "Error: max texture size is " << maxTextureSize
|
||||||
|
<< " pixels, but image size = " << loadedTextureSize << std::endl;
|
||||||
stbi_image_free(data);
|
stbi_image_free(data);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
GLenum format = (numColCh == 4) ? GL_RGBA : GL_RGB;
|
||||||
|
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, format, imgW, imgH, 0, format,
|
||||||
|
GL_UNSIGNED_BYTE, data);
|
||||||
|
glGenerateMipmap(GL_TEXTURE_2D);
|
||||||
|
|
||||||
|
stbi_image_free(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
Texture::~Texture()
|
Texture::~Texture() { glDeleteTextures(1, &id); }
|
||||||
{
|
|
||||||
glDeleteTextures(1, &id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Texture::bind(GLenum textureUnit) const
|
void Texture::bind(GLenum textureUnit) const
|
||||||
{
|
{
|
||||||
glActiveTexture(textureUnit);
|
glActiveTexture(textureUnit);
|
||||||
glBindTexture(GL_TEXTURE_2D, id);
|
glBindTexture(GL_TEXTURE_2D, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int Texture::getID() const
|
unsigned int Texture::getID() const { return id; }
|
||||||
{
|
|
||||||
return id;
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user