GRASS Programmer's Manual  6.5.svn(2012)-r51648
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines
nviz/draw.c
Go to the documentation of this file.
00001 
00017 #include <grass/nviz.h>
00018 
00019 #ifndef GL_CLAMP_TO_EDGE
00020 #define GL_CLAMP_TO_EDGE 0x812F
00021 #endif 
00022 
00023 static int sort_surfs_max(int *, int *, int *, int);
00024 
00032 int Nviz_draw_all_surf(nv_data * dc)
00033 {
00034     int i, nsurfs;
00035     int sortSurfs[MAX_SURFS], sorti[MAX_SURFS];
00036     int *surf_list;
00037     float x, y, z;
00038     int num, w;
00039 
00040     /* Get position for Light 1 */
00041     num = 1;
00042     x = dc->light[num].x;
00043     y = dc->light[num].y;
00044     z = dc->light[num].z;
00045     w = dc->light[num].z;
00046 
00047     surf_list = GS_get_surf_list(&nsurfs);
00048 
00049     sort_surfs_max(surf_list, sortSurfs, sorti, nsurfs);
00050 
00051     G_free(surf_list);
00052 
00053     /* re-initialize lights */
00054     GS_setlight_position(num, x, y, z, w);
00055     num = 2;
00056     GS_setlight_position(num, 0., 0., 1., 0);
00057 
00058     for (i = 0; i < nsurfs; i++) {
00059         GS_draw_surf(sortSurfs[i]);
00060     }
00061 
00062     /* GS_draw_cplane_fence params will change - surfs aren't used anymore */
00063     for (i = 0; i < MAX_CPLANES; i++) {
00064         if (dc->cp_on[i])
00065             GS_draw_cplane_fence(sortSurfs[0], sortSurfs[1], i);
00066     }
00067 
00068     return 1;
00069 }
00070 
00084 int sort_surfs_max(int *surf, int *id_sort, int *indices, int num)
00085 {
00086     int i, j;
00087     float maxvals[MAX_SURFS];
00088     float tmp, max = 0., tmin, tmax, tmid;
00089 
00090     for (i = 0; i < num; i++) {
00091         GS_get_zextents(surf[i], &tmin, &tmax, &tmid);
00092         if (i == 0)
00093             max = tmax;
00094         else
00095             max = max < tmax ? tmax : max;
00096         maxvals[i] = tmax;
00097     }
00098 
00099     for (i = 0; i < num; i++) {
00100         tmp = maxvals[0];
00101         indices[i] = 0;
00102         for (j = 0; j < num; j++) {
00103             if (maxvals[j] < tmp) {
00104                 tmp = maxvals[j];
00105                 indices[i] = j;
00106             }
00107         }
00108 
00109         maxvals[indices[i]] = max + 1;
00110         id_sort[i] = surf[indices[i]];
00111     }
00112 
00113     return 1;
00114 }
00115 
00121 int Nviz_draw_all_vect()
00122 {
00123     // GS_set_cancel(0);
00124 
00125     /* in case transparency is set */
00126     GS_set_draw(GSD_BOTH);
00127 
00128     GS_ready_draw();
00129 
00130     GV_alldraw_vect();
00131 
00132     GS_done_draw();
00133 
00134     GS_set_draw(GSD_BACK);
00135 
00136     // GS_set_cancel(0);
00137 
00138     return 1;
00139 }
00140 
00146 int Nviz_draw_all_site()
00147 {
00148     int i;
00149     int *site_list, nsites;
00150 
00151     site_list = GP_get_site_list(&nsites);
00152 
00153     /* in case transparency is set */
00154     GS_set_draw(GSD_BOTH);
00155 
00156     GS_ready_draw();
00157 
00158     for (i = 0; i < nsites; i++) {
00159         GP_draw_site(site_list[i]);
00160     }
00161     G_free(site_list);
00162 
00163     GS_done_draw();
00164 
00165     GS_set_draw(GSD_BACK);
00166 
00167     return 1;
00168 }
00169 
00175 int Nviz_draw_all_vol()
00176 {
00177     int *vol_list, nvols, i;
00178 
00179     vol_list = GVL_get_vol_list(&nvols);
00180 
00181     /* in case transparency is set */
00182     GS_set_draw(GSD_BOTH);
00183 
00184     GS_ready_draw();
00185 
00186     for (i = 0; i < nvols; i++) {
00187         GVL_draw_vol(vol_list[i]);
00188     }
00189 
00190     G_free(vol_list);
00191 
00192     GS_done_draw();
00193 
00194     GS_set_draw(GSD_BACK);
00195 
00196     return 1;
00197 }
00198 
00204 int Nviz_draw_all(nv_data * data)
00205 {
00206     int i;
00207     int draw_surf, draw_vect, draw_site, draw_vol;
00208     
00209     draw_surf = 1;
00210     draw_vect = 1;
00211     draw_site = 1;
00212     draw_vol = 1;
00213     /*
00214     draw_north_arrow = 0;
00215     arrow_x = 0;
00216     draw_label = 0;
00217     draw_legend = 0;
00218     draw_fringe = 0;
00219     draw_scalebar = 0;
00220     draw_bar_x = 0;
00221     */
00222     
00223     GS_set_draw(GSD_BACK);      /* needs to be BACK to avoid flickering */
00224 
00225     GS_ready_draw();
00226 
00227     GS_clear(data->bgcolor);
00228 
00229     if (draw_surf)
00230         Nviz_draw_all_surf(data);
00231 
00232     if (draw_vect)
00233         Nviz_draw_all_vect(data);
00234 
00235     if (draw_site)
00236         Nviz_draw_all_site(data);
00237 
00238     if (draw_vol)
00239         Nviz_draw_all_vol(data);
00240         
00241     for(i = 0; i < data->num_fringes; i++) {
00242         struct fringe_data * f = data->fringe[i];
00243         GS_draw_fringe(f->id, f->color, f->elev, f->where);
00244     }
00245 
00246     /* North Arrow */
00247     if (data->draw_arrow) {
00248         gsd_north_arrow(data->arrow->where, data->arrow->size,
00249                         (GLuint)NULL, data->arrow->color, data->arrow->color);
00250     }
00251 
00252     /* scale bar */
00253     for (i = 0; i < data->num_scalebars; i++) {
00254         struct scalebar_data *s = data->scalebar[i];
00255         gsd_scalebar_v2(s->where, s->size, 0, s->color, s->color);
00256     }
00257     
00258     GS_done_draw();
00259     GS_set_draw(GSD_BACK);
00260 
00261     return 1;
00262 }
00263 
00278 int Nviz_draw_quick(nv_data * data, int draw_mode)
00279 {
00280     GS_set_draw(GSD_BACK);
00281     
00282     GS_ready_draw();
00283     
00284     GS_clear(data->bgcolor);
00285     
00286     /* draw surfaces */
00287     if (draw_mode & DRAW_QUICK_SURFACE)
00288         GS_alldraw_wire();
00289     
00290     /* draw vector lines */
00291     if (draw_mode & DRAW_QUICK_VLINES)
00292         GV_alldraw_vect();
00293     
00294     /* draw vector points */
00295     if (draw_mode & DRAW_QUICK_VPOINTS)
00296         GP_alldraw_site();
00297     
00298     /* draw volumes */
00299     if (draw_mode & DRAW_QUICK_VOLUME) {
00300         GVL_alldraw_wire();
00301     }
00302     
00303     GS_done_draw();
00304     
00305     return 1;
00306 }
00307 
00315 int Nviz_load_image(GLubyte *image_data, int width, int height, int alpha)
00316 {
00317     unsigned int texture_id;
00318     int  in_format;
00319     GLenum format;
00320 
00321     if (alpha)
00322     {
00323         in_format = 4;
00324         format = GL_RGBA;
00325     }
00326     else
00327     {
00328         in_format = 3;
00329         format = GL_RGB;
00330     }
00331     glGenTextures( 1, &texture_id);
00332     glBindTexture(GL_TEXTURE_2D, texture_id);
00333     glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00334 
00335     glTexImage2D(GL_TEXTURE_2D, 0, in_format, width, height, 0,format,
00336                  GL_UNSIGNED_BYTE, image_data);
00337 
00338     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
00339     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
00340 
00341     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
00342     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
00343 
00344     glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
00345 
00346     return texture_id;
00347 }
00348 
00354 void Nviz_set_2D(int width, int height)
00355 {
00356     glEnable(GL_BLEND); // images are transparent
00357     glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00358         
00359     glMatrixMode(GL_PROJECTION);
00360     glLoadIdentity();
00361     glOrtho(0, width, 0, height, -1, 1);
00362     
00363     // set coordinate system from upper left corner
00364     glScalef(1, -1, 1);
00365     glTranslatef(0, -height, 0);
00366 
00367     glMatrixMode(GL_MODELVIEW);
00368     glLoadIdentity();
00369 }
00370 
00378 void Nviz_draw_image(int x, int y, int width, int height, int texture_id)
00379 {
00380     glBindTexture(GL_TEXTURE_2D, texture_id);
00381     GS_set_draw(GSD_FRONT);
00382 
00383     glEnable(GL_TEXTURE_2D);
00384 
00385     glBegin(GL_QUADS);
00386 
00387     glTexCoord2d(0.0,1.0);
00388     glVertex2d(x, y);
00389     glTexCoord2d(0.0,0.0);
00390     glVertex2d(x, y + height);
00391     glTexCoord2d(1.0,0.0);
00392     glVertex2d(x + width, y + height);
00393     glTexCoord2d(1.0,1.0);
00394     glVertex2d(x + width, y);
00395 
00396     glEnd();
00397 
00398     GS_done_draw();
00399     glDisable(GL_TEXTURE_2D);
00400 }
00401 
00407 void Nviz_del_texture(int texture_id)
00408 {
00409     GLuint t[1];
00410 
00411     t[0] = texture_id;
00412     glDeleteTextures(1, t);
00413 }
00414 
00419 void Nviz_get_max_texture(int *size)
00420 {
00421     glGetIntegerv(GL_MAX_TEXTURE_SIZE, size);
00422 }