Subversion Repositories gelsvn

Rev

Rev 555 | Rev 600 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 555 Rev 596
Line 10... Line 10...
10
// - w                              : toggle wireframe on/off
10
// - w                              : toggle wireframe on/off
11
// - t                              : toggle texture on/off
11
// - t                              : toggle texture on/off
12
// - f                              : switch between vertex and face normals
12
// - f                              : switch between vertex and face normals
13
// ----------------------------------------
13
// ----------------------------------------
14
 
14
 
-
 
15
#include <fstream>
15
#include <iostream>
16
#include <iostream>
16
#include <string>
17
#include <string>
17
#include <GL/glew.h>
18
#include <GL/glew.h>
18
#include "Util/ArgExtracter.h"
19
#include "Util/ArgExtracter.h"
19
#include "CGLA/Vec2i.h"
20
#include "CGLA/Vec2i.h"
Line 21... Line 22...
21
#include "GLGraphics/gel_glut.h"
22
#include "GLGraphics/gel_glut.h"
22
#include "GLGraphics/QuatTrackBall.h"
23
#include "GLGraphics/QuatTrackBall.h"
23
#include "GLGraphics/draw.h"
24
#include "GLGraphics/draw.h"
24
#include "Geometry/TriMesh.h"
25
#include "Geometry/TriMesh.h"
25
#include "Geometry/load.h"
26
#include "Geometry/load.h"
-
 
27
#include "Geometry/GridAlgorithm.h"
-
 
28
#include "Geometry/HGrid.h"
26
 
29
 
27
using namespace std;
30
using namespace std;
28
using namespace CGLA;
31
using namespace CGLA;
29
using namespace Geometry;
32
using namespace Geometry;
30
using namespace GLGraphics;
33
using namespace GLGraphics;
Line 85... Line 88...
85
	
88
	
86
	return true;
89
	return true;
87
}
90
}
88
 
91
 
89
 
92
 
90
 
-
 
91
void mouse_motion(int x, int y)
93
void mouse_motion(int x, int y)
92
{
94
{
93
    ball->roll_ball(Vec2i(x,y));
95
    ball->roll_ball(Vec2i(x,y));
94
}
96
}
95
 
97
 
Line 113... Line 115...
113
	ball->do_spin();
115
	ball->do_spin();
114
	glutTimerFunc(spin_timer, spin, 0);  
116
	glutTimerFunc(spin_timer, spin, 0);  
115
	glutPostRedisplay();
117
	glutPostRedisplay();
116
}
118
}
117
 
119
 
-
 
120
void setupshader()
-
 
121
{
-
 
122
	static GLuint vs,fs,prog;
-
 
123
	static bool was_here = false;
-
 
124
	if(!was_here)
-
 
125
	{
-
 
126
		was_here = true;
-
 
127
		const string vss = 
-
 
128
		"varying vec3 n;\n"
-
 
129
		"varying vec3 v;\n"
-
 
130
		"varying vec3 v_obj;\n"
-
 
131
		"\n"
-
 
132
		"void main(void)\n"
-
 
133
		"{\n"
-
 
134
		"	gl_Position = ftransform();\n"
-
 
135
		"   v_obj = gl_Vertex.xyz;\n"
-
 
136
		"	v = vec3(gl_ModelViewMatrix * gl_Vertex);\n"
-
 
137
		"	n = normalize(gl_NormalMatrix * gl_Normal);\n"
-
 
138
		"}\n"
-
 
139
		"\n";
-
 
140
		
-
 
141
		const string fss =
-
 
142
		"varying vec3 n;\n"
-
 
143
		"varying vec3 v;\n"
-
 
144
		"varying vec3 v_obj;\n"
-
 
145
		"\n"
-
 
146
		"vec4 glazed_shader(vec4 mat_col,  vec4 light_col, vec3 light_dir)\n"
-
 
147
		"{\n"
-
 
148
		"	vec3 e = normalize(-v);\n"
-
 
149
		"	vec3 r = normalize(2.0*dot(e, n)*n - e);\n"
-
 
150
		"	float d = max(0.05,dot(light_dir, n));\n"
-
 
151
		"	vec4 diff = mat_col * light_col *d; 	\n"
-
 
152
		"	vec4 refl = smoothstep(0.7,0.75,dot(r,light_dir)) * light_col;\n"
-
 
153
		"	return 0.1*refl + 2.25*diff;\n"
-
 
154
		"}\n"
-
 
155
		"\n"
-
 
156
		"void main(void)\n"
-
 
157
		"{\n"
-
 
158
		"	vec4 mat_col = vec4(0.7,0.6,1.0,1.0);\n"
-
 
159
		"	\n"
-
 
160
		"	vec3 light0_dir = vec3(0.0,1.0,0.0);\n"
-
 
161
		"	vec4 light0_col = vec4(0.9,0.95,0.95,1.0);\n"
-
 
162
		"	\n"
-
 
163
		"	vec3 light1_dir = vec3(0.0,0.0,1.0);\n"
-
 
164
		"	vec4 light1_col = vec4(.8,.8,.6,1.0);\n"
-
 
165
		"	\n"
-
 
166
		"	gl_FragColor = \n"
-
 
167
		"	0.5*glazed_shader(mat_col, light0_col, light0_dir)+\n"
-
 
168
		"	0.5*glazed_shader(mat_col, light1_col, light1_dir);\n"
-
 
169
		"	\n"
-
 
170
		"	gl_FragColor.a = 1.0;\n"
-
 
171
		"}\n";
-
 
172
		
-
 
173
		vs = create_glsl_shader(GL_VERTEX_SHADER, vss);
-
 
174
		print_glsl_program_log(vs);
-
 
175
		
-
 
176
		fs = create_glsl_shader(GL_FRAGMENT_SHADER, fss);
-
 
177
		print_glsl_program_log(fs);
-
 
178
		
-
 
179
		prog = glCreateProgram();
-
 
180
		
-
 
181
		if(vs) glAttachShader(prog, vs);
-
 
182
		if(fs) glAttachShader(prog, fs);
-
 
183
		
-
 
184
		glLinkProgram(prog);
-
 
185
		print_glsl_program_log(prog);
-
 
186
	}
-
 
187
	glUseProgram(prog);
-
 
188
	
-
 
189
}
-
 
