glamtk
view src/draw.c @ 9:4b1989ff2ef7
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 11 Apr 2011 03:59:03 +0300 |
parents | 9b623dc0f296 |
children |
line source
1 #include <math.h>
3 #ifndef __APPLE__
4 #include <GL/gl.h>
5 #else
6 #include <OpenGL/gl.h>
7 #endif
9 #include "draw.h"
11 struct color {
12 float r, g, b, a;
13 };
16 static void linestart(int x, int y);
17 static void lineto(int x, int y);
18 static void arcto(int x, int y, int cx, int cy, int rad);
19 static void arc(float x0, float y0, float x1, float y1, float cx, float cy, float rad, int subdiv);
22 static struct color fgcolor = {0, 0, 0, 0.5};
23 static struct color bgcolor = {0.4, 0.4, 0.4, 0.5};
25 void imtk_draw_color(float r, float g, float b, float a)
26 {
27 fgcolor.r = r;
28 fgcolor.g = g;
29 fgcolor.b = b;
30 fgcolor.a = a;
31 }
33 void imtk_draw_colorv(float *v)
34 {
35 fgcolor.r = v[0];
36 fgcolor.g = v[1];
37 fgcolor.b = v[2];
38 fgcolor.a = v[3];
39 }
41 void imtk_draw_background(float r, float g, float b, float a)
42 {
43 bgcolor.r = r;
44 bgcolor.g = g;
45 bgcolor.b = b;
46 bgcolor.a = a;
47 }
49 void imtk_draw_backgroundv(float *v)
50 {
51 bgcolor.r = v[0];
52 bgcolor.g = v[1];
53 bgcolor.b = v[2];
54 bgcolor.a = v[3];
55 }
57 void imtk_draw_rect(int x, int y, int w, int h, int rad)
58 {
59 int i;
61 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
62 glEnable(GL_LINE_SMOOTH);
63 glEnable(GL_POLYGON_SMOOTH);
64 glEnable(GL_BLEND);
65 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
66 glLineWidth(1.5);
68 for(i=0; i<2; i++) {
69 if(i == 0) {
70 glBegin(GL_POLYGON);
71 glColor4f(bgcolor.r, bgcolor.g, bgcolor.b, bgcolor.a);
72 } else {
73 glBegin(GL_LINE_STRIP);
74 glColor4f(fgcolor.r, fgcolor.g, fgcolor.b, fgcolor.a);
75 }
76 linestart(x + rad, y);
77 lineto(x + w - rad, y);
78 arcto(x + w, y + rad, x + w - rad, y + rad, rad);
79 lineto(x + w, y + h - rad);
80 arcto(x + w - rad, y + h, x + w - rad, y + h - rad, rad);
81 lineto(x + rad, y + h);
82 arcto(x, y + h - rad, x + rad, y + h - rad, rad);
83 lineto(x, y + rad);
84 arcto(x + rad, y, x + rad, y + rad, rad);
85 glEnd();
86 }
88 glPopAttrib();
89 }
92 static int px, py;
94 static void linestart(int x, int y)
95 {
96 px = x;
97 py = y;
98 glVertex2i(x, y);
99 }
101 static void lineto(int x, int y)
102 {
103 px = x;
104 py = y;
105 glVertex2i(x, y);
106 }
108 static void arcto(int x, int y, int cx, int cy, int rad)
109 {
110 arc(px, py, x, y, cx, cy, rad, 1);
111 }
113 static void arc(float x0, float y0, float x1, float y1, float cx, float cy, float rad, int subdiv)
114 {
115 float x, y, vx, vy, len;
117 x = x0 + (x1 - x0) * 0.5;
118 y = y0 + (y1 - y0) * 0.5;
119 vx = x - cx;
120 vy = y - cy;
121 len = sqrt(vx * vx + vy * vy);
122 x = cx + vx * rad / len;
123 y = cy + vy * rad / len;
125 if(!subdiv) {
126 glVertex2f(x, y);
127 glVertex2f(x1, y1);
128 return;
129 }
131 arc(x0, y0, x, y, cx, cy, rad, subdiv - 1);
132 arc(x, y, x1, y1, cx, cy, rad, subdiv - 1);
133 }