-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path5-2.cpp
More file actions
106 lines (87 loc) · 3.42 KB
/
Copy path5-2.cpp
File metadata and controls
106 lines (87 loc) · 3.42 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
#include <GL/glut.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
struct Vertex {
float x, y, z;
};
struct Face {
int v1, v2, v3;
};
std::vector<Vertex> vertices;
std::vector<Face> faces;
void load_obj(const char *filename) {
std::ifstream file(filename);
if (!file) {
std::cerr << "Error opening OBJ file: " << filename << std::endl;
exit(1);
}
std::string line;
while (std::getline(file, line)) {
if (line.substr(0, 2) == "v ") {
std::istringstream s(line.substr(2));
Vertex v;
s >> v.x; s >> v.y; s >> v.z;
vertices.push_back(v);
} else if (line.substr(0, 2) == "f ") {
std::istringstream s(line.substr(2));
Face f;
s >> f.v1; s >> f.v2; s >> f.v3;
faces.push_back(f);
}
}
file.close();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
glLoadIdentity();
gluLookAt(0, 0, 0.5, 0, 0, 0, 0, 1, 0);
glBegin(GL_TRIANGLES);
for (const Face &face : faces) {
const Vertex &v1 = vertices[face.v1 - 1];
const Vertex &v2 = vertices[face.v2 - 1];
const Vertex &v3 = vertices[face.v3 - 1];
const float nx = (v2.y - v1.y) * (v3.z - v1.z) - (v2.z - v1.z) * (v3.y - v1.y);
const float ny = (v2.z - v1.z) * (v3.x - v1.x) - (v2.x - v1.x) * (v3.z - v1.z);
const float nz = (v2.x - v1.x) * (v3.y - v1.y) - (v2.y - v1.y) * (v3.x - v1.x);
const float len = std::sqrt(nx * nx + ny * ny + nz * nz);
const float nnx = nx / len;
const float nny = ny / len;
const float nnz = nz / len;
glNormal3f(nnx, nny, nnz);
glVertex3f(v1.x, v1.y, v1.z);
glVertex3f(v2.x, v2.y, v2.z);
glVertex3f(v3.x, v3.y, v3.z);
}
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float) w / (float) h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char *argv[]) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Bunny OBJ Viewer");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
load_obj("bunny.obj");
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glutMainLoop();
return 0;
}
// 背面カリングを有効にする glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glutMainLoop(); return 0; }