Fix inline functions

This commit is contained in:
Linloir 2022-12-12 22:20:59 +08:00
parent d57a1514ee
commit 0be2b379a2
No known key found for this signature in database
GPG Key ID: 58EEB209A0F2C366
19 changed files with 186 additions and 155 deletions

View File

@ -64,11 +64,17 @@
<PostBuildEvent> <PostBuildEvent>
<Command>xcopy /y "$(SolutionDir)Libs\*.dll" "$(TargetDir)"</Command> <Command>xcopy /y "$(SolutionDir)Libs\*.dll" "$(TargetDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
<Link>
<AdditionalDependencies>$(SolutionDir)Libs\*.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<PostBuildEvent> <PostBuildEvent>
<Command>xcopy /y "$(SolutionDir)Libs\*.dll" "$(TargetDir)"</Command> <Command>xcopy /y "$(SolutionDir)Libs\*.dll" "$(TargetDir)"</Command>
</PostBuildEvent> </PostBuildEvent>
<Link>
<AdditionalDependencies>$(SolutionDir)Libs\*.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'" Label="Configuration">
<ClCompile> <ClCompile>

View File

@ -17,45 +17,6 @@ Camera::Camera(glm::vec3 position, glm::vec3 direction, glm::vec3 right, glm::ve
updateCameraState(); updateCameraState();
} }
inline glm::mat4 Camera::viewMatrix() {
return glm::lookAt(_position, _position + _front, _up);
}
inline void Camera::move(glm::vec3 direction, float step) {
_position += direction * step;
updateCameraState();
}
inline void Camera::pitch(float deltaAngle) {
_pitch += deltaAngle;
updateCameraState();
}
inline void Camera::setPitch(float angle) {
_pitch = angle;
updateCameraState();
}
inline void Camera::yaw(float deltaAngle) {
_yaw += deltaAngle;
updateCameraState();
}
inline void Camera::setYaw(float angle) {
_yaw = angle;
updateCameraState();
}
inline void Camera::roll(float deltaAngle) {
_roll += deltaAngle;
updateCameraState();
}
inline void Camera::setRoll(float angle) {
_roll = angle;
updateCameraState();
}
void Camera::updateCameraState() { void Camera::updateCameraState() {
// TODO: Implement vector update with roll pitch, and *roll* // TODO: Implement vector update with roll pitch, and *roll*
} }

View File

@ -46,3 +46,42 @@ public:
private: private:
void updateCameraState(); void updateCameraState();
}; };
inline glm::mat4 Camera::viewMatrix() {
return glm::lookAt(_position, _position + _front, _up);
}
inline void Camera::move(glm::vec3 direction, float step) {
_position += direction * step;
updateCameraState();
}
inline void Camera::pitch(float deltaAngle) {
_pitch += deltaAngle;
updateCameraState();
}
inline void Camera::setPitch(float angle) {
_pitch = angle;
updateCameraState();
}
inline void Camera::yaw(float deltaAngle) {
_yaw += deltaAngle;
updateCameraState();
}
inline void Camera::setYaw(float angle) {
_yaw = angle;
updateCameraState();
}
inline void Camera::roll(float deltaAngle) {
_roll += deltaAngle;
updateCameraState();
}
inline void Camera::setRoll(float angle) {
_roll = angle;
updateCameraState();
}

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "ebo.h" #include "ebo.h"
#include "utils.h"
ElementBufferObject::ElementBufferObject() { ElementBufferObject::ElementBufferObject() {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
@ -32,7 +31,3 @@ ElementBufferObject::ElementBufferObject(std::vector<unsigned int>&& indices) :
); );
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
inline void ElementBufferObject::dispose() const {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
}

View File

@ -2,6 +2,8 @@
#include <vector> #include <vector>
#include "utils.h"
class ElementBufferObject class ElementBufferObject
{ {
private: private:
@ -17,4 +19,8 @@ public:
inline std::vector<unsigned int> indices() const { return _indices; } inline std::vector<unsigned int> indices() const { return _indices; }
inline void dispose() const; inline void dispose() const;
}; };
inline void ElementBufferObject::dispose() const {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
}

