Split celx.cpp into separate files for each object.

ver1_6_1
Chris Laurel 2008-04-13 20:56:56 +00:00
parent 93660632d8
commit d580102e33
24 changed files with 8592 additions and 7520 deletions

View File

@ -16,6 +16,15 @@
8844CBC60D80DB020059AA3A /* precession.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8844CBC20D80DB020059AA3A /* precession.cpp */; };
8844CF0B0D931D4E0059AA3A /* planetgrid.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 8844CF080D931D4E0059AA3A /* planetgrid.cpp */; };
886544290DAC3CC4007C8027 /* visibleregion.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 886544270DAC3CC4007C8027 /* visibleregion.cpp */; };
88654A170DB29585007C8027 /* celx_celestia.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A040DB29585007C8027 /* celx_celestia.cpp */; };
88654A190DB29585007C8027 /* celx_frame.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A060DB29585007C8027 /* celx_frame.cpp */; };
88654A1B0DB29585007C8027 /* celx_gl.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A080DB29585007C8027 /* celx_gl.cpp */; };
88654A1E0DB29585007C8027 /* celx_object.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A0B0DB29585007C8027 /* celx_object.cpp */; };
88654A200DB29585007C8027 /* celx_observer.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A0D0DB29585007C8027 /* celx_observer.cpp */; };
88654A220DB29585007C8027 /* celx_phase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A0F0DB29585007C8027 /* celx_phase.cpp */; };
88654A240DB29585007C8027 /* celx_position.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A110DB29585007C8027 /* celx_position.cpp */; };
88654A260DB29585007C8027 /* celx_rotation.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A130DB29585007C8027 /* celx_rotation.cpp */; };
88654A280DB29585007C8027 /* celx_vector.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 88654A150DB29585007C8027 /* celx_vector.cpp */; };
E50CCA390C065BF900E9C76A /* eclipsefinder.cpp in Sources */ = {isa = PBXBuildFile; fileRef = E50CCA370C065BF900E9C76A /* eclipsefinder.cpp */; };
E50CCA590C065F0100E9C76A /* EclipseFinder.nib in Resources */ = {isa = PBXBuildFile; fileRef = E50CCA530C065F0100E9C76A /* EclipseFinder.nib */; };
E50CCA6A0C06632A00E9C76A /* EclipseFinderController.mm in Sources */ = {isa = PBXBuildFile; fileRef = E50CCA680C06632A00E9C76A /* EclipseFinderController.mm */; };
@ -509,6 +518,25 @@
8844CF0A0D931D4E0059AA3A /* referencemark.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = referencemark.h; path = ../src/celengine/referencemark.h; sourceTree = SOURCE_ROOT; };
886544270DAC3CC4007C8027 /* visibleregion.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = visibleregion.cpp; path = ../src/celengine/visibleregion.cpp; sourceTree = SOURCE_ROOT; };
886544280DAC3CC4007C8027 /* visibleregion.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = visibleregion.h; path = ../src/celengine/visibleregion.h; sourceTree = SOURCE_ROOT; };
88654A040DB29585007C8027 /* celx_celestia.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_celestia.cpp; path = ../src/celestia/celx_celestia.cpp; sourceTree = SOURCE_ROOT; };
88654A050DB29585007C8027 /* celx_celestia.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_celestia.h; path = ../src/celestia/celx_celestia.h; sourceTree = SOURCE_ROOT; };
88654A060DB29585007C8027 /* celx_frame.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_frame.cpp; path = ../src/celestia/celx_frame.cpp; sourceTree = SOURCE_ROOT; };
88654A070DB29585007C8027 /* celx_frame.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_frame.h; path = ../src/celestia/celx_frame.h; sourceTree = SOURCE_ROOT; };
88654A080DB29585007C8027 /* celx_gl.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_gl.cpp; path = ../src/celestia/celx_gl.cpp; sourceTree = SOURCE_ROOT; };
88654A090DB29585007C8027 /* celx_gl.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_gl.h; path = ../src/celestia/celx_gl.h; sourceTree = SOURCE_ROOT; };
88654A0A0DB29585007C8027 /* celx_internal.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_internal.h; path = ../src/celestia/celx_internal.h; sourceTree = SOURCE_ROOT; };
88654A0B0DB29585007C8027 /* celx_object.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_object.cpp; path = ../src/celestia/celx_object.cpp; sourceTree = SOURCE_ROOT; };
88654A0C0DB29585007C8027 /* celx_object.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_object.h; path = ../src/celestia/celx_object.h; sourceTree = SOURCE_ROOT; };
88654A0D0DB29585007C8027 /* celx_observer.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_observer.cpp; path = ../src/celestia/celx_observer.cpp; sourceTree = SOURCE_ROOT; };
88654A0E0DB29585007C8027 /* celx_observer.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_observer.h; path = ../src/celestia/celx_observer.h; sourceTree = SOURCE_ROOT; };
88654A0F0DB29585007C8027 /* celx_phase.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_phase.cpp; path = ../src/celestia/celx_phase.cpp; sourceTree = SOURCE_ROOT; };
88654A100DB29585007C8027 /* celx_phase.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_phase.h; path = ../src/celestia/celx_phase.h; sourceTree = SOURCE_ROOT; };
88654A110DB29585007C8027 /* celx_position.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_position.cpp; path = ../src/celestia/celx_position.cpp; sourceTree = SOURCE_ROOT; };
88654A120DB29585007C8027 /* celx_position.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_position.h; path = ../src/celestia/celx_position.h; sourceTree = SOURCE_ROOT; };
88654A130DB29585007C8027 /* celx_rotation.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_rotation.cpp; path = ../src/celestia/celx_rotation.cpp; sourceTree = SOURCE_ROOT; };
88654A140DB29585007C8027 /* celx_rotation.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_rotation.h; path = ../src/celestia/celx_rotation.h; sourceTree = SOURCE_ROOT; };
88654A150DB29585007C8027 /* celx_vector.cpp */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.cpp.cpp; name = celx_vector.cpp; path = ../src/celestia/celx_vector.cpp; sourceTree = SOURCE_ROOT; };
88654A160DB29585007C8027 /* celx_vector.h */ = {isa = PBXFileReference; fileEncoding = 30; lastKnownFileType = sourcecode.c.h; name = celx_vector.h; path = ../src/celestia/celx_vector.h; sourceTree = SOURCE_ROOT; };
E50657D70CBFB08500151204 /* imagecapture.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = imagecapture.h; path = ../src/celestia/imagecapture.h; sourceTree = SOURCE_ROOT; };
E50CCA370C065BF900E9C76A /* eclipsefinder.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = eclipsefinder.cpp; path = ../src/celestia/eclipsefinder.cpp; sourceTree = SOURCE_ROOT; };
E50CCA380C065BF900E9C76A /* eclipsefinder.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = eclipsefinder.h; path = ../src/celestia/eclipsefinder.h; sourceTree = SOURCE_ROOT; };
@ -1370,6 +1398,25 @@
F51C6D51029596B4014901DC /* celestia */ = {
isa = PBXGroup;
children = (
88654A040DB29585007C8027 /* celx_celestia.cpp */,
88654A050DB29585007C8027 /* celx_celestia.h */,
88654A060DB29585007C8027 /* celx_frame.cpp */,
88654A070DB29585007C8027 /* celx_frame.h */,
88654A080DB29585007C8027 /* celx_gl.cpp */,
88654A090DB29585007C8027 /* celx_gl.h */,
88654A0A0DB29585007C8027 /* celx_internal.h */,
88654A0B0DB29585007C8027 /* celx_object.cpp */,
88654A0C0DB29585007C8027 /* celx_object.h */,
88654A0D0DB29585007C8027 /* celx_observer.cpp */,
88654A0E0DB29585007C8027 /* celx_observer.h */,
88654A0F0DB29585007C8027 /* celx_phase.cpp */,
88654A100DB29585007C8027 /* celx_phase.h */,
88654A110DB29585007C8027 /* celx_position.cpp */,
88654A120DB29585007C8027 /* celx_position.h */,
88654A130DB29585007C8027 /* celx_rotation.cpp */,
88654A140DB29585007C8027 /* celx_rotation.h */,
88654A150DB29585007C8027 /* celx_vector.cpp */,
88654A160DB29585007C8027 /* celx_vector.h */,
F51C6D8B02959960014901DC /* celestiacore.cpp */,
E56431450776D52D00E2C4A3 /* celx.cpp */,
F51C6D8C02959960014901DC /* configfile.cpp */,
@ -1967,6 +2014,15 @@
8844CBC60D80DB020059AA3A /* precession.cpp in Sources */,
8844CF0B0D931D4E0059AA3A /* planetgrid.cpp in Sources */,
886544290DAC3CC4007C8027 /* visibleregion.cpp in Sources */,
88654A170DB29585007C8027 /* celx_celestia.cpp in Sources */,
88654A190DB29585007C8027 /* celx_frame.cpp in Sources */,
88654A1B0DB29585007C8027 /* celx_gl.cpp in Sources */,
88654A1E0DB29585007C8027 /* celx_object.cpp in Sources */,
88654A200DB29585007C8027 /* celx_observer.cpp in Sources */,
88654A220DB29585007C8027 /* celx_phase.cpp in Sources */,
88654A240DB29585007C8027 /* celx_position.cpp in Sources */,
88654A260DB29585007C8027 /* celx_rotation.cpp in Sources */,
88654A280DB29585007C8027 /* celx_vector.cpp in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};

View File

@ -284,7 +284,16 @@ APP_SOURCES = \
celestia/imagecapture.cpp \
celestia/scriptmenu.cpp \
celestia/url.cpp \
celestia/celx.cpp
celestia/celx.cpp \
celestia/celx_celestia.cpp \
celestia/celx_frame.cpp \
celestia/celx_gl.cpp \
celestia/celx_object.cpp \
celestia/celx_observer.cpp \
celestia/celx_phase.cpp \
celestia/celx_position.cpp \
celestia/celx_rotation.cpp \
celestia/celx_vector.cpp
APP_HEADERS = \
celestia/celestiacore.h \
@ -295,7 +304,17 @@ APP_HEADERS = \
celestia/imagecapture.h \
celestia/scriptmenu.h \
celestia/url.h \
celestia/celx.h
celestia/celx.h \
celestia/celx_celestia.h \
celestia/celx_internal.h \
celestia/celx_frame.h \
celestia/celx_gl.h \
celestia/celx_object.h \
celestia/celx_observer.h \
celestia/celx_phase.h \
celestia/celx_position.h \
celestia/celx_rotation.h \
celestia/celx_vector.h
macx {
APP_SOURCES -= celestia/imagecapture.cpp

View File

@ -46,7 +46,18 @@ WINSOURCES = \
winviewoptsdlg.cpp
if ENABLE_CELX
CELXSOURCES = celx.cpp
CELXSOURCES = \
celx.cpp \
celx_celestia.cpp \
celx_frame.cpp \
celx_gl.cpp \
celx_object.cpp \
celx_observer.cpp \
celx_phase.cpp \
celx_position.cpp \
celx_rotation.cpp \
celx_vector.cpp
endif
if ENABLE_GLUT

View File

@ -49,7 +49,19 @@ RESOURCES=\
$(INTDIR)\celestia.res
!IF "$(CELX)" == "enable"
OBJS=$(OBJS) $(INTDIR)\celx.obj
CELX_OBJS = \
$(INTDIR)\celx.obj \
$(INTDIR)\celx_celestia.cpp \
$(INTDIR)\celx_frame.cpp \
$(INTDIR)\celx_gl.cpp \
$(INTDIR)\celx_object.cpp \
$(INTDIR)\celx_observer.cpp \
$(INTDIR)\celx_phase.cpp \
$(INTDIR)\celx_position.cpp \
$(INTDIR)\celx_rotation.cpp \
$(INTDIR)\celx_vector.cpp
OBJS=$(OBJS) $(CELX_OBJS)
EXTRADEFS=/D "CELX" /D "LUA_VER=$(LUA_VER)"
!IF "$(LUA_VER)" == "0x050100"

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,19 @@
// celx_celestia.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: Celestia object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_celestia.h"
// TODO: This file is just a placeholder now. Need to move all code
// for the Celestia object here from celx.cpp.

View File

@ -0,0 +1,17 @@
// celx_celestia.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: Celestia object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_CELESTIA_H_
#define _CELX_CELESTIA_H_
struct lua_State;
#endif // _CELX_CELESTIA_H_

View File

@ -0,0 +1,252 @@
// celx_frame.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: frame object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_frame.h"
#include "celestiacore.h"
#include <celengine/observer.h>
int frame_new(lua_State* l, const ObserverFrame& f)
{
CelxLua celx(l);
// Use placement new to put the new frame in the userdata block.
void* block = lua_newuserdata(l, sizeof(ObserverFrame));
new (block) ObserverFrame(f);
celx.setClass(Celx_Frame);
return 1;
}
ObserverFrame* to_frame(lua_State* l, int index)
{
CelxLua celx(l);
return static_cast<ObserverFrame*>(celx.checkUserData(index, Celx_Frame));
}
static ObserverFrame* this_frame(lua_State* l)
{
CelxLua celx(l);
ObserverFrame* f = to_frame(l, 1);
if (f == NULL)
{
celx.doError("Bad frame object!");
}
return f;
}
// Convert from frame coordinates to universal.
static int frame_from(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3, "Two or three arguments required for frame:from");
ObserverFrame* frame = this_frame(l);
CelestiaCore* appCore = celx.appCore(AllErrors);
UniversalCoord* uc = NULL;
Quatd* q = NULL;
double jd = 0.0;
if (celx.isType(2, Celx_Position))
{
uc = celx.toPosition(2);
}
else if (celx.isType(2, Celx_Rotation))
{
q = celx.toRotation(2);
}
if (uc == NULL && q == NULL)
{
celx.doError("Position or rotation expected as second argument to frame:from()");
}
jd = celx.safeGetNumber(3, WrongType, "Second arg to frame:from must be a number", appCore->getSimulation()->getTime());
if (uc != NULL)
{
UniversalCoord uc1 = frame->convertToUniversal(*uc, jd);
celx.newPosition(uc1);
}
else
{
Quatd q1 = frame->convertToUniversal(*q, jd);
celx.newRotation(q1);
}
return 1;
}
// Convert from universal to frame coordinates.
static int frame_to(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3, "Two or three arguments required for frame:to");
ObserverFrame* frame = this_frame(l);
CelestiaCore* appCore = celx.appCore(AllErrors);
UniversalCoord* uc = NULL;
Quatd* q = NULL;
double jd = 0.0;
if (celx.isType(2, Celx_Position))
{
uc = celx.toPosition(2);
}
else if (celx.isType(2, Celx_Rotation))
{
q = celx.toRotation(2);
}
if (uc == NULL && q == NULL)
{
celx.doError("Position or rotation expected as second argument to frame:to()");
}
jd = celx.safeGetNumber(3, WrongType, "Second arg to frame:to must be a number", appCore->getSimulation()->getTime());
if (uc != NULL)
{
UniversalCoord uc1 = frame->convertFromUniversal(*uc, jd);
celx.newPosition(uc1);
}
else
{
Quatd q1 = frame->convertFromUniversal(*q, jd);
celx.newRotation(q1);
}
return 1;
}
static int frame_getrefobject(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for frame:getrefobject()");
ObserverFrame* frame = this_frame(l);
if (frame->getRefObject().getType() == Selection::Type_Nil)
{
celx.push(CelxValue());
}
else
{
celx.newObject(frame->getRefObject());
}
return 1;
}
static int frame_gettargetobject(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for frame:gettarget()");
ObserverFrame* frame = this_frame(l);
if (frame->getTargetObject().getType() == Selection::Type_Nil)
{
lua_pushnil(l);
}
else
{
celx.newObject(frame->getTargetObject());
}
return 1;
}
static int frame_getcoordinatesystem(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for frame:getcoordinatesystem()");
ObserverFrame* frame = this_frame(l);
string coordsys;
switch (frame->getCoordinateSystem())
{
case ObserverFrame::Universal:
coordsys = "universal"; break;
case ObserverFrame::Ecliptical:
coordsys = "ecliptic"; break;
case ObserverFrame::Equatorial:
coordsys = "equatorial"; break;
case ObserverFrame::BodyFixed:
coordsys = "bodyfixed"; break;
case ObserverFrame::ObserverLocal:
coordsys = "observer"; break;
case ObserverFrame::PhaseLock:
coordsys = "lock"; break;
case ObserverFrame::Chase:
coordsys = "chase"; break;
default:
coordsys = "invalid";
}
celx.push(coordsys.c_str());
return 1;
}
static int frame_tostring(lua_State* l)
{
CelxLua celx(l);
// TODO: print out the actual information about the frame
celx.push("[Frame]");
return 1;
}
/*! Garbage collection metamethod frame objects.
*/
static int frame_gc(lua_State* l)
{
ObserverFrame* frame = this_frame(l);
// Explicitly call the destructor since the object was created with placement new
frame->~ObserverFrame();
return 0;
}
void CreateFrameMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Frame);
celx.registerMethod("__tostring", frame_tostring);
celx.registerMethod("__gc", frame_gc);
celx.registerMethod("to", frame_to);
celx.registerMethod("from", frame_from);
celx.registerMethod("getcoordinatesystem", frame_getcoordinatesystem);
celx.registerMethod("getrefobject", frame_getrefobject);
celx.registerMethod("gettargetobject", frame_gettargetobject);
lua_pop(l, 1); // remove metatable from stack
}

