-
Notifications
You must be signed in to change notification settings - Fork 1
/
objviewer.c
141 lines (124 loc) · 3.43 KB
/
objviewer.c
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
// Small example loading and drawing a WaveFront Object File using this library
#include <stdio.h>
#include <math.h>
#include <gc.h>
#include <GL/glew.h>
#include <GL/glut.h>
#include "fsim/object.h"
#include "fsim/program.h"
#include "fsim/vertex_array_object.h"
#include "fsim/projection.h"
#include "fsim/parser.h"
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif
int width = 320;
int height = 240;
float yaw = 0;
float pitch = 0;
float distance = 200;
float scale = 1.0;
float level = 0;
program_t *program;
list_t *lists;
void transform(void)
{
float sin_yaw = sin(yaw * M_PI / 180);
float cos_yaw = cos(yaw * M_PI / 180);
float yaw_columns[4][4] = {{cos_yaw, 0, sin_yaw, 0}, {0, 1, 0, 0}, {-sin_yaw, 0, cos_yaw, 0}, {0, 0, 0, 1}};
glUniformMatrix4fv(glGetUniformLocation(program->program, "yaw"), 1, GL_FALSE, &yaw_columns[0][0]);
float sin_pitch = sin(pitch * M_PI / 180);
float cos_pitch = cos(pitch * M_PI / 180);
float pitch_columns[4][4] = {{1, 0, 0, 0}, {0, cos_pitch, -sin_pitch, 0}, {0, sin_pitch, cos_pitch, 0}, {0, 0, 0, 1}};
glUniformMatrix4fv(glGetUniformLocation(program->program, "pitch"), 1, GL_FALSE, &pitch_columns[0][0]);
float translation_columns[4][4] = {{1, 0, 0, 0}, {0, 1, 0, 0}, {0, 0, 1, 0}, {0, level * scale, -distance * scale, 1}};
glUniformMatrix4fv(glGetUniformLocation(program->program, "translation"), 1, GL_FALSE, &translation_columns[0][0]);
}
void light() {
float vector[] = {0.37139068f, 0.74278135f, 0.55708601f};
glUniform3fv(glGetUniformLocation(program->program, "ray"), 1, &vector[0]);
}
void onResize(int w, int h)
{
width = w; height = h;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}
void onDisplay(void)
{
uniform_matrix(program, "projection", projection(width, height, 0.1, 10000, 60.0));
glUseProgram(program->program);
transform();
light();
glClearColor(0.2f, 0.2f, 0.5f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
int i;
for (i=0; i<lists->size; i++)
render(get_pointer(lists)[i]);
glutSwapBuffers();
}
void onKey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT:
yaw -= 2;
break;
case GLUT_KEY_RIGHT:
yaw += 2;
break;
case GLUT_KEY_UP:
pitch -= 2;
break;
case GLUT_KEY_DOWN:
pitch += 2;
break;
case GLUT_KEY_PAGE_UP:
distance += 3;
break;
case GLUT_KEY_PAGE_DOWN:
distance -= 3;
break;
case GLUT_KEY_HOME:
level -= 3;
break;
case GLUT_KEY_END:
level += 3;
break;
default:
return;
};
glutPostRedisplay();
}
int main(int argc, char **argv)
{
if (argc < 3) {
fprintf(stderr, "Syntax: objviewer <object file> ... <scale>\n");
return 1;
};
scale = atof(argv[argc - 1]);
GC_INIT();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowSize(width, height);
glutCreateWindow("objviewer");
glewExperimental = GL_TRUE;
glewInit();
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE_ARB);
program = make_program("vertex.glsl", "fragment.glsl");
lists = make_list();
int i;
for (i=1; i<argc-1; i++) {
object_t *object = parse_file(argv[i]);
if (!object)
fprintf(stderr, "Error reading object file %s\n", argv[1]);
else {
list_t *list = make_vertex_array_object_list(program, object);
append_pointer(lists, list);
};
};
glutDisplayFunc(onDisplay);
glutReshapeFunc(onResize);
glutSpecialFunc(onKey);
glutMainLoop();
return 0;
}