23 #include <grass/config.h>
25 #if defined(OPENGL_X11)
29 #elif defined(OPENGL_AQUA)
30 #include <OpenGL/gl.h>
31 #include <OpenGL/glu.h>
32 #if defined(OPENGL_AGL)
35 #elif defined(OPENGL_WINDOWS)
41 #include <grass/gis.h>
42 #include <grass/ogsf.h>
43 #include <grass/glocale.h>
45 #define USE_GL_NORMALIZE
47 #define RED_MASK 0x000000FF
48 #define GRN_MASK 0x0000FF00
49 #define BLU_MASK 0x00FF0000
50 #define ALP_MASK 0xFF000000
52 #define INT_TO_RED(i, r) (r = (i & RED_MASK))
53 #define INT_TO_GRN(i, g) (g = (i & GRN_MASK) >> 8)
54 #define INT_TO_BLU(i, b) (b = (i & BLU_MASK) >> 16)
55 #define INT_TO_ALP(i, a) (a = (i & ALP_MASK) >> 24)
64 static int numobjs = 0;
68 static float ogl_light_amb[MAX_LIGHTS][4];
69 static float ogl_light_diff[MAX_LIGHTS][4];
70 static float ogl_light_spec[MAX_LIGHTS][4];
71 static float ogl_light_pos[MAX_LIGHTS][4];
72 static float ogl_mat_amb[4];
73 static float ogl_mat_diff[4];
74 static float ogl_mat_spec[4];
75 static float ogl_mat_emis[4];
76 static float ogl_mat_shin;
102 glDisable(GL_COLOR_MATERIAL);
103 glDisable(GL_LIGHTING);
108 glColorMaterial(GL_FRONT_AND_BACK, GL_EMISSION);
109 glEnable(GL_COLOR_MATERIAL);
110 glEnable(GL_LIGHTING);
115 glColorMaterial(GL_FRONT, GL_DIFFUSE);
116 glEnable(GL_COLOR_MATERIAL);
117 glEnable(GL_LIGHTING);
122 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
123 glEnable(GL_COLOR_MATERIAL);
124 glEnable(GL_LIGHTING);
134 glDisable(GL_COLOR_MATERIAL);
135 glEnable(GL_LIGHTING);
140 glDisable(GL_COLOR_MATERIAL);
154 glGetIntegerv(GL_COLOR_MATERIAL_PARAMETER, &mat);
168 GLUquadricObj *qobj = gluNewQuadric();
170 gluQuadricDrawStyle(qobj, GLU_SILHOUETTE);
172 glTranslatef(
x, y, 0.);
173 gluDisk(qobj, 0., rad, 32, 1);
175 gluDeleteQuadric(qobj);
188 GLUquadricObj *qobj = gluNewQuadric();
190 gluQuadricDrawStyle(qobj, GLU_FILL);
192 glTranslatef(
x, y, z);
193 gluDisk(qobj, 0., rad, 32, 1);
195 gluDeleteQuadric(qobj);
208 static int first = 1;
209 static GLUquadricObj *QOsphere;
212 QOsphere = gluNewQuadric();
215 gluQuadricNormals(QOsphere, GLU_SMOOTH);
216 gluQuadricTexture(QOsphere, GL_FALSE);
217 gluQuadricOrientation(QOsphere, GLU_OUTSIDE);
218 gluQuadricDrawStyle(QOsphere, GLU_FILL);
225 glTranslatef(center[0], center[1], center[2]);
226 gluSphere(QOsphere, (
double)siz, 24, 24);
243 glDepthMask((GLboolean) (n));
256 (n) ? glEnable(GL_CULL_FACE) : glDisable(GL_CULL_FACE);
268 glLineWidth((GLfloat) (n));
278 glBegin(GL_QUAD_STRIP);
298 glBegin(GL_TRIANGLE_STRIP);
318 glBegin(GL_TRIANGLE_STRIP);
338 glBegin(GL_TRIANGLE_FAN);
399 glBegin(GL_LINE_STRIP);
423 glShadeModel(GL_SMOOTH);
426 glShadeModel(GL_FLAT);
447 #if !defined(OPENGL_FBO)
449 glDrawBuffer(GL_FRONT_AND_BACK);
459 #if !defined(OPENGL_FBO)
461 glDrawBuffer(GL_FRONT);
471 #if !defined(OPENGL_FBO)
473 glDrawBuffer(GL_BACK);
483 #if !defined(OPENGL_FBO)
486 #if defined(OPENGL_X11)
487 glXSwapBuffers(glXGetCurrentDisplay(), glXGetCurrentDrawable());
488 #elif defined(OPENGL_AQUA)
489 aglSwapBuffers(aglGetCurrentContext());
490 #elif defined(OPENGL_WINDOWS)
491 SwapBuffers(wglGetCurrentDC());
526 glScalef(xs, ys, zs);
540 glTranslatef(dx, dy, dz);
559 glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
560 glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
561 glGetIntegerv(GL_VIEWPORT, viewport);
564 window[0] = viewport[1] + viewport[3] +
border;
565 window[1] = viewport[1] -
border;
566 window[2] = viewport[0] -
border;
567 window[3] = viewport[0] + viewport[2] +
border;
588 double modelMatrix[16],
double projMatrix[16])
592 gluProject((GLdouble) pt[
X], (GLdouble) pt[
Y], (GLdouble) pt[Z],
593 modelMatrix, projMatrix, viewport, &fx, &fy, &fz);
595 if (fx < window[2] || fx > window[3]
596 || fy < window[1] || fy > window[0])
642 G_warning(_(
"gsd_rot(): %c is an invalid axis "
643 "specification. Rotation ignored. "
644 "Please advise GRASS developers of this error"), axis);
648 glRotatef((GLfloat) angle,
x, y, z);
714 glColor4ub(
r,
g,
b, a);
725 glEnable(GL_LIGHTING);
728 #ifdef USE_GL_NORMALIZE
730 glEnable(GL_NORMALIZE);
743 ogl_mat_amb[0] = 0.1;
744 ogl_mat_amb[1] = 0.1;
745 ogl_mat_amb[2] = 0.1;
746 ogl_mat_amb[3] = 1.0;
748 ogl_mat_diff[0] = 0.8;
749 ogl_mat_diff[1] = 0.8;
750 ogl_mat_diff[2] = 0.8;
751 ogl_mat_diff[3] = 0.8;
753 ogl_mat_spec[0] = 0.8;
754 ogl_mat_spec[1] = 0.8;
755 ogl_mat_spec[2] = 0.8;
756 ogl_mat_spec[3] = 0.8;
758 ogl_mat_emis[0] = 0.0;
759 ogl_mat_emis[1] = 0.0;
760 ogl_mat_emis[2] = 0.0;
761 ogl_mat_emis[3] = 0.0;
781 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ogl_mat_amb);
782 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, ogl_mat_diff);
783 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ogl_mat_spec);
784 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, ogl_mat_emis);
785 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, ogl_mat_shin);
810 ogl_mat_spec[0] = sh;
811 ogl_mat_spec[1] = sh;
812 ogl_mat_spec[2] = sh;
813 ogl_mat_spec[3] = sh;
815 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, ogl_mat_spec);
817 ogl_mat_shin = 60. + (int)(sh * 68.);
819 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, ogl_mat_shin);
823 ogl_mat_emis[0] = (em * (emcolor & 0x0000FF)) / 255.;
824 ogl_mat_emis[1] = (em * ((emcolor & 0x00FF00) >> 8)) / 255.;
825 ogl_mat_emis[2] = (em * ((emcolor & 0xFF0000) >> 16)) / 255.;
827 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, ogl_mat_emis);
841 if (num > 0 && num <= MAX_LIGHTS) {
842 ogl_light_pos[num - 1][0] = vals->position[
X];
843 ogl_light_pos[num - 1][1] = vals->position[
Y];
844 ogl_light_pos[num - 1][2] = vals->position[Z];
845 ogl_light_pos[num - 1][3] = vals->position[W];
847 glLightfv(GL_LIGHT0 + num, GL_POSITION, ogl_light_pos[num - 1]);
849 ogl_light_diff[num - 1][0] = vals->color[0];
850 ogl_light_diff[num - 1][1] = vals->color[1];
851 ogl_light_diff[num - 1][2] = vals->color[2];
852 ogl_light_diff[num - 1][3] = .3;
854 glLightfv(GL_LIGHT0 + num, GL_DIFFUSE, ogl_light_diff[num - 1]);
856 ogl_light_amb[num - 1][0] = vals->ambient[0];
857 ogl_light_amb[num - 1][1] = vals->ambient[1];
858 ogl_light_amb[num - 1][2] = vals->ambient[2];
859 ogl_light_amb[num - 1][3] = .3;
861 glLightfv(GL_LIGHT0 + num, GL_AMBIENT, ogl_light_amb[num - 1]);
863 ogl_light_spec[num - 1][0] = vals->color[0];
864 ogl_light_spec[num - 1][1] = vals->color[1];
865 ogl_light_spec[num - 1][2] = vals->color[2];
866 ogl_light_spec[num - 1][3] = .3;
868 glLightfv(GL_LIGHT0 + num, GL_SPECULAR, ogl_light_spec[num - 1]);
884 defin = on ? num : 0;
887 glEnable(GL_LIGHT0 + num);
890 glDisable(GL_LIGHT0 + num);
916 glGetIntegerv(GL_VIEWPORT, tmp);
918 r = tmp[0] + tmp[2] - 1;
920 t = tmp[1] + tmp[3] - 1;
925 if (!*xsize || !*ysize)
928 *pixbuf = (
unsigned char *)G_malloc((*xsize) * (*ysize) * 4);
933 #if !defined(OPENGL_FBO)
934 glReadBuffer(GL_FRONT);
938 glReadPixels(
l,
b, (
r) - (
l) + 1, (
t) - (
b) + 1, GL_RGBA,
939 GL_UNSIGNED_BYTE, *pixbuf);
956 glGetIntegerv(GL_VIEWPORT, tmp);
957 glGetIntegerv(GL_MAX_VIEWPORT_DIMS, num);
976 *pixbuf = (
unsigned char *)G_malloc(xsize * ysize * 4);
981 #if !defined(OPENGL_FBO)
983 glReadBuffer(GL_FRONT);
987 glReadPixels(0, 0, xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, *pixbuf);
1000 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
1003 glDisable(GL_BLEND);
1004 glBlendFunc(GL_ONE, GL_ZERO);
1021 if (glIsEnabled(GL_CLIP_PLANE0 + (num))) {
1025 glClipPlane(GL_CLIP_PLANE0 + (num), params);
1028 glEnable(GL_CLIP_PLANE0 + (num));
1031 glDisable(GL_CLIP_PLANE0 + (num));
1047 glEnable(GL_CLIP_PLANE0 + (num));
1050 glDisable(GL_CLIP_PLANE0 + (num));
1082 glViewport(
l,
b,
r,
t);
1113 ObjList[i] = ObjList[0] + i;
1131 glNewList(ObjList[listno], GL_COMPILE_AND_EXECUTE);
1134 glNewList(ObjList[listno], GL_COMPILE);
1162 glDeleteLists(ObjList[i], 1);
1178 glCallList(ObjList[listno]);
1195 glCallList(ObjList[i]);