View File

@ -0,0 +1,22 @@
// celx_frame.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: frame object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_FRAME_H_
#define _CELX_FRAME_H_
struct lua_State;
class ObserverFrame;
extern void CreateFrameMetaTable(lua_State* l);
extern int frame_new(lua_State* l, const ObserverFrame& frame);
extern ObserverFrame* to_frame(lua_State* l, int index);
#endif // _CELX_FRAME_H_

View File

@ -0,0 +1,269 @@
// celx_gl.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: OpenGL functions
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_object.h"
#include <celengine/gl.h>
// ==================== OpenGL ====================
static int glu_LookAt(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(9, 9, "Nine arguments expected for glu.LookAt()");
float ix = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Frustum must be a number", 0.0);
float iy = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Frustum must be a number", 0.0);
float iz = (float)celx.safeGetNumber(3, WrongType, "argument 3 to gl.Frustum must be a number", 0.0);
float cx = (float)celx.safeGetNumber(4, WrongType, "argument 4 to gl.Frustum must be a number", 0.0);
float cy = (float)celx.safeGetNumber(5, WrongType, "argument 5 to gl.Frustum must be a number", 0.0);
float cz = (float)celx.safeGetNumber(6, WrongType, "argument 6 to gl.Frustum must be a number", 0.0);
float ux = (float)celx.safeGetNumber(7, WrongType, "argument 4 to gl.Frustum must be a number", 0.0);
float uy = (float)celx.safeGetNumber(8, WrongType, "argument 5 to gl.Frustum must be a number", 0.0);
float uz = (float)celx.safeGetNumber(9, WrongType, "argument 6 to gl.Frustum must be a number", 0.0);
gluLookAt(ix,iy,iz,cx,cy,cz,ux,uy,uz);
return 0;
}
static int gl_Frustum(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(6, 6, "Six arguments expected for gl.Frustum()");
float ll = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Frustum must be a number", 0.0);
float r = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Frustum must be a number", 0.0);
float b = (float)celx.safeGetNumber(3, WrongType, "argument 3 to gl.Frustum must be a number", 0.0);
float t = (float)celx.safeGetNumber(4, WrongType, "argument 4 to gl.Frustum must be a number", 0.0);
float n = (float)celx.safeGetNumber(5, WrongType, "argument 5 to gl.Frustum must be a number", 0.0);
float f = (float)celx.safeGetNumber(6, WrongType, "argument 6 to gl.Frustum must be a number", 0.0);
glFrustum(ll,r,b,t,n,f);
return 0;
}
static int gl_Ortho(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(6, 6, "Six arguments expected for gl.Ortho()");
float ll = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Ortho must be a number", 0.0);
float r = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Ortho must be a number", 0.0);
float b = (float)celx.safeGetNumber(3, WrongType, "argument 3 to gl.Ortho must be a number", 0.0);
float t = (float)celx.safeGetNumber(4, WrongType, "argument 4 to gl.Ortho must be a number", 0.0);
float n = (float)celx.safeGetNumber(5, WrongType, "argument 5 to gl.Ortho must be a number", 0.0);
float f = (float)celx.safeGetNumber(6, WrongType, "argument 6 to gl.Ortho must be a number", 0.0);
glOrtho(ll,r,b,t,n,f);
return 0;
}
static int glu_Ortho2D(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(4, 4, "Six arguments expected for gl.Ortho2D()");
float ll = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Ortho must be a number", 0.0);
float r = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Ortho must be a number", 0.0);
float b = (float)celx.safeGetNumber(3, WrongType, "argument 3 to gl.Ortho must be a number", 0.0);
float t = (float)celx.safeGetNumber(4, WrongType, "argument 4 to gl.Ortho must be a number", 0.0);
gluOrtho2D(ll,r,b,t);
return 0;
}
static int gl_TexCoord(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Two arguments expected for gl.TexCoord()");
float x = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.TexCoord must be a number", 0.0);
float y = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.TexCoord must be a number", 0.0);
glTexCoord2f(x,y);
return 0;
}
static int gl_TexParameter(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Three arguments expected for gl.TexParameter()");
// TODO: Need to ensure that these values are integers, or better yet use
// names.
float x = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.TexParameter must be a number", 0.0);
float y = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.TexParameter must be a number", 0.0);
float z = (float)celx.safeGetNumber(3, WrongType, "argument 3 to gl.TexParameter must be a number", 0.0);
glTexParameteri((GLint) x, (GLenum) y, (GLenum) z);
return 0;
}
static int gl_Vertex(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Two arguments expected for gl.Vertex()");
float x = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Vertex must be a number", 0.0);
float y = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Vertex must be a number", 0.0);
glVertex2f(x,y);
return 0;
}
static int gl_Color(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(4, 4, "Four arguments expected for gl.Color()");
float r = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Color must be a number", 0.0);
float g = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Color must be a number", 0.0);
float b = (float)celx.safeGetNumber(3, WrongType, "argument 3 to gl.Color must be a number", 0.0);
float a = (float)celx.safeGetNumber(4, WrongType, "argument 4 to gl.Color must be a number", 0.0);
glColor4f(r,g,b,a);
// glColor4f(0.8f, 0.5f, 0.5f, 1.0f);
return 0;
}
static int gl_LineWidth(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "One argument expected for gl.LineWidth()");
float n = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.LineWidth must be a number", 1.0);
glLineWidth(n);
return 0;
}
static int gl_Translate(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Two arguments expected for gl.Translate()");
float x = (float)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Translate must be a number", 0.0);
float y = (float)celx.safeGetNumber(2, WrongType, "argument 2 to gl.Translate must be a number", 0.0);
glTranslatef(x,y,0.0f);
return 0;
}
static int gl_BlendFunc(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Two arguments expected for gl.BlendFunc()");
int i = (int)celx.safeGetNumber(1, WrongType, "argument 1 to gl.BlendFunc must be a number", 0.0);
int j = (int)celx.safeGetNumber(2, WrongType, "argument 2 to gl.BlendFunc must be a number", 0.0);
glBlendFunc(i,j);
return 0;
}
static int gl_Begin(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "One argument expected for gl.Begin()");
int i = (int)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Begin must be a number", 0.0);
glBegin(i);
return 0;
}
static int gl_End(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(0, 0, "No arguments expected for gl.PopMatrix()");
glEnd();
return 0;
}
static int gl_Enable(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "One argument expected for gl.Enable()");
int i = (int)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Enable must be a number", 0.0);
glEnable(i);
return 0;
}
static int gl_Disable(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "One argument expected for gl.Disable()");
int i = (int)celx.safeGetNumber(1, WrongType, "argument 1 to gl.Disable must be a number", 0.0);
glDisable(i);
return 0;
}
static int gl_MatrixMode(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "One argument expected for gl.MatrixMode()");
int i = (int) celx.safeGetNumber(1, WrongType, "argument 1 to gl.MatrixMode must be a number", 0.0);
glMatrixMode(i);
return 0;
}
static int gl_PopMatrix(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(0, 0, "No arguments expected for gl.PopMatrix()");
glPopMatrix();
return 0;
}
static int gl_LoadIdentity(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(0, 0, "No arguments expected for gl.LoadIdentity()");
glLoadIdentity();
return 0;
}
static int gl_PushMatrix(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(0, 0, "No arguments expected for gl.PushMatrix()");
glPushMatrix();
return 0;
}
void LoadLuaGraphicsLibrary(lua_State* l)
{
CelxLua celx(l);
lua_pushstring(l, "gl");
lua_newtable(l);
celx.registerMethod("Frustum", gl_Frustum);
celx.registerMethod("Ortho", gl_Ortho);
celx.registerMethod("Color", gl_Color);
celx.registerMethod("LineWidth", gl_LineWidth);
celx.registerMethod("TexCoord", gl_TexCoord);
celx.registerMethod("TexParameter", gl_TexParameter);
celx.registerMethod("Vertex", gl_Vertex);
celx.registerMethod("Translate", gl_Translate);
celx.registerMethod("BlendFunc", gl_BlendFunc);
celx.registerMethod("Begin", gl_Begin);
celx.registerMethod("End", gl_End);
celx.registerMethod("Enable", gl_Enable);
celx.registerMethod("Disable", gl_Disable);
celx.registerMethod("MatrixMode", gl_MatrixMode);
celx.registerMethod("PopMatrix", gl_PopMatrix);
celx.registerMethod("LoadIdentity", gl_LoadIdentity);
celx.registerMethod("PushMatrix", gl_PushMatrix);
celx.registerValue("QUADS", GL_QUADS);
celx.registerValue("LIGHTING", GL_LIGHTING);
celx.registerValue("POINTS", GL_POINTS);
celx.registerValue("LINES", GL_LINES);
celx.registerValue("LINE_LOOP", GL_LINE_LOOP);
celx.registerValue("LINE_SMOOTH", GL_LINE_SMOOTH);
celx.registerValue("POLYGON", GL_POLYGON);
celx.registerValue("PROJECTION", GL_PROJECTION);
celx.registerValue("MODELVIEW", GL_MODELVIEW);
celx.registerValue("BLEND", GL_BLEND);
celx.registerValue("TEXTURE_2D", GL_TEXTURE_2D);
celx.registerValue("TEXTURE_MAG_FILTER", GL_TEXTURE_MAG_FILTER);
celx.registerValue("TEXTURE_MIN_FILTER", GL_TEXTURE_MIN_FILTER);
celx.registerValue("LINEAR", GL_LINEAR);
celx.registerValue("NEAREST", GL_NEAREST);
celx.registerValue("SRC_ALPHA", GL_SRC_ALPHA);
celx.registerValue("ONE_MINUS_SRC_ALPHA", GL_ONE_MINUS_SRC_ALPHA);
lua_settable(l, LUA_GLOBALSINDEX);
lua_pushstring(l, "glu");
lua_newtable(l);
celx.registerMethod("LookAt", glu_LookAt);
celx.registerMethod("Ortho2D", glu_Ortho2D);
lua_settable(l, LUA_GLOBALSINDEX);
}

