| 1 |
/************************************************************************* |
| 2 |
* Copyright (c) 2011 AT&T Intellectual Property |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* https://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
| 8 |
* Contributors: Details at https://graphviz.org |
| 9 |
*************************************************************************/ |
| 10 |
|
| 11 |
#include "config.h" |
| 12 |
|
| 13 |
#include <glcomp/glutils.h> |
| 14 |
#include <stdbool.h> |
| 15 |
#include <stdint.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
#include <glcomp/glcompdefs.h> |
| 19 |
|
| 20 |
/*transforms 2d windows location to 3d gl coords but depth is calculated unlike the previous function*/ |
| 21 |
void GetOGLPosRef(int x, int y, float *X, float *Y) { |
| 22 |
double wwinZ; |
| 23 |
double posX, posY; |
| 24 |
|
| 25 |
int32_t viewport[4]; |
| 26 |
double modelview[16]; |
| 27 |
double projection[16]; |
| 28 |
glGetDoublev(GL_MODELVIEW_MATRIX, modelview); |
| 29 |
glGetDoublev(GL_PROJECTION_MATRIX, projection); |
| 30 |
glGetIntegerv(GL_VIEWPORT, viewport); |
| 31 |
|
| 32 |
// draw a point to an unimportant location to get window coordinates |
| 33 |
glColor4f(0.0f, 0.0f, 0.0f, 0.001f); |
| 34 |
|
| 35 |
glBegin(GL_POINTS); |
| 36 |
glVertex3f(-100.0f, -100.0f, 0.0f); |
| 37 |
glEnd(); |
| 38 |
gluProject(-100.0, -100.0, 0.00, modelview, projection, viewport, |
| 39 |
&(double){0}, &(double){0}, &wwinZ); |
| 40 |
const double winX = x; |
| 41 |
const double winY = (double)viewport[3] - y; |
| 42 |
gluUnProject(winX, winY, wwinZ, modelview, projection, viewport, &posX, |
| 43 |
&posY, &(double){0}); // ignore Z that the caller does not need |
| 44 |
|
| 45 |
*X = (float) posX; |
| 46 |
*Y = (float) posY; |
| 47 |
} |
| 48 |
|
| 49 |
double GetOGLDistance(double l) { |
| 50 |
double wwinZ; |
| 51 |
double posX; |
| 52 |
double posXX; |
| 53 |
|
| 54 |
int32_t viewport[4]; |
| 55 |
double modelview[16]; |
| 56 |
double projection[16]; |
| 57 |
|
| 58 |
glGetDoublev(GL_MODELVIEW_MATRIX, modelview); |
| 59 |
glGetDoublev(GL_PROJECTION_MATRIX, projection); |
| 60 |
glGetIntegerv(GL_VIEWPORT, viewport); |
| 61 |
|
| 62 |
// draw a point to an unimportant location to get window coordinates |
| 63 |
glColor4f(0.0f, 0.0f, 0.0f, 0.001f); |
| 64 |
|
| 65 |
glBegin(GL_POINTS); |
| 66 |
glVertex3f(10.0f, 10.0f, 1.0f); |
| 67 |
glEnd(); |
| 68 |
gluProject(10.0, 10.0, 1.0, modelview, projection, viewport, &(double){0}, |
| 69 |
&(double){0}, &wwinZ); |
| 70 |
double x = 50.0; |
| 71 |
const double y = 50.0; |
| 72 |
double winX = x; |
| 73 |
double winY = viewport[3] - y; |
| 74 |
gluUnProject(winX, winY, wwinZ, modelview, projection, viewport, &posX, |
| 75 |
&(double){0}, &(double){0}); |
| 76 |
x += l; |
| 77 |
winX = x; |
| 78 |
winY = viewport[3] - y; |
| 79 |
gluUnProject(winX, winY, wwinZ, modelview, projection, viewport, |
| 80 |
&posXX, &(double){0}, &(double){0}); |
| 81 |
return posXX - posX; |
| 82 |
} |
| 83 |
|
| 84 |
/* |
| 85 |
functions def: returns opengl coordinates of first hit object by using screen coordinates |
| 86 |
x,y; 2D screen coordinates (usually received from mouse events |
| 87 |
X,Y,Z; pointers to coordinates values to be calculated |
| 88 |
return value: no return value |
| 89 |
|
| 90 |
|
| 91 |
*/ |
| 92 |
|
| 93 |
void to3D(int x, int y, float *X, float *Y, float *Z) { |
| 94 |
int const WIDTH = 20; |
| 95 |
|
| 96 |
int32_t viewport[4]; |
| 97 |
double modelview[16]; |
| 98 |
double projection[16]; |
| 99 |
float winZ[400]; |
| 100 |
double posX, posY, posZ; |
| 101 |
int idx; |
| 102 |
|
| 103 |
glGetDoublev(GL_MODELVIEW_MATRIX, modelview); |
| 104 |
glGetDoublev(GL_PROJECTION_MATRIX, projection); |
| 105 |
glGetIntegerv(GL_VIEWPORT, viewport); |
| 106 |
|
| 107 |
const double winX = x; |
| 108 |
const double winY = (double)viewport[3] - y; |
| 109 |
|
| 110 |
glReadPixels(x - WIDTH / 2, (int) winY - WIDTH / 2, WIDTH, WIDTH, |
| 111 |
GL_DEPTH_COMPONENT, GL_FLOAT, &winZ); |
| 112 |
float comp = -9999999; |
| 113 |
for (idx = 0; idx < WIDTH * WIDTH; idx++) { |
| 114 |
if (winZ[idx] > comp && winZ[idx] < 1) |
| 115 |
comp = winZ[idx]; |
| 116 |
} |
| 117 |
|
| 118 |
gluUnProject(winX, winY, comp, modelview, projection, viewport, &posX, |
| 119 |
&posY, &posZ); |
| 120 |
|
| 121 |
*X = (float)posX; |
| 122 |
*Y = (float)posY; |
| 123 |
*Z = (float)posZ; |
| 124 |
} |
| 125 |
|
| 126 |
#include <math.h> |
| 127 |
|
| 128 |
static glCompPoint sub(glCompPoint p, glCompPoint q) |
| 129 |
{ |
| 130 |
return (glCompPoint){.x = p.x - q.x, .y = p.y - q.y, .z = p.z - q.z}; |
| 131 |
} |
| 132 |
|
| 133 |
static double dot(glCompPoint p, glCompPoint q) |
| 134 |
{ |
| 135 |
return p.x * q.x + p.y * q.y + p.z * q.z; |
| 136 |
} |
| 137 |
|
| 138 |
static double len(glCompPoint p) |
| 139 |
{ |
| 140 |
return sqrt(dot(p, p)); |
| 141 |
} |
| 142 |
|
| 143 |
static glCompPoint blend(glCompPoint p, glCompPoint q, float m) |
| 144 |
{ |
| 145 |
glCompPoint r; |
| 146 |
|
| 147 |
r.x = p.x + m * (q.x - p.x); |
| 148 |
r.y = p.y + m * (q.y - p.y); |
| 149 |
r.z = p.z + m * (q.z - p.z); |
| 150 |
return r; |
| 151 |
} |
| 152 |
|
| 153 |
static double dist(glCompPoint p, glCompPoint q) |
| 154 |
{ |
| 155 |
return len(sub(p, q)); |
| 156 |
} |
| 157 |
|
| 158 |
/* |
| 159 |
* Given a line segment determined by two points a and b, and a 3rd point p, |
| 160 |
* return the distance between the point and the segment. |
| 161 |
* If the perpendicular from p to the line a-b is outside of the segment, |
| 162 |
* return the distance to the closer of a or b. |
| 163 |
*/ |
| 164 |
double point_to_lineseg_dist(glCompPoint p, glCompPoint a, glCompPoint b) |
| 165 |
{ |
| 166 |
float U; |
| 167 |
glCompPoint q; |
| 168 |
glCompPoint ba = sub(b, a); |
| 169 |
glCompPoint pa = sub(p, a); |
| 170 |
|
| 171 |
U = (float) (dot(pa, ba) / dot(ba, ba)); |
| 172 |
|
| 173 |
if (U > 1) |
| 174 |
q = b; |
| 175 |
else if (U < 0) |
| 176 |
q = a; |
| 177 |
else |
| 178 |
q = blend(a, b, U); |
| 179 |
|
| 180 |
return dist(p, q); |
| 181 |
} |
| 182 |
|
| 183 |
void glCompCalcWidget(glCompCommon * parent, glCompCommon * child, |
| 184 |
glCompCommon * ref) |
| 185 |
{ |
| 186 |
/*check alignments first , alignments overrides anchors */ |
| 187 |
float borderWidth; |
| 188 |
ref->height = child->height; |
| 189 |
ref->width = child->width; |
| 190 |
if(!parent) |
| 191 |
{ |
| 192 |
child->refPos.x = child->pos.x; |
| 193 |
child->refPos.y = child->pos.y; |
| 194 |
return; |
| 195 |
} |
| 196 |
borderWidth = parent->borderWidth; |
| 197 |
if (child->align != glAlignNone) //if alignment, make sure width and height is no greater than parent |
| 198 |
{ |
| 199 |
if (child->width > parent->width) |
| 200 |
ref->width = parent->width - 2.0f *borderWidth; |
| 201 |
if (child->height > parent->height) |
| 202 |
ref->height = parent->height - 2.0f *borderWidth;; |
| 203 |
|
| 204 |
} |
| 205 |
|
| 206 |
ref->pos.x = parent->refPos.x + ref->pos.x + borderWidth; |
| 207 |
ref->pos.y = parent->refPos.y + ref->pos.y + borderWidth; |
| 208 |
|
| 209 |
|
| 210 |
switch (child->align) { |
| 211 |
case glAlignLeft: |
| 212 |
ref->pos.x = parent->refPos.x + borderWidth; |
| 213 |
ref->pos.y = parent->refPos.y + borderWidth; |
| 214 |
ref->height = parent->height - 2.0f * borderWidth; |
| 215 |
break; |
| 216 |
case glAlignRight: |
| 217 |
ref->pos.x = |
| 218 |
parent->refPos.x + parent->width - child->width - borderWidth; |
| 219 |
ref->pos.y = parent->refPos.y + borderWidth; |
| 220 |
ref->height = parent->height - 2.0f * borderWidth; |
| 221 |
break; |
| 222 |
|
| 223 |
case glAlignTop: |
| 224 |
ref->pos.y = |
| 225 |
parent->refPos.y + parent->height - child->height - |
| 226 |
borderWidth; |
| 227 |
ref->pos.x = parent->refPos.x; |
| 228 |
ref->width = parent->width - 2.0f * borderWidth; |
| 229 |
break; |
| 230 |
|
| 231 |
case glAlignBottom: |
| 232 |
ref->pos.y = parent->refPos.y + borderWidth; |
| 233 |
ref->pos.x = parent->refPos.x + borderWidth; |
| 234 |
ref->width = parent->width - 2.0f * borderWidth; |
| 235 |
break; |
| 236 |
case glAlignParent: |
| 237 |
ref->pos.y = parent->refPos.y + borderWidth; |
| 238 |
ref->pos.x = parent->refPos.x + borderWidth;; |
| 239 |
ref->width = parent->width - 2.0f * borderWidth;; |
| 240 |
ref->height = parent->height - 2.0f * borderWidth; |
| 241 |
break; |
| 242 |
case glAlignCenter: |
| 243 |
case glAlignNone: |
| 244 |
break; |
| 245 |
} |
| 246 |
if (child->align == glAlignNone) // No alignment, check anchors |
| 247 |
{ |
| 248 |
ref->pos.x = parent->refPos.x + child->pos.x + borderWidth; |
| 249 |
ref->pos.y = parent->refPos.y + child->pos.y + borderWidth; |
| 250 |
|
| 251 |
if (child->anchor.leftAnchor) |
| 252 |
ref->pos.x = |
| 253 |
parent->refPos.x + child->anchor.left + borderWidth; |
| 254 |
if (child->anchor.bottomAnchor) |
| 255 |
ref->pos.y = |
| 256 |
parent->refPos.y + child->anchor.bottom + borderWidth; |
| 257 |
|
| 258 |
if (child->anchor.topAnchor) |
| 259 |
ref->height = |
| 260 |
parent->refPos.y + parent->height - ref->pos.y - |
| 261 |
child->anchor.top - borderWidth; |
| 262 |
if (child->anchor.rightAnchor) |
| 263 |
ref->width = |
| 264 |
parent->refPos.x + parent->width - ref->pos.x - |
| 265 |
child->anchor.right - borderWidth; |
| 266 |
} |
| 267 |
child->refPos.x = ref->pos.x; |
| 268 |
child->refPos.y = ref->pos.y; |
| 269 |
child->width = ref->width; |
| 270 |
child->height = ref->height; |
| 271 |
} |
| 272 |
|
| 273 |
static void glCompQuadVertex(glCompPoint p0, glCompPoint p1, glCompPoint p2, |
| 274 |
glCompPoint p3) { |
| 275 |
glVertex3f(p0.x, p0.y, p0.z); |
| 276 |
glVertex3f(p1.x, p1.y, p1.z); |
| 277 |
glVertex3f(p2.x, p2.y, p2.z); |
| 278 |
glVertex3f(p3.x, p3.y, p3.z); |
| 279 |
} |
| 280 |
|
| 281 |
void glCompSetColor(glCompColor c) { glColor4d(c.R, c.G, c.B, c.A); } |
| 282 |
|
| 283 |
void glCompDrawRectangle(glCompRect * r) |
| 284 |
{ |
| 285 |
glBegin(GL_QUADS); |
| 286 |
glVertex3f(r->pos.x, r->pos.y, r->pos.z); |
| 287 |
glVertex3f(r->pos.x + r->w, r->pos.y, r->pos.z); |
| 288 |
glVertex3f(r->pos.x + r->w, r->pos.y + r->h, r->pos.z); |
| 289 |
glVertex3f(r->pos.x, r->pos.y + r->h, r->pos.z); |
| 290 |
glEnd(); |
| 291 |
} |
| 292 |
|
| 293 |
void glCompDrawRectPrism(glCompPoint p, float w, float h, float b, |
| 294 |
glCompColor c, bool bumped) { |
| 295 |
const float d = 0.01f; |
| 296 |
float color_fac = 1.0f / 1.3f; |
| 297 |
float dim = 1; |
| 298 |
if (!bumped) { |
| 299 |
color_fac = 1.3f; |
| 300 |
b -= 2; |
| 301 |
dim = 0.5; |
| 302 |
} |
| 303 |
|
| 304 |
const glCompPoint A = {.x = p.x, .y = p.y, .z = p.z }; |
| 305 |
const glCompPoint B = {.x = p.x + w, .y = p.y, .z = p.z }; |
| 306 |
const glCompPoint C = {.x = p.x + w, .y = p.y + h, .z = p.z }; |
| 307 |
const glCompPoint D = {.x = p.x, .y = p.y + h, .z = p.z }; |
| 308 |
const glCompPoint G = {.x = p.x + b, .y = p.y + b, .z = p.z + d}; |
| 309 |
const glCompPoint H = {.x = p.x + w - b, .y = p.y + b, .z = p.z + d}; |
| 310 |
const glCompPoint E = {.x = p.x + b, .y = p.y + h - b, .z = p.z + d}; |
| 311 |
const glCompPoint F = {.x = p.x + w - b, .y = p.y + h - b, .z = p.z + d}; |
| 312 |
glBegin(GL_QUADS); |
| 313 |
glColor4d(c.R * dim, c.G * dim, c.B * dim, c.A); |
| 314 |
glCompQuadVertex(G, H, F, E); |
| 315 |
|
| 316 |
glColor4d(c.R * color_fac * dim, c.G * color_fac * dim, |
| 317 |
c.B * color_fac * dim, c.A); |
| 318 |
glCompQuadVertex(A, B, H, G); |
| 319 |
glCompQuadVertex(B, H, F, C); |
| 320 |
|
| 321 |
glColor4d(c.R / color_fac * dim, c.G / color_fac * dim, |
| 322 |
c.B / color_fac * dim, c.A); |
| 323 |
glCompQuadVertex(A, G, E, D); |
| 324 |
glCompQuadVertex(E, F, C, D); |
| 325 |
glEnd(); |
| 326 |
} |
| 327 |
|
| 328 |
double distBetweenPts(glCompPoint A, glCompPoint B, double R) { |
| 329 |
double rv = (A.x - B.x) * (A.x - B.x) + (A.y - B.y) * (A.y - B.y) + (A.z - B.z) * (A.z - B.z); |
| 330 |
rv = sqrt(rv); |
| 331 |
if (rv <=R) |
| 332 |
return 0; |
| 333 |
return rv; |
| 334 |
} |
| 335 |
|
| 336 |
int is_point_in_rectangle(float X, float Y, float RX, float RY, float RW,float RH) |
| 337 |
{ |
| 338 |
if (X >= RX && X <= RX + RW && Y >= RY && Y <= RY + RH) |
| 339 |
return 1; |
| 340 |
else |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* @dir lib/glcomp |
| 346 |
* @brief OpenGL GUI for cmd/smyrna |
| 347 |
*/ |