View File

@ -21,7 +21,7 @@ void Logger::log(LogLevel level, std::string message) {
case WARNING: case WARNING:
std::cout << "[WARNING] " << message << std::endl; std::cout << "[WARNING] " << message << std::endl;
break; break;
case ERROR: case ERR:
std::cout << "[ERROR] " << message << std::endl; std::cout << "[ERROR] " << message << std::endl;
break; break;
} }
@ -40,5 +40,5 @@ void Logger::warning(std::string message) {
} }
void Logger::error(std::string message) { void Logger::error(std::string message) {
log(ERROR, message); log(ERR, message);
} }

View File

@ -6,7 +6,7 @@
class Logger { class Logger {
public: public:
enum LogLevel { DEBUG, INFO, WARNING, ERROR }; enum LogLevel { DEBUG, INFO, WARNING, ERR };
private: private:
static LogLevel _level; static LogLevel _level;

View File

@ -38,7 +38,7 @@ void Mesh::render(const ShaderProgram& shader) const {
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0); OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0);
_vao.setActive(); _vao.setActive();
glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0); OPENGL_EXTRA_FUNCTIONS->glDrawElements(GL_TRIANGLES, _indices.size(), GL_UNSIGNED_INT, 0);
_vao.setInactive(); _vao.setInactive();
} }

View File

@ -1,7 +1,5 @@
#pragma once #pragma once
#include <GLM/ext/matrix_transform.hpp>
#include "renderable.h" #include "renderable.h"
Renderable::Renderable(Model* model) : _model(model) {} Renderable::Renderable(Model* model) : _model(model) {}
@ -32,14 +30,6 @@ void Renderable::setScale(float scale) {
_scale = glm::vec3(scale); _scale = glm::vec3(scale);
} }
inline glm::mat4 Renderable::modelMatrix() const {
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, _position);
model = model * _rotation;
model = glm::scale(model, _scale);
return model;
}
void Renderable::render(ShaderProgram shader) { void Renderable::render(ShaderProgram shader) {
_model->render(shader); _model->render(shader);
} }

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <GLM/glm.hpp> #include <GLM/glm.hpp>
#include <GLM/ext/matrix_transform.hpp>
#include "model.h" #include "model.h"
#include "shader.h" #include "shader.h"
@ -29,3 +30,11 @@ public:
public: public:
void render(ShaderProgram shader); void render(ShaderProgram shader);
}; };
inline glm::mat4 Renderable::modelMatrix() const {
glm::mat4 model = glm::mat4(1.0f);
model = glm::translate(model, _position);
model = model * _rotation;
model = glm::scale(model, _scale);
return model;
}

View File

@ -3,14 +3,8 @@
#include <GLM/gtc/type_ptr.hpp> #include <GLM/gtc/type_ptr.hpp>
#include "shader.h" #include "shader.h"
#include "utils.h"
#include "logger.h" #include "logger.h"
inline void Shader::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteShader(_shaderId);
_shaderId = 0;
}
VertexShader::VertexShader(const std::string& source){ VertexShader::VertexShader(const std::string& source){
_shaderId = OPENGL_EXTRA_FUNCTIONS->glCreateShader(GL_VERTEX_SHADER); _shaderId = OPENGL_EXTRA_FUNCTIONS->glCreateShader(GL_VERTEX_SHADER);
compile(source); compile(source);
@ -105,17 +99,4 @@ ShaderProgram::ShaderProgram(VertexShader vertexShader, FragmentShader fragmentS
vertexShader.dispose(); vertexShader.dispose();
fragmentShader.dispose(); fragmentShader.dispose();
geometryShader.dispose(); geometryShader.dispose();
}
inline void ShaderProgram::setActive() {
OPENGL_EXTRA_FUNCTIONS->glUseProgram(_programId);
}
inline void ShaderProgram::setInactive() {
OPENGL_EXTRA_FUNCTIONS->glUseProgram(0);
}
inline void ShaderProgram::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteProgram(_programId);
_programId = 0;
} }