View File

@ -0,0 +1,19 @@
// celx_gl.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: OpenGL functions
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_GL_H_
#define _CELX_GL_H_
struct lua_State;
extern void LoadLuaGraphicsLibrary(lua_State* l);
#endif // _CELX_GL_H_

View File

@ -0,0 +1,173 @@
// celx_internal.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia. Internals that should only
// be needed by modules that implement a celx object.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_INTERNAL_H_
#define _CELX_INTERNAL_H_
#include <map>
#include <string>
class CelestiaCore;
class TimelinePhase;
enum
{
Celx_Celestia = 0,
Celx_Observer = 1,
Celx_Object = 2,
Celx_Vec3 = 3,
Celx_Matrix = 4,
Celx_Rotation = 5,
Celx_Position = 6,
Celx_Frame = 7,
Celx_CelScript= 8,
Celx_Font = 9,
Celx_Image = 10,
Celx_Texture = 11,
Celx_Phase = 12,
};
// select which type of error will be fatal (call lua_error) and
// which will return a default value instead
enum FatalErrors
{
NoErrors = 0,
WrongType = 1,
WrongArgc = 2,
AllErrors = WrongType | WrongArgc,
};
class CelxLua;
class CelxValue
{
public:
enum CelxType
{
Celx_Number,
Celx_String,
Celx_Nil,
};
CelxValue() : type(Celx_Nil) {}
CelxValue(double d) : type(Celx_Number), value_number(d) {}
CelxValue(const char* s) : type(Celx_String), value_cstring(s) {}
void push(lua_State* l) const
{
switch (type)
{
case Celx_Number: lua_pushnumber(l, value_number); break;
case Celx_String: lua_pushstring(l, value_cstring); break;
case Celx_Nil: lua_pushnil(l); break;
}
}
private:
CelxType type;
union
{
double value_number;
char* value_string;
const char* value_cstring;
};
};
class CelxLua
{
public:
CelxLua(lua_State* l);
~CelxLua();
bool isType(int index, int type) const;
void setClass(int id);
void pushClassName(int id);
void* checkUserData(int index, int id);
void doError(const char* errorMessage);
void checkArgs(int minArgs, int maxArgs, const char* errorMessage);
void createClassMetatable(int id);
void registerMethod(const char* name, lua_CFunction fn);
void registerValue(const char* name, float value);
void setTable(const char* field, lua_Number value);
void setTable(const char* field, const char* value);
void newFrame(const ObserverFrame& f);
void newVector(const Vec3d& v);
void newRotation(const Quatd& q);
void newPosition(const UniversalCoord& uc);
void newObject(const Selection& sel);
void newPhase(const TimelinePhase& phase);
Vec3d* toVector(int n);
Quatd* toRotation(int n);
UniversalCoord* toPosition(int n);
Selection* toObject(int n);
ObserverFrame* toFrame(int n);
void push(const CelxValue& v1);
void push(const CelxValue& v1, const CelxValue& v2);
CelestiaCore* appCore(FatalErrors fatalErrors = NoErrors);
lua_Number safeGetNumber(int index,
FatalErrors fatalErrors = AllErrors,
const char* errorMessage = "Numeric argument expected",
lua_Number defaultValue = 0.0);
const char* safeGetString(int index,
FatalErrors fatalErrors = AllErrors,
const char* errorMessage = "String argument expected");
bool safeGetBoolean(int index,
FatalErrors fatalErrors = AllErrors,
const char* errorMsg = "Boolean argument expected",
bool defaultValue = false);
LuaState* getLuaStateObject();
// String to flag mappings
typedef std::map<std::string, uint32> FlagMap;
typedef std::map<std::string, Color*> ColorMap;
static void initMaps();
static void initRenderFlagMap();
static void initLabelFlagMap();
static void initBodyTypeMap();
static void initLocationFlagMap();
static void initOverlayElementMap();
static void initOrbitVisibilityMap();
static void initLabelColorMap();
static void initLineColorMap();
static FlagMap RenderFlagMap;
static FlagMap LabelFlagMap;
static FlagMap LocationFlagMap;
static FlagMap BodyTypeMap;
static FlagMap OverlayElementMap;
static FlagMap OrbitVisibilityMap;
static ColorMap LineColorMap;
static ColorMap LabelColorMap;
static bool mapsInitialized;
static const char* ClassNames[];
private:
lua_State* m_lua;
};
#endif // _CELX_INTERNAL_H_

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,23 @@
// celx_object.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_OBJECT_H_
#define _CELX_OBJECT_H_
struct lua_State;
class Selection;
extern void CreateObjectMetaTable(lua_State* l);
extern void ExtendObjectMetaTable(lua_State* l);
extern Selection* to_object(lua_State* l, int index);
extern int object_new(lua_State* l, const Selection& sel);
#endif // _CELX_OBJECT_H_

