source: anuga_work/development/anugavis/src/init.c @ 5380

Last change on this file since 5380 was 5380, checked in by jack, 16 years ago

Dynamic camera positioning.

File size: 4.1 KB
Line 
1#ifdef HAVE_CONFIG_H
2#  include "config.h"
3#endif
4
5#ifdef HAVE_STDLIB_H
6#  include <stdlib.h>
7#endif
8#ifdef HAVE_GL_GL_H
9#  include <GL/gl.h>
10#elif HAVE_OPENGL_GL_H
11#  include <OpenGL/gl.h>
12#endif
13#ifdef HAVE_GL_GLU_H
14#  include <GL/glu.h>
15#elif HAVE_OPENGL_GLU_H
16#  include <OpenGL/glu.h>
17#endif
18#include <netcdf.h>
19#include <SDL.h>
20#include "error.h"
21#include "globals.h"
22#include "init.h"
23#include "netcdf_util.h"
24#include "vector.h"
25#include "xfunctions.h"
26
27int AnugaVis_Init(int width, int height, const char *swwFilePath){
28  int ncstatus;
29  vector releye;
30  unsigned int i;
31  GLfloat lightAmbient[] = {0.0, 0.0, 0.0, 1.0};
32  GLfloat lightDiffuse[] = {1.0, 1.0, 1.0, 1.0};
33  anugavis.current_frame = 0;
34  anugavis.paused = 0;
35  anugavis.x = NULL;
36  anugavis.y = NULL;
37  anugavis.volumes = NULL;
38
39  anugavis.heights = NULL;
40  if((SDL_Init(SDL_INIT_VIDEO) == -1) ||
41     (SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5) == -1) ||
42     (SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5) == -1) ||
43     (SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5) == -1) ||
44     (SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16) == -1) ||
45     (SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1) == -1) ||
46     ((anugavis.screen = 
47       SDL_SetVideoMode(width, height, 16, SDL_OPENGL)) == NULL)){
48    AnugaVis_SDLError();
49    return -1;
50  }
51
52  SDL_WM_SetCaption("ANUGA Visualiser", "ANUGA Visualiser");
53  glEnable(GL_DEPTH_TEST);
54  glShadeModel(GL_FLAT);
55  glEnable(GL_NORMALIZE);
56  glMatrixMode(GL_PROJECTION);
57  gluPerspective(45.0, ((GLdouble)width)/((GLdouble)height), 0.1, 1000);
58  glMatrixMode(GL_MODELVIEW);
59  glLoadIdentity();
60  glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
61  glEnable(GL_COLOR_MATERIAL);
62  glEnable(GL_LIGHTING);
63  glEnable(GL_LIGHT0);
64  glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
65  glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
66 
67  if((ncstatus = nc_open(swwFilePath, NC_SHARE,
68                         &anugavis.netcdfId)) != NC_NOERR){
69    SDL_Quit();
70    return -1;
71  }
72
73  if((nc_inq_dimlen_by_name(anugavis.netcdfId, "number_of_points",
74                            &anugavis.number_of_points) != NC_NOERR) ||
75     (nc_inq_dimlen_by_name(anugavis.netcdfId, "number_of_vertices",
76                            &anugavis.number_of_vertices) != NC_NOERR) ||
77     (nc_inq_dimlen_by_name(anugavis.netcdfId, "number_of_volumes",
78                            &anugavis.number_of_volumes) != NC_NOERR) ||
79     (nc_inq_dimlen_by_name(anugavis.netcdfId, "number_of_timesteps",
80                            &anugavis.number_of_timesteps) != NC_NOERR) ||
81     ((anugavis.x = xmalloc(sizeof(float) * anugavis.number_of_points,
82                            "AnugaVis_Init()")) == NULL) ||
83     ((anugavis.y = xmalloc(sizeof(float) * anugavis.number_of_points,
84                            "AnugaVis_Init()")) == NULL) ||
85     ((anugavis.volumes = xmalloc(sizeof(int)
86                                  * anugavis.number_of_volumes
87                                  * anugavis.number_of_vertices,
88                                  "AnugaVis_Init()")) == NULL) ||
89     (nc_get_var_float_by_name(anugavis.netcdfId, "x",
90                               anugavis.x) != NC_NOERR) ||
91     (nc_get_var_float_by_name(anugavis.netcdfId, "y",
92                               anugavis.y) != NC_NOERR) ||
93     (nc_get_var_int_by_name(anugavis.netcdfId, "volumes",
94                             anugavis.volumes) != NC_NOERR)){
95    AnugaVis_DeInit();
96    return -1;
97  }
98
99  for(i = 0 ; i < anugavis.number_of_points ; i++){
100    anugavis.minX = MIN(anugavis.minX, anugavis.x[i]);
101    anugavis.maxX = MAX(anugavis.maxX, anugavis.x[i]);
102    anugavis.minY = MIN(anugavis.minY, anugavis.y[i]);
103    anugavis.maxY = MAX(anugavis.maxY, anugavis.y[i]);
104  }
105  anugavis.eye[0] = anugavis.minX;
106  anugavis.eye[1] = anugavis.minY;
107  anugavis.eye[2] = 0;
108  anugavis.focus[0] = anugavis.minX + (anugavis.maxX - anugavis.minX) / 2;
109  anugavis.focus[1] = anugavis.minY + (anugavis.maxY - anugavis.minY) / 2;
110  anugavis.focus[2] = 0;
111  vsub(anugavis.eye, anugavis.focus, releye);
112  anugavis.eye[2] = vlen(releye);
113  return 0;
114}
115
116void AnugaVis_DeInit(void){
117  while(anugavis.heights != NULL)
118    AnugaVis_UndefineHeightQuantity(anugavis.heights->name);
119  nc_close(anugavis.netcdfId);
120  if(anugavis.x != NULL) free(anugavis.x);
121  if(anugavis.y != NULL) free(anugavis.y);
122  if(anugavis.volumes != NULL) free(anugavis.volumes);
123  SDL_Quit();
124}
Note: See TracBrowser for help on using the repository browser.