-
Notifications
You must be signed in to change notification settings - Fork 2
/
ModelData.cpp
212 lines (182 loc) · 5.89 KB
/
ModelData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include "ModelData.hpp"
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <vector> // STL dynamic memory.
#include <fstream>
#include <sstream>
#include <sys/time.h>
// OpenGL includes
#include <GL/glew.h>
#include <GL/freeglut.h>
// Assimp includes
#include <assimp/cimport.h> // scene importer
#include <assimp/scene.h> // collects data
#include <assimp/postprocess.h> // various extra operations
#include <glm/vec3.hpp> // glm::vec3
#include <glm/vec4.hpp> // glm::vec4
#include <glm/mat4x4.hpp> // glm::mat4
#include <glm/gtc/matrix_transform.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.hpp"
using namespace std;
ModelData::ModelData(const char* file_name,GLuint shaderProgramID, GLuint Vao, bool root)
{
GLuint x=0;
if(shaderProgramID!=x&&Vao!=x)
{
this->ShaderID=shaderProgramID;
this->VAO=Vao;
}
else
{
//Maybe not the best stratergy for the shaders???
glGenVertexArrays(1,&VAO);
Shader* temp=new Shader();
this->ShaderID = temp->ShaderProgramID;
}
this->isRoot=root;
glUseProgram(this->ShaderID);
const aiScene *scene = aiImportFile( file_name, aiProcess_Triangulate | aiProcess_PreTransformVertices);
const aiMesh *mesh = scene->mMeshes[0];
this->mPointCount = mesh->mNumVertices;
glBindVertexArray(this->VAO);
GLfloat *points = NULL; // array of vertex points
GLfloat *normals = NULL; // array of vertex normals
GLfloat *texcoords = NULL; // array of texture coordinates
//A lot of help was taken from here https://github.com/capnramses/antons_opengl_tutorials_book/blob/master/13_mesh_import/main.cpp
if ( mesh->HasPositions() ) {
points = (GLfloat *)malloc( this->mPointCount * 3 * sizeof( GLfloat ) );
for ( int i = 0; i < this->mPointCount; i++ ) {
const aiVector3D *vp = &( mesh->mVertices[i] );
points[i * 3] = (GLfloat)vp->x;
points[i * 3 + 1] = (GLfloat)vp->y;
points[i * 3 + 2] = (GLfloat)vp->z;
}
}
if ( mesh->HasNormals() ) {
normals = (GLfloat *)malloc( this->mPointCount * 3 * sizeof( GLfloat ) );
for ( int i = 0; i < this->mPointCount; i++ ) {
const aiVector3D *vn = &( mesh->mNormals[i] );
normals[i * 3] = (GLfloat)vn->x;
normals[i * 3 + 1] = (GLfloat)vn->y;
normals[i * 3 + 2] = (GLfloat)vn->z;
}
}
if ( mesh->HasTextureCoords( 0 ) ) {
texcoords = (GLfloat *)malloc( this->mPointCount * 2 * sizeof( GLfloat ) );
for ( int i = 0; i < this->mPointCount; i++ ) {
const aiVector3D *vt = &( mesh->mTextureCoords[0][i] );
texcoords[i * 2] = (GLfloat)vt->x;
texcoords[i * 2 + 1] = (GLfloat)vt->y;
}
}
/* copy mesh data into VBOs */
if ( mesh->HasPositions() ) {
GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, 3 * this->mPointCount * sizeof( GLfloat ), points,
GL_STATIC_DRAW );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glEnableVertexAttribArray( 0 );
free( points );
}
if ( mesh->HasNormals() ) {
GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, 3 * this->mPointCount * sizeof( GLfloat ), normals,
GL_STATIC_DRAW );
glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, 0, NULL );
glEnableVertexAttribArray( 1 );
free( normals );
}
if ( mesh->HasTextureCoords( 0 ) ) {
GLuint vbo;
glGenBuffers( 1, &vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo );
glBufferData( GL_ARRAY_BUFFER, 2 * this->mPointCount * sizeof( GLfloat ), texcoords,
GL_STATIC_DRAW );
glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, 0, NULL );
glEnableVertexAttribArray( 2 );
free( texcoords );
}
aiReleaseImport( scene );
}
glm::mat4 ModelData::draw(glm::mat4 view, glm::mat4 persp_proj,glm::mat4 Color_view, glm::mat4 Parent)
{
glm::mat4 model=this->getModel();
// Apply the root matrix to the child matrix
if (this->isRoot) model = Parent * model;
glUseProgram(this->ShaderID);
glBindVertexArray(this->VAO);
int proj_mat_location = glGetUniformLocation(this->ShaderID, "proj");
glUniformMatrix4fv(proj_mat_location, 1, GL_FALSE, glm::value_ptr(persp_proj));
int matrix_location = glGetUniformLocation(this->ShaderID, "model");
glUniformMatrix4fv(matrix_location, 1, GL_FALSE, glm::value_ptr(model));
int view_mat_location = glGetUniformLocation(this->ShaderID, "view");
glUniformMatrix4fv(view_mat_location, 1, GL_FALSE, glm::value_ptr(view));
int Col_location=glGetUniformLocation(this->ShaderID, "Col_mat");
glUniformMatrix4fv(Col_location, 1, GL_FALSE, glm::value_ptr(Color_view));
glDrawArrays(GL_TRIANGLES, 0, this->mPointCount);
return model;
}
glm::mat4 ModelData::getModel()
{
glm::mat4 model(1.0f);
model = glm::translate(model, this->Position);
model = glm::rotate(model, glm::degrees(rotate_x) , glm::vec3(1,0,0));
model = glm::rotate(model, glm::degrees(rotate_y) , glm::vec3(0,1,0));
model = glm::rotate(model, glm::degrees(rotate_z) , glm::vec3(0,0,1));
model = glm::scale(model, glm::vec3(scale_x,scale_y, scale_z));
return model;
}
void ModelData::scaleDown(GLfloat factor)
{
this->scale_x/=factor;
this->scale_y/=factor;
this->scale_z/=factor;
}
void ModelData::scaleUP(GLfloat factor)
{
this->scale_x*=factor;
this->scale_y*=factor;
this->scale_z*=factor;
}
void ModelData::IncrementLoc(int Index,float Increment)
{
this->Position[Index]+=Increment;
Correction(Index,Increment);
}
void ModelData::UpDateLoc(int Index,float Increment)
{
this->Position[Index]=Increment;
Correction(Index,Increment);
}
void ModelData::Correction(int Index,float Increment)
{
if(Index==0)
{
if(this->Position[Index]>70)
this->Position[Index]=70;
if(this->Position[Index]<-70)
this->Position[Index]=-70;
}
else if(Index==1)
{
if(this->Position[Index]>48)
this->Position[Index]=48;
if(this->Position[Index]<-10)
this->Position[Index]=-10;
}
else if(Index==2)
{
if(this->Position[Index]>-100)
this->Position[Index]=-100;
if(this->Position[Index]<-380)
this->Position[Index]=-380;
}
}