加入了模型
|
@ -86,6 +86,8 @@
|
|||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="Camera.cpp" />
|
||||
<ClCompile Include="Mesh.cpp" />
|
||||
<ClCompile Include="Model.cpp" />
|
||||
<ClCompile Include="RendererWidget.cpp" />
|
||||
<QtRcc Include="MainWindow.qrc" />
|
||||
<QtUic Include="MainWindow.ui" />
|
||||
|
@ -102,6 +104,8 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Camera.h" />
|
||||
<ClInclude Include="Mesh.h" />
|
||||
<ClInclude Include="Model.h" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Condition="Exists('$(QtMsBuild)\qt.targets')">
|
||||
|
|
|
@ -21,7 +21,7 @@
|
|||
<UniqueIdentifier>{639EADAA-A684-42e4-A9AD-28FC9BCB8F7C}</UniqueIdentifier>
|
||||
<Extensions>ts</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="Shaders">
|
||||
<Filter Include="Resource Files\Shaders">
|
||||
<UniqueIdentifier>{60515177-3da7-420f-8f5b-27c16bb2b77b}</UniqueIdentifier>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
|
@ -49,6 +49,12 @@
|
|||
<ClCompile Include="Camera.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Mesh.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
<ClCompile Include="Model.cpp">
|
||||
<Filter>Source Files</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<QtMoc Include="RendererWidget.h">
|
||||
|
@ -57,15 +63,21 @@
|
|||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="Shaders\shader.frag">
|
||||
<Filter>Shaders</Filter>
|
||||
<Filter>Resource Files\Shaders</Filter>
|
||||
</None>
|
||||
<None Include="Shaders\shader.vert">
|
||||
<Filter>Shaders</Filter>
|
||||
<Filter>Resource Files\Shaders</Filter>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClInclude Include="Camera.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Mesh.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
<ClInclude Include="Model.h">
|
||||
<Filter>Header Files</Filter>
|
||||
</ClInclude>
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -8,7 +8,7 @@
|
|||
// Default camera values
|
||||
const float YAW = -90.0f;
|
||||
const float PITCH = 0.0f;
|
||||
const float SPEED = 2.5f;
|
||||
const float SPEED = 200.f;
|
||||
const float SENSITIVITY = 0.1f;
|
||||
const float ZOOM = 45.0f;
|
||||
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
<RCC>
|
||||
<qresource prefix="MainWindow">
|
||||
<qresource prefix="/">
|
||||
<file>Shaders/shader.frag</file>
|
||||
<file>Shaders/shader.vert</file>
|
||||
</qresource>
|
||||
</RCC>
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
#include "Mesh.h"
|
||||
|
||||
Mesh::Mesh(QOpenGLFunctions_4_5_Compatibility* glFunc, QOpenGLShaderProgram* shaderProgram, aiMatrix4x4 model)
|
||||
: glFunc(glFunc)
|
||||
, shaderProgram(shaderProgram)
|
||||
, VBO(QOpenGLBuffer::VertexBuffer)
|
||||
, EBO(QOpenGLBuffer::IndexBuffer)
|
||||
{
|
||||
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int j = 0; j < 4; j++) {
|
||||
this->model(i, j) = model[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
void Mesh::Draw()
|
||||
{
|
||||
unsigned int diffuseNr = 1;
|
||||
unsigned int specularNr = 1;
|
||||
unsigned int normalNr = 1;
|
||||
unsigned int heightNr = 1;
|
||||
|
||||
for (unsigned int i = 0; i < textures.size(); i++)
|
||||
{
|
||||
glFunc->glActiveTexture(GL_TEXTURE0 + i); // 在绑定之前激活相应的纹理单元
|
||||
// 获取纹理序号(diffuse_textureN 中的 N)
|
||||
QString number;
|
||||
QString name = textures[i]->type;
|
||||
if (name == "texture_diffuse")
|
||||
number = QString::number(diffuseNr++);
|
||||
else if (name == "texture_specular")
|
||||
number = QString::number(specularNr++);
|
||||
else if (name == "texture_normal")
|
||||
number = QString::number(normalNr++); // transfer unsigned int to stream
|
||||
else if (name == "texture_height")
|
||||
number = QString::number(heightNr++); // transfer unsigned int to stream
|
||||
textures[i]->texture.bind();
|
||||
shaderProgram->setUniformValue((name + number).toStdString().c_str(), i);
|
||||
}
|
||||
// 绘制网格
|
||||
|
||||
QOpenGLVertexArrayObject::Binder bind(&VAO);
|
||||
shaderProgram->setUniformValue("model", model);
|
||||
glFunc->glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
}
|
||||
void Mesh::setupMesh()
|
||||
{
|
||||
|
||||
VAO.create();
|
||||
VAO.bind();
|
||||
|
||||
VBO.create();
|
||||
EBO.create();
|
||||
|
||||
VBO.bind();
|
||||
VBO.allocate(vertices.data(), vertices.size() * sizeof(Vertex));
|
||||
|
||||
EBO.bind();
|
||||
EBO.allocate(indices.data(), indices.size() * sizeof(unsigned int));
|
||||
|
||||
|
||||
shaderProgram->enableAttributeArray(0);
|
||||
shaderProgram->setAttributeBuffer(0, GL_FLOAT, 0, 3, sizeof(Vertex));
|
||||
|
||||
shaderProgram->enableAttributeArray(1);
|
||||
shaderProgram->setAttributeBuffer(1, GL_FLOAT, offsetof(Vertex, Normal), 3, sizeof(Vertex));
|
||||
|
||||
shaderProgram->enableAttributeArray(2);
|
||||
shaderProgram->setAttributeBuffer(2, GL_FLOAT, offsetof(Vertex, TexCoords), 2, sizeof(Vertex));
|
||||
|
||||
shaderProgram->enableAttributeArray(1);
|
||||
shaderProgram->setAttributeBuffer(3, GL_FLOAT, offsetof(Vertex, Tangent), 3, sizeof(Vertex));
|
||||
|
||||
shaderProgram->enableAttributeArray(1);
|
||||
shaderProgram->setAttributeBuffer(4, GL_FLOAT, offsetof(Vertex, Bitangent), 3, sizeof(Vertex));
|
||||
VAO.release();
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
#pragma once
|
||||
#include <QOpenGLFunctions_4_5_Compatibility>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
#include <QVector2D>
|
||||
#include <QVector3D>
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QOpenGLBuffer>
|
||||
#include <QOpenGLVertexArrayObject>
|
||||
#include <QOpenGLTexture>
|
||||
#include <QOpenGLWidget>
|
||||
#include <assimp/Importer.hpp>
|
||||
#include <assimp/scene.h>
|
||||
#include <assimp/postprocess.h>
|
||||
|
||||
struct Vertex
|
||||
{
|
||||
QVector3D Position;
|
||||
QVector3D Normal;
|
||||
QVector2D TexCoords;
|
||||
QVector3D Tangent;
|
||||
QVector3D Bitangent;
|
||||
};
|
||||
|
||||
struct Texture
|
||||
{
|
||||
QOpenGLTexture texture;
|
||||
QString type;
|
||||
QString path;
|
||||
Texture() :texture(QOpenGLTexture::Target2D) {
|
||||
texture.create();
|
||||
texture.setWrapMode(QOpenGLTexture::DirectionS, QOpenGLTexture::Repeat);
|
||||
texture.setWrapMode(QOpenGLTexture::DirectionT, QOpenGLTexture::Repeat);
|
||||
texture.setMinMagFilters(QOpenGLTexture::LinearMipMapLinear, QOpenGLTexture::Linear);
|
||||
}
|
||||
};
|
||||
|
||||
class Mesh {
|
||||
public:
|
||||
/* 网格数据 */
|
||||
QVector<Vertex> vertices; //顶点数据
|
||||
QVector<unsigned int> indices; //索引数组
|
||||
QVector<Texture*> textures; //纹理数据
|
||||
QMatrix4x4 model; //模型矩阵
|
||||
QOpenGLFunctions_4_5_Compatibility* glFunc; //opengl函数入口
|
||||
QOpenGLShaderProgram* shaderProgram; //着色器程序
|
||||
|
||||
/* 函数 */
|
||||
Mesh(QOpenGLFunctions_4_5_Compatibility* glFunc, QOpenGLShaderProgram* shaderProgram, aiMatrix4x4 model);
|
||||
void Draw();
|
||||
void setupMesh();
|
||||
|
||||
private:
|
||||
/* 渲染数据 */
|
||||
QOpenGLVertexArrayObject VAO;
|
||||
QOpenGLBuffer VBO, EBO;
|
||||
|
||||
};
|
|
@ -0,0 +1,198 @@
|
|||
#include "Model.h"
|
||||
|
||||
#include <QOpenGLTexture>
|
||||
#include <QOpenGLContext>
|
||||
#include <QTextCodec>
|
||||
#include <iostream>
|
||||
|
||||
Model::Model(QString path, QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram)
|
||||
: context(context)
|
||||
, shaderProgram(shaderProgram)
|
||||
, directory(path)
|
||||
{
|
||||
Assimp::Importer import;
|
||||
const aiScene* scene = import.ReadFile(directory.absolutePath().toUtf8(), aiProcess_Triangulate | aiProcess_FlipUVs);
|
||||
if (!scene || scene->mFlags & AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
|
||||
{
|
||||
qDebug() << "ERROR::ASSIMP::" << import.GetErrorString() << endl;
|
||||
return;
|
||||
}
|
||||
qDebug() << directory.absolutePath() << "Load Successfully";
|
||||
qDebug() << "NumMeshes: " << scene->mNumMeshes;
|
||||
qDebug() << "NumMaterials: " << scene->mNumMaterials;
|
||||
qDebug() << "NumTextures: " << scene->mNumTextures;
|
||||
directory.cdUp();
|
||||
processNode(scene->mRootNode, scene);
|
||||
}
|
||||
|
||||
Model::~Model() //销毁对象
|
||||
{
|
||||
for (auto& it : textures_loaded) {
|
||||
it->texture.destroy();
|
||||
delete it;
|
||||
}
|
||||
for (auto& it : meshes) {
|
||||
delete it;
|
||||
}
|
||||
}
|
||||
|
||||
void Model::draw() {
|
||||
shaderProgram->bind();
|
||||
for (Mesh* mesh : meshes) {
|
||||
mesh->Draw();
|
||||
}
|
||||
}
|
||||
|
||||
void Model::destroy()
|
||||
{
|
||||
delete this;
|
||||
context->doneCurrent();
|
||||
}
|
||||
|
||||
Model* Model::createModel(QString path, QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram)
|
||||
{
|
||||
return new Model(path, context, shaderProgram);
|
||||
}
|
||||
|
||||
|
||||
void Model::processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4)
|
||||
{
|
||||
|
||||
// 处理节点所有的网格(如果有的话)
|
||||
for (unsigned int i = 0; i < node->mNumMeshes; i++)
|
||||
{
|
||||
aiMesh* mesh = scene->mMeshes[node->mMeshes[i]];
|
||||
meshes.push_back(processMesh(mesh, scene, mat4));
|
||||
|
||||
}
|
||||
// 接下来对它的子节点重复这一过程
|
||||
for (unsigned int i = 0; i < node->mNumChildren; i++)
|
||||
{
|
||||
processNode(node->mChildren[i], scene, mat4 * node->mChildren[i]->mTransformation);
|
||||
}
|
||||
}
|
||||
Mesh* Model::processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 model)
|
||||
{
|
||||
// 初始化网格
|
||||
Mesh* m_mesh = new Mesh(QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_4_5_Compatibility>(), shaderProgram, model);
|
||||
// 遍历网格的每个顶点
|
||||
for (unsigned int i = 0; i < mesh->mNumVertices; i++)
|
||||
{
|
||||
Vertex vertex;
|
||||
QVector3D vector; //将assimp的数据转化为QtOpenGL支持的数据
|
||||
|
||||
// 位置
|
||||
vector.setX(mesh->mVertices[i].x);
|
||||
vector.setY(mesh->mVertices[i].y);
|
||||
vector.setZ(mesh->mVertices[i].z);
|
||||
vertex.Position = vector;
|
||||
// 法向量
|
||||
if (mesh->mNormals) {
|
||||
vector.setX(mesh->mNormals[i].x);
|
||||
vector.setY(mesh->mNormals[i].y);
|
||||
vector.setZ(mesh->mNormals[i].z);
|
||||
vertex.Normal = vector;
|
||||
}
|
||||
// 纹理坐标
|
||||
if (mesh->mTextureCoords[0]) // does the mesh contain texture coordinates?
|
||||
{
|
||||
QVector2D vec;
|
||||
//一个顶点最多可以包含8个不同的纹理坐标。因此我们假设我们不用
|
||||
//使用一个顶点可以有多个纹理坐标的模型,所以我们总是取第一个集合(0)。
|
||||
vec.setX(mesh->mTextureCoords[0][i].x);
|
||||
vec.setY(mesh->mTextureCoords[0][i].y);
|
||||
vertex.TexCoords = vec;
|
||||
}
|
||||
else {
|
||||
vertex.TexCoords = QVector2D(0, 0);
|
||||
}
|
||||
if (mesh->mTangents) {
|
||||
// tangent
|
||||
vector.setX(mesh->mTangents[i].x);
|
||||
vector.setY(mesh->mTangents[i].y);
|
||||
vector.setZ(mesh->mTangents[i].z);
|
||||
vertex.Tangent = vector;
|
||||
}
|
||||
if (mesh->mBitangents) {
|
||||
vector.setX(mesh->mBitangents[i].x);
|
||||
vector.setY(mesh->mBitangents[i].y);
|
||||
vector.setZ(mesh->mBitangents[i].z);
|
||||
vertex.Bitangent = vector;
|
||||
}
|
||||
// bitangent
|
||||
m_mesh->vertices.push_back(vertex);
|
||||
}
|
||||
|
||||
for (unsigned int i = 0; i < mesh->mNumFaces; i++)
|
||||
{
|
||||
aiFace face = mesh->mFaces[i];
|
||||
// 将所有面的索引数据添加到索引数组中
|
||||
for (unsigned int j = 0; j < face.mNumIndices; j++) {
|
||||
m_mesh->indices.push_back(face.mIndices[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// 处理材质
|
||||
aiMaterial* material = scene->mMaterials[mesh->mMaterialIndex];
|
||||
|
||||
// 1. 漫反射贴图
|
||||
QVector<Texture*> diffuseMaps = loadMaterialTextures(material, aiTextureType_DIFFUSE, "texture_diffuse");
|
||||
for (auto& it : diffuseMaps)
|
||||
m_mesh->textures.push_back(it);
|
||||
|
||||
// 2. 镜面贴图
|
||||
QVector<Texture*> specularMaps = loadMaterialTextures(material, aiTextureType_SPECULAR, "texture_specular");
|
||||
for (auto& it : specularMaps)
|
||||
m_mesh->textures.push_back(it);
|
||||
|
||||
// 3. 法向量图
|
||||
QVector<Texture*> normalMaps = loadMaterialTextures(material, aiTextureType_HEIGHT, "texture_normal");
|
||||
for (auto& it : normalMaps)
|
||||
m_mesh->textures.push_back(it);
|
||||
|
||||
// 4. 高度图
|
||||
QVector<Texture*> heightMaps = loadMaterialTextures(material, aiTextureType_AMBIENT, "texture_height");
|
||||
for (auto& it : heightMaps)
|
||||
m_mesh->textures.push_back(it);
|
||||
|
||||
m_mesh->setupMesh();
|
||||
return m_mesh;
|
||||
}
|
||||
|
||||
QVector<Texture*> Model::loadMaterialTextures(aiMaterial* mat, aiTextureType type, QString typeName)
|
||||
{
|
||||
QVector<Texture*> textures;
|
||||
for (unsigned int i = 0; i < mat->GetTextureCount(type); i++)
|
||||
{
|
||||
aiString str;
|
||||
mat->GetTexture(type, i, &str);
|
||||
|
||||
// 检查纹理是否在之前加载过,如果是,则继续到下一个迭代:跳过加载新纹理
|
||||
bool skip = false;
|
||||
for (unsigned int j = 0; j < textures_loaded.size(); j++)
|
||||
{
|
||||
if (std::strcmp(textures_loaded[j]->path.toStdString().c_str(), str.C_Str()) == 0)
|
||||
{
|
||||
textures.push_back(textures_loaded[j]);
|
||||
skip = true; //【优化】 带有相同filepath的纹理已经加载,继续到下一个
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!skip)
|
||||
{ // 如果材质还没有加载,加载它
|
||||
Texture* texture = new Texture;
|
||||
QImage data(directory.filePath(str.C_Str()));
|
||||
if (!data.isNull()) {
|
||||
texture->texture.setData(data);
|
||||
texture->type = typeName;
|
||||
texture->path = str.C_Str();
|
||||
textures.push_back(texture);
|
||||
textures_loaded.push_back(texture); // store it as texture loaded for entire model, to ensure we won't unnecesery load duplicate textures.
|
||||
}
|
||||
else {
|
||||
qDebug() << "未能成功加载纹理:" << directory.filePath(str.C_Str());
|
||||
}
|
||||
}
|
||||
}
|
||||
return textures;
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
#pragma once
|
||||
|
||||
#include "Mesh.h"
|
||||
#include <QDir>
|
||||
|
||||
class Model
|
||||
{
|
||||
public:
|
||||
void draw();
|
||||
void destroy();
|
||||
static Model* createModel(QString path, QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram);
|
||||
|
||||
private:
|
||||
Model(QString path, QOpenGLContext* context, QOpenGLShaderProgram* shaderProgram);
|
||||
~Model();
|
||||
QOpenGLContext* context; //opengl函数入口
|
||||
QOpenGLShaderProgram* shaderProgram; //着色器程序
|
||||
|
||||
/* 模型数据 */
|
||||
QVector<Texture*>textures_loaded; //纹理
|
||||
QVector<Mesh*> meshes; //网格
|
||||
QDir directory; //模型所在路径
|
||||
|
||||
//递归遍历结点
|
||||
void processNode(aiNode* node, const aiScene* scene, aiMatrix4x4 mat4 = aiMatrix4x4());
|
||||
|
||||
//加载网格
|
||||
Mesh* processMesh(aiMesh* mesh, const aiScene* scene, aiMatrix4x4 model);
|
||||
|
||||
//加载材质纹理
|
||||
QVector<Texture*> loadMaterialTextures(aiMaterial* mat, aiTextureType type, QString typeName);
|
||||
|
||||
};
|
||||
|
After Width: | Height: | Size: 856 KiB |
After Width: | Height: | Size: 898 KiB |
After Width: | Height: | Size: 1.0 MiB |
After Width: | Height: | Size: 314 KiB |
After Width: | Height: | Size: 653 KiB |
After Width: | Height: | Size: 518 KiB |
After Width: | Height: | Size: 321 KiB |
After Width: | Height: | Size: 412 KiB |
After Width: | Height: | Size: 275 KiB |
After Width: | Height: | Size: 306 KiB |
After Width: | Height: | Size: 349 KiB |
After Width: | Height: | Size: 1.5 MiB |
After Width: | Height: | Size: 134 KiB |
After Width: | Height: | Size: 702 KiB |
After Width: | Height: | Size: 510 KiB |
After Width: | Height: | Size: 508 KiB |
After Width: | Height: | Size: 545 KiB |
After Width: | Height: | Size: 1.0 MiB |
After Width: | Height: | Size: 332 KiB |
After Width: | Height: | Size: 12 KiB |
After Width: | Height: | Size: 361 KiB |
After Width: | Height: | Size: 1.1 MiB |
After Width: | Height: | Size: 201 KiB |
After Width: | Height: | Size: 762 KiB |
After Width: | Height: | Size: 754 KiB |
After Width: | Height: | Size: 825 KiB |
After Width: | Height: | Size: 391 KiB |
After Width: | Height: | Size: 767 KiB |
After Width: | Height: | Size: 803 KiB |
After Width: | Height: | Size: 825 KiB |
After Width: | Height: | Size: 667 KiB |
After Width: | Height: | Size: 538 KiB |
After Width: | Height: | Size: 535 KiB |
After Width: | Height: | Size: 604 KiB |
After Width: | Height: | Size: 204 KiB |
After Width: | Height: | Size: 1010 KiB |
After Width: | Height: | Size: 475 KiB |
After Width: | Height: | Size: 747 KiB |
After Width: | Height: | Size: 557 KiB |
After Width: | Height: | Size: 1010 KiB |
After Width: | Height: | Size: 390 KiB |
After Width: | Height: | Size: 2.3 MiB |
After Width: | Height: | Size: 552 KiB |
After Width: | Height: | Size: 560 KiB |
After Width: | Height: | Size: 506 KiB |
After Width: | Height: | Size: 628 KiB |
After Width: | Height: | Size: 1010 KiB |
After Width: | Height: | Size: 682 KiB |
After Width: | Height: | Size: 586 KiB |
After Width: | Height: | Size: 825 KiB |
After Width: | Height: | Size: 274 KiB |
After Width: | Height: | Size: 559 KiB |
After Width: | Height: | Size: 650 KiB |
After Width: | Height: | Size: 295 KiB |
After Width: | Height: | Size: 500 KiB |
After Width: | Height: | Size: 637 KiB |
After Width: | Height: | Size: 608 KiB |
After Width: | Height: | Size: 1.2 MiB |
After Width: | Height: | Size: 254 KiB |
After Width: | Height: | Size: 189 KiB |
After Width: | Height: | Size: 519 KiB |
After Width: | Height: | Size: 102 KiB |
After Width: | Height: | Size: 852 KiB |
After Width: | Height: | Size: 354 KiB |
After Width: | Height: | Size: 404 KiB |
After Width: | Height: | Size: 379 KiB |
After Width: | Height: | Size: 1020 KiB |
After Width: | Height: | Size: 594 KiB |
After Width: | Height: | Size: 9.1 MiB |
After Width: | Height: | Size: 951 B |
|
@ -3,8 +3,7 @@
|
|||
#include <string>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
//#include <assimp/Importer.hpp>
|
||||
//#include <assimp/postprocess.h>
|
||||
|
||||
RendererWidget::RendererWidget(QWidget* parent)
|
||||
: QOpenGLWidget(parent), camera(QVector3D(0.0f, 0.0f, 3.0f))
|
||||
{
|
||||
|
@ -33,48 +32,26 @@ void RendererWidget::initializeGL()
|
|||
initializeOpenGLFunctions();
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glClearColor(0, 0, 0, 1);
|
||||
std::string vertexCode;
|
||||
std::string fragmentCode;
|
||||
std::ifstream vShaderFile;
|
||||
std::ifstream fShaderFile;
|
||||
vShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
fShaderFile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
|
||||
try
|
||||
{
|
||||
vShaderFile.open("Shaders/shader.vert");
|
||||
fShaderFile.open("Shaders/shader.frag");
|
||||
std::stringstream vShaderStream, fShaderStream;
|
||||
|
||||
vShaderStream << vShaderFile.rdbuf();
|
||||
fShaderStream << fShaderFile.rdbuf();
|
||||
|
||||
vShaderFile.close();
|
||||
fShaderFile.close();
|
||||
|
||||
vertexCode = vShaderStream.str();
|
||||
fragmentCode = fShaderStream.str();
|
||||
}
|
||||
catch (std::ifstream::failure& e)
|
||||
{
|
||||
std::cout << "ERROR::SHADER::FILE_NOT_SUCCESFULLY_READ" << std::endl;
|
||||
}
|
||||
|
||||
m_program = new QOpenGLShaderProgram;
|
||||
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexCode.c_str());
|
||||
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentCode.c_str());
|
||||
m_program->link();
|
||||
if (!m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/Shaders/shader.vert"))
|
||||
qDebug() << "ERROR:" << m_program->log();
|
||||
if (!m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/Shaders/shader.frag"))
|
||||
qDebug() << "ERROR:" << m_program->log();
|
||||
if (!m_program->link())
|
||||
qDebug() << "ERROR:" << m_program->log();
|
||||
m_program->bind();
|
||||
|
||||
m_vao.create();
|
||||
model = Model::createModel("Models/Sponza/Sponza.gltf", context(), m_program);
|
||||
|
||||
|
||||
/*m_vao.create();
|
||||
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
|
||||
|
||||
m_vbo.create();
|
||||
m_vbo.bind();
|
||||
|
||||
|
||||
|
||||
//Assimp::Importer importer;
|
||||
|
||||
GLfloat vertex[] = {
|
||||
-1,-1, 0, 0,0,
|
||||
1,-1, 0, 1,0,
|
||||
|
@ -90,7 +67,7 @@ void RendererWidget::initializeGL()
|
|||
f->glEnableVertexAttribArray(1);
|
||||
f->glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat),
|
||||
reinterpret_cast<void*>(3 * sizeof(GLfloat)));
|
||||
m_vbo.release();
|
||||
m_vbo.release();*/
|
||||
}
|
||||
|
||||
void RendererWidget::paintGL()
|
||||
|
@ -104,17 +81,16 @@ void RendererWidget::paintGL()
|
|||
// camera/view transformation
|
||||
QMatrix4x4 view = camera.GetViewMatrix();
|
||||
m_program->setUniformValue(m_program->uniformLocation("view"), view);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
//glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
model->draw();
|
||||
m_program->release();
|
||||
}
|
||||
|
||||
void RendererWidget::resizeGL(int width, int height)
|
||||
{
|
||||
m_program->bind();
|
||||
// pass projection matrix to shader (note that in this case it could change every frame)
|
||||
QMatrix4x4 projection;
|
||||
projection.perspective(camera.Zoom, (float)width / (float)height, 0.1f, 100.0f);
|
||||
projection.perspective(camera.Zoom, (float)width / (float)height, 0.1f, 10000.0f);
|
||||
m_program->setUniformValue(m_program->uniformLocation("projection"), projection);
|
||||
|
||||
}
|
||||
|
@ -171,12 +147,11 @@ void RendererWidget::mouseMoveEvent(QMouseEvent* event)
|
|||
}
|
||||
else
|
||||
{
|
||||
float xoffset = event->pos().x() - width() / 2;
|
||||
float yoffset = height() / 2 - event->pos().y();
|
||||
float xoffset = event->pos().x() - geometry().center().x();
|
||||
float yoffset = geometry().center().y() - event->pos().y();
|
||||
camera.ProcessMouseMovement(xoffset, yoffset);
|
||||
}
|
||||
|
||||
cursor().setPos(mapToGlobal(QPoint(width() / 2, height() / 2)));
|
||||
cursor().setPos(mapToGlobal(geometry().center()));
|
||||
QOpenGLWidget::mouseMoveEvent(event);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#include <QOpenGLVertexArrayObject>
|
||||
#include <QKeyEvent>
|
||||
#include "Camera.h"
|
||||
#include "Model.h"
|
||||
|
||||
class RendererWidget : public QOpenGLWidget, protected QOpenGLFunctions_4_5_Compatibility
|
||||
{
|
||||
|
@ -31,5 +32,6 @@ private:
|
|||
QOpenGLShaderProgram* m_program = nullptr;
|
||||
QOpenGLBuffer m_vbo;
|
||||
QOpenGLVertexArrayObject m_vao;
|
||||
Model* model;
|
||||
};
|
||||
|
||||
|
|
|
@ -1,8 +1,12 @@
|
|||
#version 330 core
|
||||
out vec4 FragColor;
|
||||
in vec2 texCoord;
|
||||
|
||||
in vec2 TexCoords;
|
||||
|
||||
uniform sampler2D texture_diffuse1;
|
||||
|
||||
void main()
|
||||
{
|
||||
FragColor = vec4(texCoord,1,1);
|
||||
gl_FragColor = texture2D(texture_diffuse1,TexCoords);
|
||||
if(gl_FragColor.a==0)
|
||||
discard;
|
||||
}
|
|
@ -1,11 +1,17 @@
|
|||
#version 330 core
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec3 aNormal;
|
||||
layout (location = 2) in vec2 aTexCoords;
|
||||
|
||||
out vec2 TexCoords;
|
||||
|
||||
uniform mat4 model;
|
||||
uniform mat4 view;
|
||||
uniform mat4 projection;
|
||||
layout (location = 0) in vec3 aPos;
|
||||
layout (location = 1) in vec2 aTexCoord;
|
||||
out vec2 texCoord;
|
||||
|
||||
void main()
|
||||
{
|
||||
gl_Position = projection * view * vec4(aPos, 1.0f);
|
||||
texCoord = aTexCoord;
|
||||
TexCoords = aTexCoords;
|
||||
gl_Position = projection * view * model * vec4(aPos, 1.0);
|
||||
|
||||
}
|