From e83a67ade2904204fdb20d8a0ceb4416fe099e75 Mon Sep 17 00:00:00 2001 From: andreja6 Date: Sat, 27 Oct 2018 02:24:08 -0700 Subject: [PATCH 1/8] hm --- Demo.h | 4 ++- LevelInstance.cpp | 1 + main.cpp | 67 ++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 2 deletions(-) diff --git a/Demo.h b/Demo.h index e6fef08..a6968e5 100644 --- a/Demo.h +++ b/Demo.h @@ -35,6 +35,7 @@ class Demo { // : public GApp { RenderDevice* renderDevice; UserInput* userInput; PropertyWindow* _propWindow; + void generateShadowMap(const CoordinateFrame& lightViewMatrix) const; private: void initGUI(); HWND _hWndMain; @@ -47,7 +48,8 @@ class Demo { // : public GApp { HWND _hwndToolbox; HWND _buttonTest; HWND _hwndRenderer; - + G3D::TextureRef shadowMap; + double lightProjX, lightProjY, lightProjNear, lightProjFar; protected: Stopwatch m_graphicsWatch; Stopwatch m_logicWatch; diff --git a/LevelInstance.cpp b/LevelInstance.cpp index 4bab150..89f6be7 100644 --- a/LevelInstance.cpp +++ b/LevelInstance.cpp @@ -8,6 +8,7 @@ LevelInstance::LevelInstance(void) loseMessage = "You Lost. Try Again"; timer = 60.0F; score = 0; + canDelete = false; } LevelInstance::~LevelInstance(void) diff --git a/main.cpp b/main.cpp index 4aadd6a..c30e3b7 100644 --- a/main.cpp +++ b/main.cpp @@ -86,6 +86,7 @@ static const std::string PlaceholderName = "HyperCube"; Demo *usableApp = NULL; Demo::Demo(const GAppSettings& settings,HWND parentWindow) { //: GApp(settings,window) { + lightProjX = 17; lightProjY = 17; lightProjNear = 1; lightProjFar = 40; _hWndMain = parentWindow; HMODULE hThisInstance = GetModuleHandle(NULL); @@ -729,7 +730,7 @@ void Demo::onInit() { test->color = Color3(0.2F,0.3F,1); test->setSize(Vector3(24,1,24)); test->setPosition(Vector3(0,0,0)); - test->setCFrame(test->getCFrame() * Matrix3::fromEulerAnglesXYZ(0,toRadians(0),toRadians(0))); + test->setCFrame(test->getCFrame() * Matrix3::fromEulerAnglesXYZ(0,toRadians(54),toRadians(0))); @@ -1154,6 +1155,7 @@ void Demo::exitApplication() void Demo::onGraphics(RenderDevice* rd) { + G3D::uint8 num = 0; POINT mousepos; @@ -1193,6 +1195,20 @@ void Demo::onGraphics(RenderDevice* rd) { } LightingParameters lighting(G3D::toSeconds(11, 00, 00, AM)); + + Matrix4 lightProjectionMatrix(Matrix4::orthogonalProjection(-lightProjX, lightProjX, -lightProjY, lightProjY, lightProjNear, lightProjFar)); + + CoordinateFrame lightCFrame; + lightCFrame.lookAt(-lighting.lightDirection, Vector3::unitY()); + lightCFrame.translation = lighting.lightDirection * 20; + + Matrix4 lightMVP = lightProjectionMatrix * lightCFrame.inverse(); + + if (GLCaps::supports_GL_ARB_shadow()) { + generateShadowMap(lightCFrame); + } + + renderDevice->setProjectionAndCameraMatrix(*cameraController.getCamera()); // Cyan background @@ -1224,6 +1240,16 @@ void Demo::onGraphics(RenderDevice* rd) { fntdominant->draw3D(rd, "Testing", CoordinateFrame(rd->getCameraToWorldMatrix().rotation, gamepoint), 0.04*distance, Color3::yellow(), Color3::black(), G3D::GFont::XALIGN_CENTER, G3D::GFont::YALIGN_CENTER); } */ + + rd->setAmbientLightColor(Color3::black()); + rd->setDepthTest(RenderDevice::DEPTH_LEQUAL); + rd->disableDepthWrite(); + rd->setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor)); + + rd->setBlendFunc(RenderDevice::BLEND_ONE, RenderDevice::BLEND_ONE); + if (GLCaps::supports_GL_ARB_shadow()) { + rd->configureShadowMap(1, lightMVP, shadowMap); + } rd->beforePrimitive(); @@ -1622,6 +1648,37 @@ LRESULT CALLBACK G3DProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) return 0; } + +void Demo::generateShadowMap(const CoordinateFrame& lightViewMatrix) const { + + + //debugAssert(shadowMapSize < app->renderDevice->getHeight()); + //debugAssert(shadowMapSize < app->renderDevice->getWidth()); + + //app->renderDevice->clear(debugLightMap, true, false); + + Rect2D rect = Rect2D::xywh(0, 0, 512, 512); + renderDevice->pushState(); + renderDevice->setViewport(rect); + + // Draw from the light's point of view + renderDevice->setProjectionMatrix(Matrix4::orthogonalProjection(-lightProjX, lightProjX, -lightProjY, lightProjY, lightProjNear, lightProjFar)); + renderDevice->setCameraToWorldMatrix(lightViewMatrix); + + renderDevice->disableColorWrite(); + + // We can choose to use a large bias or render from + // the backfaces in order to avoid front-face self + // shadowing. Here, we use a large offset. + renderDevice->setPolygonOffset(8); + + dataModel->render(renderDevice); + renderDevice->popState(); + + shadowMap->copyFromScreen(rect); +} + + void Demo::run() { usableApp = this; //setDebugMode(false); @@ -1653,6 +1710,14 @@ void Demo::run() { clickSound = GetFileInPath("/content/sounds/switch.wav"); dingSound = GetFileInPath("/content/sounds/electronicpingshort.wav"); sky = Sky::create(NULL, ExePath() + "/content/sky/"); + + + if (GLCaps::supports_GL_ARB_shadow()) { + shadowMap = Texture::createEmpty(512, 512, "Shadow map", TextureFormat::depth(), + Texture::CLAMP, Texture::BILINEAR_NO_MIPMAP, Texture::DIM_2D, Texture::DEPTH_LEQUAL); + } + + cursorid = cursor->openGLID(); currentcursorid = cursorid; cursorOvrid = cursorOvr->openGLID(); From 948835df9898cede5e3b4dfb59bef401c66fc61d Mon Sep 17 00:00:00 2001 From: andreja6 Date: Fri, 26 Oct 2018 19:43:46 -0700 Subject: [PATCH 2/8] Shadows work (?) --- main.cpp | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index c30e3b7..f4bec41 100644 --- a/main.cpp +++ b/main.cpp @@ -1240,13 +1240,8 @@ void Demo::onGraphics(RenderDevice* rd) { fntdominant->draw3D(rd, "Testing", CoordinateFrame(rd->getCameraToWorldMatrix().rotation, gamepoint), 0.04*distance, Color3::yellow(), Color3::black(), G3D::GFont::XALIGN_CENTER, G3D::GFont::YALIGN_CENTER); } */ - - rd->setAmbientLightColor(Color3::black()); - rd->setDepthTest(RenderDevice::DEPTH_LEQUAL); - rd->disableDepthWrite(); - rd->setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor)); - - rd->setBlendFunc(RenderDevice::BLEND_ONE, RenderDevice::BLEND_ONE); + + rd->pushState(); if (GLCaps::supports_GL_ARB_shadow()) { rd->configureShadowMap(1, lightMVP, shadowMap); } @@ -1258,7 +1253,7 @@ void Demo::onGraphics(RenderDevice* rd) { //((PartInstance*)dataModel->children[0]->children[0])->debugPrintVertexIDs(rd,fntdominant,-cameraController.getCoordinateFrame().rotation); rd->afterPrimitive(); - + rd->popState(); if(g_selectedInstances.size() > 0) { for(size_t i = 0; i < g_selectedInstances.size(); i++) From 61d07a8557b9aec9f5e492b34ebdcc277b3377ef Mon Sep 17 00:00:00 2001 From: andreja6 Date: Sat, 27 Oct 2018 09:00:39 -0700 Subject: [PATCH 3/8] "Drag" --- main.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index f4bec41..43ba8f6 100644 --- a/main.cpp +++ b/main.cpp @@ -79,7 +79,8 @@ static const int RESIZE = 2; static POINT oldGlobalMouse; static int mode = CURSOR; bool dragging = false; -Vector2 oldMouse = Vector2(0,0); +#include +Vector2 mouseDownOn = Vector2(nan(), 0); float moveRate = 0.5; static const std::string PlaceholderName = "HyperCube"; @@ -967,6 +968,17 @@ void Demo::onUserInput(UserInput* ui) { dataModel->mouseButton1Down = (GetKeyState(VK_LBUTTON) & 0x100) != 0; if (GetHoldKeyState(VK_LBUTTON)) { + if(!G3D::isNaN(mouseDownOn.x)) + { + if(abs(mouseDownOn.x - dataModel->mousex) > 4 || abs(mouseDownOn.y - dataModel->mousey) > 4) + { + dragging = true; + } + } + else + { + mouseDownOn = Vector2(dataModel->mousex, dataModel->mousey); + } if (dragging) { PartInstance* part = NULL; if(g_selectedInstances.size() > 0) @@ -983,7 +995,7 @@ void Demo::onUserInput(UserInput* ui) { { if (__nearest>__time) { - Vector3 closest = (dragRay.closestPoint(moveTo->getPosition()) * 2); + Vector3 closest = (dragRay.closestPoint(moveTo->getPosition())); part->setPosition(closest); //part->setPosition(Vector3(floor(closest.x),part->getPosition().y,floor(closest.z))); } @@ -993,6 +1005,11 @@ void Demo::onUserInput(UserInput* ui) { Sleep(10); } } + else + { + dragging = false; + mouseDownOn = Vector2(nan(), 0); + } // Camera KB Handling { if (GetKPBool(VK_OEM_COMMA)) //Left usableApp->cameraController.panLeft(); @@ -1483,7 +1500,7 @@ void Demo::onMouseRightUp(int x,int y) } void Demo::onMouseMoved(int x,int y) { - oldMouse = dataModel->getMousePos(); + //oldMouse = dataModel->getMousePos(); dataModel->mousex = x; dataModel->mousey = y; From 89f9e0bfa2228252cd11b05cbd8ce009a79b1bb9 Mon Sep 17 00:00:00 2001 From: andreja6 Date: Sat, 27 Oct 2018 11:21:09 -0700 Subject: [PATCH 4/8] Moving works-ish, fixed box selection --- TextButtonInstance.cpp | 6 ++++++ main.cpp | 31 +++++++++++++++++++++++++++---- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/TextButtonInstance.cpp b/TextButtonInstance.cpp index 5b74ecc..3ac8ec9 100644 --- a/TextButtonInstance.cpp +++ b/TextButtonInstance.cpp @@ -21,6 +21,7 @@ TextButtonInstance::TextButtonInstance(void) visible = true; className = "TextButton"; disabled = false; + selected = false; } bool TextButtonInstance::mouseInButton(float mousex, float mousey, RenderDevice* rd) @@ -102,6 +103,11 @@ void TextButtonInstance::drawObj(RenderDevice* rd, Vector2 mousePos, bool mouseD Draw::box(Box(point1, point2), rd, boxColorOvr, boxOutlineColorOvr); font->draw2D(rd, title, RelativeTo, textSize, textColorOvr, textOutlineColorOvr); } + else if(selected) + { + Draw::box(Box(point1, point2), rd, boxColorOvr, boxOutlineColorOvr); + font->draw2D(rd, title, RelativeTo, textSize, textColorOvr, textOutlineColorOvr); + } else { Draw::box(Box(point1, point2), rd, boxColor, boxOutlineColor); diff --git a/main.cpp b/main.cpp index 43ba8f6..59cc03e 100644 --- a/main.cpp +++ b/main.cpp @@ -390,6 +390,7 @@ void Demo::initGUI() button->title = "Hopper"; button->fontLocationRelativeTo = Vector2(10, 3); button->setAllColorsSame(); + button->boxOutlineColorOvr = Color3(0,255,255); button = makeTextButton(); button->boxBegin = Vector2(0, -48); @@ -402,6 +403,7 @@ void Demo::initGUI() button->title = "Controller"; button->fontLocationRelativeTo = Vector2(10, 3); button->setAllColorsSame(); + button->boxOutlineColorOvr = Color3(0,255,255); button = makeTextButton(); button->boxBegin = Vector2(0, -72); @@ -414,6 +416,7 @@ void Demo::initGUI() button->title = "Color"; button->fontLocationRelativeTo = Vector2(10, 3); button->setAllColorsSame(); + button->boxOutlineColorOvr = Color3(0,255,255); button = makeTextButton(); button->boxBegin = Vector2(0, -96); @@ -426,6 +429,7 @@ void Demo::initGUI() button->title = "Surface"; button->fontLocationRelativeTo = Vector2(10, 3); button->setAllColorsSame(); + button->boxOutlineColorOvr = Color3(0,255,255); button = makeTextButton(); button->boxBegin = Vector2(0, -120); @@ -434,10 +438,10 @@ void Demo::initGUI() button->setParent(dataModel->getGuiRoot()); button->font = fntlighttrek; button->textColor = Color3(0,255,255); - button->boxOutlineColor = Color3(0,255,255); button->title = "Model"; button->fontLocationRelativeTo = Vector2(10, 3); button->setAllColorsSame(); + button->boxOutlineColorOvr = Color3(0,255,255); button = makeTextButton(); button->boxBegin = Vector2(0, 0); @@ -451,6 +455,7 @@ void Demo::initGUI() button->textSize = 16; button->fontLocationRelativeTo = Vector2(10, 0); button->setAllColorsSame(); + button->boxColorOvr = Color4(0.6F,0.6F,0.6F,0.4F); button = makeTextButton(); button->boxBegin = Vector2(125, 0); @@ -464,6 +469,7 @@ void Demo::initGUI() button->textSize = 16; button->fontLocationRelativeTo = Vector2(10, 0); button->setAllColorsSame(); + button->boxColorOvr = Color4(0.6F,0.6F,0.6F,0.4F); button = makeTextButton(); button->boxBegin = Vector2(250, 0); @@ -477,6 +483,7 @@ void Demo::initGUI() button->textSize = 16; button->fontLocationRelativeTo = Vector2(10, 0); button->setAllColorsSame(); + button->boxColorOvr = Color4(0.6F,0.6F,0.6F,0.4F); button = makeTextButton(); button->boxBegin = Vector2(375, 0); @@ -490,6 +497,7 @@ void Demo::initGUI() button->textSize = 16; button->fontLocationRelativeTo = Vector2(10, 0); button->setAllColorsSame(); + button->boxColorOvr = Color4(0.6F,0.6F,0.6F,0.4F); button = makeTextButton(); button->boxBegin = Vector2(500, 0); @@ -503,6 +511,7 @@ void Demo::initGUI() button->textSize = 16; button->fontLocationRelativeTo = Vector2(10, 0); button->setAllColorsSame(); + button->boxColorOvr = Color4(0.6F,0.6F,0.6F,0.4F); @@ -991,13 +1000,13 @@ void Demo::onUserInput(UserInput* ui) { { float __time = testRay.intersectionTime(moveTo->getBox()); float __nearest=std::numeric_limits::infinity(); - if (__time != inf()) + if (__time != inf() && moveTo != part) { if (__nearest>__time) { Vector3 closest = (dragRay.closestPoint(moveTo->getPosition())); - part->setPosition(closest); - //part->setPosition(Vector3(floor(closest.x),part->getPosition().y,floor(closest.z))); + //part->setPosition(closest); + part->setPosition(Vector3(floor(closest.x),floor(closest.y),floor(closest.z))); } } } @@ -1346,6 +1355,20 @@ void Demo::onGraphics(RenderDevice* rd) { std::vector instances = dataModel->getWorkspace()->getAllChildren(); currentcursorid = cursorid; + bool onGUI = false; + std::vector guis = dataModel->getGuiRoot()->getAllChildren(); + for(size_t i = 0; i < guis.size(); i++) + { + if(BaseButtonInstance* button = dynamic_cast(guis.at(i))) + { + if(button->mouseInButton(dataModel->mousex,dataModel->mousey, renderDevice)) + { + onGUI = true; + break; + } + } + } + if(!onGUI) for(size_t i = 0; i < instances.size(); i++) { if(PartInstance* test = dynamic_cast(instances.at(i))) From 057d86e05a36af7f7203fed91601f1a2794259ca Mon Sep 17 00:00:00 2001 From: MusicalProgrammer <38636805+MusicalProgrammer@users.noreply.github.com> Date: Sat, 27 Oct 2018 19:32:28 -0400 Subject: [PATCH 5/8] Using new part dragging code... (Needs adjusting) --- main.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/main.cpp b/main.cpp index 59cc03e..1b9046f 100644 --- a/main.cpp +++ b/main.cpp @@ -994,10 +994,22 @@ void Demo::onUserInput(UserInput* ui) { part = (PartInstance*) g_selectedInstances.at(0); Ray dragRay = cameraController.getCamera()->worldRay(dataModel->mousex, dataModel->mousey, renderDevice->getViewport()); std::vector instances = dataModel->getWorkspace()->getAllChildren(); + for(size_t i = 0; i < instances.size(); i++) { if(PartInstance* moveTo = dynamic_cast(instances.at(i))) { + Vector3 outLocation=Vector3(0,0,0); + Vector3 outNormal=Vector3(0,0,0); + + if (moveTo!=part) { + if (CollisionDetection::collisionTimeForMovingPointFixedBox(dragRay.origin,dragRay.direction*100,moveTo->getBox(),outLocation,outNormal)!=inf()) + { + part->setPosition(Vector3(floor(outLocation.x),floor(outLocation.y+1),floor(outLocation.z))); + break; + } + } + /* float __time = testRay.intersectionTime(moveTo->getBox()); float __nearest=std::numeric_limits::infinity(); if (__time != inf() && moveTo != part) @@ -1009,6 +1021,7 @@ void Demo::onUserInput(UserInput* ui) { part->setPosition(Vector3(floor(closest.x),floor(closest.y),floor(closest.z))); } } + */ } } Sleep(10); From 98e73cd7ca59b1ae3598833a47f780d6a0f658b3 Mon Sep 17 00:00:00 2001 From: andreja6 Date: Sat, 27 Oct 2018 19:40:41 -0700 Subject: [PATCH 6/8] Kinda sorta works --- main.cpp | 51 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/main.cpp b/main.cpp index 1b9046f..b111b26 100644 --- a/main.cpp +++ b/main.cpp @@ -992,12 +992,46 @@ void Demo::onUserInput(UserInput* ui) { PartInstance* part = NULL; if(g_selectedInstances.size() > 0) part = (PartInstance*) g_selectedInstances.at(0); - Ray dragRay = cameraController.getCamera()->worldRay(dataModel->mousex, dataModel->mousey, renderDevice->getViewport()); - std::vector instances = dataModel->getWorkspace()->getAllChildren(); - - for(size_t i = 0; i < instances.size(); i++) + Ray dragRay = cameraController.getCamera()->worldRay(dataModel->mousex, dataModel->mousey, renderDevice->getViewport()); + std::vector instances = dataModel->getWorkspace()->getAllChildren(); + PartInstance* moveTo; + for(size_t i = 0; i < instances.size(); i++) { - if(PartInstance* moveTo = dynamic_cast(instances.at(i))) + + } + + + + + + + float nearest=std::numeric_limits::infinity(); + Vector3 camPos = cameraController.getCamera()->getCoordinateFrame().translation; + for(size_t i = 0; i < instances.size(); i++) + { + if(PartInstance* test = dynamic_cast(instances.at(i))) + { + float time = dragRay.intersectionTime(test->getBox()); + + if (time != inf()) + { + if (nearest>time && test != part) + { + nearest=time; + moveTo = test; + //message = "Dragging = true."; + //messageTime = System::time(); + //dragging = true; + } + } + } + } + + + + + + if(moveTo != NULL) { Vector3 outLocation=Vector3(0,0,0); Vector3 outNormal=Vector3(0,0,0); @@ -1006,7 +1040,7 @@ void Demo::onUserInput(UserInput* ui) { if (CollisionDetection::collisionTimeForMovingPointFixedBox(dragRay.origin,dragRay.direction*100,moveTo->getBox(),outLocation,outNormal)!=inf()) { part->setPosition(Vector3(floor(outLocation.x),floor(outLocation.y+1),floor(outLocation.z))); - break; + //break; } } /* @@ -1023,8 +1057,9 @@ void Demo::onUserInput(UserInput* ui) { } */ } - } - Sleep(10); + + + Sleep(10); } } else From 1dd930af49c05d5e8d806abbe9e14be9231ca1de Mon Sep 17 00:00:00 2001 From: andreja6 Date: Sat, 27 Oct 2018 20:32:48 -0700 Subject: [PATCH 7/8] fixed crashing --- main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index b111b26..8831c80 100644 --- a/main.cpp +++ b/main.cpp @@ -1031,7 +1031,7 @@ void Demo::onUserInput(UserInput* ui) { - if(moveTo != NULL) + if(nearest != inf()) { Vector3 outLocation=Vector3(0,0,0); Vector3 outNormal=Vector3(0,0,0); From 2919f4ebaac0d5a2045e6c12d8c1069c7a16d188 Mon Sep 17 00:00:00 2001 From: andreja6 Date: Sat, 27 Oct 2018 20:38:33 -0700 Subject: [PATCH 8/8] Disabled shadows --- main.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/main.cpp b/main.cpp index 8831c80..f3bf781 100644 --- a/main.cpp +++ b/main.cpp @@ -1278,9 +1278,9 @@ void Demo::onGraphics(RenderDevice* rd) { Matrix4 lightMVP = lightProjectionMatrix * lightCFrame.inverse(); - if (GLCaps::supports_GL_ARB_shadow()) { + /*if (GLCaps::supports_GL_ARB_shadow()) { generateShadowMap(lightCFrame); - } + } */ renderDevice->setProjectionAndCameraMatrix(*cameraController.getCamera()); @@ -1316,9 +1316,9 @@ void Demo::onGraphics(RenderDevice* rd) { */ rd->pushState(); - if (GLCaps::supports_GL_ARB_shadow()) { + /*if (GLCaps::supports_GL_ARB_shadow()) { rd->configureShadowMap(1, lightMVP, shadowMap); - } + }*/ rd->beforePrimitive();