summaryrefslogtreecommitdiff
path: root/src/gpudataviz.cc
blob: bcd498e5644e3450cb71f6d200b823cebc789db3 (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
#include <iostream>

#include "gpudataviz.h"
#include "boring_parts.h"
#include "gtk_win_main.h"
#include "my_GTK_GL_scene.h"

// Macro to make things readable in main() function
#define EXIT_IF_FAIL(val, expr) do { \
	if ( ! (expr) ) { \
		std::cerr << "Init error " << val << std::endl; \
		exit(val); \
	} \
} while(0)

#define CALL_TRACE do { \
	std::cout << "trace " <<__PRETTY_FUNCTION__ << std::endl; \
} while(0)

int main(int argc, char* argv[]) {
	CALL_TRACE;

	int meshWidth=64, meshHeight=64; // TODO : those vars should not be hardcoded

	// Initialize GTK
	Gtk::Main gtkKit(argc, argv); // gtk itself
	Gtk::GL::init(argc, argv);    // gtkglextmm

	// Query and print OpenGL version
  int glVersionMajor, glVersionMinor;
  EXIT_IF_FAIL(1, Gdk::GL::query_version(glVersionMajor, glVersionMinor) );
  std::cout << "OpenGL extension version - " << glVersionMajor << "." << glVersionMinor << std::endl;

	// Initialize OpenGL
	Gdk::GL::ConfigMode glMode = Gdk::GL::MODE_RGB | Gdk::GL::MODE_DEPTH | Gdk::GL::MODE_DOUBLE;
	Glib::RefPtr<Gdk::GL::Config> glconfig;
	EXIT_IF_FAIL(2, glconfig=Gdk::GL::Config::create(glMode) );

	// Initialize OpenCL
	EXIT_IF_FAIL(3, initOpenCL()==0 ); // See boring_parts.cc

	// Initialize host work memory (array for all the vertex coordinates computed by OpenCL and displayed by OpenGL)
	cl_float4 *hostWorkMem = (cl_float4 *) calloc(meshWidth * meshHeight, sizeof(cl_float4));
	EXIT_IF_FAIL(4, hostWorkMem);

	// Initialize the OpenGL scene
	MyGTKGLScene glScene(glconfig);

	// Instantiate and run the GTK app
	GTKWinMain gtkwinmain(glScene);
	gtkKit.run(gtkwinmain);

	return 0;
}

/* MyGTKGLScene implementation
	I want to keep interesting code part in this file
		in natural reading order
*/
MyGTKGLScene::MyGTKGLScene(Glib::RefPtr<Gdk::GL::Config> &glconfig) {
	CALL_TRACE;
	set_gl_capability(glconfig); // Set OpenGL-capability to the widget.
}

MyGTKGLScene::~MyGTKGLScene() {
	CALL_TRACE;
}

void MyGTKGLScene::on_size_request(Gtk::Requisition* requisition) {
	CALL_TRACE;
  
	*requisition = Gtk::Requisition();
  requisition->width = 320; requisition->height = 240;
}

void MyGTKGLScene::on_realize() {
	CALL_TRACE; // This run once at window creation time

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

	// *** OpenGL BEGIN ***
	if (!glwindow->gl_begin(get_gl_context())) {
		std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl;
		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 MyGTKGLScene::on_configure_event(GdkEventConfigure* event) {
	CALL_TRACE ; // This run mainly when GTK GL Widget is resized

	Glib::RefPtr<Gdk::GL::Window> glwindow = get_gl_window();
	// *** OpenGL BEGIN ***
	if (!glwindow->gl_begin(get_gl_context())) {
		std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl;
		return false;
	}
	int width = get_width();
	int height = get_height();
	GLdouble ratio = (GLdouble) width/height;

	glViewport(0, 0, width, height);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(60.0, ratio, 0.1, 10.0);
	glMatrixMode(GL_MODELVIEW);

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

	return true;
}

bool MyGTKGLScene::on_expose_event(GdkEventExpose* event) {
	CALL_TRACE ; // This run mainly when GTK GL Widget have to be redrawn

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

	// *** OpenGL BEGIN ***
	if (!glwindow->gl_begin(get_gl_context())) {
		std::cerr << "Oups : glwindow->gl_begin(get_gl_context())" << std::endl;
		return false;
	}

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glCallList(1);

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

	// Display the rendered image
	glwindow->swap_buffers();

	return true;
}