master
BuildTools 4 years ago
parent ef50f1afcf
commit d8be22ebdc
  1. 28
      demo/demo.cpp
  2. 2
      demo/demo.h
  3. 4
      main.cpp
  4. 3
      stuff/globals/globals.cpp
  5. 2
      stuff/globals/globals.h
  6. 16
      stuff/objects/camera.h
  7. 2
      stuff/objects/cube.h
  8. 46
      stuff/objects/ray3D.h
  9. 9
      stuff/objects/vector3.h
  10. 1
      threedee.vcxproj
  11. 3
      threedee.vcxproj.filters

@ -11,7 +11,31 @@ void game::run(SDL_Renderer* renderer)
SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y); SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y);
} }
global::entList.at(0)->x = global::mousePos.x; global::entList.at(0)->x = global::mousePos.x;
global::entList.at(0)->y = global::mousePos.y;*/ global::entList.at(0)->y = global::mousePos.y;
//global::entList.run(renderer); global::entList.run(renderer);*/
//global::cam.run(renderer);
static float pitch, yaw;
for (int y = 1; y < 250; y += 1)
{
for (int x = 1; x < 250; x += 1)
{
ray3D ray1 = ray3D::trace(vector3(x, y, 0), vector3(yaw, pitch, 1));
if (ray1.dist < 500)
{
SDL_SetRenderDrawColor(renderer, 255 - ray1.dist, 255 - ray1.dist, 255 - ray1.dist, 255);
SDL_RenderDrawPoint(renderer, x * 2, y * 2);
SETRENDER_WHITE;
}
}
}
if (GetAsyncKeyState(VK_DOWN))
pitch += 1;
if (GetAsyncKeyState(VK_UP))
pitch -= 1;
if (GetAsyncKeyState(VK_LEFT))
yaw -= 1;
if (GetAsyncKeyState(VK_RIGHT))
yaw += 1;
std::cout << global::entList3D.at(0)->x << global::entList3D.at(0)->y << std::endl;
global::entList3D.run(renderer); global::entList3D.run(renderer);
} }

@ -7,6 +7,8 @@
#include "../stuff/objects/vector2.h" #include "../stuff/objects/vector2.h"
#include "../stuff/objects/vector3.h" #include "../stuff/objects/vector3.h"
#include "../stuff/objects/ray2D.h" #include "../stuff/objects/ray2D.h"
#include "../stuff/objects/ray3D.h"
#include "../stuff/objects/camera.h"
#include "../stuff/globals/globals.h" #include "../stuff/globals/globals.h"
#include "../stuff/callbacks/callbacks.h" #include "../stuff/callbacks/callbacks.h"

@ -12,14 +12,14 @@ int main(int argc, char** argv)
SDL_Renderer* renderer = nullptr; SDL_Renderer* renderer = nullptr;
SDL_Init(SDL_INIT_VIDEO); SDL_Init(SDL_INIT_VIDEO);
window = SDL_CreateWindow("threedee", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1280, 720, SDL_WINDOW_SHOWN); window = SDL_CreateWindow("threedee", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 500, 500, SDL_WINDOW_SHOWN);
surface = SDL_GetWindowSurface(window); surface = SDL_GetWindowSurface(window);
renderer = SDL_CreateRenderer(window, -1, NULL); renderer = SDL_CreateRenderer(window, -1, NULL);
SETRENDER_WHITE; SETRENDER_WHITE;
SDL_Event eventHandler; SDL_Event eventHandler;
//global::entList.push_back(&square(200, 200, 50)); //global::entList.push_back(&square(200, 200, 50));
global::entList3D.push_back(&cube(200, 200, 200, 50)); global::entList3D.push_back(&cube(1, 1, 50, 50));
while (global::running) while (global::running)
{ {

@ -3,4 +3,5 @@
bool global::running = true; bool global::running = true;
vector2 global::mousePos; vector2 global::mousePos;
entityList2D global::entList; entityList2D global::entList;
entityList3D global::entList3D; entityList3D global::entList3D;
camera global::cam(vector3(0, 0, 0), vector3(0, 0, 0));

@ -1,5 +1,6 @@
#pragma once #pragma once
#include "../objects/vector3.h" #include "../objects/vector3.h"
#include "../objects/camera.h"
#include "../objects/entityList2D.h" #include "../objects/entityList2D.h"
#include "../objects/entityList3D.h" #include "../objects/entityList3D.h"
namespace global namespace global
@ -8,4 +9,5 @@ namespace global
extern vector2 mousePos; extern vector2 mousePos;
extern entityList2D entList; extern entityList2D entList;
extern entityList3D entList3D; extern entityList3D entList3D;
extern camera cam;
} }

