-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
169 lines (127 loc) · 4.74 KB
/
main.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
#include <utils.hh>
#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>
using namespace std;
GLuint vertex_ID;
GLuint texture_ID[2];
int * vertex_count;
GLuint p; //program
GLuint time_anim = 0; //for animation
GLuint toon = 0; //for toon shading
GLuint twotex = 0; //for toon shading
MeshObject mesh_object = MeshObject("bunny/reconstruction/bun_zipper.ply");;
void display_cb(void)
{
#ifdef DEBUG
// printf("display callback called\n");
#endif
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(p);
unsigned int location_projection_matrix = glGetUniformLocation(p,
"projection_matrix");
unsigned int location_modelview_matrix = glGetUniformLocation(p,
"modelview_matrix");
GLfloat mp[16];
GLfloat mv[16];
glGetFloatv (GL_PROJECTION_MATRIX, mp);
glGetFloatv (GL_MODELVIEW_MATRIX, mv);
glUniformMatrix4fv (location_projection_matrix, 1, GL_FALSE, mp);
glUniformMatrix4fv (location_modelview_matrix, 1, GL_FALSE, mv);
//time step for animation
// if (time > 0)
// time++;
glUniform1i(glGetUniformLocation(p, "time"),time_anim);
glUniform1i(glGetUniformLocation(p, "toon"),toon);
glUniform1i(glGetUniformLocation(p, "twotex"),twotex);
//here goes actual drawing code
glFrontFace(GL_CCW);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glEnable(GL_DEPTH_TEST);
//z-buffering
glDepthFunc(GL_LESS);
glDepthRange(0.0, 1.0);
glEnable(GL_DEPTH_TEST);
//vertex array
glBindVertexArray (vertex_ID);
//draw the bunny according to indices and the vertices in the server buffer
glDrawElements(GL_TRIANGLES,
3*mesh_object.amount_of_faces,
GL_UNSIGNED_INT,
mesh_object.faces_indices);
glFlush();
glutSwapBuffers();
}
int main (int argc, char **argv) {
cout << "starting main function\n";
//initialize glut, create a window
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH);
glutInitWindowSize (600, 600);
glutCreateWindow ("OpenGL Test");
//glewinit error reporting
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
fprintf(stdout, "Status: Using GLEW %s\n", glewGetString(GLEW_VERSION));
printf ("Supported OpenGL version: %s\n", glGetString (GL_VERSION));
//connecting the UI interaction function
//setting up the vertex array and then buffers
GLuint vertex_array_object_ID;
unsigned int number_of_arrays = 1;
glGenVertexArrays (number_of_arrays, &vertex_array_object_ID);
glBindVertexArray (vertex_array_object_ID);
vertex_ID = vertex_array_object_ID;
// three buffers: vertex, vertex_normal,, TODO textures and colors
unsigned int number_of_buffers = 3;
unsigned int vertex_buffer_object_ID[number_of_buffers];
glGenBuffers (number_of_buffers, vertex_buffer_object_ID);
//what is this?
//unsigned int index_buffer_object_ID[1];
//glGenBuffers(1, index_buffer_object_ID);
//bind vertices data to the buffer
unsigned int elements_per_vertex = 3;
//unsigned int elements_per_triangle = 3 * elements_per_vertex;
glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer_object_ID[0]);
glBufferData (GL_ARRAY_BUFFER,
elements_per_vertex * mesh_object.amount_of_vertices * sizeof (float),
mesh_object.vertices,
GL_STATIC_DRAW);
//vertex position x y z in the buffer
int vertex_position_location = 0;
glBindAttribLocation(p, vertex_position_location, "vertex_position");
glVertexAttribPointer (vertex_position_location,
elements_per_vertex,
GL_FLOAT,
GL_FALSE,
0,
0);
glEnableVertexAttribArray (vertex_position_location);
//vertex normal buffer
glBindBuffer (GL_ARRAY_BUFFER, vertex_buffer_object_ID[1]);
glBufferData(GL_ARRAY_BUFFER,
mesh_object.amount_of_vertices * 3 * sizeof(float),
mesh_object.vertex_normals,
GL_STATIC_DRAW);
unsigned int location_vertex_normal = 1;
glBindAttribLocation(p, location_vertex_normal, "vertex_normal");
glVertexAttribPointer(location_vertex_normal,
3,
GL_FLOAT,
GL_FALSE,
0,
0);
glEnableVertexAttribArray(location_vertex_normal);
#ifdef DEBUG
printf(" attaching display function and running main loop\n");
#endif
glutDisplayFunc(display_cb);
glutMainLoop ();
return 0;
}