Compare commits

..

2 Commits

Author SHA1 Message Date
ead9c883fe view rotation and translation 2026-01-13 20:15:21 -06:00
cdf49949c3 add xinput 2026-01-13 19:21:17 -06:00
5 changed files with 163 additions and 24 deletions

View File

@ -24,10 +24,11 @@ CFLAGS += -Wno-unknown-pragmas
CFLAGS += -I./include
CFLAGS += -municode
LDFLAGS += -municode
LIBS += -ld3d10
CXXFLAGS += -fno-exceptions
LIBS = -ld3d10
all: $(BUILD_TYPE)/d3d10.exe
$(BUILD_TYPE)/%.fxo: src/%.fx
@ -60,6 +61,7 @@ OBJS = \
$(BUILD_TYPE)/main.obj \
$(BUILD_TYPE)/print.obj \
$(BUILD_TYPE)/render_state.obj \
$(BUILD_TYPE)/input.obj \
$(BUILD_TYPE)/main.res
$(BUILD_TYPE)/d3d10.exe: $(OBJS)

20
include/input.hpp Normal file
View File

@ -0,0 +1,20 @@
#pragma once
#ifndef _INPUT_HPP_
#define _INPUT_HPP_
HRESULT InitInput(HINSTANCE hInstance);
void UpdateInput();
struct Joystate {
int buttons;
float triggerL;
float triggerR;
float thumbLX;
float thumbLY;
float thumbRX;
float thumbRY;
};
extern Joystate g_Joystate;
#endif

View File

