summaryrefslogtreecommitdiff
path: root/tests/gtk_gl.cc
blob: 0423612b3d7854a59b1942498e64c565ba5211fc (plain)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// -*- C++ -*-
/*
 * simple.cc:
 * Simple gtkglextmm example.
 *
 * written by Naofumi Yasufuku  <naofumi@users.sourceforge.net>
 */

#include <iostream>
#include <cstdlib>

#include <gtkmm.h>

#include <gtkglmm.h>

#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif

#include <GL/gl.h>
#include <GL/glu.h>


//
// OpenGL frame buffer configuration utilities.
//

struct GLConfigUtil
{
	static void print_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig,
			const char* attrib_str,
			int attrib,
			bool is_boolean);

	static void examine_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig);
};

//
// Print a configuration attribute.
//
void GLConfigUtil::print_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig,
		const char* attrib_str,
		int attrib,
		bool is_boolean)
{
	int value;

	if (glconfig->get_attrib(attrib, value))
	{
		std::cout << attrib_str << " = ";
		if (is_boolean)
			std::cout << (value == true ? "true" : "false") << std::endl;
		else
			std::cout << value << std::endl;
	}
	else
	{
		std::cout << "*** Cannot get "
			<< attrib_str
			<< " attribute value\n";
	}
}

//
// Print configuration attributes.
//
void GLConfigUtil::examine_gl_attrib(const Glib::RefPtr<const Gdk::GL::Config>& glconfig)
{
	std::cout << "\nOpenGL visual configurations :\n\n";

	std::cout << "glconfig->is_rgba() = "
		<< (glconfig->is_rgba() ? "true" : "false")
		<< std::endl;
	std::cout << "glconfig->is_double_buffered() = "
		<< (glconfig->is_double_buffered() ? "true" : "false")
		<< std::endl;
	std::cout << "glconfig->is_stereo() = "
		<< (glconfig->is_stereo() ? "true" : "false")
		<< std::endl;
	std::cout << "glconfig->has_alpha() = "
		<< (glconfig->has_alpha() ? "true" : "false")
		<< std::endl;
	std::cout << "glconfig->has_depth_buffer() = "
		<< (glconfig->has_depth_buffer() ? "true" : "false")
		<< std::endl;
	std::cout << "glconfig->has_stencil_buffer() = "
		<< (glconfig->has_stencil_buffer() ? "true" : "false")
		<< std::endl;
	std::cout << "glconfig->has_accum_buffer() = "
		<< (glconfig->has_accum_buffer() ? "true" : "false")
		<< std::endl;

	std::cout << std::endl;

	print_gl_attrib(glconfig, "Gdk::GL::USE_GL",           Gdk::GL::USE_GL,           true);
	print_gl_attrib(glconfig, "Gdk::GL::BUFFER_SIZE",      Gdk::GL::BUFFER_SIZE,      false);
	print_gl_attrib(glconfig, "Gdk::GL::LEVEL",            Gdk::GL::LEVEL,            false);
	print_gl_attrib(glconfig, "Gdk::GL::RGBA",             Gdk::GL::RGBA,             true);
	print_gl_attrib(glconfig, "Gdk::GL::DOUBLEBUFFER",     Gdk::GL::DOUBLEBUFFER,     true);
	print_gl_attrib(glconfig, "Gdk::GL::STEREO",           Gdk::GL::STEREO,           true);
	print_gl_attrib(glconfig, "Gdk::GL::AUX_BUFFERS",      Gdk::GL::AUX_BUFFERS,      false);
	print_gl_attrib(glconfig, "Gdk::GL::RED_SIZE",         Gdk::GL::RED_SIZE,         false);
	print_gl_attrib(glconfig, "Gdk::GL::GREEN_SIZE",       Gdk::GL::GREEN_SIZE,       false);
	print_gl_attrib(glconfig, "Gdk::GL::BLUE_SIZE",        Gdk::GL::BLUE_SIZE,        false);
	print_gl_attrib(glconfig, "Gdk::GL::ALPHA_SIZE",       Gdk::GL::ALPHA_SIZE,       false);
	print_gl_attrib(glconfig, "Gdk::GL::DEPTH_SIZE",       Gdk::GL::DEPTH_SIZE,       false);
	print_gl_attrib(glconfig, "Gdk::GL::STENCIL_SIZE",     Gdk::GL::STENCIL_SIZE,     false);
	print_gl_attrib(glconfig, "Gdk::GL::ACCUM_RED_SIZE",   Gdk::GL::ACCUM_RED_SIZE,   false);
	print_gl_attrib(glconfig, "Gdk::GL::ACCUM_GREEN_SIZE", Gdk::GL::ACCUM_GREEN_SIZE, false);
	print_gl_attrib(glconfig, "Gdk::GL::ACCUM_BLUE_SIZE",  Gdk::GL::ACCUM_BLUE_SIZE,  false);
	print_gl_attrib(glconfig, "Gdk::GL::ACCUM_ALPHA_SIZE", Gdk::GL::ACCUM_ALPHA_SIZE, false);

	std::cout << std::endl;
}


//
// Simple OpenGL scene.
//