View File

@ -4,6 +4,8 @@
#include <vector> #include <vector>
#include <GLM/glm.hpp> #include <GLM/glm.hpp>
#include "utils.h"
class Shader { class Shader {
protected: protected:
unsigned int _shaderId = -1; unsigned int _shaderId = -1;
@ -18,6 +20,11 @@ protected:
virtual void compile(const std::string& sourceFilePath) = 0; virtual void compile(const std::string& sourceFilePath) = 0;
}; };
inline void Shader::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteShader(_shaderId);
_shaderId = 0;
}
class VertexShader : public Shader { class VertexShader : public Shader {
public: public:
VertexShader() {} VertexShader() {}
@ -67,3 +74,16 @@ public:
inline void dispose(); inline void dispose();
}; };
inline void ShaderProgram::setActive() {
OPENGL_EXTRA_FUNCTIONS->glUseProgram(_programId);
}
inline void ShaderProgram::setInactive() {
OPENGL_EXTRA_FUNCTIONS->glUseProgram(0);
}
inline void ShaderProgram::dispose() {
OPENGL_EXTRA_FUNCTIONS->glDeleteProgram(_programId);
_programId = 0;
}

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#define STB_IMAGE_IMPLEMENTATION
#include <STBImage/stb_image.h> #include <STBImage/stb_image.h>
#include <qopengl.h> #include <qopengl.h>
@ -6,7 +7,6 @@
#include "texture.h" #include "texture.h"
#include "logger.h" #include "logger.h"
#include "utils.h"
QOpenGLContext* sharedContext; QOpenGLContext* sharedContext;
@ -17,7 +17,7 @@ Texture::Texture(TextureType type, std::string path) {
OPENGL_FUNCTIONS->glGenTextures(1, &_id); OPENGL_FUNCTIONS->glGenTextures(1, &_id);
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, _id); OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, _id);
// Set the texture wrapping parameters // Set the texture wrapping parameters
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method) OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); // Set texture wrapping to GL_REPEAT (usually basic wrapping method)
OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); OPENGL_FUNCTIONS->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
@ -39,6 +39,10 @@ Texture::Texture(TextureType type, std::string path) {
else if (nrChannels == 4) { else if (nrChannels == 4) {
format = GL_RGBA; format = GL_RGBA;
} }
else {
Logger::error("Unexpected channel count");
return;
}
OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data); OPENGL_FUNCTIONS->glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
OPENGL_FUNCTIONS->glGenerateMipmap(GL_TEXTURE_2D); OPENGL_FUNCTIONS->glGenerateMipmap(GL_TEXTURE_2D);

View File

@ -3,6 +3,8 @@
#include <string> #include <string>
#include <qopenglcontext.h> #include <qopenglcontext.h>
#include "utils.h"
enum TextureType { DIFFUSE, SPECULAR }; enum TextureType { DIFFUSE, SPECULAR };
class Texture { class Texture {
@ -19,6 +21,14 @@ public:
inline TextureType type() const { return _type; } inline TextureType type() const { return _type; }
inline std::string path() const { return _path; } inline std::string path() const { return _path; }
inline void bind() const; void bind() const;
inline void unbind() const; void unbind() const;
}; };
inline void Texture::bind() const {
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, _id);
}
inline void Texture::unbind() const {
OPENGL_FUNCTIONS->glBindTexture(GL_TEXTURE_2D, 0);
}

View File

