mirror of
https://github.com/Linloir/SceneEditor.git
synced 2025-12-16 23:18:12 +08:00
Update mesh class
- set vertex attribute on setup mesh - use new API of shader.setUniform
This commit is contained in:
parent
c911ac1613
commit
955c8da52e
@ -22,7 +22,7 @@ Mesh::Mesh(const std::vector<Vertex>& vertices, const std::vector<Texture>& text
|
||||
void Mesh::render(const ShaderProgram& shader) const {
|
||||
unsigned int diffuseNr = 1;
|
||||
unsigned int specularNr = 1;
|
||||
for (unsigned int i = 0; i < _textures.size(); i++) {
|
||||
for (int i = 0; i < _textures.size(); i++) {
|
||||
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0 + i); // activate proper texture unit before binding
|
||||
// retrieve texture number (the N in diffuse_textureN)
|
||||
std::string number;
|
||||
@ -32,7 +32,7 @@ void Mesh::render(const ShaderProgram& shader) const {
|
||||
else if (_textures[i].type() == TextureType::SPECULAR)
|
||||
number = std::to_string(specularNr++);
|
||||
|
||||
OPENGL_EXTRA_FUNCTIONS->glUniform1i(OPENGL_EXTRA_FUNCTIONS->glGetUniformLocation(shader.programId(), (name + number).c_str()), i);
|
||||
shader.setUniform(name + number, i);
|
||||
_textures[i].bind();
|
||||
}
|
||||
OPENGL_EXTRA_FUNCTIONS->glActiveTexture(GL_TEXTURE0);
|
||||
@ -44,4 +44,9 @@ void Mesh::render(const ShaderProgram& shader) const {
|
||||
|
||||
void Mesh::setupMesh() {
|
||||
_vao = VertexArrayObject(_vertices, _indices);
|
||||
_vao.setVertexAttributePointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
|
||||
_vao.setVertexAttributePointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)OFFSETOF(Vertex, _normal));
|
||||
_vao.setVertexAttributePointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)OFFSETOF(Vertex, _texCoords));
|
||||
//_vao.setVertexAttributePointer(3, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)OFFSETOF(Vertex, _tangent));
|
||||
//_vao.setVertexAttributePointer(4, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)OFFSETOF(Vertex, _bitangent));
|
||||
}
|
||||
|
||||
@ -15,19 +15,25 @@ Model::~Model() {
|
||||
|
||||
// file path is ...\\...\\.obj, and processnode & processmesh have been called here
|
||||
void Model::loadModel(std::string path) {
|
||||
Logger::info("Loading model from path: " + path);
|
||||
Assimp::Importer importer;
|
||||
const aiScene* scene = importer.ReadFile(
|
||||
path, aiProcess_Triangulate | aiProcess_FlipUVs);
|
||||
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode) {
|
||||
Logger::error("Failed to load model: " + std::string(importer.GetErrorString()));
|
||||
_status = ERR;
|
||||
return;
|
||||
}
|
||||
|
||||
// Convert all '\' to '/'
|
||||
std::replace(path.begin(), path.end(), '\\', '/');
|
||||
_directory = path.substr(0, path.find_last_of('/'));
|
||||
|
||||
Logger::info("Model read successfully");
|
||||
Logger::info("Processing model nodes");
|
||||
processNode(scene->mRootNode, scene);
|
||||
_status = LOADED;
|
||||
Logger::info("Model loaded");
|
||||
}
|
||||
|
||||
void Model::processNode(aiNode* node, const aiScene* scene) {
|
||||
@ -48,12 +54,15 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene) {
|
||||
std::vector<unsigned int> indices;
|
||||
std::vector<Texture> textures;
|
||||
|
||||
Logger::debug("Processing mesh with " + std::to_string(mesh->mNumVertices) + " vertices");
|
||||
// Process vertices
|
||||
for (unsigned int i = 0; i < mesh->mNumVertices; i++) {
|
||||
// Create placeholder vectors
|
||||
glm::vec3 vertexPosition = glm::vec3(0.0f);
|
||||
glm::vec3 vertexNormal = glm::vec3(0.0f);
|
||||
glm::vec2 vertexTextureCoordinate = glm::vec2(0.0f);
|
||||
glm::vec3 vertexTangent = glm::vec3(0.0f);
|
||||
glm::vec3 vertexBitangent = glm::vec3(0.0f);
|
||||
|
||||
// Process vertex positions
|
||||
vertexPosition.x = mesh->mVertices[i].x;
|
||||
@ -69,8 +78,23 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene) {
|
||||
|
||||
// Process vertex texture coordinates
|
||||
if (mesh->mTextureCoords[0]) {
|
||||
// Process texture coordinates
|
||||
vertexTextureCoordinate.x = mesh->mTextureCoords[0][i].x;
|
||||
vertexTextureCoordinate.y = mesh->mTextureCoords[0][i].y;
|
||||
|
||||
//// Process vertex tangents
|
||||
//if (mesh->mTangents) {
|
||||
// vertexTangent.x = mesh->mTangents[i].x;
|
||||
// vertexTangent.y = mesh->mTangents[i].y;
|
||||
// vertexTangent.z = mesh->mTangents[i].z;
|
||||
//}
|
||||
//
|
||||
//// Process vertex bitangents
|
||||
//if (mesh->mBitangents) {
|
||||
// vertexBitangent.x = mesh->mBitangents[i].x;
|
||||
// vertexBitangent.y = mesh->mBitangents[i].y;
|
||||
// vertexBitangent.z = mesh->mBitangents[i].z;
|
||||
//}
|
||||
}
|
||||
else {
|
||||
vertexTextureCoordinate = glm::vec2(0.0f, 0.0f);
|
||||
@ -80,13 +104,17 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene) {
|
||||
Vertex newVertex = {
|
||||
vertexPosition,
|
||||
vertexNormal,
|
||||
vertexTextureCoordinate
|
||||
vertexTextureCoordinate,
|
||||
//vertexTangent,
|
||||
//vertexBitangent
|
||||
};
|
||||
|
||||
// Add vertex to vertices
|
||||
vertices.push_back(newVertex);
|
||||
}
|
||||
Logger::debug("Vertices vector memory usage: " + std::to_string(vertices.size() * sizeof(Vertex) / 1024) + " KB");
|
||||
|
||||
Logger::debug("Processing mesh with " + std::to_string(mesh->mNumFaces) + " faces");
|
||||
// Process indices
|
||||
for (unsigned int i = 0; i < mesh->mNumFaces; i++) {
|
||||
aiFace face = mesh->mFaces[i];
|
||||
@ -94,7 +122,9 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene) {
|
||||
indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
}
|
||||
Logger::debug("Indices vector memory usage: " + std::to_string(indices.size() * sizeof(unsigned int) / 1024) + " KB");
|
||||
|
||||
Logger::debug("Processing mesh materials");
|
||||
// Process material
|
||||
if (mesh->mMaterialIndex >= 0) {
|
||||
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
@ -107,8 +137,10 @@ Mesh Model::processMesh(aiMesh* mesh, const aiScene* scene) {
|
||||
std::vector<Texture> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, TextureType::SPECULAR);
|
||||
textures.insert(textures.end(), specularMaps.begin(), specularMaps.end());
|
||||
}
|
||||
Logger::debug("Textures vector memory usage: " + std::to_string(textures.size() * sizeof(Texture) / 1024) + " KB");
|
||||
|
||||
return Mesh(vertices, indices, textures);
|
||||
Logger::debug("Mesh processed");
|
||||
return Mesh(std::move(vertices), std::move(indices), std::move(textures));
|
||||
}
|
||||
|
||||
std::vector<Texture> Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type, TextureType textureType) {
|
||||
@ -121,6 +153,7 @@ std::vector<Texture> Model::loadMaterialTextures(aiMaterial* mat, aiTextureType
|
||||
if (std::strcmp(_texturesLoaded[j].path().data(), str.C_Str()) == 0) {
|
||||
textures.push_back(_texturesLoaded[j]);
|
||||
skip = true;
|
||||
Logger::debug("Texture already loaded, skipped");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,6 +70,11 @@ void SceneViewer::initializeGL() {
|
||||
_shaderProgram.attachShader(fragmentShader);
|
||||
vertexShader.dispose();
|
||||
fragmentShader.dispose();
|
||||
|
||||
Model* backpackModel = new Model("E:\\Repositories\\CollegeProjects\\CGAssignments\\FinalProject\\Models\\backpack\\backpack.obj");
|
||||
Logger::info("Model loaded");
|
||||
Renderable renderable(backpackModel);
|
||||
_objects.push_back(backpackModel);
|
||||
}
|
||||
|
||||
void SceneViewer::resizeGL(int w, int h) {
|
||||
@ -79,7 +84,9 @@ void SceneViewer::resizeGL(int w, int h) {
|
||||
void SceneViewer::paintGL() {
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
_vao.bind();
|
||||
_shaderProgram.bind();
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
for (auto object : _objects) {
|
||||
object.render(_shaderProgram);
|
||||
}
|
||||
}
|
||||
@ -4,7 +4,7 @@
|
||||
#include <qopenglfunctions.h>
|
||||
#include <qopenglextrafunctions.h>
|
||||
|
||||
#define offsetof(s,m) (size_t)&reinterpret_cast<const volatile char&>((((s *)0)->m))
|
||||
#define OFFSETOF(TYPE, ELEMENT) ((size_t)&(((TYPE *)0)->ELEMENT))
|
||||
|
||||
#define OPENGL_FUNCTIONS QOpenGLContext::currentContext()->functions()
|
||||
#define OPENGL_EXTRA_FUNCTIONS QOpenGLContext::currentContext()->extraFunctions()
|
||||
|
||||
@ -12,3 +12,6 @@ Vertex::Vertex(glm::vec3 position, glm::vec3 normal) :
|
||||
|
||||
Vertex::Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoords) :
|
||||
_position(position), _normal(normal), _texCoords(texCoords) {}
|
||||
|
||||
//Vertex::Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoords, glm::vec3 tangent, glm::vec3 bitangent) :
|
||||
// _position(position), _normal(normal), _texCoords(texCoords), _tangent(tangent), _bitangent(bitangent) {}
|
||||
|
||||
@ -2,20 +2,16 @@
|
||||
|
||||
#include <GLM/glm.hpp>
|
||||
|
||||
class Vertex {
|
||||
private:
|
||||
struct Vertex {
|
||||
glm::vec3 _position = glm::vec3(0.0f);
|
||||
glm::vec3 _normal = glm::vec3(0.0f);
|
||||
glm::vec2 _texCoords = glm::vec2(0.0f);
|
||||
|
||||
public:
|
||||
//glm::vec3 _tangent = glm::vec3(0.0f);
|
||||
//glm::vec3 _bitangent = glm::vec3(0.0f);
|
||||
|
||||
Vertex();
|
||||
Vertex(glm::vec3 position);
|
||||
Vertex(glm::vec3 position, glm::vec3 normal);
|
||||
Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoords);
|
||||
|
||||
public:
|
||||
inline glm::vec3 position() const { return _position; }
|
||||
inline glm::vec3 normal() const { return _normal; }
|
||||
inline glm::vec2 texCoords() const { return _texCoords; }
|
||||
//Vertex(glm::vec3 position, glm::vec3 normal, glm::vec2 texCoords, glm::vec3 tangent, glm::vec3 bitangent);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user