Changeset 5267


Ignore:
Timestamp:
May 2, 2008, 12:13:55 PM (16 years ago)
Author:
jack
Message:

AnugaVis?: compile the data into openGL display lists.

Location:
anuga_work/development/anugavis/src
Files:
8 edited
2 moved

Legend:

Unmodified
Added
Removed
  • anuga_work/development/anugavis/src/Makefile.am

    r5263 r5267  
    1515                        netcdf_util.c \
    1616                        netcdf_util.h \
    17                         xmalloc.c \
    18                         xmalloc.h
     17                        xfunctions.c \
     18                        xfunctions.h
    1919libanugavis_a_CFLAGS = @SDL_CFLAGS@ @GLU_CFLAGS@
    2020libanugavis_a_LIBADD = @LIBOBJS@
  • anuga_work/development/anugavis/src/anugavis_simple.c

    r5264 r5267  
    1414    return 1;
    1515  }
     16  printf("%d\n", anugavis.number_of_timesteps);
    1617  if(AnugaVis_DefineHeightQuantity("elevation", 0, 1, 128, 128, 128) == -1){
    1718    printf("AnugaVis_DefineHeightQuantity() Error: %s\n", AnugaVis_GetError());
  • anuga_work/development/anugavis/src/error.c

    r5260 r5267  
     1#ifdef HAVE_CONFIG_H
     2#  include "config.h"
     3#endif
     4
     5#ifdef HAVE_GL_GLU_H
     6#  include <GL/glu.h>
     7#elif HAVE_OPENGL_GLU_H
     8#  include <OpenGL/glu.h>
     9#endif
    110#include <stdarg.h>
    211#include <netcdf.h>
     
    413#include "error.h"
    514
    6 #define MAX_ERROR_LEN 100
     15#define MAX_ERROR_LEN 200
    716
    817static char error[MAX_ERROR_LEN];
     
    2736  AnugaVis_SetError("NetCDF Error: %s", nc_strerror(ncerr));
    2837}
     38
     39void AnugaVis_OpenGLError(GLenum errorCode){
     40  AnugaVis_SetError("OpenGL Error: %s", gluErrorString(errorCode));
     41}
  • anuga_work/development/anugavis/src/error.h

    r5259 r5267  
    11#ifndef ANUGAVIS_ERROR_H
    22#define ANUGAVIS_ERROR_H
     3
     4#ifdef HAVE_CONFIG_H
     5#  include "config.h"
     6#endif
     7
     8#ifdef HAVE_GL_GL_H
     9#  include <GL/gl.h>
     10#elif HAVE_OPENGL_GL_H
     11#  include <OpenGL/gl.h>
     12#endif
    313
    414/* Set the error message with a printf-style format string.
    515 */
    616void AnugaVis_SetError(const char *format, ...);
    7 
    817/* Report a SDL error.
    918 */
    1019void AnugaVis_SDLError(void);
    11 
    1220/* Report a NetCDF error.
    1321 */
    1422void AnugaVis_NetCDFError(int ncerr);
     23/* Report an OpenGL error.
     24 */
     25void AnugaVis_OpenGLError(GLenum errorCode);
    1526
    1627/* Get detailed error information. The returned string should not be
  • anuga_work/development/anugavis/src/globals.h

    r5264 r5267  
    88  SDL_Surface *screen;
    99  int netcdfId;
     10  /* These are populated from the NetCDF file */
    1011  size_t number_of_points;
    1112  size_t number_of_vertices;
    1213  size_t number_of_volumes;
     14  size_t number_of_timesteps;
    1315  float *x;
    1416  float *y;
  • anuga_work/development/anugavis/src/height_quantity.c

    r5261 r5267  
    99#  include <string.h>
    1010#endif
     11#ifdef HAVE_GL_GL_H
     12#  include <GL/gl.h>
     13#elif HAVE_OPENGL_GL_H
     14#  include <OpenGL/gl.h>
     15#endif
    1116#include <netcdf.h>
    1217#include <SDL.h>
     
    1419#include "globals.h"
    1520#include "height_quantity.h"
    16 #include "xmalloc.h"
     21#include "netcdf_util.h"
     22#include "xfunctions.h"
     23
     24static int compileDisplayLists(struct height_quantity_simple *height){
     25  int frame;
     26  unsigned int vol;
     27  unsigned int vertex;
     28  int vertIndex;
     29  int x[3];
     30  int y[3];
     31  int z[3];
     32  float *heights = xmalloc(sizeof(float)
     33                           * anugavis.number_of_points
     34                           * height->frames, "compileDisplayLists()");
     35  if(heights == NULL)
     36    return -1;
     37  if(nc_get_var_float_by_name(anugavis.netcdfId,
     38                              height->name, heights) != NC_NOERR){
     39    free(heights);
     40    return -1;
     41  }
     42
     43  for(frame = 0 ; frame < height->frames ; frame++){
     44    glNewList(height->displayLists + frame, GL_COMPILE);
     45    for(vol = 0 ; vol < anugavis.number_of_volumes ; vol++){
     46      glBegin(GL_POLYGON);
     47      for(vertex = 0 ; vertex < 3 ; vertex++){ /* Calculate normal */
     48        vertIndex = anugavis.volumes[vol*anugavis.number_of_vertices + vertex];
     49        x[vertex] = anugavis.x[vertIndex];
     50        y[vertex] = anugavis.y[vertIndex];
     51        z[vertex] = heights[frame * anugavis.number_of_timesteps + vertIndex];
     52      }
     53      glNormal3f((y[1] - y[0]) * (z[2] - z[0]) - (z[1] - z[0]) * (y[2] - y[0]),
     54                 (z[1] - z[0]) * (x[2] - x[0]) - (x[1] - x[0]) * (z[2] - z[0]),
     55                 (x[1] - x[0]) * (y[2] - y[0]) - (y[1] - y[0]) * (x[2] - x[0]));
     56      for(vertex = 0 ; vertex < anugavis.number_of_vertices ; vertex++){
     57        vertIndex = anugavis.volumes[vol*anugavis.number_of_vertices + vertex];
     58        glVertex3f(anugavis.x[vertIndex], anugavis.y[vertIndex],
     59                   heights[frame * anugavis.number_of_timesteps + vertIndex]);
     60      }
     61      glEnd();
     62    }
     63    glEndList();
     64  }
     65
     66  free(heights);
     67  return 0;
     68}
     69
     70static void freeHeightQuantity(struct height_quantity_simple *height){
     71  if(height->name != NULL) free(height->name);
     72  if(glIsList(height->displayLists) == GL_TRUE)
     73    glDeleteLists(height->displayLists, height->frames);
     74  free(height);
     75}
    1776
    1877int AnugaVis_DefineHeightQuantity(const char *name,
     
    2382  int ncvarid;
    2483  int numdims;
     84  GLenum glerror;
    2585  /* Check that it's actually in the NetCDF file */
    2686  if((ncstatus = nc_inq_varid(anugavis.netcdfId, name, &ncvarid)) != NC_NOERR){
     
    3797                         "AnugaVis_DefineHeightQuantity()")) == NULL)
    3898      return -1;
     99
     100    height->name = NULL;
     101    height->displayLists = -1;
     102
     103    if((height->name = xstrdup(name,
     104                               "AnugaVis_DefineHeightQuantity()")) == NULL){
     105      freeHeightQuantity(height);
     106      return -1;
     107    }
     108
     109    /* If the NetCDF variable is 2-dimensional, then it's dynamic. */
     110    if((ncstatus = nc_inq_varndims(anugavis.netcdfId,
     111                                   ncvarid, &numdims)) != NC_NOERR){
     112      AnugaVis_NetCDFError(ncstatus);
     113      freeHeightQuantity(height);
     114      return -1;
     115    }
     116
     117    height->frames = (numdims == 2) ? anugavis.number_of_timesteps : 1;
     118    height->displayLists = glGenLists(height->frames);
     119    if((glerror = glGetError()) != GL_NO_ERROR){
     120      AnugaVis_OpenGLError(glerror);
     121      freeHeightQuantity(height);
     122      return -1;
     123    }
     124
     125    if(compileDisplayLists(height) == -1){
     126      freeHeightQuantity(height);
     127      return -1;
     128    }
     129
    39130    height->next = anugavis.heights;
    40131    anugavis.heights = height;
    41132  }
    42  
    43   /* If the NetCDF variable is 2-dimensional, then it's dynamic. */
    44   if((ncstatus = nc_inq_varndims(anugavis.netcdfId,
    45                                  ncvarid, &numdims)) != NC_NOERR){
    46     AnugaVis_NetCDFError(ncstatus);
    47     return -1;
    48   }
    49   height->dynamic = (numdims == 2);
     133
    50134  height->offset = offset;
    51135  height->scale = scale;
     
    60144  if(!strcmp(name, height->name)){
    61145    anugavis.heights = height->next;
    62     free(height);
     146    freeHeightQuantity(height);
    63147    return;
    64148  }
     
    68152      nextHeight = height->next;
    69153      height->next = height->next->next;
    70       free(nextHeight);
     154      freeHeightQuantity(nextHeight);
    71155      return;
    72156    }
  • anuga_work/development/anugavis/src/height_quantity.h

    r5259 r5267  
    22#define HEIGHT_QUANTITY_H
    33
     4#ifdef HAVE_CONFIG_H
     5#  include "config.h"
     6#endif
     7
     8#ifdef HAVE_GL_GL_H
     9#  include <GL/gl.h>
     10#elif HAVE_OPENGL_GL_H
     11#  include <OpenGL/gl.h>
     12#endif
    413#include <SDL.h>
    514
    615struct height_quantity_simple{
    716  char *name;
    8   uint8_t dynamic;
     17  uint8_t frames;
    918  double offset;
    1019  double scale;
    1120  Uint32 color;
     21  GLuint displayLists;
    1222  struct height_quantity_simple *next;
    1323};
  • anuga_work/development/anugavis/src/init.c

    r5264 r5267  
    2020#include "error.h"
    2121#include "globals.h"
     22#include "init.h"
    2223#include "netcdf_util.h"
    23 #include "xmalloc.h"
     24#include "xfunctions.h"
    2425
    2526int AnugaVis_Init(int width, int height, const char *swwFilePath){
     
    4344  }
    4445
     46  SDL_WM_SetCaption("ANUGA Visualiser", "ANUGA Visualiser");
    4547  glEnable(GL_DEPTH_TEST);
    4648  glShadeModel(GL_FLAT);
     
    6365     (nc_inq_dimlen_by_name(anugavis.netcdfId, "number_of_volumes",
    6466                            &anugavis.number_of_volumes) != NC_NOERR) ||
     67     (nc_inq_dimlen_by_name(anugavis.netcdfId, "number_of_timesteps",
     68                            &anugavis.number_of_timesteps) != NC_NOERR) ||
    6569     ((anugavis.x = xmalloc(sizeof(float) * anugavis.number_of_points,
    6670                            "AnugaVis_Init()")) == NULL) ||
     
    7781     (nc_get_var_int_by_name(anugavis.netcdfId, "volumes",
    7882                             anugavis.volumes) != NC_NOERR)){
    79     nc_close(anugavis.netcdfId);
    80     if(anugavis.x != NULL) free(anugavis.x);
    81     if(anugavis.y != NULL) free(anugavis.y);
    82     if(anugavis.volumes != NULL) free(anugavis.volumes);
    83     SDL_Quit();
     83    AnugaVis_DeInit();
    8484    return -1;
    8585  }
     
    9191    AnugaVis_UndefineHeightQuantity(anugavis.heights->name);
    9292  nc_close(anugavis.netcdfId);
    93   free(anugavis.x);
    94   free(anugavis.y);
    95   free(anugavis.volumes);
     93  if(anugavis.x != NULL) free(anugavis.x);
     94  if(anugavis.y != NULL) free(anugavis.y);
     95  if(anugavis.volumes != NULL) free(anugavis.volumes);
    9696  SDL_Quit();
    9797}
  • anuga_work/development/anugavis/src/xfunctions.c

    r5266 r5267  
    1010#endif
    1111#include "error.h"
    12 #include "xmalloc.h"
     12#include "xfunctions.h"
    1313
    1414void *xmalloc(size_t size, const char *message){
  • anuga_work/development/anugavis/src/xfunctions.h

    r5266 r5267  
    1 #ifndef XMALLOC_H
    2 #define XMALLOC_H
     1#ifndef XFUNCTIONS_H
     2#define XFUNCTIONS_H
    33
    44/* For a given func, xfunc behaves like func but sets an error message
    5  * using AnugaVis_SetError().
     5 * using AnugaVis_SetError() if something goes wrong.
    66 */
    77extern void *xmalloc(size_t size, const char *message);
Note: See TracChangeset for help on using the changeset viewer.