glamtk

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