| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348 |
- #include "pch.h"
- #include "Camera3D.h"
- #include "CppUnitTest.h"
- #include "DX12GraphicsApi.h"
- #include "Globals.h"
- #include "Image.h"
- #include "Model3D.h"
- #include "RenderThread.h"
- #include "Screen.h"
- #include "Texture.h"
- #include "Window.h"
- #include "World3D.h"
- using namespace Microsoft::VisualStudio::CppUnitTestFramework;
- namespace FrameworkTests
- {
- TEST_CLASS (Cam3Tests)
- {
- public:
- TEST_METHOD (TestProjection)
- {
- Framework::Cam3D cam;
- cam.setPosition(Framework::Vec3<float>(0, 0, 0));
- cam.setRotation(Framework::Vec3<float>(0, 0, 0));
- Framework::Vec3<float> target(0, 1000, 0);
- cam.setViewDirection(target);
- Framework::Mat4<float> projection = cam.getProjectionMatrix();
- Framework::Mat4<float> inverse = cam.getInverseProjectionMatrix();
- Framework::Mat4<float> view = cam.getViewMatrix();
- Framework::Mat4<float> iView = cam.getInverseViewMatrix();
- for (int i = 0; i < 10; i++)
- {
- for (int j = 0; j < 10; j++)
- {
- Framework::Vec4<float> origin(0, 0, 0, 1.f);
- Framework::Vec4<float> target(
- i / 5.f - 1.f, j / 5.f - 1.f, -1.f, 1.f);
- Framework::Vec4<float> transformedOrigin = iView * origin;
- Framework::Vec4<float> transformedTarget
- = iView * (inverse * target);
- Framework::Vec4<float> transformedDirection
- = transformedTarget - transformedOrigin;
- }
- }
- }
- TEST_METHOD (TestRaytracing)
- {
- Framework::initFramework();
- Framework::Logging::zLoggingHandler()->addChannel(
- new Framework::Logging::OutputDebugStringLoggingChannel());
- Framework::setDebugDX(1);
- Framework::NativeWindow window;
- WNDCLASS wc = Framework::F_Normal(GetModuleHandle(0));
- wc.lpszClassName = "Factory Craft";
- window.create(WS_POPUPWINDOW, wc);
- Framework::Monitor m = Framework::getMonitor(0);
- window.setBounds(Framework::Point(m.x, m.y),
- Framework::Point(m.width, m.height));
- window.setDisplayMode(SW_SHOWNORMAL);
- window.setPreCloseAction([&window](void* p, void* f) {
- Framework::StopMessageLoop(window.getWindowHandle());
- });
- window.setMouseAction(Framework::_ret1ME);
- window.setKeyboardAction(Framework::_ret1TE);
- Framework::DirectX12* api = new Framework::DirectX12();
- Framework::Screen3D screen(
- dynamic_cast<Framework::NativeWindow*>(window.getThis()), api);
- screen.setHandleUserInputsOnTick(0);
- window.setScreen(
- dynamic_cast<Framework::Screen*>(screen.getThis()));
- screen.setFillColor(0);
- screen.setTestRend(0);
- Framework::Cam3D cam;
- cam.setPosition({0.f, -1000.f, 0.f});
- Framework::Vec3<float> target(0, 1000, 0);
- cam.setViewDirection(target);
- Framework::World3D world;
- Framework::Model3DData* data
- = screen.zGraphicsApi()->createModel("cube");
- data->setAmbientFactor(0.f);
- data->setDiffusFactor(1.f);
- data->setSpecularFactor(0.f);
- float size = 1;
- float left, right, top, bottom;
- // Calculate the screen coordinates of the left side of the bitmap.
- right = (float)((-size / 2.0));
- // Calculate the screen coordinates of the right side of the bitmap.
- left = right + (float)size;
- // Calculate the screen coordinates of the top of the bitmap.
- top = (float)(size / 2.0);
- // Calculate the screen coordinates of the bottom of the bitmap.
- bottom = top - (float)size;
- float front = -size / 2;
- float back = front + size;
- Framework::Vertex3D* vertecies = new Framework::Vertex3D[24];
- for (int i = 0; i < 24; i++)
- vertecies[i].knochenId = 0;
- // front side
- vertecies[0].pos = Framework::Vec3<float>(left, front, top);
- vertecies[0].tPos = Framework::Vec2<float>(0.f, 0.f);
- vertecies[1].pos = Framework::Vec3<float>(right, front, top);
- vertecies[1].tPos = Framework::Vec2<float>(1.f, 0.f);
- vertecies[2].pos = Framework::Vec3<float>(left, front, bottom);
- vertecies[2].tPos = Framework::Vec2<float>(0.f, 1.f);
- vertecies[3].pos = Framework::Vec3<float>(right, front, bottom);
- vertecies[3].tPos = Framework::Vec2<float>(1.f, 1.f);
- // back side
- vertecies[4].pos = Framework::Vec3<float>(right, back, top);
- vertecies[4].tPos = Framework::Vec2<float>(0.0f, 0.0f);
- vertecies[5].pos = Framework::Vec3<float>(left, back, top);
- vertecies[5].tPos = Framework::Vec2<float>(1.0f, 0.0f);
- vertecies[6].pos = Framework::Vec3<float>(right, back, bottom);
- vertecies[6].tPos = Framework::Vec2<float>(0.0f, 1.0f);
- vertecies[7].pos = Framework::Vec3<float>(left, back, bottom);
- vertecies[7].tPos = Framework::Vec2<float>(1.0f, 1.0f);
- // left side
- vertecies[8].pos = Framework::Vec3<float>(left, back, top);
- vertecies[8].tPos = Framework::Vec2<float>(0.f, 0.f);
- vertecies[9].pos = Framework::Vec3<float>(left, front, top);
- vertecies[9].tPos = Framework::Vec2<float>(1.f, 0.f);
- vertecies[10].pos = Framework::Vec3<float>(left, back, bottom);
- vertecies[10].tPos = Framework::Vec2<float>(0.f, 1.f);
- vertecies[11].pos = Framework::Vec3<float>(left, front, bottom);
- vertecies[11].tPos = Framework::Vec2<float>(1.f, 1.f);
- // right side
- vertecies[12].pos = Framework::Vec3<float>(right, front, top);
- vertecies[12].tPos = Framework::Vec2<float>(0.0f, 0.0f);
- vertecies[13].pos = Framework::Vec3<float>(right, back, top);
- vertecies[13].tPos = Framework::Vec2<float>(1.0f, 0.0f);
- vertecies[14].pos = Framework::Vec3<float>(right, front, bottom);
- vertecies[14].tPos = Framework::Vec2<float>(0.0f, 1.0f);
- vertecies[15].pos = Framework::Vec3<float>(right, back, bottom);
- vertecies[15].tPos = Framework::Vec2<float>(1.0f, 1.0f);
- // top side
- vertecies[16].pos = Framework::Vec3<float>(left, back, top);
- vertecies[16].tPos = Framework::Vec2<float>(0.f, 0.f);
- vertecies[17].pos = Framework::Vec3<float>(right, back, top);
- vertecies[17].tPos = Framework::Vec2<float>(1.f, 0.f);
- vertecies[18].pos = Framework::Vec3<float>(left, front, top);
- vertecies[18].tPos = Framework::Vec2<float>(0.f, 1.f);
- vertecies[19].pos = Framework::Vec3<float>(right, front, top);
- vertecies[19].tPos = Framework::Vec2<float>(1.f, 1.f);
- // botom side
- vertecies[20].pos = Framework::Vec3<float>(left, front, bottom);
- vertecies[20].tPos = Framework::Vec2<float>(0.0f, 0.0f);
- vertecies[21].pos = Framework::Vec3<float>(right, front, bottom);
- vertecies[21].tPos = Framework::Vec2<float>(1.0f, 0.0f);
- vertecies[22].pos = Framework::Vec3<float>(left, back, bottom);
- vertecies[22].tPos = Framework::Vec2<float>(0.0f, 1.0f);
- vertecies[23].pos = Framework::Vec3<float>(right, back, bottom);
- vertecies[23].tPos = Framework::Vec2<float>(1.0f, 1.0f);
- data->setVertecies(vertecies, 24);
- // the order of the polygons has to be NORTH (front), EAST (left),
- // SOUTH (back), WEST (right), TOP, BOTTOM according to the Area
- // definition front side
- Framework::Polygon3D* p
- = new Framework::Polygon3D(); // looking from (0,0,0) to (1,0,0)
- // to see this side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0;
- p->indexList[1] = 1;
- p->indexList[2] = 2;
- p->indexList[3] = 1;
- p->indexList[4] = 3;
- p->indexList[5] = 2;
- data->addPolygon(p);
- // left side
- p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,1,0)
- // to see this
- // side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 8;
- p->indexList[1] = 1 + 8;
- p->indexList[2] = 2 + 8;
- p->indexList[3] = 1 + 8;
- p->indexList[4] = 3 + 8;
- p->indexList[5] = 2 + 8;
- data->addPolygon(p);
- // back side
- p = new Framework::Polygon3D(); // looking from (0,0,0) to (-1,0,0)
- // to see this
- // side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 4;
- p->indexList[1] = 1 + 4;
- p->indexList[2] = 2 + 4;
- p->indexList[3] = 1 + 4;
- p->indexList[4] = 3 + 4;
- p->indexList[5] = 2 + 4;
- data->addPolygon(p);
- // right side
- p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,-1,0)
- // to see this
- // side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 12;
- p->indexList[1] = 1 + 12;
- p->indexList[2] = 2 + 12;
- p->indexList[3] = 1 + 12;
- p->indexList[4] = 3 + 12;
- p->indexList[5] = 2 + 12;
- data->addPolygon(p);
- // top side
- p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,0,-1)
- // to see this
- // side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 16;
- p->indexList[1] = 1 + 16;
- p->indexList[2] = 2 + 16;
- p->indexList[3] = 1 + 16;
- p->indexList[4] = 3 + 16;
- p->indexList[5] = 2 + 16;
- data->addPolygon(p);
- // botom side
- p = new Framework::Polygon3D(); // looking from (0,0,0) to (0,0,1)
- // to see this
- // side
- p->indexAnz = 6;
- p->indexList = new int[p->indexAnz];
- p->indexList[0] = 0 + 20;
- p->indexList[1] = 1 + 20;
- p->indexList[2] = 2 + 20;
- p->indexList[3] = 1 + 20;
- p->indexList[4] = 3 + 20;
- p->indexList[5] = 2 + 20;
- data->addPolygon(p);
- data->calculateNormals();
- Framework::Image* img1 = new Framework::Image();
- img1->newImage(1, 1, 0xFFFF0000);
- Framework::Texture* red
- = screen.zGraphicsApi()->createOrGetTexture("red", img1);
- Framework::Image* img2 = new Framework::Image();
- img2->newImage(1, 1, 0xFF00FF00);
- Framework::Texture* green
- = screen.zGraphicsApi()->createOrGetTexture("green", img2);
- Framework::Image* img3 = new Framework::Image();
- img3->newImage(1, 1, 0xFF0000FF);
- Framework::Texture* blue
- = screen.zGraphicsApi()->createOrGetTexture("blue", img3);
- Framework::Image* img4 = new Framework::Image();
- img4->newImage(1, 1, 0xFFFFFF00);
- Framework::Texture* yellow
- = screen.zGraphicsApi()->createOrGetTexture("yellow", img4);
- Framework::Image* img5 = new Framework::Image();
- img5->newImage(1, 1, 0xFFFF00FF);
- Framework::Texture* pink
- = screen.zGraphicsApi()->createOrGetTexture("pink", img5);
- Framework::Image* img6 = new Framework::Image();
- img6->newImage(1, 1, 0xFFFFFFFF);
- Framework::Texture* white
- = screen.zGraphicsApi()->createOrGetTexture("white", img6);
- Framework::Model3DTexture* texture
- = new Framework::Model3DTexture();
- texture->setPolygonTexture(0, red);
- texture->setPolygonTexture(1, green);
- texture->setPolygonTexture(2, blue);
- texture->setPolygonTexture(3, yellow);
- texture->setPolygonTexture(4, pink);
- texture->setPolygonTexture(5, white);
- Framework::Array<Framework::Model3D*> cubes;
- std::function<void(int, int, int)> addCube
- = [&cubes, &world, &data, &texture](int x, int y, int z) {
- Framework::Model3D* cube = new Framework::Model3D();
- cube->setModelData(dynamic_cast<Framework::Model3DData*>(
- data->getThis()));
- cube->setPosition(x, y, z);
- cube->setSize(5 + rand() % 5);
- cube->setRotation(
- rand() % 10 / 5, rand() % 10 / 5, rand() % 10 / 5);
- cube->setModelTextur(
- dynamic_cast<Framework::Model3DTexture*>(
- texture->getThis()));
- world.addDrawable(cube);
- cubes.add(cube);
- };
- for (int i = 0; i < 10; i++)
- {
- addCube(rand() % 1000 - 500,
- rand() % 1000 - 500,
- rand() % 1000 - 500);
- }
- cam.setWorld(dynamic_cast<Framework::World3D*>(world.getThis()));
- cam.addStyle(Framework::Cam3D::Style::Movable
- | Framework::Cam3D::Style::Rotatable
- | Framework::Cam3D::Style::Zoomable
- | Framework::Cam3D::Style::Tick);
- cam.setMovementSpeed(0.1f);
- cam.setScreenPosition(0, 0);
- cam.setScreenSize(screen.getBackBufferSize());
- screen.addKamera(dynamic_cast<Framework::Cam3D*>(cam.getThis()));
- Framework::RenderTh rTh;
- rTh.setMaxFps(-1);
- rTh.setQuiet(0);
- rTh.setMaxFps(-1);
- rTh.setScreen(dynamic_cast<Framework::Screen*>(screen.getThis()));
- double sum = 0;
- rTh.setTickFunktion([&cubes, &sum, &addCube, &api, &screen](
- void* p, void* f, double tick) {
- for (Framework::Model3D* cube : cubes)
- {
- cube->setRotationX(cube->getXRotation() + tick * 2);
- cube->setRotationY(cube->getXRotation() + tick / 5);
- cube->setRotationZ(cube->getXRotation() + tick / 25);
- cube->setSize(
- cube->getSize() + tick * 5 * (sum > 2.5 ? 1 : -1));
- }
- sum += tick;
- if (sum > 10)
- {
- sum -= 10;
- screen.render();
- addCube(rand() % 1000 - 500,
- rand() % 1000 - 500,
- rand() % 1000 - 500);
- }
- });
- rTh.beginn();
- Framework::StartMessageLoop();
- rTh.terminate();
- }
- };
- } // namespace FrameworkTests
|