@ -1,5 +1,19 @@
#pragma once #pragma once
#include "vector3.h"
#include "SDL.h"
#include <iostream>
struct camera struct camera
{ {
vector3 position;
vector3 rotation;
camera(vector3 position_, vector3 rotation_)
{
this->position = position_;
this->rotation = rotation_;
}
void run(SDL_Renderer* renderer)
{
//ray3D ray = ray3D::trace(vector3(0, 0, -10), vector3(0, 0, 1));
//std::cout << ray.dist << std::endl;
}
}; };

@ -16,7 +16,7 @@ struct cube : public square
} }
void run(SDL_Renderer* renderer) void run(SDL_Renderer* renderer)
{ {
//SDL_Rect rect{ this->x, this->y, this->radius, this->radius }; SDL_Rect rect{ this->x, this->y, this->radius, this->radius};
//SDL_RenderFillRect(renderer, &rect); //SDL_RenderFillRect(renderer, &rect);
} }
}; };

@ -0,0 +1,46 @@
#pragma once
#include <numeric>
#include "../globals/globals.h"
#include "vector3.h"
#include "cube.h"
struct ray3D
{
vector3 start;
vector3 end;
float dist;
cube* hitEnt;
static ray3D trace(vector3 start, vector3 direction)
{
ray3D ray{ start, start, -1 };
vector3 point = start;
bool hit = false;
while (!hit)
{
for (int i = 0; i < global::entList3D.size(); i++)
{
cube* object = global::entList3D.at(i);
if (inCube(point, vector3(object->x, object->y, object->z), vector3(object->x + object->radius, object->y + object->radius, object->z + object->radius)) || ray.dist > 500)
{
ray.start = start;
ray.end = point;
ray.dist = start.distance(point);
ray.hitEnt = object;
hit = true;
break;
}
}
ray.dist = start.distance(point);
point.x += direction.x;
point.y += direction.y;
point.z += direction.z;
}
return ray;
}
//temp
static bool inCube(vector3 point, vector3 a, vector3 b)
{
return (point.x > a.x && point.y > a.y && point.x < b.x && point.y < b.y && point.z > a.z && point.z < b.z);
}
};

@ -18,4 +18,13 @@ struct vector3 : public vector2
this->y = y_; this->y = y_;
this->z = z_; this->z = z_;
} }
float length()
{
return sqrtf((x * x) + (y * y) + (z * z));
}
float distance(vector3& other)
{
vector3 dist = vector3(this->x - other.x, this->y - other.y, this->z - other.z);
return dist.length();
}
}; };

@ -249,6 +249,7 @@
<ClInclude Include="stuff\objects\cube.h" /> <ClInclude Include="stuff\objects\cube.h" />
<ClInclude Include="stuff\objects\entityList2D.h" /> <ClInclude Include="stuff\objects\entityList2D.h" />
<ClInclude Include="stuff\objects\ray2D.h" /> <ClInclude Include="stuff\objects\ray2D.h" />
<ClInclude Include="stuff\objects\ray3D.h" />
<ClInclude Include="stuff\objects\square.h" /> <ClInclude Include="stuff\objects\square.h" />
<ClInclude Include="stuff\objects\vector2.h" /> <ClInclude Include="stuff\objects\vector2.h" />
<ClInclude Include="stuff\objects\vector3.h" /> <ClInclude Include="stuff\objects\vector3.h" />

@ -309,6 +309,9 @@
<ClInclude Include="demo\demo.h"> <ClInclude Include="demo\demo.h">
<Filter>Header Files</Filter> <Filter>Header Files</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="stuff\objects\ray3D.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="main.cpp"> <ClCompile Include="main.cpp">

Loading…
Cancel
Save