|
Yes. I used that tutorial from "drunkenhyena.com" to understand more about the direct 3d
The code I'm working is huge.
Here is the whole code.
main.cpp
#include "common.h"
#pragma comment(lib, "d3d9")
#pragma comment(lib, "user32")
#pragma comment(lib, "gdi32")
extern IDirect3D9 *g_d3d9;
extern IDirect3DDevice9 *g_device;
extern D3DPRESENT_PARAMETERS g_pparams;
extern const unsigned Width;
extern const unsigned Height;
extern HRESULT Init_d3d9(IDirect3D9 **d3d9);
extern void Init_pparams(HWND hwnd, D3DFORMAT format, D3DFORMAT depth, D3DPRESENT_PARAMETERS *pparams);
extern HRESULT Init_device(IDirect3D9 *d3d9, HWND hwnd, D3DPRESENT_PARAMETERS *pparams, IDirect3DDevice9 **device);
extern void Kill_d3d9(IDirect3D9 **d3d9, IDirect3DDevice9 **device);
HRESULT load_RAW(const char *fname, IDirect3DVertexBuffer9 **buffer, int *count)
{
HRESULT hr = E_FAIL;
FILE *f = fopen(fname, "r");
if(f)
{
(*count) = 0;
float tmp[3] = {0};
while(fscanf(f, "%g %g %g", &tmp[0], &tmp[1], &tmp[2]) == 3)(*count)++;
rewind(f);
HUD_VERTEX vert[(*count)];
memset(vert, 0, sizeof(vert));
for(int i = 0; i < (*count); i++)
{
fscanf(f, "%g %g %g", &(vert[i].x), &(vert[i].y), &(vert[i].z));
if(ferror(f))
{
fclose(f);
return hr;
}
}
fclose(f);
int byteLen = sizeof(vert);
hr = g_device->CreateVertexBuffer(byteLen, D3DUSAGE_WRITEONLY, HUD_VERTEX_fvf, D3DPOOL_MANAGED, buffer, NULL);
if(FAILED(hr))return hr;
void *ptr = NULL;
hr = (*buffer)->Lock(0, 0, &ptr, 0);
if(FAILED(hr))return hr;
memcpy(ptr, vert, byteLen);
hr = (*buffer)->Unlock();
if(FAILED(hr))return hr;
}
fclose(f);
return hr;
}
void Resources_Init()
{
}
void Resources_Free()
{
}
HRESULT Device_Render(IDirect3DVertexBuffer9 *buffer, int count)
{
HRESULT hr = D3D_OK;
hr = g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0), 0.0, 0);
if(FAILED(hr))return hr;
hr = g_device->BeginScene();
if(FAILED(hr))return hr;
g_device->SetFVF(HUD_VERTEX_fvf);
g_device->SetStreamSource(0, buffer, 0, sizeof(HUD_VERTEX));
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, count);
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_KEYDOWN:
case WM_CLOSE:
case WM_LBUTTONDOWN:
PostQuitMessage(0);
return 0;
default: return DefWindowProc(hwnd, message, wParam, lParam);
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
WNDCLASS wc;
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Render";
RegisterClass(&wc);
HWND hwnd = CreateWindow("Render", "Test window", WS_POPUP | WS_VISIBLE, 0, 0, Width, Height, NULL, NULL, hInstance, NULL);
HRESULT hr = D3D_OK;
hr = Init_d3d9(&g_d3d9);
if(FAILED(hr))
{
LOG("Init_d3d9() failed!", hr);
DestroyWindow(hwnd);
return 0;
}
Init_pparams(hwnd, D3DFMT_A8R8G8B8, D3DFMT_D24S8, &g_pparams);
hr = Init_device(g_d3d9, hwnd, &g_pparams, &g_device);
if(FAILED(hr))
{
LOG("Init_device() failed!", hr);
Kill_d3d9(&g_d3d9, &g_device);
DestroyWindow(hwnd);
return 0;
}
int circleTriangles = 0;
IDirect3DVertexBuffer9 *circle = NULL;
hr = load_RAW("circle.raw", &circle, &circleTriangles);
if(FAILED(hr))
{
LOG("load_RAW() failed!", hr);
Kill_d3d9(&g_d3d9, &g_device);
DestroyWindow(hwnd);
return 0;
}
MSG msg;
while(GetMessage(&msg, NULL, 0, 0))
{
hr = Device_Render(circle, circleTriangles + 1);
if(FAILED(hr))
{
LOG("Device_Render() failed!", hr);
Kill_d3d9(&g_d3d9, &g_device);
DestroyWindow(hwnd);
return 0;
}
hr = g_device->TestCooperativeLevel();
if(FAILED(hr))
{
LOG("TestCooperativeLevel() failed!", hr);
Kill_d3d9(&g_d3d9, &g_device);
DestroyWindow(hwnd);
return 0;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
Kill_d3d9(&g_d3d9, &g_device);
DestroyWindow(hwnd);
return msg.wParam;
}
d3d9_api.cpp
#include "common.h"
IDirect3D9 *g_d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
IDirect3DDevice9 *g_device = NULL;
D3DPRESENT_PARAMETERS g_pparams = {0};
extern const unsigned Width = GetSystemMetrics(SM_CXSCREEN);
extern const unsigned Height = GetSystemMetrics(SM_CYSCREEN);
HRESULT Init_d3d9(IDirect3D9 **d3d9)
{
LOG(__FUNCTION__);
(*d3d9) = Direct3DCreate9(D3D_SDK_VERSION);
if(!(*d3d9))return E_FAIL;
else return D3D_OK;
}
void Init_pparams(HWND hwnd, D3DFORMAT format, D3DFORMAT depth, D3DPRESENT_PARAMETERS *pparams)
{
LOG(__FUNCTION__);
memset(pparams, 0, sizeof(D3DPRESENT_PARAMETERS));
pparams->BackBufferCount = 1;
pparams->MultiSampleType = D3DMULTISAMPLE_NONE;
pparams->MultiSampleQuality = 0;
pparams->SwapEffect = D3DSWAPEFFECT_DISCARD;
pparams->hDeviceWindow = hwnd;
pparams->Flags = 0;
pparams->FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
pparams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
pparams->BackBufferFormat = format;
if(depth == D3DFMT_UNKNOWN)pparams->EnableAutoDepthStencil = false;
else pparams->EnableAutoDepthStencil = true;
pparams->AutoDepthStencilFormat = depth;
pparams->Windowed = false;
pparams->BackBufferWidth = Width;
pparams->BackBufferHeight = Height;
}
HRESULT Init_device(IDirect3D9 *d3d9, HWND hwnd, D3DPRESENT_PARAMETERS *pparams, IDirect3DDevice9 **device)
{
LOG(__FUNCTION__);
HRESULT hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, pparams, device);
return hr;
}
void Kill_d3d9(IDirect3D9 **d3d9, IDirect3DDevice9 **device)
{
LOG(__FUNCTION__);
if((*device))
{
(*device)->Release();
(*device) = NULL;
}
if((*d3d9))
{
(*d3d9)->Release();
(*d3d9) = NULL;
}
}
extern void Resources_Init();
extern void Resources_Free();
HRESULT Reset_device(IDirect3DDevice9 *device, D3DPRESENT_PARAMETERS *pparams, HRESULT hr)
{
if(hr == D3DERR_DEVICELOST)
{
Resources_Free();
Sleep(500);
}
else if(hr == D3DERR_DEVICENOTRESET)
{
hr = device->Reset(pparams);
if(SUCCEEDED(hr))Resources_Init();
}
return hr;
}
common.h
#include <d3d9.h>
#include <stdio.h>
class gameLog
{
public:
gameLog(const char *file = "log.log")
{
FILE *f = fopen(file, "w");
fclose(f);
}
void operator()(const char *str, HRESULT hr = D3D_OK, const char *file = "log.log")
{
FILE *f = fopen(file, "a");
if(hr != D3D_OK) fprintf(f, "%s\tHRESULT: %#x\n", str, hr);
else fprintf(f, "%s\n", str);
fclose(f);
}
};
static gameLog LOG;
struct HUD_VERTEX
{
float x, y, z, rhw;
unsigned color;
};
const unsigned HUD_VERTEX_fvf = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;
typedef HRESULT (WINAPI *D3DXCreateTextureFromFile_ENTRY)(IDirect3DDevice9 *pDevice, LPCTSTR pSrcFile, IDirect3DTexture9 **ppTexture);
inline HRESULT WINAPI D3DXCreateTextureFromFile(IDirect3DDevice9 *pDevice, LPCTSTR pSrcFile, IDirect3DTexture9 **ppTexture)
{
HINSTANCE hlib = LoadLibrary("d3dx9.dll");
if(hlib)
{
#ifdef UNICODE
D3DXCreateTextureFromFile_ENTRY func = (D3DXCreateTextureFromFile_ENTRY)GetProcAddress(hlib, "D3DXCreateTextureFromFileW");
#else
D3DXCreateTextureFromFile_ENTRY func = (D3DXCreateTextureFromFile_ENTRY)GetProcAddress(hlib, "D3DXCreateTextureFromFileA");
#endif
if(func)return func(pDevice, pSrcFile, ppTexture);
else return E_FAIL;
}
else return E_FAIL;
}
|
|
|
|
|
I did kind-of ask for that didn't I?? Okay... That's gonna take me a bit to look through....
_________________________
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
|
|
|
|
|
By the way I'm using Blender 2.49a for exporting the geometry files
Here
HRESULT load_RAW(const char *fname, IDirect3DVertexBuffer9 **buffer, int *count)
{
HRESULT hr = E_FAIL;
FILE *f = fopen(fname, "r");
if(f)
{
(*count) = 0;
float tmp[3] = {0};
while(fscanf(f, "%g %g %g", &tmp[0], &tmp[1], &tmp[2]) == 3)(*count)++;
rewind(f);
HUD_VERTEX vert[(*count)];
memset(vert, 0, sizeof(vert));
for(int i = 0; i < (*count); i++)
{
fscanf(f, "%g %g %g", &(vert[i].x), &(vert[i].y), &(vert[i].z));
if(ferror(f))
{
fclose(f);
return hr;
}
}
fclose(f);
int byteLen = sizeof(vert);
hr = g_device->CreateVertexBuffer(byteLen, D3DUSAGE_WRITEONLY, HUD_VERTEX_fvf, D3DPOOL_MANAGED, buffer, NULL);
if(FAILED(hr))return hr;
void *ptr = NULL;
hr = (*buffer)->Lock(0, 0, &ptr, 0);
if(FAILED(hr))return hr;
memcpy(ptr, vert, byteLen);
hr = (*buffer)->Unlock();
if(FAILED(hr))return hr;
}
fclose(f);
return hr;
}
And here
HRESULT Device_Render(IDirect3DVertexBuffer9 *buffer, int count)
{
HRESULT hr = D3D_OK;
hr = g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0), 0.0, 0);
if(FAILED(hr))return hr;
hr = g_device->BeginScene();
if(FAILED(hr))return hr;
g_device->SetFVF(HUD_VERTEX_fvf);
g_device->SetStreamSource(0, buffer, 0, sizeof(HUD_VERTEX));
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, count);
g_device->EndScene();
g_device->Present(NULL, NULL, NULL, NULL);
}
|
|
|
|
|
I have never looked at the RAW output from Blender, and without compiling this only one thing "jumps" out at me....
g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, count);
Are you sure that Blender is outputting a Triangle Strip or a list of triangles?
http://en.wikipedia.org/wiki/File:Triangle_Strip_Small.png[^]
In this example a Triangle strip would be vertexes A, B, C, D, E, F.
But a list of triangles would be:
A,B,C, C,B,D, E,C,D, F,E,D
You can see how incorrectly sending the right destination from one to the other could render with bizarre results....
_________________________
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
|
|
|
|
|
Blender is outputting a strange kind of list of triangles. For instance, a circle, it seems that Blender is outputting a triangle fan.
|
|
|
|
|
ant-damage wrote: it seems that Blender is outputting a triangle fan.
Then you will need to define this as a triangle fan for display....
_________________________
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
|
|
|
|
|
I think that I've found the solution for my problem here.
|
|
|
|
|
The RAW Triangle Format is parsed by reading lines, not individual values. That was what I didn't know.
|
|
|
|
|
congrats on finding it! I never worked with raw, we use other model formats.
_________________________
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
|
|
|
|
|
By the way, which formats do you use?
|
|
|
|
|
3dStudio, OpenFlight... Mostly
basically everything supported by open scene graph.
I haven't written my own loader for years.
_________________________
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
|
|
|
|
|
Hey, i just wanted to ask if theres a way i can read the data of a file so i can paste it to another file?
Like i read the data of cmd.exe and paste it to whatever.exe?
|
|
|
|
|
Please be clear what you are asking.
ALLERSLIT wrote: i can read the data of a file so i can paste it to another file?
cmd.exe is not a file to be read from, it's an executable.
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
Executables are files though
|
|
|
|
|
and an ice cube is water. What's your point?
I know the language. I've read a book. - _Madmatt
|
|
|
|
|
That it's fine to read from cmd.exe, it's just unusual
|
|
|
|
|
I believe we are going to need more details about what you want to do...till now it sounds like you either want to read file data to the clipboard to paste it somewhere OR you need a filecopy method (like copy in a dos prompt).
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
I want to create a program out of a program.
We all have a cmd.exe in our windows folder, how would i be able to create the cmd.exe out of my own program?
|
|
|
|
|
What would your program do?
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Are you asking how to find executable code inside a program and copy it to another file, in order to run it?
If so, you have to learn the structure of the program.
|
|
|
|
|
ALLERSLIT wrote: Hey, i just wanted to ask if theres a way i can read the data of a file so i can paste it to another file?
Like i read the data of cmd.exe and paste it to whatever.exe?
back in the 1990's, all programs started at the same offset. This made chaining programs easy. In fact a tool called LzExe[^] would compress an executable code, replace the loader function, and chain to the offset starting block.
Now it is a little more difficult. Without knowing your intentions, few people here are likely to give you a step-by-step walk-through. The process is similar, but you will have to read the EXE header and still replace it, that much remains the same, chaining to the code is a little more difficult, but not impossible, but the act of which will cause any security product on the machine such as anti-virus software to halt your action.
If your intent is only to insert program code into a process there are other safer ways to do this without modifying an executable. Dlls offer the safest disk method of injecting code into programs. There are also active methods of inserting code into running processes.
So.... as others have asked, what exactly are you trying to do? modify an executable inserting your own code? for what reason? what are you trying to accomplish?
_________________________
John Andrew Holmes "It is well to remember that the entire universe, with one trifling exception, is composed of others."
Shhhhh.... I am not really here. I am a figment of your imagination.... I am still in my cave so this must be an illusion....
|
|
|
|
|
You already asked this question (although in a slightly different way) here[^]. Reading the contents of an executable file and writing it somewhere else is the same as reading and writing any file; files are composed of bytes and bytes are bytes are bytes. In either case your objective is not clear, try rewording your question to clarify what problem you are actually trying to solve.
It's time for a new signature.
|
|
|
|
|
Why array starts with zero?
Best wishes
Nikesh
|
|
|
|
|
Because it's natural.
-- the C developer
...Or [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Because 0 is the starting number
|
|
|
|
|