@ -1,10 +1,6 @@
#pragma once #pragma once
#include <qopengl.h>
#include "vao.h" #include "vao.h"
#include "utils.h"
#include "logger.h"
VertexArrayObject::VertexArrayObject() { VertexArrayObject::VertexArrayObject() {
OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenVertexArrays(1, &_id);
@ -42,66 +38,4 @@ VertexArrayObject::VertexArrayObject(VertexBufferObject&& vbo, ElementBufferObje
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id()); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id()); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0); OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::setActive() const {
// Check is id is valid
if (_id == -1) {
Logger::error("Binding an invalid VertexArrayObject");
return;
}
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
}
inline void VertexArrayObject::setInactive() const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindVertexBufferObject(const VertexBufferObject& vbo) {
_vbo = vbo;
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindVertexBufferObject(VertexBufferObject&& vbo) {
_vbo = std::move(vbo);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindElementBufferObject(const ElementBufferObject& ebo) {
_ebo = ebo;
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindElementBufferObject(ElementBufferObject&& ebo) {
_ebo = std::move(ebo);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::setVertexAttributePointer(
unsigned int index,
int size,
unsigned int type,
bool normalized,
unsigned int stride,
const void* pointer
) const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(
index,
size,
type,
normalized,
stride,
pointer
);
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(index);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
} }

View File

@ -4,6 +4,8 @@
#include "vbo.h" #include "vbo.h"
#include "ebo.h" #include "ebo.h"
#include "utils.h"
#include "logger.h"
class VertexArrayObject { class VertexArrayObject {
private: private:
@ -31,4 +33,78 @@ public:
inline void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const; inline void setVertexAttributePointer(unsigned int index, int size, unsigned int type, bool normalized, unsigned int stride, const void* pointer) const;
inline void enableVertexAttribute(unsigned int index) const; inline void enableVertexAttribute(unsigned int index) const;
inline void disableVertexAttribute(unsigned int index) const; inline void disableVertexAttribute(unsigned int index) const;
}; };
inline void VertexArrayObject::setActive() const {
// Check is id is valid
if (_id == -1) {
Logger::error("Binding an invalid VertexArrayObject");
return;
}
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
}
inline void VertexArrayObject::setInactive() const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindVertexBufferObject(const VertexBufferObject& vbo) {
_vbo = vbo;
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindVertexBufferObject(VertexBufferObject&& vbo) {
_vbo = std::move(vbo);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, _vbo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindElementBufferObject(const ElementBufferObject& ebo) {
_ebo = ebo;
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::bindElementBufferObject(ElementBufferObject&& ebo) {
_ebo = std::move(ebo);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo.id());
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::setVertexAttributePointer(
unsigned int index,
int size,
unsigned int type,
bool normalized,
unsigned int stride,
const void* pointer
) const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glVertexAttribPointer(
index,
size,
type,
normalized,
stride,
pointer
);
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(index);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::enableVertexAttribute(unsigned int index) const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glEnableVertexAttribArray(index);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}
inline void VertexArrayObject::disableVertexAttribute(unsigned int index) const {
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(_id);
OPENGL_EXTRA_FUNCTIONS->glDisableVertexAttribArray(index);
OPENGL_EXTRA_FUNCTIONS->glBindVertexArray(0);
}

View File

@ -1,7 +1,6 @@
#pragma once #pragma once
#include "vbo.h" #include "vbo.h"
#include "utils.h"
VertexBufferObject::VertexBufferObject() { VertexBufferObject::VertexBufferObject() {
OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id); OPENGL_EXTRA_FUNCTIONS->glGenBuffers(1, &_id);
@ -32,7 +31,3 @@ VertexBufferObject::VertexBufferObject(std::vector<Vertex>&& vertices) :
); );
OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0); OPENGL_EXTRA_FUNCTIONS->glBindBuffer(GL_ARRAY_BUFFER, 0);
} }
inline void VertexBufferObject::dispose() const {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
}

View File

@ -3,6 +3,7 @@
#include <vector> #include <vector>
#include "vertex.h" #include "vertex.h"
#include "utils.h"
class VertexBufferObject { class VertexBufferObject {
private: private:
@ -18,4 +19,8 @@ public:
inline std::vector<Vertex> vertices() const { return _vertices; } inline std::vector<Vertex> vertices() const { return _vertices; }
inline void dispose() const; inline void dispose() const;
}; };
inline void VertexBufferObject::dispose() const {
OPENGL_EXTRA_FUNCTIONS->glDeleteBuffers(1, &_id);
}

BIN
Libs/assimp-vc143-mtd.lib Normal file

Binary file not shown.