class SimpleGLScene : public Gtk::DrawingArea,
	public Gtk::GL::Widget<SimpleGLScene>
{
	public:
		SimpleGLScene();
		virtual ~SimpleGLScene();

	protected:
		virtual void on_realize();
		virtual bool on_configure_event(GdkEventConfigure* event);
		virtual bool on_expose_event(GdkEventExpose* event);

};

SimpleGLScene::SimpleGLScene()
{
	//
	// Configure OpenGL-capable visual.
	//

	Glib::RefPtr<Gdk::GL::Config> glconfig;

	// Try double-buffered visual
	glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB    |
			Gdk::GL::MODE_DEPTH  |
			Gdk::GL::MODE_DOUBLE);
	if (!glconfig)
	{
		std::cerr << "*** Cannot find the double-buffered visual.\n"
			<< "*** Trying single-buffered visual.\n";

		// Try single-buffered visual
		glconfig = Gdk::GL::Config::create(Gdk::GL::MODE_RGB   |
				Gdk::GL::MODE_DEPTH);
		if (!glconfig)
		{
			std::cerr << "*** Cannot find any OpenGL-capable visual.\n";
			std::exit(1);
		}
	}

	// print frame buffer attributes.
	GLConfigUtil::examine_gl_attrib(glconfig);

	//
	// Set OpenGL-capability to the widget.
	//

	set_gl_capability(glconfig);
}

SimpleGLScene::~SimpleGLScene()
{
}

void SimpleGLScene::on_realize()
{
	// We need to call the base on_realize()
	Gtk::DrawingArea::on_realize();

	//
	// Get GL::Window.
	//

	Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

	//
	// GL calls.
	//

	// *** OpenGL BEGIN ***
	if (!glwindow->gl_begin(get_gl_context()))
		return;

	GLUquadricObj* qobj = gluNewQuadric();
	gluQuadricDrawStyle(qobj, GLU_FILL);
	glNewList(1, GL_COMPILE);
	gluSphere(qobj, 1.0, 20, 20);
	glEndList();

	static GLfloat light_diffuse[] = {1.0, 0.0, 0.0, 1.0};
	static GLfloat light_position[] = {1.0, 1.0, 1.0, 0.0};
	glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glEnable(GL_DEPTH_TEST);

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClearDepth(1.0);

	glViewport(0, 0, get_width(), get_height());

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(40.0, 1.0, 1.0, 10.0);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, 3.0,
			0.0, 0.0, 0.0,
			0.0, 1.0, 0.0);
	glTranslatef(0.0, 0.0, -3.0);

	glwindow->gl_end();
	// *** OpenGL END ***
}

bool SimpleGLScene::on_configure_event(GdkEventConfigure* event)
{
	//
	// Get GL::Window.
	//

	Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

	//
	// GL calls.
	//

	// *** OpenGL BEGIN ***
	if (!glwindow->gl_begin(get_gl_context()))
		return false;

	glViewport(0, 0, get_width(), get_height());

	glwindow->gl_end();
	// *** OpenGL END ***

	return true;
}

bool SimpleGLScene::on_expose_event(GdkEventExpose* event)
{
	//
	// Get GL::Window.
	//

	Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();

	//
	// GL calls.
	//

	// *** OpenGL BEGIN ***
	if (!glwindow->gl_begin(get_gl_context()))
		return false;

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glCallList(1);

	// Swap buffers.
	if (glwindow->is_double_buffered())
		glwindow->swap_buffers();
	else
		glFlush();

	glwindow->gl_end();
	// *** OpenGL END ***

	return true;
}


//
// The application class.
//

class Simple : public Gtk::Window
{
	public:
		Simple();
		virtual ~Simple();

	protected:
		// signal handlers:
		void on_button_quit_clicked();

	protected:
		// member widgets:
		Gtk::VBox m_VBox;
		SimpleGLScene m_SimpleGLScene;
		Gtk::Button m_ButtonQuit;
};

Simple::Simple()
	: m_VBox(false, 0), m_ButtonQuit("Quit")
{
	//
	// Top-level window.
	//

	set_title("Simple");

	// Get automatically redrawn if any of their children changed allocation.
	set_reallocate_redraws(true);

	add(m_VBox);

	//
	// Simple OpenGL scene.
	//

	m_SimpleGLScene.set_size_request(200, 200);

	m_VBox.pack_start(m_SimpleGLScene);

	//
	// Simple quit button.
	//

	m_ButtonQuit.signal_clicked().connect(
			sigc::mem_fun(*this, &Simple::on_button_quit_clicked));

	m_VBox.pack_start(m_ButtonQuit, Gtk::PACK_SHRINK, 0);

	//
	// Show window.
	//

	show_all();
}

Simple::~Simple()
{}

void Simple::on_button_quit_clicked()
{
	Gtk::Main::quit();
}


//
// Main.
//

int main(int argc, char** argv)
{
	Gtk::Main kit(argc, argv);

	//
	// Init gtkglextmm.
	//

	Gtk::GL::init(argc, argv);

	//
	// Query OpenGL extension version.
	//

	int major, minor;
	Gdk::GL::query_version(major, minor);
	std::cout << "OpenGL extension version - "
		<< major << "." << minor << std::endl;

	//
	// Instantiate and run the application.
	//

	Simple simple;

	kit.run(simple);

	return 0;
}