summaryrefslogtreecommitdiff
path: root/src/draw.c
blob: 97659782867f0c2fe30e746405815f965038d26e (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


#include "program.h"
#include <GL/gl.h>

static void realize(GtkWidget* widget, gpointer data)
{
#if 0
	GdkGLContext* context = gtk_widget_get_gl_context(widget);
	GdkGLDrawable* drawable = gtk_widget_get_gl_drawable(widget);
	(void)data;

	if (!gdk_gl_drawable_gl_begin(drawable, context))
		return;

	gdk_gl_drawable_gl_end(drawable);
#else
	(void)widget;
	(void)data;
#endif
}

static gboolean configure(GtkWidget* widget, GdkEventConfigure* e, gpointer data)
{
#if 0
	GdkGLContext* context = gtk_widget_get_gl_context(widget);
	GdkGLDrawable* drawable = gtk_widget_get_gl_drawable(widget);
	(void)data;
	(void)e;

	if (!gdk_gl_drawable_gl_begin(drawable, context))
		return FALSE;

	gdk_gl_drawable_gl_end (drawable);
#else
	(void)widget;
	(void)data;
	(void)e;
#endif
	return TRUE;
}

static void draw_tri(struct program *p)
{
	glClearColor(0.3, 0.1, 0.3, 0.5);
	glViewport(0, 0, (GLint)p->draw.width, (GLint)p->draw.height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(-1.0, 1.0, -1.0, 1.0, -0.5, 1000.0);
	glMatrixMode(GL_MODELVIEW);

	glClear(GL_COLOR_BUFFER_BIT);

	glBegin(GL_TRIANGLES);
	glColor3f(.8,0,0);
	glVertex3f(-0.9, -0.9, -30.0);
	glColor3f(0,.9,0);
	glVertex3f( 0.9, -0.9, -30.0);
	glColor3f(0,0,.7);
	glVertex3f( 0.0,  0.9, -30.0);
	glEnd();
}

gboolean draw_gl_begin(struct program *p)
{
	GtkWidget *widget = GTK_WIDGET(p->main.draw);
	GdkGLContext* context = gtk_widget_get_gl_context(widget);
	GdkGLDrawable* drawable = gtk_widget_get_gl_drawable(widget);

	return gdk_gl_drawable_gl_begin(drawable, context);
}

void draw_gl_end(struct program *p)
{
	GtkWidget *widget = GTK_WIDGET(p->main.draw);
	GdkGLDrawable* drawable = gtk_widget_get_gl_drawable(widget);

	gdk_gl_drawable_gl_end(drawable);
}

void draw_ortho_top_left(struct program *p)
{
	glViewport(0, 0, (GLint)p->draw.width, (GLint)p->draw.height);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0, p->draw.width, p->draw.height, 0.0, -0.5, 1000.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void draw_checker(guint width, guint height, struct program *p)
{
	int x;
	int y;
	int i;

	glClearColor(0.3, 0.3, 0.3, 0.5);
	glClear(GL_COLOR_BUFFER_BIT);

	glColor3f(.8,.8,.8);
	glBegin(GL_QUADS);
	for (i = 0, x = 0; x < (int)p->draw.width; i++) {
		x = (width * i) - (width / 2);
		y = ((i % 2) * height) - (height / 2);
		for (; y < (int)p->draw.height; y += height * 2) {
			glVertex3f( x +   0.0, y + height, -30.0);
			glVertex3f( x + width, y + height, -30.0);
			glVertex3f( x + width, y +    0.0, -30.0);
			glVertex3f( x +   0.0, y +    0.0, -30.0);
		}
	}
	glEnd();
}

static gboolean expose(GtkWidget* widget, GdkEventExpose* e, gpointer data)
{
	struct program *p = (struct program *)data;
	GdkGLContext* context = gtk_widget_get_gl_context(widget);
	GdkGLDrawable* drawable = gtk_widget_get_gl_drawable(widget);
	(void)e;

	p->draw.width = widget->allocation.width;
	p->draw.height = widget->allocation.height;

	if (!gdk_gl_drawable_gl_begin(drawable, context))
		return FALSE;

	if (p->selected.type == TYPE_TEXTURE)
		texture_draw(p);
	else
		draw_tri(p);

	if (gdk_gl_drawable_is_double_buffered (drawable))
		gdk_gl_drawable_swap_buffers(drawable);
	else
		glFlush();

	gdk_gl_drawable_gl_end(drawable);

	return TRUE;
}

void draw_setup(GtkDrawingArea *draw, struct program *p)
{
	GObject *obj = G_OBJECT(draw);

	gtk_widget_set_gl_capability(GTK_WIDGET(draw), p->draw.config, NULL,
	                             TRUE, GDK_GL_RGBA_TYPE);

	g_signal_connect_after(obj, "realize", G_CALLBACK(realize), p);
	g_signal_connect(obj, "configure-event", G_CALLBACK(configure), p);
	g_signal_connect(obj, "expose-event", G_CALLBACK(expose), p);
}