View File

@ -0,0 +1,907 @@
// celx_observer.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: observer object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_observer.h"
//#include <celengine/body.h>
//#include <celengine/timelinephase.h>
#include "celestiacore.h"
// ==================== Observer ====================
int observer_new(lua_State* l, Observer* o)
{
CelxLua celx(l);
Observer** ud = static_cast<Observer**>(lua_newuserdata(l, sizeof(Observer*)));
*ud = o;
celx.setClass(Celx_Observer);
return 1;
}
Observer* to_observer(lua_State* l, int index)
{
CelxLua celx(l);
Observer** o = static_cast<Observer**>(lua_touserdata(l, index));
CelestiaCore* appCore = celx.appCore(AllErrors);
// Check if pointer is still valid, i.e. is used by a view:
if (o != NULL && getViewByObserver(appCore, *o) != NULL)
{
return *o;
}
return NULL;
}
static Observer* this_observer(lua_State* l)
{
CelxLua celx(l);
Observer* obs = to_observer(l, 1);
if (obs == NULL)
{
celx.doError("Bad observer object (maybe tried to access a deleted view?)!");
}
return obs;
}
static int observer_isvalid(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for observer:isvalid()");
lua_pushboolean(l, to_observer(l, 1) != NULL);
return 1;
}
static int observer_tostring(lua_State* l)
{
lua_pushstring(l, "[Observer]");
return 1;
}
static int observer_setposition(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for setpos");
Observer* o = this_observer(l);
UniversalCoord* uc = celx.toPosition(2);
if (uc == NULL)
{
celx.doError("Argument to observer:setposition must be a position");
}
o->setPosition(*uc);
return 0;
}
static int observer_setorientation(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for setorientation");
Observer* o = this_observer(l);
Quatd* q = celx.toRotation(2);
if (q == NULL)
{
celx.doError("Argument to observer:setorientation must be a rotation");
}
o->setOrientation(*q);
return 0;
}
static int observer_getorientation(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected to observer:getorientation()");
Observer* o = this_observer(l);
celx.newRotation(o->getOrientation());
return 1;
}
static int observer_rotate(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for rotate");
Observer* o = this_observer(l);
Quatd* q = celx.toRotation(2);
if (q == NULL)
{
celx.doError("Argument to observer:setpos must be a rotation");
}
Quatf qf((float) q->w, (float) q->x, (float) q->y, (float) q->z);
o->rotate(qf);
return 0;
}
static int observer_lookat(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 4, "Two or three arguments required for lookat");
int argc = lua_gettop(l);
Observer* o = this_observer(l);
UniversalCoord* from = NULL;
UniversalCoord* to = NULL;
Vec3d* upd = NULL;
if (argc == 3)
{
to = celx.toPosition(2);
upd = celx.toVector(3);
if (to == NULL)
{
celx.doError("Argument 1 (of 2) to observer:lookat must be of type position");
}
}
else
{
if (argc == 4)
{
from = celx.toPosition(2);
to = celx.toPosition(3);
upd = celx.toVector(4);
if (to == NULL || from == NULL)
{
celx.doError("Argument 1 and 2 (of 3) to observer:lookat must be of type position");
}
}
}
if (upd == NULL)
{
celx.doError("Last argument to observer:lookat must be of type vector");
}
Vec3d nd;
if (from == NULL)
{
nd = (*to) - o->getPosition();
}
else
{
nd = (*to) - (*from);
}
// need Vec3f instead:
Vec3f up = Vec3f((float) upd->x, (float) upd->y, (float) upd->z);
Vec3f n = Vec3f((float) nd.x, (float) nd.y, (float) nd.z);
n.normalize();
Vec3f v = n ^ up;
v.normalize();
Vec3f u = v ^ n;
Quatf qf = Quatf(Mat3f(v, u, -n));
o->setOrientation(qf);
return 0;
}
static int observer_gototable(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Expected one table as argument to goto");
Observer* o = this_observer(l);
if (!lua_istable(l, 2))
{
lua_pushstring(l, "Argument to goto must be a table");
}
Observer::JourneyParams jparams;
jparams.duration = 5.0;
jparams.from = o->getPosition();
jparams.to = o->getPosition();
jparams.initialOrientation = o->getOrientation();
jparams.finalOrientation = o->getOrientation();
jparams.startInterpolation = 0.25;
jparams.endInterpolation = 0.75;
jparams.accelTime = 0.5;
jparams.traj = Observer::Linear;
lua_pushstring(l, "duration");
lua_gettable(l, 2);
jparams.duration = celx.safeGetNumber(3, NoErrors, "", 5.0);
lua_settop(l, 2);
lua_pushstring(l, "from");
lua_gettable(l, 2);
UniversalCoord* from = celx.toPosition(3);
if (from != NULL)
jparams.from = *from;
lua_settop(l, 2);
lua_pushstring(l, "to");
lua_gettable(l, 2);
UniversalCoord* to = celx.toPosition(3);
if (to != NULL)
jparams.to = *to;
lua_settop(l, 2);
lua_pushstring(l, "initialOrientation");
lua_gettable(l, 2);
Quatd* rot1 = celx.toRotation(3);
if (rot1 != NULL)
jparams.initialOrientation = *rot1;
lua_settop(l, 2);
lua_pushstring(l, "finalOrientation");
lua_gettable(l, 2);
Quatd* rot2 = celx.toRotation(3);
if (rot2 != NULL)
jparams.finalOrientation = *rot2;
lua_settop(l, 2);
lua_pushstring(l, "startInterpolation");
lua_gettable(l, 2);
jparams.startInterpolation = celx.safeGetNumber(3, NoErrors, "", 0.25);
lua_settop(l, 2);
lua_pushstring(l, "endInterpolation");
lua_gettable(l, 2);
jparams.endInterpolation = celx.safeGetNumber(3, NoErrors, "", 0.75);
lua_settop(l, 2);
lua_pushstring(l, "accelTime");
lua_gettable(l, 2);
jparams.accelTime = celx.safeGetNumber(3, NoErrors, "", 0.5);
lua_settop(l, 2);
jparams.duration = max(0.0, jparams.duration);
jparams.accelTime = min(1.0, max(0.1, jparams.accelTime));
jparams.startInterpolation = min(1.0, max(0.0, jparams.startInterpolation));
jparams.endInterpolation = min(1.0, max(0.0, jparams.endInterpolation));
// args are in universal coords, let setFrame handle conversion:
ObserverFrame tmp = *(o->getFrame());
o->setFrame(ObserverFrame::Universal, Selection());
o->gotoJourney(jparams);
o->setFrame(tmp);
return 0;
}
// First argument is the target object or position; optional second argument
// is the travel time
static int observer_goto(lua_State* l)
{
CelxLua celx(l);
if (lua_gettop(l) == 2 && lua_istable(l, 2))
{
// handle this in own function
return observer_gototable(l);
}
celx.checkArgs(1, 5, "One to four arguments expected to observer:goto");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
UniversalCoord* uc = celx.toPosition(2);
if (sel == NULL && uc == NULL)
{
celx.doError("First arg to observer:goto must be object or position");
}
double travelTime = celx.safeGetNumber(3, WrongType, "Second arg to observer:goto must be a number", 5.0);
double startInter = celx.safeGetNumber(4, WrongType, "Third arg to observer:goto must be a number", 0.25);
double endInter = celx.safeGetNumber(5, WrongType, "Fourth arg to observer:goto must be a number", 0.75);
if (startInter < 0 || startInter > 1) startInter = 0.25;
if (endInter < 0 || endInter > 1) startInter = 0.75;
// The first argument may be either an object or a position
if (sel != NULL)
{
o->gotoSelection(*sel, travelTime, startInter, endInter, Vec3f(0, 1, 0), ObserverFrame::ObserverLocal);
}
else
{
o->gotoLocation(*uc, o->getOrientation(), travelTime);
}
return 0;
}
static int observer_gotolonglat(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 7, "One to five arguments expected to observer:gotolonglat");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First arg to observer:gotolonglat must be an object");
}
double defaultDistance = sel->radius() * 5.0;
double longitude = celx.safeGetNumber(3, WrongType, "Second arg to observer:gotolonglat must be a number", 0.0);
double latitude = celx.safeGetNumber(4, WrongType, "Third arg to observer:gotolonglat must be a number", 0.0);
double distance = celx.safeGetNumber(5, WrongType, "Fourth arg to observer:gotolonglat must be a number", defaultDistance);
double travelTime = celx.safeGetNumber(6, WrongType, "Fifth arg to observer:gotolonglat must be a number", 5.0);
distance = distance / KM_PER_LY;
Vec3f up(0.0f, 1.0f, 0.0f);
if (lua_gettop(l) >= 7)
{
Vec3d* uparg = celx.toVector(7);
if (uparg == NULL)
{
celx.doError("Sixth argument to observer:gotolonglat must be a vector");
}
up = Vec3f((float)uparg->x, (float)uparg->y, (float)uparg->z);
}
o->gotoSelectionLongLat(*sel, travelTime, distance, (float)longitude, (float)latitude, up);
return 0;
}
// deprecated: wrong name, bad interface.
static int observer_gotolocation(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3,"Expected one or two arguments to observer:gotolocation");
Observer* o = this_observer(l);
double travelTime = celx.safeGetNumber(3, WrongType, "Second arg to observer:gotolocation must be a number", 5.0);
if (travelTime < 0)
travelTime = 0.0;
UniversalCoord* uc = celx.toPosition(2);
if (uc != NULL)
{
o->gotoLocation(*uc, o->getOrientation(), travelTime);
}
else
{
celx.doError("First arg to observer:gotolocation must be a position");
}
return 0;
}
static int observer_gotodistance(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 5, "One to four arguments expected to observer:gotodistance");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First arg to observer:gotodistance must be object");
}
double distance = celx.safeGetNumber(3, WrongType, "Second arg to observer:gotodistance must be a number", 20000);
double travelTime = celx.safeGetNumber(4, WrongType, "Third arg to observer:gotodistance must be a number", 5.0);
Vec3f up(0,1,0);
if (lua_gettop(l) > 4)
{
Vec3d* up_arg = celx.toVector(5);
if (up_arg == NULL)
{
celx.doError("Fourth arg to observer:gotodistance must be a vector");
}
up.x = (float)up_arg->x;
up.y = (float)up_arg->y;
up.z = (float)up_arg->z;
}
o->gotoSelection(*sel, travelTime, astro::kilometersToLightYears(distance), up, ObserverFrame::Universal);
return 0;
}
static int observer_gotosurface(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3, "One to two arguments expected to observer:gotosurface");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First arg to observer:gotosurface must be object");
}
double travelTime = celx.safeGetNumber(3, WrongType, "Second arg to observer:gotosurface must be a number", 5.0);
// This is needed because gotoSurface expects frame to be geosync:
o->geosynchronousFollow(*sel);
o->gotoSurface(*sel, travelTime);
return 0;
}
static int observer_center(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3, "Expected one or two arguments for to observer:center");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:center must be an object");
}
double travelTime = celx.safeGetNumber(3, WrongType, "Second arg to observer:center must be a number", 5.0);
o->centerSelection(*sel, travelTime);
return 0;
}
static int observer_centerorbit(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3, "Expected one or two arguments for to observer:center");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:centerorbit must be an object");
}
double travelTime = celx.safeGetNumber(3, WrongType, "Second arg to observer:centerorbit must be a number", 5.0);
o->centerSelectionCO(*sel, travelTime);
return 0;
}
static int observer_cancelgoto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "Expected no arguments to observer:cancelgoto");
Observer* o = this_observer(l);
o->cancelMotion();
return 0;
}
static int observer_follow(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for observer:follow");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:follow must be an object");
}
o->follow(*sel);
return 0;
}
static int observer_synchronous(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for observer:synchronous");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:synchronous must be an object");
}
o->geosynchronousFollow(*sel);
return 0;
}
static int observer_lock(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for observer:lock");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:phaseLock must be an object");
}
o->phaseLock(*sel);
return 0;
}
static int observer_chase(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for observer:chase");
Observer* o = this_observer(l);
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:chase must be an object");
}
o->chase(*sel);
return 0;
}
static int observer_track(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for observer:track");
Observer* o = this_observer(l);
// If the argument is nil, clear the tracked object
if (lua_isnil(l, 2))
{
o->setTrackedObject(Selection());
}
else
{
// Otherwise, turn on tracking and set the tracked object
Selection* sel = celx.toObject(2);
if (sel == NULL)
{
celx.doError("First argument to observer:center must be an object");
}
o->setTrackedObject(*sel);
}
return 0;
}
static int observer_gettrackedobject(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected to observer:gettrackedobject");
Observer* o = this_observer(l);
celx.newObject(o->getTrackedObject());
return 1;
}
// Return true if the observer is still moving as a result of a goto, center,
// or similar command.
static int observer_travelling(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected to observer:travelling");
Observer* o = this_observer(l);
if (o->getMode() == Observer::Travelling)
lua_pushboolean(l, 1);
else
lua_pushboolean(l, 0);
return 1;
}
// Return the observer's current time as a Julian day number
static int observer_gettime(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected to observer:gettime");
Observer* o = this_observer(l);
lua_pushnumber(l, o->getTime());
return 1;
}
// Return the observer's current position
static int observer_getposition(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected to observer:getposition");
Observer* o = this_observer(l);
celx.newPosition(o->getPosition());
return 1;
}
static int observer_getsurface(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "One argument expected to observer:getsurface()");
Observer* obs = this_observer(l);
lua_pushstring(l, obs->getDisplayedSurface().c_str());
return 1;
}
static int observer_setsurface(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to observer:setsurface()");
Observer* obs = this_observer(l);
const char* s = lua_tostring(l, 2);
if (s == NULL)
obs->setDisplayedSurface("");
else
obs->setDisplayedSurface(s);
return 0;
}
static int observer_getframe(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for observer:getframe()");
Observer* obs = this_observer(l);
const ObserverFrame* frame = obs->getFrame();
celx.newFrame(*frame);
return 1;
}
static int observer_setframe(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for observer:setframe()");
Observer* obs = this_observer(l);
ObserverFrame* frame;
frame = celx.toFrame(2);
if (frame != NULL)
{
obs->setFrame(*frame);
}
else
{
celx.doError("Argument to observer:setframe must be a frame");
}
return 0;
}
static int observer_setspeed(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for observer:setspeed()");
Observer* obs = this_observer(l);
double speed = celx.safeGetNumber(2, AllErrors, "First argument to observer:setspeed must be a number");
obs->setTargetSpeed((float)speed);
return 0;
}
static int observer_getspeed(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No argument expected for observer:getspeed()");
Observer* obs = this_observer(l);
lua_pushnumber(l, (lua_Number)obs->getTargetSpeed());
return 1;
}
static int observer_setfov(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to observer:setfov()");
Observer* obs = this_observer(l);
double fov = celx.safeGetNumber(2, AllErrors, "Argument to observer:setfov() must be a number");
if ((fov >= degToRad(0.001f)) && (fov <= degToRad(120.0f)))
{
obs->setFOV((float) fov);
celx.appCore(AllErrors)->setZoomFromFOV();
}
return 0;
}
static int observer_getfov(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No argument expected to observer:getfov()");
Observer* obs = this_observer(l);
lua_pushnumber(l, obs->getFOV());
return 1;
}
static int observer_splitview(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 3, "One or two arguments expected for observer:splitview()");
Observer* obs = this_observer(l);
CelestiaCore* appCore = celx.appCore(AllErrors);
const char* splitType = celx.safeGetString(2, AllErrors, "First argument to observer:splitview() must be a string");
View::Type type = (compareIgnoringCase(splitType, "h") == 0) ? View::HorizontalSplit : View::VerticalSplit;
double splitPos = celx.safeGetNumber(3, WrongType, "Number expected as argument to observer:splitview()", 0.5);
if (splitPos < 0.1)
splitPos = 0.1;
if (splitPos > 0.9)
splitPos = 0.9;
View* view = getViewByObserver(appCore, obs);
appCore->splitView(type, view, (float)splitPos);
return 0;
}
static int observer_deleteview(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No argument expected for observer:deleteview()");
Observer* obs = this_observer(l);
CelestiaCore* appCore = celx.appCore(AllErrors);
View* view = getViewByObserver(appCore, obs);
appCore->deleteView(view);
return 0;
}
static int observer_singleview(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No argument expected for observer:singleview()");
Observer* obs = this_observer(l);
CelestiaCore* appCore = celx.appCore(AllErrors);
View* view = getViewByObserver(appCore, obs);
appCore->singleView(view);
return 0;
}
static int observer_equal(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Wrong number of arguments for comparison!");
Observer* o1 = this_observer(l);
Observer* o2 = to_observer(l, 2);
lua_pushboolean(l, (o1 == o2));
return 1;
}
static int observer_setlocationflags(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for observer:setlocationflags()");
Observer* obs = this_observer(l);
if (!lua_istable(l, 2))
{
celx.doError("Argument to observer:setlocationflags() must be a table");
}
lua_pushnil(l);
int locationFlags = obs->getLocationFilter();
while (lua_next(l, -2) != 0)
{
string key;
bool value = false;
if (lua_isstring(l, -2))
{
key = lua_tostring(l, -2);
}
else
{
celx.doError("Keys in table-argument to observer:setlocationflags() must be strings");
}
if (lua_isboolean(l, -1))
{
value = lua_toboolean(l, -1) != 0;
}
else
{
celx.doError("Values in table-argument to observer:setlocationflags() must be boolean");
}
if (CelxLua::LocationFlagMap.count(key) == 0)
{
cerr << "Unknown key: " << key << "\n";
}
else
{
int flag = CelxLua::LocationFlagMap[key];
if (value)
{
locationFlags |= flag;
}
else
{
locationFlags &= ~flag;
}
}
lua_pop(l,1);
}
obs->setLocationFilter(locationFlags);
return 0;
}
static int observer_getlocationflags(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for observer:getlocationflags()");
Observer* obs = this_observer(l);
lua_newtable(l);
CelxLua::FlagMap::const_iterator it = CelxLua::LocationFlagMap.begin();
const int locationFlags = obs->getLocationFilter();
while (it != CelxLua::LocationFlagMap.end())
{
string key = it->first;
lua_pushstring(l, key.c_str());
lua_pushboolean(l, (it->second & locationFlags) != 0);
lua_settable(l,-3);
it++;
}
return 1;
}
void CreateObserverMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Observer);
celx.registerMethod("__tostring", observer_tostring);
celx.registerMethod("isvalid", observer_isvalid);
celx.registerMethod("goto", observer_goto);
celx.registerMethod("gotolonglat", observer_gotolonglat);
celx.registerMethod("gotolocation", observer_gotolocation);
celx.registerMethod("gotodistance", observer_gotodistance);
celx.registerMethod("gotosurface", observer_gotosurface);
celx.registerMethod("cancelgoto", observer_cancelgoto);
celx.registerMethod("setposition", observer_setposition);
celx.registerMethod("lookat", observer_lookat);
celx.registerMethod("setorientation", observer_setorientation);
celx.registerMethod("getorientation", observer_getorientation);
celx.registerMethod("getspeed", observer_getspeed);
celx.registerMethod("setspeed", observer_setspeed);
celx.registerMethod("getfov", observer_getfov);
celx.registerMethod("setfov", observer_setfov);
celx.registerMethod("rotate", observer_rotate);
celx.registerMethod("center", observer_center);
celx.registerMethod("centerorbit", observer_centerorbit);
celx.registerMethod("follow", observer_follow);
celx.registerMethod("synchronous", observer_synchronous);
celx.registerMethod("chase", observer_chase);
celx.registerMethod("lock", observer_lock);
celx.registerMethod("track", observer_track);
celx.registerMethod("gettrackedobject", observer_gettrackedobject);
celx.registerMethod("travelling", observer_travelling);
celx.registerMethod("getframe", observer_getframe);
celx.registerMethod("setframe", observer_setframe);
celx.registerMethod("gettime", observer_gettime);
celx.registerMethod("getposition", observer_getposition);
celx.registerMethod("getsurface", observer_getsurface);
celx.registerMethod("setsurface", observer_setsurface);
celx.registerMethod("splitview", observer_splitview);
celx.registerMethod("deleteview", observer_deleteview);
celx.registerMethod("singleview", observer_singleview);
celx.registerMethod("getlocationflags", observer_getlocationflags);
celx.registerMethod("setlocationflags", observer_setlocationflags);
celx.registerMethod("__eq", observer_equal);
lua_pop(l, 1); // remove metatable from stack
}

