-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmain_simple.cpp
More file actions
156 lines (144 loc) · 4.74 KB
/
main_simple.cpp
File metadata and controls
156 lines (144 loc) · 4.74 KB
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
#include <fstream>
#include <math.h>
#include <time.h>
#include <vector>
#include <GL/freeglut.h>
#include <GL/glext.h>
#include "../Library/loadpng.h"
#include "../Library/process_image.h"
#include "../Library/gl_texture.h"
#define WIDTH 600
#define HEIGHT 600
class Model {
private:
class Face {
public:
int edge;
int *vertices;
int *texcoords;
int normal;
Face(int edge, int *vertices, int *texcoords, int normal = -1) {
this->edge = edge;
this->vertices = vertices;
this->texcoords = texcoords;
this->normal = normal;
}
};
std::vector<float *> vertices;
std::vector<float *> texcoords;
std::vector<float *> normals;
std::vector<Face> faces;
GLuint list;
public:
void load(const char *filename) {
std::string line;
std::vector<std::string> lines;
std::ifstream in(filename);
if (!in.is_open()) {
printf("Cannot load model %s\n", filename);
return;
}
while (!in.eof()) {
std::getline(in, line);
lines.push_back(line);
}
in.close();
float a, b, c;
for (std::string &line : lines) {
if (line[0] == 'v') {
if (line[1] == ' ') {
sscanf(line.c_str(), "v %f %f %f", &a, &b, &c);
vertices.push_back(new float[3]{a, b, c});
} else if (line[1] == 't') {
sscanf(line.c_str(), "vt %f %f", &a, &b);
texcoords.push_back(new float[2]{a, b});
} else {
sscanf(line.c_str(), "vn %f %f %f", &a, &b, &c);
normals.push_back(new float[3]{a, b, c});
}
} else if (line[0] == 'f') {
int v0, v1, v2, t0, t1, t2, n;
sscanf(line.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v0, &t0, &n, &v1, &t1, &n, &v2, &t2, &n);
int *v = new int[3]{v0 - 1, v1 - 1, v2 - 1};
faces.push_back(Face(3, v, NULL, n - 1));
}
}
list = glGenLists(1);
glNewList(list, GL_COMPILE);
for (Face &face : faces) {
if (face.normal != -1)
glNormal3fv(normals[face.normal]);
else
glDisable(GL_LIGHTING);
glBegin(GL_POLYGON);
for (int i = 0; i < face.edge; i++)
glVertex3fv(vertices[face.vertices[i]]);
glEnd();
if (face.normal == -1)
glEnable(GL_LIGHTING);
}
glEndList();
printf("Model: %s\n", filename);
printf("Vertices: %d\n", vertices.size());
printf("Texcoords: %d\n", texcoords.size());
printf("Normals: %d\n", normals.size());
printf("Faces: %d\n", faces.size());
for (float *f : vertices)
delete f;
vertices.clear();
for (float *f : texcoords)
delete f;
texcoords.clear();
for (float *f : normals)
delete f;
normals.clear();
faces.clear();
}
void draw() { glCallList(list); }
};
Model model;
void init() {
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat light_pos[] = {-1.0f, 10.0f, 100.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.0, 1.0, 1.0, 2000.0);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LINE_SMOOTH);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
model.load("Models/Lowpoly_Fox.obj");
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, -30.0f, -500.0f);
glRotatef(30.0f, 1.0f, -1.0f, 0.0f);
model.draw();
glutSwapBuffers();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
glEnable(GL_MULTISAMPLE);
glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
glutSetOption(GLUT_MULTISAMPLE, 8);
int POS_X = (glutGet(GLUT_SCREEN_WIDTH) - WIDTH) >> 1;
int POS_Y = (glutGet(GLUT_SCREEN_HEIGHT) - HEIGHT) >> 1;
glutInitWindowPosition(POS_X, POS_Y);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("Load Model");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}