Custom model loading
This commit is contained in:
parent
bb8439b513
commit
a6a1f61e22
91
LoadingModels/ImportedModel.cpp
Normal file
91
LoadingModels/ImportedModel.cpp
Normal file
@ -0,0 +1,91 @@
|
|||||||
|
#include <fstream>
|
||||||
|
#include <sstream>
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
#include "ImportedModel.h"
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
ImportedModel::ImportedModel() {}
|
||||||
|
|
||||||
|
ImportedModel::ImportedModel(const char *filePath) {
|
||||||
|
ModelImporter modelImporter = ModelImporter();
|
||||||
|
modelImporter.parseOBJ(filePath);
|
||||||
|
numVertices = modelImporter.getNumVertices();
|
||||||
|
std::vector<float> verts = modelImporter.getVertices();
|
||||||
|
std::vector<float> tcs = modelImporter.getTextureCoordinates();
|
||||||
|
std::vector<float> normals = modelImporter.getNormals();
|
||||||
|
|
||||||
|
for (int i = 0; i < numVertices; i++) {
|
||||||
|
vertices.push_back(glm::vec3(verts[i*3], verts[i*3+1], verts[i*3+2]));
|
||||||
|
texCoords.push_back(glm::vec2(tcs[i*2], tcs[i*2+1]));
|
||||||
|
normalVecs.push_back(glm::vec3(normals[i*3], normals[i*3+1], normals[i*3+2]));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int ImportedModel::getNumVertices() { return numVertices; }
|
||||||
|
std::vector<glm::vec3> ImportedModel::getVertices() { return vertices; }
|
||||||
|
std::vector<glm::vec2> ImportedModel::getTextureCoords() { return texCoords; }
|
||||||
|
std::vector<glm::vec3> ImportedModel::getNormals() { return normalVecs; }
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------
|
||||||
|
|
||||||
|
ModelImporter::ModelImporter() {}
|
||||||
|
|
||||||
|
void ModelImporter::parseOBJ(const char *filePath) {
|
||||||
|
float x, y, z;
|
||||||
|
string content;
|
||||||
|
ifstream fileStream(filePath, ios::in);
|
||||||
|
string line = "";
|
||||||
|
while (!fileStream.eof()) {
|
||||||
|
getline(fileStream, line);
|
||||||
|
if (line.compare(0, 2, "v ") == 0) {
|
||||||
|
stringstream ss(line.erase(0, 1));
|
||||||
|
ss >> x; ss >> y; ss >> z;
|
||||||
|
vertVals.push_back(x);
|
||||||
|
vertVals.push_back(y);
|
||||||
|
vertVals.push_back(z);
|
||||||
|
}
|
||||||
|
if (line.compare(0, 2, "vt") == 0) {
|
||||||
|
stringstream ss(line.erase(0, 2));
|
||||||
|
ss >> x; ss >> y;
|
||||||
|
stVals.push_back(x);
|
||||||
|
stVals.push_back(y);
|
||||||
|
}
|
||||||
|
if (line.compare(0, 2, "vn") == 0) {
|
||||||
|
stringstream ss(line.erase(0, 2));
|
||||||
|
ss >> x; ss >> y; ss >> z;
|
||||||
|
normVals.push_back(x);
|
||||||
|
normVals.push_back(y);
|
||||||
|
normVals.push_back(z);
|
||||||
|
}
|
||||||
|
if (line.compare(0, 2, "f ") == 0) {
|
||||||
|
string oneCorner, v, t, n;
|
||||||
|
stringstream ss(line.erase(0, 2));
|
||||||
|
for (int i = 0; i < 3; i++) {
|
||||||
|
getline(ss, oneCorner, ' ');
|
||||||
|
stringstream oneCornerSS(oneCorner);
|
||||||
|
getline(oneCornerSS, v, '/');
|
||||||
|
getline(oneCornerSS, t, '/');
|
||||||
|
getline(oneCornerSS, n, '/');
|
||||||
|
|
||||||
|
int vertRef = (stoi(v) - 1) * 3;
|
||||||
|
int tcRef = (stoi(t) - 1) * 2;
|
||||||
|
int normRef = (stoi(n) - 1) * 3;
|
||||||
|
|
||||||
|
triangleVerts.push_back(vertVals[vertRef]);
|
||||||
|
triangleVerts.push_back(vertVals[vertRef + 1]);
|
||||||
|
triangleVerts.push_back(vertVals[vertRef + 2]);
|
||||||
|
|
||||||
|
textureCoords.push_back(stVals[tcRef]);
|
||||||
|
textureCoords.push_back(stVals[tcRef + 1]);
|
||||||
|
|
||||||
|
normals.push_back(normVals[normRef]);
|
||||||
|
normals.push_back(normVals[normRef + 1]);
|
||||||
|
normals.push_back(normVals[normRef + 2]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int ModelImporter::getNumVertices() { return (triangleVerts.size()/3); }
|
||||||
|
std::vector<float> ModelImporter::getVertices() { return triangleVerts; }
|
||||||
|
std::vector<float> ModelImporter::getTextureCoordinates() { return textureCoords; }
|
||||||
|
std::vector<float> ModelImporter::getNormals() { return normals; }
|
35
LoadingModels/ImportedModel.h
Normal file
35
LoadingModels/ImportedModel.h
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class ImportedModel
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int numVertices;
|
||||||
|
std::vector<glm::vec3> vertices;
|
||||||
|
std::vector<glm::vec2> texCoords;
|
||||||
|
std::vector<glm::vec3> normalVecs;
|
||||||
|
public:
|
||||||
|
ImportedModel();
|
||||||
|
ImportedModel(const char *filePath);
|
||||||
|
int getNumVertices();
|
||||||
|
std::vector<glm::vec3> getVertices();
|
||||||
|
std::vector<glm::vec2> getTextureCoords();
|
||||||
|
std::vector<glm::vec3> getNormals();
|
||||||
|
};
|
||||||
|
|
||||||
|
class ModelImporter
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
std::vector<float> vertVals;
|
||||||
|
std::vector<float> triangleVerts;
|
||||||
|
std::vector<float> textureCoords;
|
||||||
|
std::vector<float> stVals;
|
||||||
|
std::vector<float> normals;
|
||||||
|
std::vector<float> normVals;
|
||||||
|
public:
|
||||||
|
ModelImporter();
|
||||||
|
void parseOBJ(const char *filePath);
|
||||||
|
int getNumVertices();
|
||||||
|
std::vector<float> getVertices();
|
||||||
|
std::vector<float> getTextureCoordinates();
|
||||||
|
std::vector<float> getNormals();
|
||||||
|
};
|
@ -139,6 +139,7 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="ImportedModel.cpp" />
|
||||||
<ClCompile Include="main.cpp" />
|
<ClCompile Include="main.cpp" />
|
||||||
<ClCompile Include="Sphere.cpp" />
|
<ClCompile Include="Sphere.cpp" />
|
||||||
<ClCompile Include="Torus.cpp" />
|
<ClCompile Include="Torus.cpp" />
|
||||||
@ -148,6 +149,7 @@
|
|||||||
<None Include="vertShader.glsl" />
|
<None Include="vertShader.glsl" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClInclude Include="ImportedModel.h" />
|
||||||
<ClInclude Include="ShaderError.h" />
|
<ClInclude Include="ShaderError.h" />
|
||||||
<ClInclude Include="Sphere.h" />
|
<ClInclude Include="Sphere.h" />
|
||||||
<ClInclude Include="Torus.h" />
|
<ClInclude Include="Torus.h" />
|
||||||
|
@ -24,6 +24,9 @@
|
|||||||
<ClCompile Include="Torus.cpp">
|
<ClCompile Include="Torus.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="ImportedModel.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Include="fragShader.glsl">
|
<None Include="fragShader.glsl">
|
||||||
@ -46,5 +49,8 @@
|
|||||||
<ClInclude Include="Torus.h">
|
<ClInclude Include="Torus.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="ImportedModel.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
@ -10,9 +10,10 @@
|
|||||||
#include <stack>
|
#include <stack>
|
||||||
#include "Sphere.h"
|
#include "Sphere.h"
|
||||||
#include "Torus.h"
|
#include "Torus.h"
|
||||||
|
#include "ImportedModel.h"
|
||||||
|
|
||||||
#define numVAOs 1
|
#define numVAOs 1
|
||||||
#define numVBOs 8
|
#define numVBOs 11
|
||||||
|
|
||||||
GLuint renderingProgram;
|
GLuint renderingProgram;
|
||||||
GLuint vao[numVAOs];
|
GLuint vao[numVAOs];
|
||||||
@ -29,6 +30,7 @@ GLuint pyrTex;
|
|||||||
|
|
||||||
Sphere sphere(48);
|
Sphere sphere(48);
|
||||||
Torus torus(1.9,0.4,48);
|
Torus torus(1.9,0.4,48);
|
||||||
|
ImportedModel shuttle("shuttle.obj");
|
||||||
|
|
||||||
void setupVertices(void) {
|
void setupVertices(void) {
|
||||||
glGenVertexArrays(1, vao);
|
glGenVertexArrays(1, vao);
|
||||||
@ -96,10 +98,37 @@ void setupVertices(void) {
|
|||||||
|
|
||||||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[7]);
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo[7]);
|
||||||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(float), indices.data(), GL_STATIC_DRAW);
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(float), indices.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
vertices=shuttle.getVertices();
|
||||||
|
uvs=shuttle.getTextureCoords();
|
||||||
|
normals=shuttle.getNormals();
|
||||||
|
|
||||||
|
pvalues.clear();
|
||||||
|
tvalues.clear();
|
||||||
|
nvalues.clear();
|
||||||
|
for(int i=0;i<shuttle.getNumVertices();i++){
|
||||||
|
pvalues.push_back(vertices[i].x);
|
||||||
|
pvalues.push_back(vertices[i].y);
|
||||||
|
pvalues.push_back(vertices[i].z);
|
||||||
|
tvalues.push_back(uvs[i].s);
|
||||||
|
tvalues.push_back(uvs[i].t);
|
||||||
|
nvalues.push_back(normals[i].x);
|
||||||
|
nvalues.push_back(normals[i].y);
|
||||||
|
nvalues.push_back(normals[i].z);
|
||||||
|
}
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[7]);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, pvalues.size()*sizeof(float), pvalues.data(), GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[8]);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, tvalues.size()*sizeof(float), tvalues.data(),GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER, vbo[9]);
|
||||||
|
glBufferData(GL_ARRAY_BUFFER, nvalues.size()*sizeof(float), nvalues.data(), GL_STATIC_DRAW);
|
||||||
}
|
}
|
||||||
|
|
||||||
void setupTextures(){
|
void setupTextures(){
|
||||||
pyrTex=utils::loadTexture("saturn.jpg");
|
pyrTex=utils::loadTexture("spstob_1.jpg");
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
glBindTexture(GL_TEXTURE_2D,pyrTex);
|
glBindTexture(GL_TEXTURE_2D,pyrTex);
|
||||||
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
|
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);
|
||||||
@ -110,7 +139,7 @@ void setupTextures(){
|
|||||||
|
|
||||||
void init(GLFWwindow* window) {
|
void init(GLFWwindow* window) {
|
||||||
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
|
renderingProgram=utils::createShaderProgram("vertShader.glsl","fragShader.glsl");
|
||||||
camera={0.0f,0.0f,-10.0f};
|
camera={0.0f,5.0f,-10.0f};
|
||||||
setupVertices();
|
setupVertices();
|
||||||
setupTextures();
|
setupTextures();
|
||||||
}
|
}
|
||||||
@ -143,6 +172,19 @@ void DrawTorus(){
|
|||||||
glDrawElements(GL_TRIANGLES,torus.getNumIndices(),GL_UNSIGNED_INT,0);
|
glDrawElements(GL_TRIANGLES,torus.getNumIndices(),GL_UNSIGNED_INT,0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DrawShuttle(){
|
||||||
|
glUniformMatrix4fv(mvLoc,1,GL_FALSE,glm::value_ptr(mvMat));
|
||||||
|
glUniformMatrix4fv(projLoc,1,GL_FALSE,glm::value_ptr(pMat));
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[7]);
|
||||||
|
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,0);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[8]);
|
||||||
|
glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,0,0);
|
||||||
|
glBindBuffer(GL_ARRAY_BUFFER,vbo[9]);
|
||||||
|
glVertexAttribPointer(2,3,GL_FLOAT,GL_FALSE,0,0);
|
||||||
|
|
||||||
|
glDrawArrays(GL_TRIANGLES,0,shuttle.getNumVertices());
|
||||||
|
}
|
||||||
|
|
||||||
double lastTime=0;
|
double lastTime=0;
|
||||||
|
|
||||||
void display(GLFWwindow* window, double currentTime) {
|
void display(GLFWwindow* window, double currentTime) {
|
||||||
@ -168,20 +210,23 @@ void display(GLFWwindow* window, double currentTime) {
|
|||||||
pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f);
|
pMat=glm::perspective(utils::degToRad(60),aspect,0.1f,1000.f);
|
||||||
|
|
||||||
vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
|
vMat=glm::translate(glm::mat4(1.0f),glm::vec3{camera.x,camera.y,camera.z});
|
||||||
|
vMat=glm::lookAt(glm::vec3{camera.x,camera.y,camera.z},{0,-3,0},{0,1,0});
|
||||||
|
|
||||||
mMat=glm::translate(glm::mat4(1.0f),{0.f,0.f,0.f});
|
mMat=glm::translate(glm::mat4(1.0f),{0.f,0.f,0.f});
|
||||||
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f});
|
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,-0.2f});
|
||||||
mMat=glm::scale(mMat,{3,3,3});
|
mMat=glm::scale(mMat,{8,8,8});
|
||||||
mvMat=vMat*mMat;
|
mvMat=vMat*mMat;
|
||||||
|
|
||||||
DrawSphere();
|
//DrawSphere();
|
||||||
|
|
||||||
|
DrawShuttle();
|
||||||
|
|
||||||
mMat=glm::translate(glm::mat4(1.0f),{0.f,0.f,0.f});
|
mMat=glm::translate(glm::mat4(1.0f),{0.f,-3.f,0.f});
|
||||||
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f});
|
mMat=glm::rotate(mMat,float(currentTime/4),{0.f,1.f,0.4f});
|
||||||
mMat=glm::scale(mMat,{3,0.1,3});
|
mMat=glm::scale(mMat,{3,0.1,3});
|
||||||
mvMat=vMat*mMat;
|
mvMat=vMat*mMat;
|
||||||
|
|
||||||
DrawTorus();
|
//DrawTorus();
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
|
BIN
LoadingModels/spstob_1.jpg
Normal file
BIN
LoadingModels/spstob_1.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 555 KiB |
Loading…
x
Reference in New Issue
Block a user