View File

@ -0,0 +1,21 @@
// celx_observer.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: observer object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_OBSERVER_H_
#define _CELX_OBSERVER_H_
struct lua_State;
class Observer;
extern void CreateObserverMetaTable(lua_State* l);
extern int observer_new(lua_State* l, Observer* obs);
#endif // _CELX_OBSERVER_H_

View File

@ -0,0 +1,242 @@
// celx_phase.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: phase object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_phase.h"
#include <celengine/timelinephase.h>
// We want to avoid copying TimelinePhase objects, so we can't make them
// userdata. But, they can't be lightuserdata either because they need to
// be reference counted, and Lua doesn't garbage collect lightuserdata. The
// solution is the PhaseReference object, which just wraps a TimelinePhase
// pointer.
class PhaseReference
{
public:
PhaseReference(const TimelinePhase& _phase) :
phase(&_phase)
{
phase->addRef();
}
~PhaseReference()
{
phase->release();
}
const TimelinePhase* phase;
};
int phase_new(lua_State* l, const TimelinePhase& phase)
{
CelxLua celx(l);
// Use placement new to put the new phase reference in the userdata block.
void* block = lua_newuserdata(l, sizeof(PhaseReference));
new (block) PhaseReference(phase);
celx.setClass(Celx_Phase);
return 1;
}
static const TimelinePhase* to_phase(lua_State* l, int index)
{
CelxLua celx(l);
PhaseReference* ref = static_cast<PhaseReference*>(celx.checkUserData(index, Celx_Phase));
return ref == NULL ? NULL : ref->phase;
}
static const TimelinePhase* this_phase(lua_State* l)
{
CelxLua celx(l);
const TimelinePhase* phase = to_phase(l, 1);
if (phase == NULL)
{
celx.doError("Bad phase object!");
}
return phase;
}
/*! phase:timespan()
*
* Return the start and end times for this timeline phase.
*
* \verbatim
* -- Example: retrieve the start and end times of the first phase
* -- of Cassini's timeline:
* --
* cassini = celestia:find("Sol/Cassini")
* phases = cassini:timeline()
* begintime, endtime = phases[1]:timespan()
*
* \endverbatim
*/
static int phase_timespan(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments allowed for to phase:timespan");
const TimelinePhase* phase = this_phase(l);
celx.push(phase->startTime(), phase->endTime());
//lua_pushnumber(l, phase->startTime());
//lua_pushnumber(l, phase->endTime());
return 2;
}
/*! frame phase:orbitframe()
*
* Return the orbit frame for this timeline phase.
*/
static int phase_orbitframe(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments allowed for to phase:orbitframe");
const TimelinePhase* phase = this_phase(l);
const ReferenceFrame* f = phase->orbitFrame();
celx.newFrame(ObserverFrame(*f));
return 1;
}
/*! frame phase:bodyframe()
*
* Return the body frame for this timeline phase.
*/
static int phase_bodyframe(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments allowed for to phase:bodyframe");
const TimelinePhase* phase = this_phase(l);
const ReferenceFrame* f = phase->bodyFrame();
celx.newFrame(ObserverFrame(*f));
return 1;
}
/*! position phase:getposition(time: t)
*
* Return the position in frame coordinates at the specified time.
* Times outside the span covered by the phase are automatically clamped
* to either the beginning or ending of the span.
*/
static int phase_getposition(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for phase:getposition");
const TimelinePhase* phase = this_phase(l);
double tdb = celx.safeGetNumber(2, WrongType, "Argument to phase:getposition() must be number", 0.0);
if (tdb < phase->startTime())
tdb = phase->startTime();
else if (tdb > phase->endTime())
tdb = phase->endTime();
celx.newPosition(UniversalCoord(phase->orbit()->positionAtTime(tdb) * astro::kilometersToMicroLightYears(1.0)));
return 1;
}
/*! rotation phase:getorientation(time: t)
*
* Return the orientation in frame coordinates at the specified time.
* Times outside the span covered by the phase are automatically clamped
* to either the beginning or ending of the span.
*/
static int phase_getorientation(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument required for phase:getorientation");
const TimelinePhase* phase = this_phase(l);
double tdb = celx.safeGetNumber(2, WrongType, "Argument to phase:getorientation() must be number", 0.0);
if (tdb < phase->startTime())
tdb = phase->startTime();
else if (tdb > phase->endTime())
tdb = phase->endTime();
celx.newRotation(phase->rotationModel()->orientationAtTime(tdb));
return 1;
}
/*! __tostring metamethod
* Convert a phase to a string (currently just "[Phase]")
*/
static int phase_tostring(lua_State* l)
{
lua_pushstring(l, "[Phase]");
return 1;
}
/*! __gc metamethod
* Garbage collection for phases.
*/
static int phase_gc(lua_State* l)
{
CelxLua celx(l);
PhaseReference* ref = static_cast<PhaseReference*>(celx.checkUserData(1, Celx_Phase));
if (ref == NULL)
{
celx.doError("Bad phase object during garbage collection!");
}
else
{
// Explicitly call the destructor since the object was created with placement new
ref->~PhaseReference();
}
return 0;
}
void CreatePhaseMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Phase);
celx.registerMethod("__tostring", phase_tostring);
celx.registerMethod("__gc", phase_gc);
celx.registerMethod("timespan", phase_timespan);
celx.registerMethod("orbitframe", phase_orbitframe);
celx.registerMethod("bodyframe", phase_bodyframe);
celx.registerMethod("getposition", phase_getposition);
celx.registerMethod("getorientation", phase_getorientation);
lua_pop(l, 1); // remove metatable from stack
}