@ -67,7 +67,7 @@ PS_INPUT VSInstanced(VS_INPUT_INSTANCED input)
{
VS_INPUT input_vs = (VS_INPUT)0;
input_vs.Pos = mul(float4(input.Pos.xyz, 1), input.mTransform);
input_vs.Pos = mul(float4(input.Pos.xyz * 0.3f, 1), input.mTransform);
//input_vs.Pos = float4(input.Pos.xyz, 1) + float4(0, input.InstanceID * 3, 0, 0);
input_vs.Normal = input.Normal;
input_vs.Tex = input.Tex;

68
src/input.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <windows.h>
#include <assert.h>
#include <XInput.h>
#include "print.hpp"
#include "input.hpp"
Joystate g_Joystate;
typedef DWORD (WINAPI * lpXInputGetCapabilities)(DWORD, DWORD, XINPUT_CAPABILITIES *);
typedef DWORD (WINAPI * lpXInputGetState)(DWORD, XINPUT_STATE *);
//lpXInputGetCapabilities g_XInputGetCapabilities;
lpXInputGetState g_XInputGetState;
HRESULT InitInput(HINSTANCE hInstance)
{
const char * libs[] = {
"xinput1_4.dll",
"xinput1_3.dll",
"xinput9_1_0.dll",
"xinput1_2.dll",
"xinput1_1.dll",
};
const int libs_length = (sizeof (libs)) / (sizeof (libs[0]));
HMODULE hLib;
for (int i = 0; i < libs_length; i++) {
hLib = LoadLibraryA(libs[i]);
if (hLib != NULL) {
//print("using xinput: %s\n", libs[i]);
break;
}
}
if (hLib == NULL) {
print("no xinput module found\n");
return E_FAIL;
}
//g_XInputGetCapabilities = (lpXInputGetCapabilities)GetProcAddress(hLib, "XInputGetCapabilities");
//assert(g_XInputGetCapabilities != NULL);
g_XInputGetState = (lpXInputGetState)GetProcAddress(hLib, "XInputGetState");
assert(g_XInputGetState != NULL);
return S_OK;
}
void UpdateInput()
{
XINPUT_STATE state;
for (DWORD i = 0; i < 4; i++) {
DWORD ret = g_XInputGetState(i, &state);
if (ret != ERROR_SUCCESS) {
continue;
}
break;
}
const float trigger = 1.0f / 255.0f;
const float thumb = 1.0f / 32768.0f;
g_Joystate.buttons = state.Gamepad.wButtons;
g_Joystate.triggerL = ((float)state.Gamepad.bLeftTrigger) * trigger;
g_Joystate.triggerR = ((float)state.Gamepad.bRightTrigger) * trigger;
g_Joystate.thumbLX = ((float)state.Gamepad.sThumbLX) * thumb;
g_Joystate.thumbLY = ((float)state.Gamepad.sThumbLY) * thumb;
g_Joystate.thumbRX = ((float)state.Gamepad.sThumbRX) * thumb;
g_Joystate.thumbRY = ((float)state.Gamepad.sThumbRY) * thumb;
}

View File

@ -8,6 +8,7 @@
#include "globals.hpp"
#include "print.hpp"
#include "render_state.hpp"
#include "input.hpp"
#include "gltf.hpp"
#include "gltf_instance.hpp"
@ -119,12 +120,18 @@ XMFLOAT4 g_vLightColors[2] = {
XMFLOAT4(0.9f, 0.0f, 0.0f, 1.0f)
};
//
XMVECTOR g_Eye = XMVectorSet(0.0f, 0.0f, -2.0f, 1.0f);
XMVECTOR g_At = XMVectorSet(0.0f, 0.0f, 0.0f, 1.0f);
// forward declarations
HRESULT InitWindow(HINSTANCE hInstance, int nCmdShow);
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
HRESULT InitDirect3DDevice();
void Render();
void Render(float t);
void Update(float t);
BOOL Resize();
void InitializeNodeInstances();
@ -151,6 +158,17 @@ const FontSize g_FontSize = {
WindowSize g_ViewportSize;
float GetTime()
{
static DWORD dwTimeStart = 0;
DWORD dwTimeCur = GetTickCount();
if (dwTimeStart == 0)
dwTimeStart = dwTimeCur;
float t = (dwTimeCur - dwTimeStart) / 1000.0f;
return t;
}
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
//FreeConsole();
@ -168,6 +186,11 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
InitializeNodeInstances();
if (FAILED(InitInput(hInstance))) {
print("InitInput\n");
return 0;
}
MSG msg = {};
while (msg.message != WM_QUIT) {
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
@ -189,8 +212,12 @@ int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLi
TranslateMessage(&msg);
DispatchMessage(&msg);
} else {
if (Resize())
Render();
if (Resize()) {
UpdateInput();
float t = GetTime();
Update(t);
Render(t);
}
}
}
@ -1129,11 +1156,6 @@ HRESULT InitDirect3DDevice()
g_World1 = XMMatrixIdentity();
g_World2 = XMMatrixIdentity();
XMVECTOR Eye = XMVectorSet(0.0f, 0.0f, -2.0f, 0.0f);
XMVECTOR At = XMVectorSet(0.0f, 0.0f, 0.0f, 0.0f);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
g_View = XMMatrixLookAtLH(Eye, At, Up);
float fFov = XM_PI * 0.5f;
float fAspect = width / (float)height;
float fNear = 0.1f;
@ -1439,10 +1461,22 @@ void RenderFont()
print("g_pVertexBuffersFont->Map");
}
int length = sprint("bloomPasses: %d\n"
" exposure: %f",
g_bloomPasses,
g_exposure);
sprint("bloomPasses: %d\n"
" exposure: %f\n\n"
"triggerL: % 5.3f\n"
"triggerR: % 5.3f\n"
" thumbLX: % 5.3f\n"
" thumbLY: % 5.3f\n"
" thumbRX: % 5.3f\n"
" thumbRY: % 5.3f",
g_bloomPasses,
g_exposure,
g_Joystate.triggerL,
g_Joystate.triggerR,
g_Joystate.thumbLX,
g_Joystate.thumbLY,
g_Joystate.thumbRX,
g_Joystate.thumbRY);
const char start_advance = 10;
int hadvance = start_advance;
@ -1509,7 +1543,7 @@ void RenderFont()
for (UINT p = 0; p < techDesc.Passes; p++) {
g_pTechniqueFont->GetPassByIndex(p)->Apply(0);
g_pd3dDevice->Draw(length, 0);
g_pd3dDevice->Draw(ix, 0);
}
}
@ -1592,6 +1626,16 @@ void RenderBloom()
}
}
float deadzone(float f)
{
const float threshold = 0.25f;
if (fabsf(f) <= threshold)
return 0.0f;
else {
return copysignf(fabsf(f) - threshold, f);
}
}
void Update(float t)
{
XMVECTOR vLightDirs[2] = {
@ -1606,6 +1650,19 @@ void Update(float t)
XMMATRIX mRotate0 = XMMatrixRotationY(0.4f * t);
XMVECTOR lightDir0 = XMVector4Transform(vLightDirs[0], mRotate0);
XMStoreFloat4(&g_vLightDirs[0], lightDir0);
// view
XMMATRIX mRotateView = XMMatrixRotationY(deadzone(g_Joystate.thumbLX) * 0.002f);
XMMATRIX mTranslateView = XMMatrixTranslation(deadzone(g_Joystate.thumbRX) * 0.002f,
0.0f,
deadzone(g_Joystate.thumbRY) * 0.002f);
g_Eye = XMVector4Transform(g_Eye, mTranslateView * mRotateView);
XMVECTOR Up = XMVectorSet(0.0f, 1.0f, 0.0f, 0.0f);
g_View = XMMatrixLookAtLH(g_Eye, g_At, Up);
}
void RenderVolume(float t)
@ -1681,16 +1738,8 @@ void RenderVolumeMesh()
}
}
void Render()
void Render(float t)
{
static DWORD dwTimeStart = 0;
DWORD dwTimeCur = GetTickCount();
if (dwTimeStart == 0)
dwTimeStart = dwTimeCur;
float t = (dwTimeCur - dwTimeStart) / 1000.0f;
Update(t);
// clear
const float ClearColor[4] = { 0.2f, 0.125f, 0.2f, 1.0f };