From f9dfcecfe063b7b0e23c593275ede012b68b0fcc Mon Sep 17 00:00:00 2001 From: curt Date: Fri, 16 May 1997 15:58:23 +0000 Subject: [PATCH] Initial revision. --- Aircraft/Makefile | 56 ++++++++++ Aircraft/aircraft.c | 35 ++++++ Aircraft/aircraft.h | 41 +++++++ Controls/Makefile | 53 +++++++++ Controls/controls.h | 35 ++++++ FDM/Makefile | 55 ++++++++++ FDM/flight.h | 40 +++++++ FDM/slew.c | 76 +++++++++++++ FDM/slew.h | 33 ++++++ Main/Makefile | 60 +++++++++++ Main/gltkkey.c | 64 +++++++++++ Main/gltkkey.h | 30 ++++++ Main/gltkmain.c | 255 ++++++++++++++++++++++++++++++++++++++++++++ Main/mat3.h | 147 +++++++++++++++++++++++++ Main/mesh2ogl.c | 115 ++++++++++++++++++++ Scenery/Makefile | 75 +++++++++++++ Scenery/common.c | 34 ++++++ Scenery/common.h | 30 ++++++ Scenery/mesh.c | 92 ++++++++++++++++ Scenery/mesh.h | 53 +++++++++ Scenery/parser.h | 24 +++++ Scenery/parser.y | 243 +++++++++++++++++++++++++++++++++++++++++ Scenery/scanner.l | 211 ++++++++++++++++++++++++++++++++++++ Scenery/scenery.h | 26 +++++ Simulator/limits.h | 26 +++++ 25 files changed, 1909 insertions(+) create mode 100644 Aircraft/Makefile create mode 100644 Aircraft/aircraft.c create mode 100644 Aircraft/aircraft.h create mode 100644 Controls/Makefile create mode 100644 Controls/controls.h create mode 100644 FDM/Makefile create mode 100644 FDM/flight.h create mode 100644 FDM/slew.c create mode 100644 FDM/slew.h create mode 100644 Main/Makefile create mode 100644 Main/gltkkey.c create mode 100644 Main/gltkkey.h create mode 100644 Main/gltkmain.c create mode 100644 Main/mat3.h create mode 100644 Main/mesh2ogl.c create mode 100644 Scenery/Makefile create mode 100644 Scenery/common.c create mode 100644 Scenery/common.h create mode 100644 Scenery/mesh.c create mode 100644 Scenery/mesh.h create mode 100644 Scenery/parser.h create mode 100644 Scenery/parser.y create mode 100644 Scenery/scanner.l create mode 100644 Scenery/scenery.h create mode 100644 Simulator/limits.h diff --git a/Aircraft/Makefile b/Aircraft/Makefile new file mode 100644 index 000000000..2f74dc69c --- /dev/null +++ b/Aircraft/Makefile @@ -0,0 +1,56 @@ +#--------------------------------------------------------------------------- +# Makefile +# +# Written by Curtis Olson, started May 1997. +# +# $Id$ +# (Log is kept at end of this file) +#--------------------------------------------------------------------------- + + +TARGET = libaircraft.a + +CFILES = aircraft.c +HFILES = aircraft.h +OFILES = $(CFILES:.c=.o) + +CC = gcc +CFLAGS = -g -Wall +# CFLAGS = -O2 -Wall + +AR = ar + +INCLUDES = + +LIBS = + + +#--------------------------------------------------------------------------- +# Primary Targets +#--------------------------------------------------------------------------- + +$(TARGET): $(OFILES) $(HFILES) + $(AR) rv $(TARGET) $(OFILES) + +all: $(TARGET) + +clean: + rm -f *.o $(TARGET) *~ core + + +#--------------------------------------------------------------------------- +# Secondary Targets +#--------------------------------------------------------------------------- + +aircraft.o: aircraft.c aircraft.h + $(CC) $(CFLAGS) $(INCLUDES) -c aircraft.c + +aircraft.h: ../flight/flight.h ../controls/controls.h + touch aircraft.h + + +#--------------------------------------------------------------------------- +# $Log$ +# Revision 1.1 1997/05/16 15:58:23 curt +# Initial revision. +# diff --git a/Aircraft/aircraft.c b/Aircraft/aircraft.c new file mode 100644 index 000000000..0138349d9 --- /dev/null +++ b/Aircraft/aircraft.c @@ -0,0 +1,35 @@ +/************************************************************************** + * aircraft.c -- various aircraft routines + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#include + +#include "aircraft.h" + + +/* Display various parameters to stdout */ +void aircraft_debug(int type) { + struct flight_params *f; + struct control_params *c; + + f = ¤t_aircraft.flight; + c = ¤t_aircraft.controls; + + printf("Pos = (%.2f,%.2f,%.2f) Dir = %.2f\n", + f->pos_x, f->pos_y, f->pos_z, f->Psi); + printf("Elev = %.2f, Aileron = %.2f, Rudder = %.2f\n", + c->elev, c->aileron, c->rudder); +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 15:58:24 curt +/* Initial revision. +/* + */ diff --git a/Aircraft/aircraft.h b/Aircraft/aircraft.h new file mode 100644 index 000000000..2996cd6e1 --- /dev/null +++ b/Aircraft/aircraft.h @@ -0,0 +1,41 @@ +/************************************************************************** + * aircraft.h -- define shared aircraft parameters + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef AIRCRAFT_H +#define AIRCRAFT_H + +#include "../flight/flight.h" +#include "../controls/controls.h" + + +/* Define a structure containing all the parameters for an aircraft */ +struct aircraft_params { + struct flight_params flight; + struct control_params controls; +}; + + +/* current_aircraft contains all the parameters of the aircraft + currently being operated. */ +extern struct aircraft_params current_aircraft; + + +/* Display various parameters to stdout */ +void aircraft_debug(int type); + + +#endif AIRCRAFT_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 15:58:25 curt +/* Initial revision. +/* + */ diff --git a/Controls/Makefile b/Controls/Makefile new file mode 100644 index 000000000..af6e4f562 --- /dev/null +++ b/Controls/Makefile @@ -0,0 +1,53 @@ +#--------------------------------------------------------------------------- +# Makefile +# +# Written by Curtis Olson, started May 1997. +# +# $Id$ +# (Log is kept at end of this file) +#--------------------------------------------------------------------------- + + +TARGET = stamp-done + +CFILES = +HFILES = controls.h +OFILES = $(CFILES:.c=.o) + +CC = gcc +CFLAGS = -g -Wall +# CFLAGS = -O2 -Wall + +AR = ar + +INCLUDES = + +LIBS = + + +#--------------------------------------------------------------------------- +# Primary Targets +#--------------------------------------------------------------------------- + +$(TARGET): $(OFILES) $(HFILES) + touch stamp-done + +all: $(TARGET) + +clean: + rm -f *.o $(TARGET) *~ core + + +#--------------------------------------------------------------------------- +# Secondary Targets +#--------------------------------------------------------------------------- + +controls.h: ../limits.h + touch controls.h + + +#--------------------------------------------------------------------------- +# $Log$ +# Revision 1.1 1997/05/16 15:59:47 curt +# Initial revision. +# diff --git a/Controls/controls.h b/Controls/controls.h new file mode 100644 index 000000000..1de0b716f --- /dev/null +++ b/Controls/controls.h @@ -0,0 +1,35 @@ +/************************************************************************** + * controls.h -- defines a standard interface to all flight sim controls + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef CONTROLS_H +#define CONTROLS_H + + +#include "../limits.h" + + +/* Define a structure containing the control parameters */ + +struct control_params { + double aileron; + double elev; + double rudder; + double throttle[MAX_ENGINES]; +}; + + +#endif CONTROLS_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 15:59:48 curt +/* Initial revision. +/* + */ diff --git a/FDM/Makefile b/FDM/Makefile new file mode 100644 index 000000000..a27d97853 --- /dev/null +++ b/FDM/Makefile @@ -0,0 +1,55 @@ +#--------------------------------------------------------------------------- +# Makefile +# +# Written by Curtis Olson, started May 1997. +# +# $Id$ +# (Log is kept at end of this file) +#--------------------------------------------------------------------------- + + +TARGET=libflight.a + +CFILES = slew.c +OFILES = $(CFILES:.c=.o) + +CC = gcc +CFLAGS = -g -Wall +# CFLAGS = -O2 -Wall + +AR = ar + +INCLUDES = + +LIBS = + + +#--------------------------------------------------------------------------- +# Primary Targets +#--------------------------------------------------------------------------- + +$(TARGET): $(OFILES) + $(AR) rv $(TARGET) $(OFILES) + +all: $(TARGET) + +clean: + rm -f *.o $(TARGET) *~ core + + +#--------------------------------------------------------------------------- +# Secondary Targets +#--------------------------------------------------------------------------- + +flight.h: slew.h + touch flight.h + +slew.o: slew.c slew.h flight.h ../aircraft/aircraft.h ../controls/controls.h + $(CC) $(CFLAGS) $(INCLUDES) -c slew.c + + +#--------------------------------------------------------------------------- +# $Log$ +# Revision 1.1 1997/05/16 16:04:44 curt +# Initial revision. +# diff --git a/FDM/flight.h b/FDM/flight.h new file mode 100644 index 000000000..eb2752041 --- /dev/null +++ b/FDM/flight.h @@ -0,0 +1,40 @@ +/************************************************************************** + * flight.h -- define shared flight model parameters + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef FLIGHT_H +#define FLIGHT_H + + +#include "slew.h" + + +/* Define a structure containing the shared flight model parameters */ + +struct flight_params { + double pos_x, pos_y, pos_z; /* temporary position variables */ + double vel_x, vel_y, vel_z; /* temporary velocity variables */ + + double Phi; /* Roll angle */ + double Theta; /* Pitch angle */ + double Psi; /* Heading angle */ + double vel_Phi; + double vel_Theta; + double vel_Psi; +}; + + +#endif FLIGHT_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:04:45 curt +/* Initial revision. +/* + */ diff --git a/FDM/slew.c b/FDM/slew.c new file mode 100644 index 000000000..3395aa6b0 --- /dev/null +++ b/FDM/slew.c @@ -0,0 +1,76 @@ +/************************************************************************** + * slew.c -- the "slew" flight model + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#include + +#include "slew.h" +#include "flight.h" +#include "../aircraft/aircraft.h" +#include "../controls/controls.h" + + +#ifndef PI2 +#define PI2 (M_PI + M_PI) +#endif + + +/* reset flight params to a specific position */ +void slew_init(double pos_x, double pos_y, double pos_z, double heading) { + struct flight_params *f; + + f = ¤t_aircraft.flight; + + f->pos_x = pos_x; + f->pos_y = pos_y; + f->pos_z = pos_z; + + f->vel_x = 0.0; + f->vel_y = 0.0; + f->vel_z = 0.0; + + f->Phi = 0.0; + f->Theta = 0.0; + f->Psi = 0.0; + + f->vel_Phi = 0.0; + f->vel_Theta = 0.0; + f->vel_Psi = 0.0; + + f->Psi = heading; +} + + +/* update position based on inputs, positions, velocities, etc. */ +void slew_update() { + struct flight_params *f; + struct control_params *c; + + f = ¤t_aircraft.flight; + c = ¤t_aircraft.controls; + + f->Psi += c->aileron; + if ( f->Psi > PI2 ) { + f->Psi -= PI2; + } else if ( f->Psi < 0 ) { + f->Psi += PI2; + } + + f->vel_x = -c->elev; + + f->pos_x = f->pos_x + (cos(f->Psi) * f->vel_x); + f->pos_y = f->pos_y + (sin(f->Psi) * f->vel_x); +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:04:45 curt +/* Initial revision. +/* + */ diff --git a/FDM/slew.h b/FDM/slew.h new file mode 100644 index 000000000..15b6803e8 --- /dev/null +++ b/FDM/slew.h @@ -0,0 +1,33 @@ +/************************************************************************** + * slew.h -- the "slew" flight model + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef SLEW_H +#define SLEW_H + + +#include "flight.h" + + +/* reset flight params to a specific position */ + +void slew_init(double pos_x, double pos_y, double pos_z, double heading); + +/* update position based on inputs, positions, velocities, etc. */ +void slew_update(); + + +#endif SLEW_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:04:46 curt +/* Initial revision. +/* + */ diff --git a/Main/Makefile b/Main/Makefile new file mode 100644 index 000000000..c0108b8ab --- /dev/null +++ b/Main/Makefile @@ -0,0 +1,60 @@ +#--------------------------------------------------------------------------- +# Makefile +# +# Written by Curtis Olson, started May 1997. +# +# $Id$ +# (Log is kept at end of this file) +#--------------------------------------------------------------------------- + + +TARGET=viewer + +CFILES = gltkmain.c gltkkey.c mesh2ogl.c +OFILES = $(CFILES:.c=.o) +AFILES = ../flight/libflight.a ../aircraft/libaircraft.a ../scenery/libscenery.a + +CC = gcc +CFLAGS = -g -Wall +# CFLAGS = -O2 -Wall + +INCLUDES = -I/usr/include/mesa + +MESA_LIBS = -L/usr/lib/mesa -lMesaaux -lMesatk -lMesaGLU -lMesaGL +X11_LIBS = -L/usr/X11R6/lib -lX11 -lXext +LIBS = $(MESA_LIBS) $(X11_LIBS) -lm -lfl + + + +#--------------------------------------------------------------------------- +# Primary Targets +#--------------------------------------------------------------------------- + +$(TARGET): $(OFILES) $(AFILES) + $(CC) -o $(TARGET) $(OFILES) $(AFILES) $(LIBS) + +all: $(TARGET) + +clean: + rm -f *.o $(TARGET) *~ core + + +#--------------------------------------------------------------------------- +# Secondary Targets +#--------------------------------------------------------------------------- + +gltkmain.o: gltkmain.c gltkkey.h ../aircraft/aircraft.h ../scenery/scenery.h + $(CC) $(CFLAGS) $(INCLUDES) -c gltkmain.c + +gltkkey.o: gltkkey.c gltkkey.h ../aircraft/aircraft.h + $(CC) $(CFLAGS) $(INCLUDES) -c gltkkey.c + +mesh2ogl.o: mesh2ogl.c ../scenery/mesh.h + $(CC) $(CFLAGS) $(INCLUDES) -c mesh2ogl.c + + +#--------------------------------------------------------------------------- +# $Log$ +# Revision 1.1 1997/05/16 16:05:51 curt +# Initial revision. +# diff --git a/Main/gltkkey.c b/Main/gltkkey.c new file mode 100644 index 000000000..8901eb8dc --- /dev/null +++ b/Main/gltkkey.c @@ -0,0 +1,64 @@ +/************************************************************************** + * tkglkey.c -- handle tkgl keyboard events + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#include + +/* assumes -I/usr/include/mesa in compile command */ +#include "gltk.h" + +#include "gltkkey.h" +#include "../aircraft/aircraft.h" + + +/* Handle keyboard events */ +GLenum key(int k, GLenum mask) { + struct control_params *c; + + c = ¤t_aircraft.controls; + + switch (k) { + case TK_UP: + c->elev -= 0.01; + return GL_TRUE; + case TK_DOWN: + c->elev += 0.01; + return GL_TRUE; + case TK_LEFT: + c->aileron += 0.01; + return GL_TRUE; + case TK_RIGHT: + c->aileron -= 0.01; + return GL_TRUE; + case 1 /* TK_END */: + c->rudder -= 0.01; + return GL_TRUE; + case 2 /* TK_PGDWN */: + c->rudder += 0.01; + return GL_TRUE; + case TK_a: + c->throttle[0] -= 0.05; + return GL_TRUE; + case TK_s: + c->throttle[0] += 0.05; + case TK_ESCAPE: + tkQuit(); + } + + printf("Key hit = %d\n", k); + + return GL_FALSE; +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:05:51 curt +/* Initial revision. +/* + */ diff --git a/Main/gltkkey.h b/Main/gltkkey.h new file mode 100644 index 000000000..4c055836b --- /dev/null +++ b/Main/gltkkey.h @@ -0,0 +1,30 @@ +/************************************************************************** + * tkglkey.h -- handle tkgl keyboard events + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef TKGLKEY_H +#define TKGLKEY_H + + +/* assumes -I/usr/include/mesa in compile command */ +#include "gltk.h" + + +/* Handle keyboard events */ +GLenum key(int k, GLenum mask); + + +#endif TKGLKEY_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:05:53 curt +/* Initial revision. +/* + */ diff --git a/Main/gltkmain.c b/Main/gltkmain.c new file mode 100644 index 000000000..4214b585c --- /dev/null +++ b/Main/gltkmain.c @@ -0,0 +1,255 @@ +/************************************************************************** + * gltkmain.c -- top level sim routines + * + * Written by Curtis Olson for Mesa (OpenGL), started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#include +#include +#include +#include + +/* assumes -I/usr/include/mesa in compile command */ +#include "gltk.h" + +#include "gltkkey.h" +#include "../aircraft/aircraft.h" +#include "../scenery/scenery.h" + + +/* This a record containing all the info for the aircraft currently + being operated */ +struct aircraft_params current_aircraft; + +/* temporary hack */ +extern struct mesh *mesh_ptr; + +/* Function prototypes */ +GLint make_mesh(); +static void draw_mesh(); + + +/* view parameters */ +static GLfloat win_ratio = 1.0; + +/* pointer to terrain mesh structure */ +static GLint mesh; + +/* init_view() -- Setup view parameters */ +static void init_view() { + /* if the 4th field is 0.0, this specifies a direction ... */ + static GLfloat pos[4] = {2.0, 2.0, 3.0, 0.0 }; + + glLightfv( GL_LIGHT0, GL_POSITION, pos ); + glEnable( GL_CULL_FACE ); + glEnable( GL_LIGHTING ); + glEnable( GL_LIGHT0 ); + glEnable( GL_DEPTH_TEST ); + /* glEnable( GL_FOG ); + glFog( GL_FOG_MODE, GL_LINEAR ); + glFogf( GL_FOG_END, 1000.0 ); */ + glClearColor(0.6, 0.6, 0.9, 1.0); +} + + +/* init_scene() -- build all objects */ +static void init_scene() { + + /* make terrain mesh */ + mesh = make_mesh(); + + /* If enabled, normal vectors specified with glNormal are scaled + to unit length after transformation. See glNormal. */ + glEnable( GL_NORMALIZE ); +} + + +/* create the terrain mesh */ +GLint make_mesh() { + GLint mesh; + + mesh = mesh_to_ogl(mesh_ptr); + + return(mesh); +} + + +/* create the terrain mesh */ +GLint make_mesh_old() { + GLint mesh; + static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 }; + + mesh = glGenLists(1); + glNewList(mesh, GL_COMPILE); + glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); + glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */ + + glBegin(GL_POLYGON); + glVertex3f(-10.0, -10.0, 0.0); + glVertex3f(0.0, -10.0, 0.0); + glVertex3f(0.0, 0.0, 1.0); + glVertex3f(-10.0, 0.0, 1.0); + glEnd(); + + glBegin(GL_POLYGON); + glVertex3f(-10.0, 0.0, 1.0); + glVertex3f(0.0, 0.0, 1.0); + glVertex3f(0.0, 10.0, 0.0); + glVertex3f(-10.0, 10.0, 0.0); + glEnd(); + + glBegin(GL_POLYGON); + glVertex3f(0.0, 0.0, 0.0); + glVertex3f(10.0, 0.0, 2.0); + glVertex3f(10.0, 10.0, 2.0); + glVertex3f(0.0, 10.0, 0.0); + glEnd(); + + glBegin(GL_POLYGON); + glVertex3f(0.0, -10.0, -1.0); + glVertex3f(10.0, -10.0, 0.0); + glVertex3f(10.0, 0.0, -1.0); + glVertex3f(0.0, 0.0, 0.0); + glEnd(); + + glEndList(); + + return(mesh); +} + + +/* update the view volume */ +static void update_view() { + struct flight_params *f; + + f = ¤t_aircraft.flight; + + /* Tell GL we are about to modify the projection parameters */ + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + + gluPerspective(80.0, 1.0/win_ratio, 1.0, 6000.0); + gluLookAt(f->pos_x, f->pos_y, f->pos_z, + f->pos_x + cos(f->Psi), f->pos_y + sin(f->Psi), f->pos_z - 0.5, + 0.0, 0.0, 1.0); +} + + +/* draw the scene */ +static void draw_scene( void ) { + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + /* update view volume parameters */ + update_view(); + + /* Tell GL we are switching to model view parameters */ + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + + /* glTranslatef(0.0, 0.0, -5.0); */ + + glPushMatrix(); + + /* draw terrain mesh */ + draw_mesh(); + + glPopMatrix(); + + tkSwapBuffers(); +} + + +/* draw the terrain mesh */ +static void draw_mesh() { + glCallList(mesh); +} + + +/* What should we do when we have nothing else to do? How about get + * ready for the next move?*/ +static void idle( void ) +{ + slew_update(); + aircraft_debug(1); + + draw_scene(); +} + + +/* new window size or exposure */ +static void reshape( int width, int height ) { + /* Do this so we can call reshape(0,0) ourselves without having to know + * what the values of width & height are. */ + if ( (height > 0) && (width > 0) ) { + win_ratio = (GLfloat) height / (GLfloat) width; + } + + /* Inform gl of our view window size */ + glViewport(0, 0, (GLint)width, (GLint)height); + + update_view(); + + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); +} + + +/************************************************************************** + * Main ... + **************************************************************************/ + +int main( int argc, char *argv[] ) { + + /* parse the scenery file */ + parse_scenery(argv[1]); + + /* Define initial window size */ + tkInitPosition(0, 0, 400, 400); + + /* Define Display Parameters */ + tkInitDisplayMode( TK_RGB | TK_DEPTH | TK_DOUBLE | TK_DIRECT ); + + /* Initialize the main window */ + if (tkInitWindow("Terrain Demo") == GL_FALSE) { + tkQuit(); + } + + /* setup view parameters */ + init_view(); + + /* build all objects */ + init_scene(); + + /* Set initial position and slew parameters */ + slew_init(-406658.0, 129731.0, 344, 0.79); + + /* call reshape() on expose events */ + tkExposeFunc( reshape ); + + /* call reshape() on window resizes */ + tkReshapeFunc( reshape ); + + /* call key() on keyboard event */ + tkKeyDownFunc( key ); + + /* call idle() whenever there is nothing else to do */ + tkIdleFunc( idle ); + + /* draw the scene */ + tkDisplayFunc( draw_scene ); + + /* pass control off to the tk event handler */ + tkExec(); + + return(0); +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:05:52 curt +/* Initial revision. +/* + */ diff --git a/Main/mat3.h b/Main/mat3.h new file mode 100644 index 000000000..70f7a616c --- /dev/null +++ b/Main/mat3.h @@ -0,0 +1,147 @@ +/* Copyright 1988, Brown Computer Graphics Group. All Rights Reserved. */ + +/* ------------------------------------------------------------------------- + Public MAT3 include file + ------------------------------------------------------------------------- */ + +#ifndef MAT3_HAS_BEEN_INCLUDED +#define MAT3_HAS_BEEN_INCLUDED + +/* ----------------------------- Constants ------------------------------ */ + +/* + * Make sure the math library .h file is included, in case it wasn't. + */ + +#ifndef HUGE +#include +#endif +#include + + +#define MAT3_DET0 -1 /* Indicates singular mat */ +#define MAT3_EPSILON 1e-12 /* Close enough to zero */ +#define MAT3_PI 3.141592653589793 /* Pi */ + +/* ------------------------------ Types --------------------------------- */ + +typedef double MAT3mat[4][4]; /* 4x4 matrix */ +typedef double MAT3vec[3]; /* Vector */ +typedef double MAT3hvec[4]; /* Vector with homogeneous coord */ + +/* ------------------------------ Macros -------------------------------- */ + +/* Tests if a number is within EPSILON of zero */ +#define MAT3_IS_ZERO(N) ((N) < MAT3_EPSILON && (N) > -MAT3_EPSILON) + +/* Sets a vector to the three given values */ +#define MAT3_SET_VEC(V,X,Y,Z) ((V)[0]=(X), (V)[1]=(Y), (V)[2]=(Z)) + +/* Tests a vector for all components close to zero */ +#define MAT3_IS_ZERO_VEC(V) (MAT3_IS_ZERO((V)[0]) && \ + MAT3_IS_ZERO((V)[1]) && \ + MAT3_IS_ZERO((V)[2])) + +/* Dot product of two vectors */ +#define MAT3_DOT_PRODUCT(V1,V2) \ + ((V1)[0]*(V2)[0] + (V1)[1]*(V2)[1] + (V1)[2]*(V2)[2]) + +/* Copy one vector to other */ +#define MAT3_COPY_VEC(TO,FROM) ((TO)[0] = (FROM)[0], \ + (TO)[1] = (FROM)[1], \ + (TO)[2] = (FROM)[2]) + +/* Normalize vector to unit length, using TEMP as temporary variable. + * TEMP will be zero if vector has zero length */ +#define MAT3_NORMALIZE_VEC(V,TEMP) \ + if ((TEMP = sqrt(MAT3_DOT_PRODUCT(V,V))) > MAT3_EPSILON) { \ + TEMP = 1.0 / TEMP; \ + MAT3_SCALE_VEC(V,V,TEMP); \ + } else TEMP = 0.0 + +/* Scale vector by given factor, storing result vector in RESULT_V */ +#define MAT3_SCALE_VEC(RESULT_V,V,SCALE) \ + MAT3_SET_VEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), (V)[2]*(SCALE)) + +/* Adds vectors V1 and V2, storing result in RESULT_V */ +#define MAT3_ADD_VEC(RESULT_V,V1,V2) \ + MAT3_SET_VEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \ + (V1)[2]+(V2)[2]) + +/* Subtracts vector V2 from V1, storing result in RESULT_V */ +#define MAT3_SUB_VEC(RESULT_V,V1,V2) \ + MAT3_SET_VEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \ + (V1)[2]-(V2)[2]) + +/* Multiplies vectors V1 and V2, storing result in RESULT_V */ +#define MAT3_MULT_VEC(RESULT_V,V1,V2) \ + MAT3_SET_VEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \ + (V1)[2]*(V2)[2]) + +/* Sets RESULT_V to the linear combination of V1 and V2, scaled by + * SCALE1 and SCALE2, respectively */ +#define MAT3_LINEAR_COMB(RESULT_V,SCALE1,V1,SCALE2,V2) \ + MAT3_SET_VEC(RESULT_V, (SCALE1)*(V1)[0] + (SCALE2)*(V2)[0], \ + (SCALE1)*(V1)[1] + (SCALE2)*(V2)[1], \ + (SCALE1)*(V1)[2] + (SCALE2)*(V2)[2]) + +/* Several of the vector macros are useful for homogeneous-coord vectors */ +#define MAT3_SET_HVEC(V,X,Y,Z,W) ((V)[0]=(X), (V)[1]=(Y), \ + (V)[2]=(Z), (V)[3]=(W)) + +#define MAT3_COPY_HVEC(TO,FROM) ((TO)[0] = (FROM)[0], \ + (TO)[1] = (FROM)[1], \ + (TO)[2] = (FROM)[2], \ + (TO)[3] = (FROM)[3]) + +#define MAT3_SCALE_HVEC(RESULT_V,V,SCALE) \ + MAT3_SET_HVEC(RESULT_V, (V)[0]*(SCALE), (V)[1]*(SCALE), \ + (V)[2]*(SCALE), (V)[3]*(SCALE)) + +#define MAT3_ADD_HVEC(RESULT_V,V1,V2) \ + MAT3_SET_HVEC(RESULT_V, (V1)[0]+(V2)[0], (V1)[1]+(V2)[1], \ + (V1)[2]+(V2)[2], (V1)[3]+(V2)[3]) + +#define MAT3_SUB_HVEC(RESULT_V,V1,V2) \ + MAT3_SET_HVEC(RESULT_V, (V1)[0]-(V2)[0], (V1)[1]-(V2)[1], \ + (V1)[2]-(V2)[2], (V1)[3]-(V2)[3]) + +#define MAT3_MULT_HVEC(RESULT_V,V1,V2) \ + MAT3_SET_HVEC(RESULT_V, (V1)[0]*(V2)[0], (V1)[1]*(V2)[1], \ + (V1)[2]*(V2)[2], (V1)[3]*(V2)[3]) + +/* ------------------------------ Entries ------------------------------- */ + + +/* In MAT3geom.c */ +void MAT3direction_matrix (MAT3mat result_mat, MAT3mat mat); +int MAT3normal_matrix (MAT3mat result_mat, MAT3mat mat); +void MAT3rotate (MAT3mat result_mat, MAT3vec axis, double angle_in_radians); +void MAT3translate (MAT3mat result_mat, MAT3vec trans); +void MAT3scale (MAT3mat result_mat, MAT3vec scale); +void MAT3shear(MAT3mat result_mat, double xshear, double yshear); + +/* In MAT3mat.c */ +void MAT3identity(MAT3mat); +void MAT3zero(MAT3mat); +void MAT3copy (MAT3mat to, MAT3mat from); +void MAT3mult (MAT3mat result, MAT3mat, MAT3mat); +void MAT3transpose (MAT3mat result, MAT3mat); +int MAT3invert (MAT3mat result, MAT3mat); +void MAT3print (MAT3mat, FILE *fp); +void MAT3print_formatted (MAT3mat, FILE *fp, + char *title, char *head, char *format, char *tail); +extern int MAT3equal(); +extern double MAT3trace(); +extern int MAT3power(); +extern int MAT3column_reduce(); +extern int MAT3kernel_basis(); + +/* In MAT3vec.c */ +void MAT3mult_vec(MAT3vec result_vec, MAT3vec vec, MAT3mat mat); +int MAT3mult_hvec (MAT3hvec result_vec, MAT3hvec vec, MAT3mat mat, int normalize); +void MAT3cross_product(MAT3vec result,MAT3vec,MAT3vec); +void MAT3perp_vec(MAT3vec result_vec, MAT3vec vec, int is_unit); + +#endif MAT3_HAS_BEEN_INCLUDED + diff --git a/Main/mesh2ogl.c b/Main/mesh2ogl.c new file mode 100644 index 000000000..90a8b8dcf --- /dev/null +++ b/Main/mesh2ogl.c @@ -0,0 +1,115 @@ +/************************************************************************** + * mesh2ogl.c -- walk through a mesh data structure and make ogl calls + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +/* assumes -I/usr/include/mesa in compile command */ +#include "gltk.h" + +#include "../scenery/mesh.h" +#include "mat3.h" + + +/* Sets the first vector to be the cross-product of the last two + vectors. */ +static void mat3_cross_product(float result_vec[3], register float vec1[3], + register float vec2[3]) { + float tempvec[3]; + register float *temp = tempvec; + + temp[0] = vec1[1] * vec2[2] - vec1[2] * vec2[1]; + temp[1] = vec1[2] * vec2[0] - vec1[0] * vec2[2]; + temp[2] = vec1[0] * vec2[1] - vec1[1] * vec2[0]; + + MAT3_COPY_VEC(result_vec, temp); +} + + +/* walk through mesh and make ogl calls */ +GLint mesh_to_ogl(struct mesh *m) { + GLint mesh; + static GLfloat color[4] = { 0.3, 0.7, 0.2, 1.0 }; + + float x1, y1, x2, y2, z11, z12, z21, z22; + float v1[3], v2[3], normal[3]; + int i, j, istep, jstep, iend, jend; + float temp; + + istep = jstep = 50; /* Detail level 1 -- 1200 ... */ + + mesh = glGenLists(1); + glNewList(mesh, GL_COMPILE); + glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, color ); + glShadeModel( GL_FLAT ); /* glShadeModel( GL_SMOOTH ); */ + + iend = m->cols - 1; + jend = m->rows - 1; + + y1 = m->originy; + y2 = y1 + (m->col_step * istep); + + for ( i = 0; i < iend; i += istep ) { + x1 = m->originx; + x2 = x1 + (m->row_step * jstep); + for ( j = 0; j < jend; j += jstep ) { + z11 = 0.12 * m->mesh_data[j * m->rows + i ]; + z12 = 0.12 * m->mesh_data[j * m->rows + (i+istep)]; + z21 = 0.12 * m->mesh_data[(j+jstep) * m->rows + i ]; + z22 = 0.12 * m->mesh_data[(j+jstep) * m->rows + (i+istep)]; + + /* printf("x1 = %f y1 = %f\n", x1, y1); + printf("x2 = %f y2 = %f\n", x2, y2); + printf("z11 = %f z12 = %f z21 = %f z22 = %f\n", + z11, z12, z21, z22); */ + + v1[0] = x2 - x1; v1[1] = 0; v1[2] = z21 - z11; + v2[0] = x2 - x1; v2[1] = y2 - y1; v2[2] = z22 - z11; + mat3_cross_product(normal, v1, v2); + MAT3_NORMALIZE_VEC(normal,temp); + glNormal3fv(normal); + glBegin(GL_POLYGON); + glVertex3f(x1, y1, z11); + glVertex3f(x2, y1, z21); + glVertex3f(x2, y2, z22); + /* printf("(%f, %f, %f)\n", x1, y1, z11); + printf("(%f, %f, %f)\n", x2, y1, z21); + printf("(%f, %f, %f)\n", x2, y2, z22); */ + glEnd(); + + v1[0] = x2 - x1; v1[1] = y2 - y1; v1[2] = z22 - z11; + v2[0] = 0; v2[1] = y2 - y1; v2[2] = z12 - z11; + mat3_cross_product(normal, v1, v2); + MAT3_NORMALIZE_VEC(normal,temp); + glNormal3fv(normal); + glBegin(GL_POLYGON); + glVertex3f(x1, y1, z11); + glVertex3f(x2, y2, z22); + glVertex3f(x1, y2, z12); + /* printf("(%f, %f, %f)\n", x1, y1, z11); + printf("(%f, %f, %f)\n", x2, y2, z22); + printf("(%f, %f, %f)\n", x1, y2, z12); */ + glEnd(); + + x1 = x2; + x2 = x1 + (m->row_step * jstep); + } + y1 = y2; + y2 = y1 + (m->col_step * istep); + } + + glEndList(); + + return(mesh); +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:05:52 curt +/* Initial revision. +/* + */ diff --git a/Scenery/Makefile b/Scenery/Makefile new file mode 100644 index 000000000..e3a5c1f77 --- /dev/null +++ b/Scenery/Makefile @@ -0,0 +1,75 @@ +#--------------------------------------------------------------------------- +# Makefile +# +# Written by Curtis Olson, started May 1997. +# +# $Id$ +# (Log is kept at end of this file) +#--------------------------------------------------------------------------- + + +TARGET = libscenery.a + +CFILES = scanner.c parser.c common.c mesh.c +HFILES = +LFILES = scanner.l +YFILES = parser.y +OFILES = $(CFILES:.c=.o) + +CC = gcc +CFLAGS = -g -Wall +# CFLAGS = -O2 -Wall + +FLEX = flex +BISON = bison -v +AR = ar + +INCLUDES = + +LIBS = -lfl + + +#--------------------------------------------------------------------------- +# Primary Targets +#--------------------------------------------------------------------------- + +all: $(TARGET) + +$(TARGET): $(OFILES) $(HFILES) + $(AR) rv $(TARGET) $(OFILES) + +clean: + rm -f *.o $(TARGET) *~ core + + +#--------------------------------------------------------------------------- +# Secondary Targets +#--------------------------------------------------------------------------- + +scanner.c: scanner.l parser.h + $(FLEX) -oscanner.c scanner.l + +scanner.o: scanner.c + $(CC) $(CFLAGS) -c scanner.c + +parser.h: parser.y + $(BISON) -o parser.c -d parser.y + +parser.c: parser.y common.h mesh.h scenery.h + $(BISON) -o parser.c parser.y + +parser.o: parser.c + $(CC) $(CFLAGS) -c parser.c + +common.o: common.c common.h + $(CC) $(CFLAGS) -c common.c + +mesh.o: mesh.c mesh.h common.h + $(CC) $(CFLAGS) -c mesh.c + + +#--------------------------------------------------------------------------- +# $Log$ +# Revision 1.1 1997/05/16 16:07:02 curt +# Initial revision. +# diff --git a/Scenery/common.c b/Scenery/common.c new file mode 100644 index 000000000..a1009bac9 --- /dev/null +++ b/Scenery/common.c @@ -0,0 +1,34 @@ +/************************************************************************** + * common.c -- common utilities and support routines for the parser + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#include "common.h" + + +/* strip the enclosing quotes from a string, works within the current + string and thus is destructive. "hello" ==> hello */ +char *strip_quotes(char *s) { + int len; + + /* strip first character */ + s++; + + /* toast last character */ + len = strlen(s); + s[len-1] = '\0'; + + return(s); +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:07:03 curt +/* Initial revision. +/* + */ diff --git a/Scenery/common.h b/Scenery/common.h new file mode 100644 index 000000000..d3e01d68f --- /dev/null +++ b/Scenery/common.h @@ -0,0 +1,30 @@ +/************************************************************************** + * common.h -- common utilities and support routines for the parser + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef COMMON_H +#define COMMON_H + + +/* Maximum length for an identifier */ +#define MAX_IDENT_LEN 33 /* 32 + 1 for string terminator */ + +/* strip the enclosing quotes from a string, works within the current + string and thus is destructive. */ +char *strip_quotes(char *s); + + +#endif COMMON_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:07:04 curt +/* Initial revision. +/* + */ diff --git a/Scenery/mesh.c b/Scenery/mesh.c new file mode 100644 index 000000000..d939d4086 --- /dev/null +++ b/Scenery/mesh.c @@ -0,0 +1,92 @@ +/************************************************************************** + * mesh.c -- data structures and routines for processing terrain meshes + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#include +#include +#include /* atof(), atoi() */ +#include + +#include "mesh.h" +#include "common.h" + + +/* return a pointer to a new mesh structure (no data array allocated yet) */ +struct mesh *(new_mesh)() { + struct mesh *mesh_ptr; + + mesh_ptr = (struct mesh *)malloc(sizeof(struct mesh)); + + if ( mesh_ptr == 0 ) { + printf("Virtual memory exceeded\n"); + exit(-1); + } + + mesh_ptr->cur_row = 0; + mesh_ptr->cur_col = 0; + + return(mesh_ptr); +} + + +/* return a pointer to a dynamically allocated array */ +float *(new_mesh_data)(int nrows, int ncols) { + float *mesh_data_ptr; + + mesh_data_ptr = (float *)malloc(nrows * ncols * sizeof(float)); + + if ( mesh_data_ptr == 0 ) { + printf("Virtual memory exceeded\n"); + exit(-1); + } + + printf("Allocated float(%d, %d)\n", nrows, ncols); + + return(mesh_data_ptr); +} + + +/* set the option name in the mesh data structure */ +void mesh_set_option_name(struct mesh *m, char *name) { + if ( strlen(name) < MAX_IDENT_LEN ) { + strcpy(m->option_name, name); + } else { + strncpy(m->option_name, name, MAX_IDENT_LEN - 1); + m->option_name[MAX_IDENT_LEN - 1] = '\0'; + } +} + +/* set an option value in the mesh data structure */ +void mesh_set_option_value(struct mesh *m, char *value) { + printf("Setting %s to %s\n", m->option_name, value); + + if ( strcmp(m->option_name, "originx") == 0 ) { + m->originx = atof(value); + } else if ( strcmp(m->option_name, "originy") == 0 ) { + m->originy = atof(value); + } else if ( strcmp(m->option_name, "rows") == 0 ) { + m->rows = atoi(value); + } else if ( strcmp(m->option_name, "cols") == 0 ) { + m->cols = atoi(value); + } else if ( strcmp(m->option_name, "row_step") == 0 ) { + m->row_step = atof(value); + } else if ( strcmp(m->option_name, "col_step") == 0 ) { + m->col_step = atof(value); + } else { + printf("Unknown option %s with value %s, ignoring ...\n", + m->option_name, value); + } +} + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:07:04 curt +/* Initial revision. +/* + */ diff --git a/Scenery/mesh.h b/Scenery/mesh.h new file mode 100644 index 000000000..fd186143b --- /dev/null +++ b/Scenery/mesh.h @@ -0,0 +1,53 @@ +/************************************************************************** + * mesh.h -- data structures and routines for processing terrain meshes + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef MESH_H +#define MESH_H + + +struct mesh { + /* start coordinates (in arc seconds) */ + double originx, originy; + + /* number of rows and columns */ + int rows, cols; + + /* Distance between row and column data points (in arc seconds) */ + double row_step, col_step; + + /* pointer to the actual mesh data dynamically allocated */ + float *mesh_data; + + /* a temporary values for the parser to use */ + char option_name[32]; + int cur_row, cur_col; +}; + + +/* return a pointer to a new mesh structure (no data array allocated yet) */ +struct mesh *(new_mesh)(); + +/* return a pointer to a dynamically allocated array */ +float *(new_mesh_data)(int nrows, int ncols); + +/* set the option name in the mesh data structure */ +void mesh_set_option_name(struct mesh *m, char *name); + +/* set an option value in the mesh data structure */ +void mesh_set_option_value(struct mesh *m, char *value); + +#endif MESH_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:07:05 curt +/* Initial revision. +/* + */ diff --git a/Scenery/parser.h b/Scenery/parser.h new file mode 100644 index 000000000..36120b348 --- /dev/null +++ b/Scenery/parser.h @@ -0,0 +1,24 @@ +#ifndef YYSTYPE +#define YYSTYPE int +#endif +#define IncludeSym 258 +#define MeshSym 259 +#define RowSym 260 +#define ChunkSym 261 +#define BoundsSym 262 +#define PlaceSym 263 +#define Identifier 264 +#define Number 265 +#define StringLiteral 266 +#define HashSym 267 +#define EqualSym 268 +#define LBraceSym 269 +#define RBraceSym 270 +#define LParenSym 271 +#define RParenSym 272 +#define CommaSym 273 +#define BadStringLiteral 274 +#define ErrorMisc 275 + + +extern YYSTYPE yylval; diff --git a/Scenery/parser.y b/Scenery/parser.y new file mode 100644 index 000000000..a69976662 --- /dev/null +++ b/Scenery/parser.y @@ -0,0 +1,243 @@ +/************************************************************************** + * parser.y -- scenery file parser + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +/* C pass through */ +%{ +#include +#include +#include +#include + +#include "common.h" +#include "mesh.h" +#include "scenery.h" + + + /*#DEFINE YYDEBUG 1 */ + + /* interfacing with scanner.l (lex) */ + extern int line_num; + extern char *yytext; + int push_input_stream ( char *input ); + + /* we must define this ourselves */ + int yyerror(char *s); + + /* handle for a mesh structure */ + struct mesh *mesh_ptr; +%} + + +/* top level reserved words */ +%token IncludeSym MeshSym RowSym ChunkSym BoundsSym PlaceSym + +/* basic tokens */ +%token Identifier Number StringLiteral + +/* symbol tokens */ +%token HashSym EqualSym LBraceSym RBraceSym LParenSym RParenSym CommaSym + +/* error tokens */ +%token BadStringLiteral ErrorMisc + + +/* Start Symbol */ +%start start + + +/* Rules Section */ +%% + +start : + object_list +; + +object_list : + object + | object_list object +; + +object : + include + | mesh + | chunk +; + +/* includes */ +include : + HashSym IncludeSym StringLiteral + { + yytext = strip_quotes(yytext); + printf("Need to include %s\n", yytext); + push_input_stream(yytext); + } +; + +/* terrain mesh rules */ +mesh : + MeshSym + { + /* allocate a structure for this terrain mesh */ + mesh_ptr = new_mesh(); + } + Identifier LBraceSym mesh_body RBraceSym + { + /* The following two lines *SHOULD* be here, they are just temporarily + commented out until the scenery mngmt stuff gets hashed out. */ + + /* free up the mem used by this structure */ + /* free(mesh_ptr->mesh_data); */ + /* free(mesh_ptr); */ + } +; + +mesh_body : + mesh_header_list + { + /* We've read the headers, so allocate a pseudo 2d array */ + mesh_ptr->mesh_data = new_mesh_data(mesh_ptr->rows, mesh_ptr->cols); + printf("Beginning to read mesh data\n"); + } + mesh_row_list +; + +mesh_header_list : + mesh_header + | mesh_header_list mesh_header +; + +mesh_header : + Identifier + { + mesh_set_option_name(mesh_ptr, yytext); + } + EqualSym Number + { + mesh_set_option_value(mesh_ptr, yytext); + } +; + +mesh_row_list : + mesh_row + | mesh_row_list mesh_row +; + +mesh_row : + RowSym + { + /* printf("Ready to read a row\n"); */ + mesh_ptr->cur_col = 0; + } + EqualSym LParenSym mesh_row_items RParenSym + { + mesh_ptr->cur_row++; + } +; + +mesh_row_items : + mesh_item + | mesh_row_items mesh_item +; + +mesh_item : + Number + { + /* mesh data is a pseudo 2d array */ + mesh_ptr->mesh_data[mesh_ptr->cur_row * mesh_ptr->rows + + mesh_ptr->cur_col] = atof(yytext); + mesh_ptr->cur_col++; + } +; + +/* chunk rules */ +chunk : ChunkSym Identifier LBraceSym chunk_body RBraceSym + ; + +chunk_body : chunk_header_list chunk_bounds place_list + ; + +chunk_header_list : chunk_header + | chunk_header_list chunk_header + ; + +chunk_header : Identifier EqualSym StringLiteral + ; + +chunk_bounds : BoundsSym EqualSym LBraceSym + Number CommaSym + Number CommaSym + Number CommaSym + Number CommaSym + Number CommaSym + Number CommaSym + Number CommaSym + Number + RBraceSym + ; + +/* place rules */ +place_list : place | place_list place + ; + +place : PlaceSym Identifier LBraceSym place_options_list RBraceSym + ; + +place_options_list : place_option | place_options_list place_option + ; + +place_option : Identifier EqualSym place_value + ; + +place_value : geodetic_coord | Number + ; + +geodetic_coord : LParenSym Number CommaSym Number CommaSym + Number RParenSym + ; + + +/* C Function Section */ +%% + + +int yyerror(char *s) { + printf("Error: %s at line %d.\n", s, line_num); + return 0; +} + + +/* this is a simple main for testing the parser */ + +/* +int main(int argc, char **argv) { +#ifdef YYDEBUG + yydebug = 1; +#endif + + printf("input file = %s\n", argv[1]); + push_input_stream(argv[1]); + yyparse(); + + return 0; +} +*/ + + +/* parse a scenery file */ +int parse_scenery(char *file) { + int result; + + printf("input file = %s\n", file); + push_input_stream(file); + result = yyparse(); + + /* return(result) */ + return(mesh_ptr); +} diff --git a/Scenery/scanner.l b/Scenery/scanner.l new file mode 100644 index 000000000..8dd5a486b --- /dev/null +++ b/Scenery/scanner.l @@ -0,0 +1,211 @@ +/************************************************************************** + * scenery.l -- Lexical Analyzer for scenery files + * + * Written by Curtis Olson, started May 1997. + * + * NOTE: Compiles with flex and gcc. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +/* C Pass Through */ +%{ + #include + #include "parser.h" + + int line_num = 1; + char c; + + /* custom print routine */ + static int scanner_debug = 0; + static int scanner_msg_len; + static char scanner_msg[1024]; + + static void scanner_print(char *s) { + if ( scanner_debug ) { + printf("%s", s); + } + } + + /* Routines to manage a stack of nested input buffers (for + processing the #include directive */ + #define MAX_INCLUDE_DEPTH 10 + static YY_BUFFER_STATE include_stack[MAX_INCLUDE_DEPTH]; + static int include_stack_ptr = 0; + + int push_input_stream ( char *input ) { + FILE *yyin_save; + if ( include_stack_ptr >= MAX_INCLUDE_DEPTH ) { + fprintf( stderr, "Scanner says: Includes nested too deeply\n" ); + return(0); + } + + /* save yyin in case the following fails */ + yyin_save = yyin; + + yyin = fopen( input, "r" ); + if ( ! yyin ) { + fprintf( stderr, "Scanner says: cannot open '%s'\n", input ); + + /* The failed attempt destroyed yyin, so restore it */ + yyin = yyin_save; + + return(0); + } + + include_stack[include_stack_ptr++] = YY_CURRENT_BUFFER; + yy_switch_to_buffer( yy_create_buffer(yyin, YY_BUF_SIZE) ); + + return(1); + } + + int pop_input_stream () { + if ( --include_stack_ptr < 1 ) { + /* end of last input stream */ + return(0); + } else { + /* end of current input stream, restore previous and continue */ + yy_delete_buffer( YY_CURRENT_BUFFER ); + yy_switch_to_buffer( include_stack[include_stack_ptr] ); + return(1); + } + } +%} + + +/* Definitions */ +letter [A-Za-z] +digit [0-9] +connecter [_-] +ident {letter}({letter}|{digit}|{connecter})* + +integer [+-]?{digit}+ + +plain_real {integer}"."{integer} +short_exp_real {integer}[Ee][+-]?{integer} +exp_real {plain_real}[Ee][+-]?{integer} +real [+-]?{plain_real}|{short_exp_real}|{exp_real} + +number {real}|{integer} + +string \"[^"\n]+\" + +bad_string \"([^"\n]|\n)+\" + +ws [ \t]+ +other . + + +/* Rules */ +%% + +include { scanner_print("return IncludeSym\n"); + return IncludeSym; + } + +mesh { scanner_print("return MeshSym\n"); + return MeshSym; + } + +row { scanner_print("return RowSym\n"); + return RowSym; + } + +chunk { scanner_print("return ChunkSym\n"); + return ChunkSym; + } + +bounds { scanner_print("return BoundsSym\n"); + return BoundsSym; + } + +place { scanner_print("return PlaceSym\n"); + return PlaceSym; + } + +{ident} { scanner_msg_len = snprintf(scanner_msg, 1024, + "return Identifier = %s\n", yytext); + scanner_msg[scanner_msg_len] = '\0'; + scanner_print(scanner_msg); + return Identifier; + } + +{number} { scanner_print("return Number\n"); + return Number; + } + +{string} { scanner_msg_len = snprintf(scanner_msg, 1024, + "return StringLiteral = %s\n", yytext); + scanner_msg[scanner_msg_len] = '\0'; + scanner_print(scanner_msg); + return StringLiteral; + } + +{bad_string} { scanner_msg_len = snprintf(scanner_msg, 1024, + "return BadStringLiteral = %s\n", yytext); + scanner_msg[scanner_msg_len] = '\0'; + scanner_print(scanner_msg); + return BadStringLiteral; + } + +"\n" { line_num++; + scanner_msg_len = snprintf(scanner_msg, 1024, + "Line number = %d\n", line_num); + scanner_msg[scanner_msg_len] = '\0'; + scanner_print(scanner_msg); + } + +"#" { scanner_print("return HashSym\n"); + return HashSym; + } + +"=" { scanner_print("return EqualSym\n"); + return EqualSym; + } + +"," { scanner_print("return CommaSym\n"); + return CommaSym; + } + +"{" { scanner_print("return LBraceSym\n"); + return LBraceSym; + } + +"}" { scanner_print("return RBraceSym\n"); + return RBraceSym; + } + +"(" { scanner_print("return LParenSym\n"); + return LParenSym; + } + +")" { scanner_print("return RParenSym\n"); + return RParenSym; + } + +"//" { /* burn through "#" comment */ + c = input(); + while ( (c != '\n') ) { + c = input(); + /* scanner_print("%c", c); */ + } + unput(c); + /* line_num++; */ + } + +{ws} { ; } + +{other} { scanner_msg_len = snprintf(scanner_msg, 1024, + "Scanned some unexpected text = `%s'\n", yytext); + scanner_msg[scanner_msg_len] = '\0'; + scanner_print(scanner_msg); + return ErrorMisc; + } + +<> { if ( pop_input_stream() == 0 ) { + /* End of last input stream */ + yyterminate(); + } + } diff --git a/Scenery/scenery.h b/Scenery/scenery.h new file mode 100644 index 000000000..970aa343e --- /dev/null +++ b/Scenery/scenery.h @@ -0,0 +1,26 @@ +/************************************************************************** + * scenery.h -- data structures and routines for processing terrain meshes + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef SCENERY_H +#define SCENERY_H + + +/* parse a scenery file */ +int parse_scenery(char *file); + + +#endif SCENERY_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:07:07 curt +/* Initial revision. +/* + */ diff --git a/Simulator/limits.h b/Simulator/limits.h new file mode 100644 index 000000000..ec1b5ae71 --- /dev/null +++ b/Simulator/limits.h @@ -0,0 +1,26 @@ +/************************************************************************** + * limits.h -- program wide limits + * + * Written by Curtis Olson, started May 1997. + * + * $Id$ + * (Log is kept at end of this file) + **************************************************************************/ + + +#ifndef LIMITS_H +#define LIMITS_H + + +/* Maximum number of engines for a single aircraft */ +#define MAX_ENGINES 10 + + +#endif LIMITS_H + + +/* $Log$ +/* Revision 1.1 1997/05/16 16:08:00 curt +/* Initial revision. +/* + */ -- 2.39.2