View File

@ -0,0 +1,21 @@
// celx_phase.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: phase object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_PHASE_H_
#define _CELX_PHASE_H_
struct lua_State;
class TimelinePhase;
extern void CreatePhaseMetaTable(lua_State* l);
extern int phase_new(lua_State* l, const TimelinePhase& phase);
#endif // _CELX_PHASE_H_

View File

@ -0,0 +1,335 @@
// celx_position.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: position object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_position.h"
// ==================== Position ====================
// a 128-bit per component universal coordinate
int position_new(lua_State* l, const UniversalCoord& uc)
{
CelxLua celx(l);
UniversalCoord* ud = reinterpret_cast<UniversalCoord*>(lua_newuserdata(l, sizeof(UniversalCoord)));
*ud = uc;
celx.setClass(Celx_Position);
return 1;
}
UniversalCoord* to_position(lua_State* l, int index)
{
CelxLua celx(l);
return static_cast<UniversalCoord*>(celx.checkUserData(index, Celx_Position));
}
static UniversalCoord* this_position(lua_State* l)
{
CelxLua celx(l);
UniversalCoord* uc = to_position(l, 1);
if (uc == NULL)
{
celx.doError("Bad position object!");
}
return uc;
}
static int position_get(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Invalid access of position-component");
UniversalCoord* uc = this_position(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in position-access");
double value = 0.0;
if (key == "x")
value = uc->x;
else if (key == "y")
value = uc->y;
else if (key == "z")
value = uc->z;
else
{
if (lua_getmetatable(l, 1))
{
lua_pushvalue(l, 2);
lua_rawget(l, -2);
return 1;
}
else
{
celx.doError("Internal error: couldn't get metatable");
}
}
lua_pushnumber(l, (lua_Number)value);
return 1;
}
static int position_set(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Invalid access of position-component");
UniversalCoord* uc = this_position(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in position-access");
double value = celx.safeGetNumber(3, AllErrors, "Position components must be numbers");
if (key == "x")
uc->x = value;
else if (key == "y")
uc->y = value;
else if (key == "z")
uc->z = value;
else
{
celx.doError("Invalid key in position-access");
}
return 0;
}
static int position_getx(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for position:getx()");
UniversalCoord* uc = this_position(l);
lua_Number x;
x = uc->x;
lua_pushnumber(l, x);
return 1;
}
static int position_gety(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for position:gety()");
UniversalCoord* uc = this_position(l);
lua_Number y;
y = uc->y;
lua_pushnumber(l, y);
return 1;
}
static int position_getz(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for position:getz()");
UniversalCoord* uc = this_position(l);
lua_Number z;
z = uc->z;
lua_pushnumber(l, z);
return 1;
}
static int position_vectorto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to position:vectorto");
UniversalCoord* uc = this_position(l);
UniversalCoord* uc2 = to_position(l, 2);
if (uc2 == NULL)
{
celx.doError("Argument to position:vectorto must be a position");
}
celx.newVector(*uc2 - *uc);
return 1;
}
static int position_orientationto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Two arguments expected for position:orientationto");
UniversalCoord* src = this_position(l);
UniversalCoord* target = to_position(l, 2);
if (target == NULL)
{
celx.doError("First argument to position:orientationto must be a position");
}
Vec3d* upd = celx.toVector(3);
if (upd == NULL)
{
celx.doError("Second argument to position:orientationto must be a vector");
}
Vec3d src2target = *target - *src;
src2target.normalize();
Vec3d v = src2target ^ *upd;
v.normalize();
Vec3d u = v ^ src2target;
Quatd qd = Quatd(Mat3d(v, u, -src2target));
celx.newRotation(qd);
return 1;
}
static int position_tostring(lua_State* l)
{
// TODO: print out the coordinate as it would appear in a cel:// URL
lua_pushstring(l, "[Position]");
return 1;
}
static int position_distanceto(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to position:distanceto()");
UniversalCoord* uc = this_position(l);
UniversalCoord* uc2 = to_position(l, 2);
if (uc2 == NULL)
{
celx.doError("Position expected as argument to position:distanceto");
}
Vec3d v = *uc2 - *uc;
lua_pushnumber(l, astro::microLightYearsToKilometers(v.length()));
return 1;
}
static int position_add(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for addition");
UniversalCoord* p1 = NULL;
UniversalCoord* p2 = NULL;
Vec3d* v2 = NULL;
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Position))
{
p1 = celx.toPosition(1);
p2 = celx.toPosition(2);
// this is not very intuitive, as p1-p2 is a vector
celx.newPosition(*p1 + *p2);
}
else
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Vec3))
{
p1 = celx.toPosition(1);
v2 = celx.toVector(2);
celx.newPosition(*p1 + *v2);
}
else
{
celx.doError("Bad position addition!");
}
return 1;
}
static int position_sub(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for subtraction");
UniversalCoord* p1 = NULL;
UniversalCoord* p2 = NULL;
Vec3d* v2 = NULL;
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Position))
{
p1 = celx.toPosition(1);
p2 = celx.toPosition(2);
celx.newVector(*p1 - *p2);
}
else
if (celx.isType(1, Celx_Position) && celx.isType(2, Celx_Vec3))
{
p1 = celx.toPosition(1);
v2 = celx.toVector(2);
celx.newPosition(*p1 - *v2);
}
else
{
celx.doError("Bad position subtraction!");
}
return 1;
}
static int position_addvector(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected to position:addvector()");
UniversalCoord* uc = this_position(l);
Vec3d* v3d = celx.toVector(2);
if (v3d == NULL)
{
celx.doError("Vector expected as argument to position:addvector");
}
else
if (uc != NULL && v3d != NULL)
{
UniversalCoord ucnew = *uc + *v3d;
position_new(l, ucnew);
}
return 1;
}
void CreatePositionMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Position);
celx.registerMethod("__tostring", position_tostring);
celx.registerMethod("distanceto", position_distanceto);
celx.registerMethod("vectorto", position_vectorto);
celx.registerMethod("orientationto", position_orientationto);
celx.registerMethod("addvector", position_addvector);
celx.registerMethod("__add", position_add);
celx.registerMethod("__sub", position_sub);
celx.registerMethod("__index", position_get);
celx.registerMethod("__newindex", position_set);
celx.registerMethod("getx", position_getx);
celx.registerMethod("gety", position_gety);
celx.registerMethod("getz", position_getz);
lua_pop(l, 1); // remove metatable from stack
}