190
 
118
void display()
191
void display()
119
{
192
{
120
	static unsigned int l;
193
	static unsigned int l;
121
    if(redo_display_list)
194
    if(redo_display_list)
122
    {
195
    {
Line 129... Line 202...
129
		glutTimerFunc(spin_timer, spin, 0);	
202
		glutTimerFunc(spin_timer, spin, 0);	
130
	}
203
	}
131
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
204
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
132
    glLoadIdentity();
205
    glLoadIdentity();
133
    ball->set_gl_modelview();
206
    ball->set_gl_modelview();
134
	
-
 
135
	if(do_wireframe)
207
	if(do_wireframe)
136
	{
208
	{
137
		if(GLEW_EXT_geometry_shader4)
209
		if(GLEW_EXT_geometry_shader4)
138
			draw_triangles_in_wireframe(mesh, per_vertex_normals, Vec3f(1,0,0));
210
			draw_triangles_in_wireframe(mesh, per_vertex_normals, Vec3f(1,0,0));
139
		else
211
		else
140
			draw_wireframe_oldfashioned(mesh, per_vertex_normals, Vec3f(1,0,0));
212
			draw_wireframe_oldfashioned(mesh, per_vertex_normals, Vec3f(1,0,0));
141
	}
213
	}
-
 
214
	else if(!do_textures)
-
 
215
	{
-
 
216
		setupshader();	
-
 
217
		glCallList(l);
-
 
218
	}
142
	else
219
	else
143
		glCallList(l);
220
		glCallList(l);
144
		
221
	
145
    glutSwapBuffers();
222
    glutSwapBuffers();
146
}
223
}
147
 
224
 
148
void keyboard(unsigned char key, int x, int y)
225
void keyboard(unsigned char key, int x, int y)
149
{
226
{
150
    switch(key)
227
    switch(key)
151
    {
228
    {
152
		case '\033': exit(0); break;
229
		case '\033': exit(0); break;
153
		case 'w': do_wireframe = !do_wireframe; break;
230
		case 'w': do_wireframe = !do_wireframe; break;
154
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
231
		case 'f': per_vertex_normals = !per_vertex_normals; redo_display_list = true; break;
-
 
232
		case 's': 
-
 
233
		{
-
 
234
			ofstream f("ball.out", ios::binary);
-
 
235
			if(f) f.write(reinterpret_cast<const char*>(ball),sizeof(QuatTrackBall));
-
 
236
		}
-
 
237
		case 'l':
-
 
238
		{
-
 
239
			ifstream f("ball.out", ios::binary);
-
 
240
			if(f) f.read(reinterpret_cast<char*>(ball),sizeof(QuatTrackBall));
-
 
241
		}
-
 
242
		case 't': do_textures = !do_textures;
155
        break;
243
		break;
156
    }
244
    }
157
	redo_display_list=true;
245
	redo_display_list=true;
158
}
246
}
159
 
247
 
160
int main(int argc, char** argv)
248
int main(int argc, char** argv)
161
{
249
{
162
	Util::ArgExtracter ae(argc, argv);
250
	Util::ArgExtracter ae(argc, argv);
163
	
251
	
164
	bool redo_normals = ae.extract("-n");
252
	bool redo_normals = ae.extract("-n");
165
 
253
	
166
    // GLUT INIT
254
    // GLUT INIT
167
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
255
    glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
168
    glutInitWindowSize(win_size_x, win_size_y);
256
    glutInitWindowSize(win_size_x, win_size_y);
169
    glutInit(&argc, argv);
257
    glutInit(&argc, argv);
170
    main_window = glutCreateWindow("OBJ Viewer");
258
    main_window = glutCreateWindow("OBJ Viewer");
Line 209... Line 297...
209
	// Setup projection
297
	// Setup projection
210
	glMatrixMode(GL_PROJECTION);
298
	glMatrixMode(GL_PROJECTION);
211
	glLoadIdentity();
299
	glLoadIdentity();
212
	gluPerspective(53,1.0f,r/100.0,r*3.0);
300
	gluPerspective(53,1.0f,r/100.0,r*3.0);
213
	glMatrixMode(GL_MODELVIEW);
301
	glMatrixMode(GL_MODELVIEW);
214
 
302
	
215
	// Pass control to GLUT
303
	// Pass control to GLUT
216
	glutMainLoop();
304
	glutMainLoop();
217
	
305
	
218
	return 0;
306
	return 0;
219
}
307
}