Some small fixes and add cube.obj

This commit is contained in:
Skia 2016-05-24 01:07:36 +02:00
parent 3eb68fccbd
commit 478d36f657
4 changed files with 85 additions and 11 deletions

View file

@ -5,13 +5,12 @@
*/
#include <iostream>
#include <GL/glut.h>
#include <GL/gl.h>
#include <model.h>
#include <camera.h>
#include <vectors.h>
#include <GL/freeglut.h>
#include <GL/gl.h>
Model m;
Camera c;
@ -37,28 +36,35 @@ void display()
glLoadIdentity();
c.updateCamera();
glTranslatef(0, -0.5, 1);
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glScalef(0.05, 0.05, 0.05);
glTranslatef(0, -0.2, 1);
glRotatef(-90, 1.0f, 0.0f, 0.0f);
glRotatef(angle, 0.0f, 0.0f, 1.0f);
glScalef(0.5, 0.5, 0.5);
glBegin(GL_TRIANGLES);
unsigned int size = m.getVertexList().size();
glColor3f(1.0, 0.0, 0.0);
glBegin(GL_TRIANGLES); // TODO maybe move that to some glDrawArray for optimization?
unsigned int size = m.getFaceList().size();
for (unsigned int i = 0; i < size; i++)
{
Vec3 v = m.getVertexList()[i];
glColor3f(1.0, 0.0, (float)i/size);
Face f = m.getFaceList()[i];
Vec3 v = m.getVertexList()[f.getV1().getVertexIndex()];
glVertex3f(v.x, v.y, v.z);
v = m.getVertexList()[f.getV2().getVertexIndex()];
glVertex3f(v.x, v.y, v.z);
v = m.getVertexList()[f.getV3().getVertexIndex()];
glVertex3f(v.x, v.y, v.z);
// cout << "Face: " << f << "\n";
}
glEnd();
glutSwapBuffers();
angle += 0.2;
angle += 0.9;
}
/* Timer to refresh every 15ms */
void timer(int value) {
glutPostRedisplay();
glutTimerFunc(15, timer, 0);
glutTimerFunc(50, timer, 0);
}
/* Resize the window */

View file

@ -23,6 +23,7 @@ class FaceVertex
return os << "(" << v.v[0] << ", " << v.v[1] << ", " << v.v[2] << ")";
}
friend std::istream& operator>>(std::istream& is, FaceVertex& obj);
int getVertexIndex() { return v[0]; }
};
@ -36,6 +37,9 @@ class Face
friend std::ostream& operator<<(std::ostream& os, const Face& f) {
return os << f.v1 << f.v2 << f.v3;
}
FaceVertex getV1() { return v1; }
FaceVertex getV2() { return v2; }
FaceVertex getV3() { return v3; }
};
class Model
@ -44,6 +48,9 @@ class Model
void loadFile(const char *filename);
void saveModel();
void printVertex();
std::vector<Face> getFaceList() {
return this->face_list;
}
std::vector<Vec3> getVertexList() {
return this->vertex_list;
}

13
res/cube.mtl Normal file
View file

@ -0,0 +1,13 @@
newmtl cube
Ns 10.0000
Ni 1.5000
d 1.0000
Tr 0.0000
Tf 1.0000 1.0000 1.0000
illum 2
Ka 0.0000 0.0000 0.0000
Kd 0.5880 0.5880 0.5880
Ks 0.0000 0.0000 0.0000
Ke 0.0000 0.0000 0.0000
map_Ka cube.png
map_Kd cube.png

48
res/cube.obj Normal file
View file

@ -0,0 +1,48 @@
# cube.obj
#
o cube
mtllib cube.mtl
v -0.500000 -0.500000 0.500000
v 0.500000 -0.500000 0.500000
v -0.500000 0.500000 0.500000
v 0.500000 0.500000 0.500000
v -0.500000 0.500000 -0.500000
v 0.500000 0.500000 -0.500000
v -0.500000 -0.500000 -0.500000
v 0.500000 -0.500000 -0.500000
vt 0.000000 0.000000
vt 1.000000 0.000000
vt 0.000000 1.000000
vt 1.000000 1.000000
vn 0.000000 0.000000 1.000000
vn 0.000000 1.000000 0.000000
vn 0.000000 0.000000 -1.000000
vn 0.000000 -1.000000 0.000000
vn 1.000000 0.000000 0.000000
vn -1.000000 0.000000 0.000000
g cube
usemtl cube
s 1
f 1/1/1 2/2/1 3/3/1
f 3/3/1 2/2/1 4/4/1
s 2
f 3/1/2 4/2/2 5/3/2
f 5/3/2 4/2/2 6/4/2
s 3
f 5/4/3 6/3/3 7/2/3
f 7/2/3 6/3/3 8/1/3
s 4
f 7/1/4 8/2/4 1/3/4
f 1/3/4 8/2/4 2/4/4
s 5
f 2/1/5 8/2/5 4/3/5
f 4/3/5 8/2/5 6/4/5
s 6
f 7/1/6 1/2/6 5/3/6
f 5/3/6 1/2/6 3/4/6