View File

@ -0,0 +1,21 @@
// celx_position.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: position object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_POSITION_H_
#define _CELX_POSITION_H_
struct lua_State;
extern void CreatePositionMetaTable(lua_State* l);
extern int position_new(lua_State* l, const UniversalCoord& uc);
extern UniversalCoord* to_position(lua_State* l, int index);
#endif // _CELX_POSITION_H_

View File

@ -0,0 +1,270 @@
// celx_rotation.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: rotation object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_vector.h"
int rotation_new(lua_State* l, const Quatd& qd)
{
CelxLua celx(l);
Quatd* q = reinterpret_cast<Quatd*>(lua_newuserdata(l, sizeof(Quatd)));
*q = qd;
celx.setClass(Celx_Rotation);
return 1;
}
Quatd* to_rotation(lua_State* l, int index)
{
CelxLua celx(l);
return static_cast<Quatd*>(celx.checkUserData(index, Celx_Rotation));
}
static Quatd* this_rotation(lua_State* l)
{
CelxLua celx(l);
Quatd* q = to_rotation(l, 1);
if (q == NULL)
{
celx.doError("Bad rotation object!");
}
return q;
}
static int rotation_add(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for add");
Quatd* q1 = to_rotation(l, 1);
Quatd* q2 = to_rotation(l, 2);
if (q1 == NULL || q2 == NULL)
{
celx.doError("Addition only defined for two rotations");
}
else
{
Quatd result = *q1 + *q2;
rotation_new(l, result);
}
return 1;
}
static int rotation_mult(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for multiplication");
Quatd* r1 = NULL;
Quatd* r2 = NULL;
//Vec3d* v = NULL;
lua_Number s = 0.0;
if (celx.isType(1, Celx_Rotation) && celx.isType(2, Celx_Rotation))
{
r1 = to_rotation(l, 1);
r2 = to_rotation(l, 2);
rotation_new(l, *r1 * *r2);
}
else
if (celx.isType(1, Celx_Rotation) && lua_isnumber(l, 2))
{
r1 = to_rotation(l, 1);
s = lua_tonumber(l, 2);
rotation_new(l, *r1 * s);
}
else
if (lua_isnumber(l, 1) && celx.isType(2, Celx_Rotation))
{
s = lua_tonumber(l, 1);
r1 = to_rotation(l, 2);
rotation_new(l, *r1 * s);
}
else
{
celx.doError("Bad rotation multiplication!");
}
return 1;
}
static int rotation_imag(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for rotation_imag");
Quatd* q = this_rotation(l);
vector_new(l, imag(*q));
return 1;
}
static int rotation_real(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for rotation_real");
Quatd* q = this_rotation(l);
lua_pushnumber(l, real(*q));
return 1;
}
static int rotation_transform(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "One argument expected for rotation:transform()");
Quatd* q = this_rotation(l);
Vec3d* v = to_vector(l, 2);
if (v == NULL)
{
celx.doError("Argument to rotation:transform() must be a vector");
}
vector_new(l, *v * q->toMatrix3());
return 1;
}
static int rotation_setaxisangle(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Two arguments expected for rotation:setaxisangle()");
Quatd* q = this_rotation(l);
Vec3d* v = to_vector(l, 2);
if (v == NULL)
{
celx.doError("setaxisangle: first argument must be a vector");
}
double angle = celx.safeGetNumber(3, AllErrors, "second argument to rotation:setaxisangle must be a number");
q->setAxisAngle(*v, angle);
return 0;
}
static int rotation_slerp(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Two arguments expected for rotation:slerp()");
Quatd* q1 = this_rotation(l);
Quatd* q2 = to_rotation(l, 2);
if (q2 == NULL)
{
celx.doError("slerp: first argument must be a rotation");
}
double t = celx.safeGetNumber(3, AllErrors, "second argument to rotation:slerp must be a number");
rotation_new(l, Quatd::slerp(*q1, *q2, t));
return 1;
}
static int rotation_get(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Invalid access of rotation-component");
Quatd* q3 = this_rotation(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in rotation-access");
double value = 0.0;
if (key == "x")
value = imag(*q3).x;
else if (key == "y")
value = imag(*q3).y;
else if (key == "z")
value = imag(*q3).z;
else if (key == "w")
value = real(*q3);
else
{
if (lua_getmetatable(l, 1))
{
lua_pushvalue(l, 2);
lua_rawget(l, -2);
return 1;
}
else
{
celx.doError("Internal error: couldn't get metatable");
}
}
lua_pushnumber(l, (lua_Number)value);
return 1;
}
static int rotation_set(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Invalid access of rotation-component");
Quatd* q3 = this_rotation(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in rotation-access");
double value = celx.safeGetNumber(3, AllErrors, "Rotation components must be numbers");
Vec3d v = imag(*q3);
double w = real(*q3);
if (key == "x")
v.x = value;
else if (key == "y")
v.y = value;
else if (key == "z")
v.z = value;
else if (key == "w")
w = value;
else
{
celx.doError("Invalid key in rotation-access");
}
*q3 = Quatd(w, v);
return 0;
}
static int rotation_tostring(lua_State* l)
{
CelxLua celx(l);
lua_pushstring(l, "[Rotation]");
return 1;
}
void CreateRotationMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Rotation);
celx.registerMethod("real", rotation_real);
celx.registerMethod("imag", rotation_imag);
celx.registerMethod("transform", rotation_transform);
celx.registerMethod("setaxisangle", rotation_setaxisangle);
celx.registerMethod("slerp", rotation_slerp);
celx.registerMethod("__tostring", rotation_tostring);
celx.registerMethod("__add", rotation_add);
celx.registerMethod("__mul", rotation_mult);
celx.registerMethod("__index", rotation_get);
celx.registerMethod("__newindex", rotation_set);
lua_pop(l, 1); // remove metatable from stack
}

