Moved over from Win32 resources to purely nfpack; Included primitives and default shader in base.nfpack; Camera fixes

This commit is contained in:
grayson committed 2021-09-01 13:08:21 -05:00
1 parent 48821afd76
commit c0f7634b5f
32 files changed
+5062 -224

No files matched your search

+6 -5
View File
@@ -137,6 +137,8 @@ namespace nf {
void Application::trackMouse(bool track) {
m_trackingMouse = track;
if (m_trackingMouse == true)
m_mouseTrackFirst = true;
}
void Application::getMouseDiff(int& x, int& y) {
@@ -183,7 +185,7 @@ namespace nf {
x = monX - (m_currentConfig.width / 2);
y = monY - (m_currentConfig.height / 2);
}
//TODO: Test fullscreen graphics
void Application::toggleFullscreen() {
DWORD wndStyle = GetWindowLong(m_window, GWL_STYLE);
if (wndStyle & WS_OVERLAPPEDWINDOW) {
@@ -223,15 +225,14 @@ namespace nf {
m_mouseY = 0;
if (m_trackingMouse) {
static bool first = true;
int middleX = m_currentConfig.width / 2;
int middleY = m_currentConfig.height / 2;
m_mouseDiffX += m_mouseX - middleX;
m_mouseDiffY += middleY - m_mouseY;
if (first) {
if (m_mouseTrackFirst) {
m_mouseDiffX = 0;
m_mouseDiffY = 0;
first = false;
m_mouseTrackFirst = false;
}
POINT middle = { middleX, middleY };
ClientToScreen(m_window, &middle);
@@ -286,7 +287,7 @@ namespace nf {
m_currentState = m_states[m_nextState];
m_currentState->onEnter();
}
//TODO: mouse position input
LRESULT CALLBACK Application::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
Application* app = (Application*)GetProp(hWnd, L"App");
switch (uMsg) {
+20 -1
View File
@@ -16,6 +16,10 @@ namespace nf {
delete[] data;
}
AShader::~AShader() {
}
AssetPack::AssetPack() {
}
@@ -55,12 +59,20 @@ namespace nf {
}
if (extension == "png") {
ATexture* texture = new ATexture;
texture->data = new unsigned char[assetSize];
texture->data = new char[assetSize];
std::memcpy(texture->data, &assetContents[0], assetSize);
texture->size = assetSize;
m_assets[assetName] = texture;
continue;
}
if (extension == "shader") {
AShader* shader = new AShader;
shader->data = new char[assetSize + 1];
std::memcpy(shader->data, &assetContents[0], assetSize);
shader->data[assetSize] = '\0';
m_assets[assetName] = shader;
continue;
}
Error("Invalid asset extention in pack \"" + (std::string)packName + (std::string)"\"!");
}
}
@@ -81,4 +93,11 @@ namespace nf {
delete curr.second;
}
}
AModel* BaseAssets::cube;
AModel* BaseAssets::plane;
AModel* BaseAssets::sphere;
AModel* BaseAssets::cone;
AModel* BaseAssets::cylinder;
AModel* BaseAssets::torus;
}
+31 -22
View File
@@ -7,17 +7,26 @@
namespace nf {
Camera::Camera(Application* app) :
m_app(app)
m_app(app),
m_type(Type::NF_CAMERA_UI),
m_position(0.0),
m_front(0.0, 0.0, -1.0),
m_yaw(-90.0f),
m_pitch(0.0)
{
m_type = Type::NF_CAMERA_UI;
}
void Camera::setType(Type cameraType) {
m_type = cameraType;
if (m_type == Type::NF_CAMERA_FIRST_PERSON || m_type == Type::NF_CAMERA_ORBIT)
m_app->trackMouse(true);
else
m_app->trackMouse(false);
if (cameraType != m_type) {
m_type = cameraType;
if (m_type == Type::NF_CAMERA_FIRST_PERSON || m_type == Type::NF_CAMERA_ORBIT)
m_app->trackMouse(true);
else
m_app->trackMouse(false);
m_yaw = -90.0f;
m_pitch = 0.0f;
}
}
Camera::Type Camera::getType() const {
@@ -26,24 +35,26 @@ namespace nf {
void Camera::moveForward(double speed) {
Vec3 temp = m_front * speed;
m_position = { m_position.x + temp.x, m_position.y + temp.y, m_position.z + temp.z };
m_position += temp;
}
void Camera::moveBackward(double speed) {
Vec3 temp = m_front * speed;
m_position = { m_position.x - temp.x, m_position.y - temp.y, m_position.z - temp.z };
m_position -= temp;
}
void Camera::moveRight(double speed) {
glm::vec3 front = { m_front.x, m_front.y, m_front.z };
glm::vec3 temp = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0))) * (float)speed;
m_position = { m_position.x + temp.x, m_position.y + temp.y, m_position.z + temp.z };
Vec3 move = { temp.x, temp.y, temp.z };
m_position += move;
}
void Camera::moveLeft(double speed) {
glm::vec3 front = { m_front.x, m_front.y, m_front.z };
glm::vec3 temp = glm::normalize(glm::cross(front, glm::vec3(0.0, 1.0, 0.0))) * (float)speed;
m_position = { m_position.x - temp.x, m_position.y - temp.y, m_position.z - temp.z };
Vec3 move = { temp.x, temp.y, temp.z };
m_position -= move;
}
void Camera::setPosition(double x, double y, double z) {
@@ -68,18 +79,16 @@ namespace nf {
m_app->getMouseDiff(mouseDiffx, mouseDiffy);
float mouseX = (float)mouseDiffx * 0.1f;
float mouseY = (float)mouseDiffy * 0.1f;
static float yaw = -90.0f;
static float pitch = 0.0f;
yaw += mouseX;
pitch += mouseY;
if (pitch > 89.0f)
pitch = 89.0f;
if (pitch < -89.0f)
pitch = -89.0f;
m_yaw += mouseX;
m_pitch += mouseY;
if (m_pitch > 89.0f)
m_pitch = 89.0f;
if (m_pitch < -89.0f)
m_pitch = -89.0f;
glm::vec3 rotation;
rotation.x = std::cos(glm::radians(yaw)) * std::cos(glm::radians(pitch));
rotation.y = std::sin(glm::radians(pitch));
rotation.z = std::sin(glm::radians(yaw)) * std::cos(glm::radians(pitch));
rotation.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
rotation.y = std::sin(glm::radians(m_pitch));
rotation.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
rotation = glm::normalize(rotation);
m_front = { rotation.x, rotation.y, rotation.z };
glm::vec3 position(m_position.x, m_position.y, m_position.z);
+1 -1
View File
@@ -14,7 +14,7 @@ namespace nf {
void Entity::create(Asset* modelAsset, Asset* textureAsset) {
AModel& model = *(AModel*)modelAsset;
if (model.alreadyLoaded) {
if (model.alreadyLoaded && textureAsset == nullptr) {
m_model = model.loadedModel;
return;
}
+1 -1
View File
@@ -10,7 +10,7 @@ namespace nf {
Model::Model() {
}
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const unsigned char* textureData, size_t textureSize) {
void Model::create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinatesBufferData, size_t textureCoordinatesBufferSize, const char* textureData, size_t textureSize) {
m_vao = new VertexArray;
m_vao->addBuffer(vertexBufferData, vertexBufferSize);
m_vao->push<float>(3);
+11 -7
View File
@@ -6,7 +6,6 @@
#include "Application.h"
#include "Utility.h"
#include "resource.h"
namespace nf {
Renderer::Renderer(Application* app) {
@@ -55,19 +54,24 @@ namespace nf {
glEnable(GL_CULL_FACE);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Win32Res vs(IDR_DEFAULTVERTEX);
const char* defaultVertex = (const char*)vs.ptr;
Win32Res fs(IDR_DEFAULTFRAGMENT);
const char* defaultFragment = (const char*)fs.ptr;
if (defaultVertex == nullptr || defaultFragment == nullptr)
Error("Default engine resources not found! Please link nf.res to your application!");
baseAP.load("base.nfpack");
const char* defaultVertex = baseAP["defaultVertex.shader"]->data;
const char* defaultFragment = baseAP["defaultFragment.shader"]->data;
m_defaultShader = new Shader(defaultVertex, defaultFragment);
BaseAssets::cube = (AModel*)baseAP["cube.obj"];
BaseAssets::plane = (AModel*)baseAP["plane.obj"];
BaseAssets::sphere = (AModel*)baseAP["sphere.obj"];
BaseAssets::cone = (AModel*)baseAP["cone.obj"];
BaseAssets::cylinder = (AModel*)baseAP["cylinder.obj"];
BaseAssets::torus = (AModel*)baseAP["torus.obj"];
}
void Renderer::render(Entity& in) {
if (&in == nullptr)
Error("Tried to render Entity before being created!");
m_lGame.push_back(&in);
//TODO: Sort transparent objects by distance; Farthest first
}
void Renderer::doFrame(Camera* camera) {
+2 -2
View File
@@ -11,10 +11,10 @@ namespace nf {
glGenTextures(1, &m_id);
}
void Texture::create(const unsigned char* textureData, size_t textureSize) {
void Texture::create(const char* textureData, size_t textureSize) {
int width, height, nChannels;
stbi_set_flip_vertically_on_load(true);
unsigned char* texture = stbi_load_from_memory(textureData, textureSize, &width, &height, &nChannels, 0);
unsigned char* texture = stbi_load_from_memory((unsigned char*)textureData, textureSize, &width, &height, &nChannels, 0);
if (!texture)
Error("Texture failed to load from memory!");
glBindTexture(GL_TEXTURE_2D, m_id);
-14
View File
@@ -53,20 +53,6 @@ namespace nf {
}
#endif
Win32Res::Win32Res(int id) :
ptr(nullptr),
size(0)
{
HRSRC src = FindResource(NULL, MAKEINTRESOURCE(id), RT_RCDATA);
if (src) {
HGLOBAL temp = LoadResource(NULL, src);
if (temp) {
ptr = LockResource(temp);
size = (size_t)SizeofResource(NULL, src);
}
}
}
const wchar_t* toWide(const char* in) {
int length = std::strlen(in) + 1;
wchar_t* out = new wchar_t[length];
+1
View File
@@ -77,6 +77,7 @@ namespace nf {
bool m_input[164];
int m_mouseX, m_mouseY;
bool m_trackingMouse;
bool m_mouseTrackFirst;
int m_mouseDiffX, m_mouseDiffY;
//Renderer object to use OpenGL to render the current state
+17 -2
View File
@@ -5,11 +5,12 @@ namespace nf {
class Model;
struct Asset {
char* data;
virtual ~Asset();
};
struct AModel : Asset {
char* data;
bool alreadyLoaded;
Model* loadedModel;
@@ -17,12 +18,15 @@ namespace nf {
};
struct ATexture : Asset {
unsigned char* data;
size_t size;
~ATexture() override;
};
struct AShader : Asset {
~AShader() override;
};
class AssetPack {
public:
AssetPack();
@@ -35,4 +39,15 @@ namespace nf {
private:
std::unordered_map<std::string, Asset*> m_assets;
};
struct BaseAssets {
static AModel* cube;
static AModel* plane;
static AModel* sphere;
static AModel* cone;
static AModel* cylinder;
static AModel* torus;
static ATexture* logo;
};
}
+2
View File
@@ -34,5 +34,7 @@ namespace nf {
Vec3 m_position;
Vec3 m_front;
float m_yaw;
float m_pitch;
};
}
+1 -1
View File
@@ -8,7 +8,7 @@ namespace nf {
public:
Model();
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates, size_t textureCoordinatesBufferSize, const unsigned char* textureData, size_t textureSize);
void create(const void* vertexBufferData, const size_t vertexBufferSize, const void* indexBufferData, size_t indexBufferCount, const void* textureCoordinates, size_t textureCoordinatesBufferSize, const char* textureData, size_t textureSize);
void bind() override;
~Model();
+1
View File
@@ -132,6 +132,7 @@ namespace nf {
bool m_input[164];
int m_mouseX, m_mouseY;
bool m_trackingMouse;
bool m_mouseTrackFirst;
int m_mouseDiffX, m_mouseDiffY;
//Renderer object to use OpenGL to render the current state
+2
View File
@@ -24,6 +24,8 @@ namespace nf {
HDC m_hdc;
HGLRC m_hglrc;
AssetPack baseAP;
std::vector<Entity*> m_lGame;
std::vector<Drawable*> m_lUI;
Shader* m_defaultShader;
+1 -1
View File
@@ -5,7 +5,7 @@ namespace nf {
public:
Texture();
void create(const unsigned char* textureData, size_t textureSize);
void create(const char* textureData, size_t textureSize);
void bind();
~Texture();
+13 -8
View File
@@ -39,20 +39,25 @@ __debugbreak();}
std::exit(-1);}
#endif
//TODO: Delete this after moving everything to base.nfpack
struct Win32Res {
Win32Res(int id);
void* ptr;
size_t size;
};
struct Vec3 {
Vec3() {}
Vec3() : x(0.0), y(0.0), z(0.0) {}
Vec3(double x1) : x(x1), y(x1), z(x1) {}
Vec3(double x1, double y1, double z1) : x(x1), y(y1), z(z1) {}
Vec3 operator*(const double scalar) {
return Vec3(x * scalar, y * scalar, z * scalar);
}
Vec3& operator+=(const Vec3& rhs) {
this->x += rhs.x;
this->y += rhs.y;
this->z += rhs.z;
return *this;
}
Vec3& operator-=(const Vec3& rhs) {
this->x -= rhs.x;
this->y -= rhs.y;
this->z -= rhs.z;
return *this;
}
double x, y, z;
};
-17
View File
@@ -1,17 +0,0 @@
//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by NothinFancy.rc
//
#define IDR_DEFAULTVERTEX 201
#define IDR_DEFAULTFRAGMENT 202
// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif