librw/tools/lights/main.cpp

395 lines
7.8 KiB
C++
Raw Normal View History

2020-04-19 12:00:35 +01:00
#include <rw.h>
#include <skeleton.h>
#include <assert.h>
2021-03-03 01:00:52 +00:00
#include "main.h"
#include "lights.h"
2020-04-19 12:00:35 +01:00
rw::V3d zero = { 0.0f, 0.0f, 0.0f };
rw::EngineOpenParams engineOpenParams;
float FOV = 70.0f;
2021-03-03 01:00:52 +00:00
rw::RGBA ForegroundColor = { 200, 200, 200, 255 };
rw::RGBA BackgroundColor = { 64, 64, 64, 0 };
rw::World *World;
rw::Camera *Camera;
2020-04-19 12:00:35 +01:00
rw::V3d Xaxis = { 1.0f, 0.0, 0.0f };
rw::V3d Yaxis = { 0.0f, 1.0, 0.0f };
rw::V3d Zaxis = { 0.0f, 0.0, 1.0f };
2021-03-03 01:00:52 +00:00
rw::World*
CreateWorld(void)
2020-04-19 12:00:35 +01:00
{
2021-03-03 01:00:52 +00:00
rw::BBox bb;
2020-04-19 12:00:35 +01:00
2021-03-03 01:00:52 +00:00
bb.inf.x = bb.inf.y = bb.inf.z = -100.0f;
bb.sup.x = bb.sup.y = bb.sup.z = 100.0f;
2020-04-19 12:00:35 +01:00
2021-03-03 01:00:52 +00:00
return rw::World::create(&bb);
2020-04-19 12:00:35 +01:00
}
2021-03-03 01:00:52 +00:00
rw::Camera*
CreateCamera(rw::World *world)
2020-04-19 12:00:35 +01:00
{
2021-03-03 01:00:52 +00:00
rw::Camera *camera;
camera = sk::CameraCreate(sk::globals.width, sk::globals.height, 1);
assert(camera);
camera->setNearPlane(0.1f);
camera->setFarPlane(300.0f);
camera->setFOV(FOV, (float)sk::globals.width/sk::globals.height);
world->addCamera(camera);
return camera;
2020-04-19 12:00:35 +01:00
}
bool
CreateTestScene(rw::World *world)
{
rw::Clump *clump;
rw::StreamFile in;
const char *filename = "checker.dff";
if(in.open(filename, "rb") == NULL){
printf("couldn't open file\n");
return false;
}
if(!rw::findChunk(&in, rw::ID_CLUMP, NULL, NULL))
return false;
clump = rw::Clump::streamRead(&in);
in.close();
if(clump == nil)
return false;
rw::Clump *clone;
rw::Frame *clumpFrame;
rw::V3d pos;
float zOffset = 75.0f;
// Bottom panel
clumpFrame = clump->getFrame();
clumpFrame->rotate(&Xaxis, 90.0f, rw::COMBINEREPLACE);
pos.x = 0.0f;
pos.y = -25.0f;
pos.z = zOffset;
clumpFrame->translate(&pos, rw::COMBINEPOSTCONCAT);
// only need to add once
world->addClump(clump);
// Top panel
clone = clump->clone();
clumpFrame = clone->getFrame();
clumpFrame->rotate(&Xaxis, -90.0f, rw::COMBINEREPLACE);
pos.x = 0.0f;
pos.y = 25.0f;
pos.z = zOffset;
clumpFrame->translate(&pos, rw::COMBINEPOSTCONCAT);
// Left panel
clone = clump->clone();
clumpFrame = clone->getFrame();
clumpFrame->rotate(&Xaxis, 0.0f, rw::COMBINEREPLACE);
clumpFrame->rotate(&Yaxis, 90.0f, rw::COMBINEPOSTCONCAT);
pos.x = 25.0f;
pos.y = 0.0f;
pos.z = zOffset;
clumpFrame->translate(&pos, rw::COMBINEPOSTCONCAT);
// Right panel
clone = clump->clone();
clumpFrame = clone->getFrame();
clumpFrame->rotate(&Xaxis, 0.0f, rw::COMBINEREPLACE);
clumpFrame->rotate(&Yaxis, -90.0f, rw::COMBINEPOSTCONCAT);
pos.x = -25.0f;
pos.y = 0.0f;
pos.z = zOffset;
clumpFrame->translate(&pos, rw::COMBINEPOSTCONCAT);
// Back panel
clone = clump->clone();
clumpFrame = clone->getFrame();
clumpFrame->rotate(&Xaxis, 0.0f, rw::COMBINEREPLACE);
pos.x = 0.0f;
pos.y = 0.0f;
pos.z = zOffset + 25.0f;
clumpFrame->translate(&pos, rw::COMBINEPOSTCONCAT);
2021-03-03 01:00:52 +00:00
// BBox
pos.x = 25.0f;
pos.y = 25.0f;
pos.z = zOffset - 25.0f;
RoomBBox.initialize(&pos);
pos.x = -25.0f;
pos.y = -25.0f;
pos.z = zOffset + 25.0f;
RoomBBox.addPoint(&pos);
2020-04-19 12:00:35 +01:00
return 1;
}
2021-03-03 01:00:52 +00:00
void
Initialize(void)
{
sk::globals.windowtitle = "Lights example";
sk::globals.width = 1280;
sk::globals.height = 800;
sk::globals.quit = 0;
}
2020-04-19 12:00:35 +01:00
bool
2021-03-03 01:00:52 +00:00
Initialize3D(void)
2020-04-19 12:00:35 +01:00
{
if(!sk::InitRW())
return false;
2021-03-03 01:00:52 +00:00
World = CreateWorld();
2020-04-19 12:00:35 +01:00
BaseAmbientLight = CreateBaseAmbientLight();
AmbientLight = CreateAmbientLight();
DirectLight = CreateDirectLight();
PointLight = CreatePointLight();
SpotLight = CreateSpotLight();
SpotSoftLight = CreateSpotSoftLight();
2021-03-03 01:00:52 +00:00
Camera = CreateCamera(World);
2020-04-19 12:00:35 +01:00
2021-03-03 01:00:52 +00:00
CreateTestScene(World);
2020-04-19 12:00:35 +01:00
ImGui_ImplRW_Init();
ImGui::StyleColorsClassic();
return true;
}
2021-03-03 01:15:38 +00:00
void
Terminate3D(void)
{
FORLIST(lnk, World->clumps){
rw::Clump *clump = rw::Clump::fromWorld(lnk);
World->removeClump(clump);
clump->destroy();
}
if(Camera){
World->removeCamera(Camera);
sk::CameraDestroy(Camera);
Camera = nil;
}
LightsDestroy();
if(World){
World->destroy();
World = nil;
}
sk::TerminateRW();
}
2021-03-03 01:00:52 +00:00
bool
attachPlugins(void)
2020-04-19 12:00:35 +01:00
{
2021-03-03 01:00:52 +00:00
rw::ps2::registerPDSPlugin(40);
rw::ps2::registerPluginPDSPipes();
rw::registerMeshPlugin();
rw::registerNativeDataPlugin();
rw::registerAtomicRightsPlugin();
rw::registerMaterialRightsPlugin();
rw::xbox::registerVertexFormatPlugin();
rw::registerSkinPlugin();
rw::registerUserDataPlugin();
rw::registerHAnimPlugin();
rw::registerMatFXPlugin();
rw::registerUVAnimPlugin();
rw::ps2::registerADCPlugin();
return true;
2020-04-19 12:00:35 +01:00
}
void
Gui(void)
{
2021-03-03 01:00:52 +00:00
// ImGui::ShowDemoWindow(nil);
2020-04-19 12:00:35 +01:00
static bool showLightWindow = true;
ImGui::Begin("Lights", &showLightWindow);
2021-03-03 01:00:52 +00:00
ImGui::Checkbox("Light", &LightOn);
ImGui::Checkbox("Draw Light", &LightDrawOn);
if(ImGui::Checkbox("Base Ambient", &BaseAmbientLightOn)){
if(BaseAmbientLightOn){
if(BaseAmbientLight->world == nil)
World->addLight(BaseAmbientLight);
}else{
if(BaseAmbientLight->world)
World->removeLight(BaseAmbientLight);
}
2020-04-19 12:00:35 +01:00
}
2021-03-03 01:00:52 +00:00
ImGui::RadioButton("Ambient Light", &LightTypeIndex, 0);
ImGui::RadioButton("Point Light", &LightTypeIndex, 1);
ImGui::RadioButton("Directional Light", &LightTypeIndex, 2);
ImGui::RadioButton("Spot Light", &LightTypeIndex, 3);
ImGui::RadioButton("Soft Spot Light", &LightTypeIndex, 4);
ImGui::ColorEdit3("Color", (float*)&LightColor);
float radAngle = LightConeAngle/180.0f*M_PI;
ImGui::SliderAngle("Cone angle", &radAngle, 0.0f, 180.0f);
LightConeAngle = radAngle/M_PI*180.0f;
ImGui::SliderFloat("Radius", &LightRadius, 0.1f, 500.0f);
2020-04-19 12:00:35 +01:00
ImGui::End();
}
void
2021-03-03 01:00:52 +00:00
Render(float timeDelta)
2020-04-19 12:00:35 +01:00
{
2021-03-03 01:00:52 +00:00
Camera->clear(&BackgroundColor, rw::Camera::CLEARIMAGE|rw::Camera::CLEARZ);
Camera->beginUpdate();
2020-04-19 12:00:35 +01:00
ImGui_ImplRW_NewFrame(timeDelta);
2021-03-03 01:00:52 +00:00
World->render();
if(LightDrawOn && CurrentLight)
DrawCurrentLight();
2020-04-19 12:00:35 +01:00
Gui();
ImGui::EndFrame();
ImGui::Render();
2021-03-03 01:00:52 +00:00
Camera->endUpdate();
Camera->showRaster(0);
}
void
Idle(float timeDelta)
{
LightsUpdate();
Render(timeDelta);
2020-04-19 12:00:35 +01:00
}
2021-03-03 01:00:52 +00:00
int MouseX, MouseY;
int MouseDeltaX, MouseDeltaY;
int MouseButtons;
int CtrlDown;
bool RotateLight;
bool TranslateLightXY;
bool TranslateLightZ;
2020-04-19 12:00:35 +01:00
void
KeyUp(int key)
{
2021-03-03 01:00:52 +00:00
switch(key){
case sk::KEY_LCTRL:
case sk::KEY_RCTRL:
CtrlDown = 0;
break;
}
2020-04-19 12:00:35 +01:00
}
void
KeyDown(int key)
{
switch(key){
2021-03-03 01:00:52 +00:00
case sk::KEY_LCTRL:
case sk::KEY_RCTRL:
CtrlDown = 1;
break;
2020-04-19 12:00:35 +01:00
case sk::KEY_ESC:
sk::globals.quit = 1;
break;
}
}
2021-03-03 01:00:52 +00:00
void
MouseBtn(sk::MouseState *mouse)
{
MouseButtons = mouse->buttons;
RotateLight = !CtrlDown && !!(MouseButtons&1);
TranslateLightXY = CtrlDown && !!(MouseButtons&1);
TranslateLightZ = CtrlDown && !!(MouseButtons&4);
}
void
MouseMove(sk::MouseState *mouse)
{
MouseDeltaX = mouse->posx - MouseX;
MouseDeltaY = mouse->posy - MouseY;
MouseX = mouse->posx;
MouseY = mouse->posy;
if(RotateLight)
LightRotate(-MouseDeltaX, MouseDeltaY);
if(TranslateLightXY)
LightTranslateXY(-MouseDeltaX*0.1f, -MouseDeltaY*0.1f);
if(TranslateLightZ)
LightTranslateZ(-MouseDeltaY*0.1f);
}
2020-04-19 12:00:35 +01:00
sk::EventStatus
AppEventHandler(sk::Event e, void *param)
{
using namespace sk;
Rect *r;
2021-03-03 01:00:52 +00:00
MouseState *ms;
2020-04-19 12:00:35 +01:00
ImGuiEventHandler(e, param);
2021-03-03 01:00:52 +00:00
ImGuiIO &io = ImGui::GetIO();
2020-04-19 12:00:35 +01:00
switch(e){
case INITIALIZE:
2021-03-03 01:00:52 +00:00
Initialize();
2020-04-19 12:00:35 +01:00
return EVENTPROCESSED;
case RWINITIALIZE:
2021-03-03 01:00:52 +00:00
return Initialize3D() ? EVENTPROCESSED : EVENTERROR;
2021-03-03 01:15:38 +00:00
case RWTERMINATE:
Terminate3D();
return EVENTPROCESSED;
2020-04-19 12:00:35 +01:00
case PLUGINATTACH:
return attachPlugins() ? EVENTPROCESSED : EVENTERROR;
case KEYDOWN:
KeyDown(*(int*)param);
return EVENTPROCESSED;
case KEYUP:
KeyUp(*(int*)param);
return EVENTPROCESSED;
2021-03-03 01:00:52 +00:00
case MOUSEBTN:
if(!io.WantCaptureMouse){
ms = (MouseState*)param;
MouseBtn(ms);
}else
MouseButtons = 0;
return EVENTPROCESSED;
case MOUSEMOVE:
MouseMove((MouseState*)param);
return EVENTPROCESSED;
2020-04-19 12:00:35 +01:00
case RESIZE:
r = (Rect*)param;
// TODO: register when we're minimized
if(r->w == 0) r->w = 1;
if(r->h == 0) r->h = 1;
sk::globals.width = r->w;
sk::globals.height = r->h;
2021-03-03 01:00:52 +00:00
if(::Camera){
sk::CameraSize(::Camera, r);
::Camera->setFOV(FOV, (float)sk::globals.width/sk::globals.height);
2020-04-19 12:00:35 +01:00
}
break;
case IDLE:
2021-03-03 01:00:52 +00:00
Idle(*(float*)param);
2020-04-19 12:00:35 +01:00
return EVENTPROCESSED;
}
return sk::EVENTNOTPROCESSED;
}