View File

@ -0,0 +1,23 @@
// celx_rotation.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: position object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_ROTATION_H_
#define _CELX_ROTATION_H_
#include <celmath/quaternion.h>
struct lua_State;
extern void CreateRotationMetaTable(lua_State* l);
extern int rotation_new(lua_State* l, const Quatd& qd);
extern Quatd* to_rotation(lua_State* l, int index);
#endif // _CELX_ROTATION_H_

View File

@ -0,0 +1,303 @@
// celx_vector.cpp
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: vector object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "celx.h"
#include "celx_internal.h"
#include "celx_vector.h"
int vector_new(lua_State* l, const Vec3d& v)
{
CelxLua celx(l);
Vec3d* v3 = reinterpret_cast<Vec3d*>(lua_newuserdata(l, sizeof(Vec3d)));
*v3 = v;
celx.setClass(Celx_Vec3);
return 1;
}
Vec3d* to_vector(lua_State* l, int index)
{
CelxLua celx(l);
return static_cast<Vec3d*>(celx.checkUserData(index, Celx_Vec3));
}
static Vec3d* this_vector(lua_State* l)
{
CelxLua celx(l);
Vec3d* v3 = to_vector(l, 1);
if (v3 == NULL)
{
celx.doError("Bad vector object!");
}
return v3;
}
static int vector_sub(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for sub");
Vec3d* op1 = celx.toVector(1);
Vec3d* op2 = celx.toVector(2);
if (op1 == NULL || op2 == NULL)
{
celx.doError("Subtraction only defined for two vectors");
}
else
{
Vec3d result = *op1 - *op2;
celx.newVector(result);
}
return 1;
}
static int vector_get(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Invalid access of vector-component");
Vec3d* v3 = this_vector(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in vector-access");
double value = 0.0;
if (key == "x")
value = v3->x;
else if (key == "y")
value = v3->y;
else if (key == "z")
value = v3->z;
else
{
if (lua_getmetatable(l, 1))
{
lua_pushvalue(l, 2);
lua_rawget(l, -2);
return 1;
}
else
{
celx.doError("Internal error: couldn't get metatable");
}
}
lua_pushnumber(l, (lua_Number)value);
return 1;
}
static int vector_set(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(3, 3, "Invalid access of vector-component");
Vec3d* v3 = this_vector(l);
string key = celx.safeGetString(2, AllErrors, "Invalid key in vector-access");
double value = celx.safeGetNumber(3, AllErrors, "Vector components must be numbers");
if (key == "x")
v3->x = value;
else if (key == "y")
v3->y = value;
else if (key == "z")
v3->z = value;
else
{
celx.doError("Invalid key in vector-access");
}
return 0;
}
static int vector_getx(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for vector:getx");
Vec3d* v3 = this_vector(l);
lua_Number x;
x = static_cast<lua_Number>(v3->x);
lua_pushnumber(l, x);
return 1;
}
static int vector_gety(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for vector:gety");
Vec3d* v3 = this_vector(l);
lua_Number y;
y = static_cast<lua_Number>(v3->y);
lua_pushnumber(l, y);
return 1;
}
static int vector_getz(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for vector:getz");
Vec3d* v3 = this_vector(l);
lua_Number z;
z = static_cast<lua_Number>(v3->z);
lua_pushnumber(l, z);
return 1;
}
static int vector_normalize(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for vector:normalize");
Vec3d* v = this_vector(l);
Vec3d vn(*v);
vn.normalize();
celx.newVector(vn);
return 1;
}
static int vector_length(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(1, 1, "No arguments expected for vector:length");
Vec3d* v = this_vector(l);
double length = v->length();
lua_pushnumber(l, (lua_Number)length);
return 1;
}
static int vector_add(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for addition");
Vec3d* v1 = NULL;
Vec3d* v2 = NULL;
UniversalCoord* p = NULL;
if (celx.isType(1, Celx_Vec3) && celx.isType(2, Celx_Vec3))
{
v1 = celx.toVector(1);
v2 = celx.toVector(2);
celx.newVector(*v1 + *v2);
}
else
if (celx.isType(1, Celx_Vec3) && celx.isType(2, Celx_Position))
{
v1 = celx.toVector(1);
p = celx.toPosition(2);
celx.newPosition(*p + *v1);
}
else
{
celx.doError("Bad vector addition!");
}
return 1;
}
static int vector_mult(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for multiplication");
Vec3d* v1 = NULL;
Vec3d* v2 = NULL;
Quatd* q = NULL;
lua_Number s = 0.0;
if (celx.isType(1, Celx_Vec3) && celx.isType(2, Celx_Vec3))
{
v1 = celx.toVector(1);
v2 = celx.toVector(2);
lua_pushnumber(l, *v1 * *v2);
}
else
if (celx.isType(1, Celx_Vec3) && lua_isnumber(l, 2))
{
v1 = celx.toVector(1);
s = lua_tonumber(l, 2);
celx.newVector(*v1 * s);
}
else
if (celx.isType(1, Celx_Vec3) && celx.isType(2, Celx_Rotation))
{
v1 = celx.toVector(1);
q = celx.toRotation(2);
celx.newRotation(*v1 * *q);
}
else
if (lua_isnumber(l, 1) && celx.isType(2, Celx_Vec3))
{
s = lua_tonumber(l, 1);
v1 = celx.toVector(2);
celx.newVector(*v1 * s);
}
else
{
celx.doError("Bad vector multiplication!");
}
return 1;
}
static int vector_cross(lua_State* l)
{
CelxLua celx(l);
celx.checkArgs(2, 2, "Need two operands for multiplication");
Vec3d* v1 = NULL;
Vec3d* v2 = NULL;
if (celx.isType(1, Celx_Vec3) && celx.isType(2, Celx_Vec3))
{
v1 = celx.toVector(1);
v2 = celx.toVector(2);
celx.newVector(*v1 ^ *v2);
}
else
{
celx.doError("Bad vector multiplication!");
}
return 1;
}
static int vector_tostring(lua_State* l)
{
lua_pushstring(l, "[Vector]");
return 1;
}
void CreateVectorMetaTable(lua_State* l)
{
CelxLua celx(l);
celx.createClassMetatable(Celx_Vec3);
celx.registerMethod("__tostring", vector_tostring);
celx.registerMethod("__add", vector_add);
celx.registerMethod("__sub", vector_sub);
celx.registerMethod("__mul", vector_mult);
celx.registerMethod("__pow", vector_cross);
celx.registerMethod("__index", vector_get);
celx.registerMethod("__newindex", vector_set);
celx.registerMethod("getx", vector_getx);
celx.registerMethod("gety", vector_gety);
celx.registerMethod("getz", vector_getz);
celx.registerMethod("normalize", vector_normalize);
celx.registerMethod("length", vector_length);
lua_pop(l, 1); // remove metatable from stack
}

View File

@ -0,0 +1,21 @@
// celx_vector.h
//
// Copyright (C) 2003-2008, the Celestia Development Team
//
// Lua script extensions for Celestia: vector object
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#ifndef _CELX_VECTOR_H_
#define _CELX_VECTOR_H_
struct lua_State;
extern void CreateVectorMetaTable(lua_State* l);
extern int vector_new(lua_State* l, const Vec3d& v);
extern Vec3d* to_vector(lua_State* l, int index);
#endif // _CELX_VECTOR_H_