Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Naptha

Pages: [1] 2 3
1
General chat / Re: It's been 10 years!
« on: February 13, 2011 »
Hey!

Well I've not dropped in here for a very long time but it is great to see the place still running.  :)

Maybe if I get some time I will finally enter a compo this year.  ;D

2
Found the problem!  Apparently this is an artifact of the texture filtering, since I actually want the exact pixels I rendered initially I didn't need filtering anyway so just turning it off in the shader worked.  :||

Now to make it do something interesting... :P

3
Here's two screenshots of what I'm seeing.. I'm sure it's not just my eyes..  ???

One without the shader has even brightness, the shader one is darker at the edges.


4
Well it took me longer to get around to implementing a shader than I thought.  I have a very basic shader which should do nothing except display the quad with the texture I've rendered to.  However, it seems to have some kind of lighting effect (although lighting is turned off) whereby the stars get brighter in the centre of the screen.  It's not an unpleasant effect but I've been tearing my hair out trying to figure out why it's happening.   :whack:  Can anyone shed any light on it?

Here's the shader:
Code: [Select]
// Global Variables --------------------------

float4x4 matWVP : WorldViewProjection;
texture StarTex;


// Vertex shader -----------------------------

struct VS_INPUT
{
float3 Pos : POSITION;
float2 Tex : TEXCOORD0;
};

struct VS_OUTPUT
{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};

VS_OUTPUT vs_main( VS_INPUT In )
{
VS_OUTPUT Out;
Out.Pos = mul(float4(In.Pos, 1.0f), matWVP);
Out.Tex = In.Tex;
return Out;
}


// Pixel shader ------------------------------

sampler StarTexSampler =
sampler_state
{
Texture = (StarTex);
MipFilter = LINEAR;
MinFilter = LINEAR;
MagFilter = LINEAR;
};

float4 ps_main(float2 texCoord: TEXCOORD0) : COLOR
{
return tex2D(StarTexSampler, texCoord);
}


// Technique --------------------------------

technique Blur
{
    pass P0
    {
        VertexShader = compile vs_2_0 vs_main();
        PixelShader = compile ps_2_0 ps_main();
    }
}

And here's the code:
Code: [Select]
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN

#include <d3d9.h>
#include <d3dx9.h>

// lib replacement stuff --------------------------------------------------------
// Thanks to Jim for most of this :)

static unsigned int next = 1;

/*__forceinline void MEMSET( void *_dst, int _val, size_t _sz )
{
   while ( _sz ) ((BYTE *)_dst)[--_sz] = _val;
}*/

int rand(void)
{
next = next * 1103515245 + 12345;
return (int)((next/65536) % 32768);
}

void srand(unsigned int seed)
{
next = seed;
}

extern "C"
{
int _fltused;

void __declspec(naked) _chkstk(void)
{
#define _PAGESIZE_ 1000h

__asm
{
push    ecx

; Calculate new TOS.

lea     ecx, [esp] + 8 - 4      ; TOS before entering function + size for ret value
sub     ecx, eax                ; new TOS

; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.

sbb     eax, eax                ; 0 if CF==0, ~0 if CF==1
not     eax                     ; ~0 if TOS did not wrapped around, 0 otherwise
and     ecx, eax                ; set to 0 if wraparound

mov     eax, esp                ; current TOS
and     eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary

cs10:
cmp     ecx, eax                ; Is new TOS
jb      short cs20              ; in probed page?
mov     eax, ecx                ; yes.
pop     ecx
xchg    esp, eax                ; update esp
mov     eax, dword ptr [eax]    ; get return address
mov     dword ptr [esp], eax    ; and put it at new TOS
ret

; Find next lower page and probe
cs20:
sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
test    dword ptr [eax],eax     ; probe page.
jmp     short cs10

}
}
}

// ------------------------------------------------------------------------------

struct VERTEX
{
    FLOAT x, y, z;
    DWORD color;
};

struct QUADVERT
{
FLOAT x, y, z;
FLOAT u1, v1;
};

#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
#define D3DFVF_QUADVERT (D3DFVF_XYZ | D3DFVF_TEX1)
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define MAX_STARS 2000
#define LAYERS 4

struct Stars
{
float x;
float y;
int speed;
};
static Stars stars[MAX_STARS];
void initStars()
{
for(int i=0; i<MAX_STARS; i++)
{
stars[i].x = (float)((rand()%SCREEN_WIDTH)-SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
int temp = ((rand()%LAYERS)+1)-(rand()%LAYERS);
stars[i].speed = temp < 1 ? ((rand()%LAYERS)+1)-(rand()%LAYERS) : temp;
}
}

void WinMainCRTStartup()
{
    HWND hWnd = ( CreateWindowEx(WS_EX_TOPMOST, "edit",0,0,0,0,0,0,0,0,0,0) );

static int d3dpp[] = {
SCREEN_WIDTH,
SCREEN_HEIGHT,
D3DFMT_X8R8G8B8,
1,
D3DMULTISAMPLE_NONE,
0,
D3DSWAPEFFECT_DISCARD,
(int)hWnd,
0,
1,
D3DFMT_D16,
0,
0,
0
};

LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    LPDIRECT3DDEVICE9 pDevice;
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                        D3DCREATE_HARDWARE_VERTEXPROCESSING,
                        (D3DPRESENT_PARAMETERS*)&d3dpp, &pDevice );

ShowCursor(FALSE);

pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDevice->SetRenderState(D3DRS_ZENABLE, FALSE);
pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

D3DXMatrixOrthoLH(&Ortho2D, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, 1.0f);
D3DXMatrixIdentity(&Identity);

pDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
pDevice->SetTransform(D3DTS_VIEW, &Identity);

srand(0);

// Create point vertices
VERTEX vert[LAYERS];

for(int i=0; i<LAYERS; i++)
{
int shade = (i+1)*(255/LAYERS);
vert[i].x = 0;
vert[i].y = 0;
vert[i].z = 0;
vert[i].color = D3DCOLOR_XRGB(shade, shade, shade);
}

LPDIRECT3DVERTEXBUFFER9 pVertBuffer = NULL;
pDevice->CreateVertexBuffer(LAYERS * sizeof(VERTEX), D3DUSAGE_WRITEONLY,
                                 D3DFVF_VERTEX, D3DPOOL_MANAGED, &pVertBuffer, NULL);
VOID* pVoid;
pVertBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vert, sizeof(vert));
    pVertBuffer->Unlock();

// Create quad vertices
QUADVERT quad[] =
{
{-(SCREEN_WIDTH/2)-1, (SCREEN_HEIGHT/2)+1, 0, 0, 0},
{(SCREEN_WIDTH/2), (SCREEN_HEIGHT/2)+1, 0, 1, 0},
{-(SCREEN_WIDTH/2)-1, -(SCREEN_HEIGHT/2), 0, 0, 1},
{(SCREEN_WIDTH/2), -(SCREEN_HEIGHT/2), 0, 1, 1},
};

LPDIRECT3DVERTEXBUFFER9 pQuadBuffer = NULL;
pDevice->CreateVertexBuffer(4*sizeof(QUADVERT), D3DUSAGE_WRITEONLY,
                                 D3DFVF_QUADVERT, D3DPOOL_MANAGED, &pQuadBuffer, NULL);
pVoid = NULL;
pQuadBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, quad, sizeof(quad));
    pQuadBuffer->Unlock();

// initialise stars
initStars();

// initialise offscreen render target
LPDIRECT3DTEXTURE9 pTex = NULL;
D3DXCreateTexture(pDevice, SCREEN_WIDTH, SCREEN_HEIGHT, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTex);
LPDIRECT3DSURFACE9 pSurf = NULL, pBackBuffer = NULL;
pTex->GetSurfaceLevel(0, &pSurf);
pDevice->GetRenderTarget(0, &pBackBuffer);
D3DXMATRIX matTranslate;

// Initialise shader effect
LPD3DXEFFECT pBlurFX = NULL;
LPD3DXBUFFER pErrors = NULL;
D3DXCreateEffectFromFile(pDevice, "shader.fx", 0, 0, 0, 0, &pBlurFX, &pErrors);
pBlurFX->SetTechnique("Blur");

// Start render loop
while (!GetAsyncKeyState(VK_ESCAPE))
{
// Render geometry to offscreen surface
pDevice->BeginScene();
pDevice->SetRenderTarget(0, pSurf);
pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pDevice->SetFVF(D3DFVF_VERTEX);
pDevice->SetStreamSource(0, pVertBuffer, 0, sizeof(VERTEX));

for(int i=0; i<MAX_STARS; i++)
{
D3DXMatrixTranslation(&matTranslate,stars[i].x,stars[i].y,0.0f);
pDevice->SetTransform(D3DTS_WORLD, &matTranslate);

stars[i].x -= stars[i].speed;
if(stars[i].x < (float)-SCREEN_WIDTH/2)
{
stars[i].x = (float)(SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
}
pDevice->DrawPrimitive(D3DPT_POINTLIST, (stars[i].speed)-1, 1);
}

// Render fullscreen quad with effect
pDevice->SetRenderTarget(0, pBackBuffer);
pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(100, 100, 100), 1.0f, 0);
pDevice->SetFVF(D3DFVF_QUADVERT);
pDevice->SetStreamSource(0, pQuadBuffer, 0, sizeof(QUADVERT));
D3DXMATRIX wvp = Identity * Identity * Ortho2D;
pBlurFX->SetMatrix("matWVP", &wvp);
pBlurFX->SetTexture("StarTex", pTex);
UINT numPasses = 0;
pBlurFX->Begin(&numPasses, 0);
for(UINT i=0; i < numPasses; i++)
{
pBlurFX->BeginPass(i);
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
pBlurFX->EndPass();
}
pBlurFX->End();
pDevice->EndScene();

pDevice->Present( NULL, NULL, NULL, NULL );
}

ExitProcess(0);
}

It's quite subtle to notice so I've attached two versions of the program, one using the shader, one not.

5
Yes, you were right, my bad :P, it was right for OGL though.

I think the problem that I see here was due to quad size, not sure, I changed your quad setting to this code and it looks ok here.

Code: [Select]
// Create quad vertices
QUADVERT quad[4] =
{
{-((SCREEN_WIDTH-1)/2), ((SCREEN_HEIGHT-1)/2),  0.0f,  0.0f, 0.0f},
{((SCREEN_WIDTH-1)/2), ((SCREEN_HEIGHT-1)/2),   0.0f,  1.0f, 0.0f},
{-((SCREEN_WIDTH-1)/2), -((SCREEN_HEIGHT-1)/2), 0.0f,  0.0f, 1.0f},
{((SCREEN_WIDTH-1)/2), -((SCREEN_HEIGHT-1)/2),  0.0f,  1.0f, 1.0f},
};

You're right, I had wondered about the dimensions of the quad so I started clearing the back buffer to gray to show up any gaps.

Now I'm working on a motion blurring shader which I think is almost done (but probably not), will post it when it works. :)

6
But I thought..  in D3D texture coordinates 0,0 is the top left..?  ???

Edit:  just to put my mind at rest, I changed my texture coordinates to yours and altered the code so that it would draw all stars with a y coordinate of 100.  Assuming the texture is drawn the right way up, that would draw a line of stars closer to the top of screen, but when I ran it, it was closer to the bottom...

7
You need to make your texture coordinates in the same sequence of your triangle strip.

In words that a simpleton can understand?  :-[

8
Fixed!  :updance:

My hunch was right - the only thing set when rendering the quad that wasn't reset when rendering the starfield in the next frame was the texture.  I didn't think that would matter, but adding...

Code: [Select]
pDevice->SetTexture(0,0);
.. before drawing the stars fixed it.

Why direct3d wants to draw a texture on a point primitive with no texture coordinates remains unclear...  ??? ::)

Now on to my first pixel shader!  ;D

9
@rbz: Thanks, I fixed 1 & 2.  :)

Are you sure the texture coordinates are wrong?  It's definitely displaying the texture across the whole quad.  I still have the problem that it's not scrolling though, and the stars are disappearing.

Edit: Well, I discovered the problem but no idea what the cause is... the starfield is rendered once correctly as I saw, but after the first frame it does in fact render the stars with their updated positions each frame.  The effect I observed where the stars gradually disappeared is apparently because I wasn't clearing the texture each frame, and having drawn the stars correctly on the first frame, it then draws them black!  I have no idea why it would do that, I assume something changed after the first pass through the render-to-texture section of the render loop causes it but as far as I can see everything should be reset correctly.  ???

Here's the render loop I have now:

Code: [Select]
// Start render loop
while (!GetAsyncKeyState(VK_ESCAPE))
{

// Render geometry to offscreen surface
pDevice->BeginScene();
pDevice->SetRenderTarget(0, pSurf);
//pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

pDevice->SetFVF(D3DFVF_VERTEX);
pDevice->SetStreamSource(0, pVertBuffer, 0, sizeof(VERTEX));

for(int i=0; i<MAX_STARS; i++)
{
D3DXMatrixTranslation(&matTranslate,stars[i].x,stars[i].y,0.0f);
pDevice->SetTransform(D3DTS_WORLD, &matTranslate);

stars[i].x += stars[i].speed;
if(stars[i].x > (float)SCREEN_WIDTH/2)
{
stars[i].x = (float)(-SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
}
pDevice->DrawPrimitive(D3DPT_POINTLIST, (stars[i].speed)-1, 1);
}

// Render fullscreen quad
pDevice->SetRenderTarget(0, pBackBuffer);
pDevice->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pDevice->SetFVF(D3DFVF_QUADVERT);
pDevice->SetStreamSource(0, pQuadBuffer, 0, sizeof(QUADVERT));
pDevice->SetTransform(D3DTS_WORLD, &Identity);
pDevice->SetTexture(0,pTex);
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);
pDevice->EndScene();

pDevice->Present( NULL, NULL, NULL, NULL );
}

Uncommenting the first Clear() will result in a black screen, you can alter the colour it clears to in order to see the stars being drawn in black.

10
Having some trouble rendering to a texture and getting it to display correctly.  The starfield is displayed so I assume the quad is correctly positioned etc, however it doesn't scroll - the stars simply start disappering and don't reappear.

I'm tired so I have probably done something silly.  ???

Code: [Select]
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN

#include <d3d9.h>
#include <d3dx9.h>

// lib replacement stuff --------------------------------------------------------
// Thanks to Jim for most of this :)

static unsigned int next = 1;

__forceinline void MEMSET( void *_dst, int _val, size_t _sz )
{
   while ( _sz ) ((BYTE *)_dst)[--_sz] = _val;
}

int rand(void)
{
next = next * 1103515245 + 12345;
return (int)((next/65536) % 32768);
}

void srand(unsigned int seed)
{
next = seed;
}

extern "C"
{
int _fltused;

void __declspec(naked) _chkstk(void)
{
#define _PAGESIZE_ 1000h

__asm
{
push    ecx

; Calculate new TOS.

lea     ecx, [esp] + 8 - 4      ; TOS before entering function + size for ret value
sub     ecx, eax                ; new TOS

; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.

sbb     eax, eax                ; 0 if CF==0, ~0 if CF==1
not     eax                     ; ~0 if TOS did not wrapped around, 0 otherwise
and     ecx, eax                ; set to 0 if wraparound

mov     eax, esp                ; current TOS
and     eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary

cs10:
cmp     ecx, eax                ; Is new TOS
jb      short cs20              ; in probed page?
mov     eax, ecx                ; yes.
pop     ecx
xchg    esp, eax                ; update esp
mov     eax, dword ptr [eax]    ; get return address
mov     dword ptr [esp], eax    ; and put it at new TOS
ret

; Find next lower page and probe
cs20:
sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
test    dword ptr [eax],eax     ; probe page.
jmp     short cs10

}
}
}

// ------------------------------------------------------------------------------

struct VERTEX
{
    FLOAT x, y, z;
    DWORD color;
};

struct QUADVERT
{
FLOAT x, y, z;
FLOAT u1, v1;
};

#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
#define D3DFVF_QUADVERT (D3DFVF_XYZ | D3DFVF_TEX1)
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define MAX_STARS 2000
#define LAYERS 5

struct Stars
{
float x;
float y;
int speed;
};
static Stars stars[MAX_STARS];
void initStars()
{
for(int i=0; i<MAX_STARS; i++)
{
stars[i].x = (float)((rand()%SCREEN_WIDTH)-SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
stars[i].speed = (rand()%LAYERS)+1;
}
}

void WinMainCRTStartup()
{
    HWND hWnd = ( CreateWindowEx(WS_EX_TOPMOST, "edit",0,0,0,0,0,0,0,0,0,0) );

static int d3dpp[] = {
SCREEN_WIDTH,
SCREEN_HEIGHT,
D3DFMT_X8R8G8B8,
1,
D3DMULTISAMPLE_2_SAMPLES,
0,
D3DSWAPEFFECT_DISCARD,
(int)hWnd,
0,
1,
D3DFMT_D16,
0,
0,
0
};

LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    LPDIRECT3DDEVICE9 pDevice;
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                        D3DCREATE_HARDWARE_VERTEXPROCESSING,
                        (D3DPRESENT_PARAMETERS*)&d3dpp, &pDevice );

ShowCursor(FALSE);

pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);

D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

D3DXMatrixOrthoLH(&Ortho2D, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, 1.0f);
D3DXMatrixIdentity(&Identity);

pDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
pDevice->SetTransform(D3DTS_VIEW, &Identity);

srand(0);

// Create point vertices
VERTEX vert[LAYERS];

for(int i=0; i<LAYERS; i++)
{
int shade = (i+1)*(255/LAYERS);
vert[i].x = 0;
vert[i].y = 0;
vert[i].z = 0;
vert[i].color = D3DCOLOR_XRGB(shade, shade, shade);
}

LPDIRECT3DVERTEXBUFFER9 pVertBuffer = NULL;
pDevice->CreateVertexBuffer(LAYERS * sizeof(VERTEX), D3DUSAGE_WRITEONLY,
                                 D3DFVF_VERTEX, D3DPOOL_MANAGED, &pVertBuffer, NULL);
VOID* pVoid;
pVertBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vert, sizeof(vert));
    pVertBuffer->Unlock();

// Create quad vertices
QUADVERT quad[4] =
{
{-(SCREEN_WIDTH/2), (SCREEN_HEIGHT/2), 0, 0, 0},
{(SCREEN_WIDTH/2), (SCREEN_HEIGHT/2), 0, 1, 0},
{-(SCREEN_WIDTH/2), -(SCREEN_HEIGHT/2), 0, 0, 1},
{(SCREEN_WIDTH/2), -(SCREEN_HEIGHT/2), 0, 1, 1},
};

LPDIRECT3DVERTEXBUFFER9 pQuadBuffer = NULL;
pDevice->CreateVertexBuffer(4*sizeof(QUADVERT), D3DUSAGE_WRITEONLY,
                                 D3DFVF_QUADVERT, D3DPOOL_MANAGED, &pQuadBuffer, NULL);
pVoid = NULL;
pQuadBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, quad, sizeof(quad));
    pQuadBuffer->Unlock();

// initialise stars
initStars();

// initialise offscreen render target
LPDIRECT3DTEXTURE9 pTex = NULL;
D3DXCreateTexture(pDevice, SCREEN_WIDTH, SCREEN_HEIGHT, 1, D3DUSAGE_RENDERTARGET, D3DFMT_X8R8G8B8, D3DPOOL_DEFAULT, &pTex);
LPDIRECT3DSURFACE9 pSurf = NULL, pBackBuffer = NULL;
pTex->GetSurfaceLevel(0, &pSurf);

// Start render loop
while (!GetAsyncKeyState(VK_ESCAPE))
{

// Render geometry to offscreen surface
pDevice->GetRenderTarget(0, &pBackBuffer);
pDevice->SetRenderTarget(0, pSurf);

pDevice->BeginScene();

pDevice->SetFVF(D3DFVF_VERTEX);
pDevice->SetStreamSource(0, pVertBuffer, 0, sizeof(VERTEX));
D3DXMATRIX matTranslate;

for(int i=0; i<MAX_STARS; i++)
{
D3DXMatrixTranslation(&matTranslate,stars[i].x,stars[i].y,0.0f);
pDevice->SetTransform(D3DTS_WORLD, &matTranslate);

stars[i].x += stars[i].speed;
if(stars[i].x > (float)SCREEN_WIDTH/2)
{
stars[i].x = (float)(-SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
}
pDevice->DrawPrimitive(D3DPT_POINTLIST, (stars[i].speed)-1, 1);
}

pDevice->EndScene();

// Render fullscreen quad
pDevice->SetRenderTarget(0, pBackBuffer);
pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pDevice->SetFVF(D3DFVF_QUADVERT);
pDevice->SetStreamSource(0, pQuadBuffer, 0, sizeof(QUADVERT));
pDevice->SetTransform(D3DTS_WORLD, &Identity);

pDevice->BeginScene();

pDevice->SetTexture(0,pTex);
pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2);

pDevice->EndScene();

pDevice->Present( NULL, NULL, NULL, NULL );
}

ExitProcess(0);
}

11
Projects / Re: Supplex - Dynasty Wars 100%
« on: August 03, 2008 »

The text that is written under the main header appears as gibberish here (winxp).  Otherwise fine, looking and sounding good.  :)
Do you mean the % (percent) symbol ? If yes, it was done exactly like I see it on emulator, anyway that font does not have this symbol, I guess :)

Nope, I mean like this:

12
Projects / Re: Supplex - Dynasty Wars 100%
« on: August 03, 2008 »
The text that is written under the main header appears as gibberish here (winxp).  Otherwise fine, looking and sounding good.  :)

13
Challenges & Competitions / Re: [REDUCED RES] Xalboy
« on: August 03, 2008 »
Just awesome.  :goodpost:

14
Challenges & Competitions / Re: [REDUCED RES] PL4SMA
« on: August 03, 2008 »
I really think that if you post something with rapid flickering you should say so in your post, this is a real issue for some people...

However, I liked it for as long as I was able to watch it.  :)

15
I would like to see this framework in any case, I for one have no idea how to do dithering for instance and would love an example.

If I made an entry it'd be in c++ anyway, so I'd be rewriting any code if it's in freebasic.  And since I have no expectation of winning, being disqualified is not as much of an issue as learning how to do it in the first place.  ;D

Also, I liked this entry a lot, it ran no problem on XP here.  :clap:

16
Well, I really want to join in with one of these compos, but this one looks a bit beyond my ability..  :-[

17
General chat / Re: Speaker seeker
« on: July 31, 2008 »
I'd have a look online before buying components retail, places like Maplins and PC World are pretty overpriced by comparison.  I can personally recommend the following sites:

www.overclockers.co.uk
www.microdirect.co.uk
www.komplett.co.uk

Helpful if you have someone experienced when you're choosing parts as some items might be listed as OEM (original equipment manufacturer) meaning that they won't come in nice retail boxes and might not have things like cables, manuals etc.  OEM parts are usually quite a bit cheaper though so if you know what you're doing you can save some cash - however even retail items will be cheaper online.  You also need to make sure all your bits will work together, although those sites have great support - email your shopping list to tech support at Overclockers and they'll tell you if it's all ok and make suggestions for improvements.

18
There are a few leaks to watch out for that I've come across:

Code: [Select]
Cat* kitty1 = new Cat();
Cat* kitty2 = new Cat();
kitty1 = kitty2;

The instance of Cat originally pointed to by kitty1 no longer has a pointer and therefore can't be freed.

Code: [Select]
void oops()
{
    Cat* kitty = new Cat();
}

Pointer kitty is local to function oops(), if it goes out of scope (without being returned from the function or otherwise passed on) then the instance of Cat no longer has a pointer and therefore can't be freed.

Code: [Select]
Cat* kitty = new Cat("Molly");
kitty = new Cat("Betty");
delete kitty;

First we create a cat called Molly, then we want kitty to point to a new cat called Betty. Finally we delete kitty.  Betty is history, but we reassigned the only pointer to Molly, so that instance can't be freed.

I'm sure there are lots more but that's all I know (I'm also a noob)  ;D

Oh, and it's worth remembering that you don't HAVE to instantiate classes on the heap.

19
I've been looking into pixel shaders and there seems to be very little info on them that's freely available.  :-\

If I understand it right, a pixel shader acts on all pixels contained within the primitive currently being drawn.  If that's the case, how do you create a pixel shader that acts on the full screen?  Do you need to render to a texture then draw that (using the shader) as a sprite or on a quad?

20
Well, I think I have the colors/perspective thing sorted, though I'm not sure how good my solution is.  I wanted to be able to change the number of layers so I put that into a #define, all the calculations are based off that, and I think they are right.

But let me know what you think.  ;D

Code: [Select]
#define VC_EXTRALEAN
#define WIN32_LEAN_AND_MEAN

#include <d3d9.h>
#include <d3dx9.h>

// lib replacement stuff --------------------------------------------------------
// Thanks to Jim for most of this :)

static unsigned int next = 1;

__forceinline void MEMSET( void *_dst, int _val, size_t _sz )
{
   while ( _sz ) ((BYTE *)_dst)[--_sz] = _val;
}

int rand(void)
{
next = next * 1103515245 + 12345;
return (int)((next/65536) % 32768);
}

void srand(unsigned int seed)
{
next = seed;
}

extern "C"
{
int _fltused;

void __declspec(naked) _chkstk(void)
{
#define _PAGESIZE_ 1000h

__asm
{
push    ecx

; Calculate new TOS.

lea     ecx, [esp] + 8 - 4      ; TOS before entering function + size for ret value
sub     ecx, eax                ; new TOS

; Handle allocation size that results in wraparound.
; Wraparound will result in StackOverflow exception.

sbb     eax, eax                ; 0 if CF==0, ~0 if CF==1
not     eax                     ; ~0 if TOS did not wrapped around, 0 otherwise
and     ecx, eax                ; set to 0 if wraparound

mov     eax, esp                ; current TOS
and     eax, not ( _PAGESIZE_ - 1) ; Round down to current page boundary

cs10:
cmp     ecx, eax                ; Is new TOS
jb      short cs20              ; in probed page?
mov     eax, ecx                ; yes.
pop     ecx
xchg    esp, eax                ; update esp
mov     eax, dword ptr [eax]    ; get return address
mov     dword ptr [esp], eax    ; and put it at new TOS
ret

; Find next lower page and probe
cs20:
sub     eax, _PAGESIZE_         ; decrease by PAGESIZE
test    dword ptr [eax],eax     ; probe page.
jmp     short cs10

}
}
}

// ------------------------------------------------------------------------------

#define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)
#define SCREEN_WIDTH 1024
#define SCREEN_HEIGHT 768
#define MAX_STARS 2000
#define LAYERS 4

struct VERTEX
{
    FLOAT x, y, z;
    DWORD color;
};

struct Stars
{
float x;
float y;
int speed;
};

static Stars stars[MAX_STARS];

void initStars()
{
for(int i=0; i<MAX_STARS; i++)
{
stars[i].x = (float)((rand()%SCREEN_WIDTH)-SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
stars[i].speed = (rand()%LAYERS)+1;
}
}

void WinMainCRTStartup()
{
    HWND hWnd = ( CreateWindowEx(WS_EX_TOPMOST, "edit",0,0,0,0,0,0,0,0,0,0) );

static int d3dpp[] = {
SCREEN_WIDTH,
SCREEN_HEIGHT,
D3DFMT_X8R8G8B8,
1,
D3DMULTISAMPLE_2_SAMPLES,
0,
D3DSWAPEFFECT_DISCARD,
(int)hWnd,
0,
1,
D3DFMT_D16,
0,
0,
0
};

LPDIRECT3D9 pD3D = Direct3DCreate9( D3D_SDK_VERSION );
    LPDIRECT3DDEVICE9 pDevice;
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                        D3DCREATE_HARDWARE_VERTEXPROCESSING,
                        (D3DPRESENT_PARAMETERS*)&d3dpp, &pDevice );

ShowCursor(FALSE);

pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
pDevice->SetRenderState(D3DRS_ZENABLE, TRUE);

D3DXMATRIX Ortho2D;
D3DXMATRIX Identity;

D3DXMatrixOrthoLH(&Ortho2D, SCREEN_WIDTH, SCREEN_HEIGHT, 0.0f, 1.0f);
D3DXMatrixIdentity(&Identity);

pDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
pDevice->SetTransform(D3DTS_WORLD, &Identity);
pDevice->SetTransform(D3DTS_VIEW, &Identity);

srand(0);

// Create point vertex
VERTEX vert[LAYERS];

for(int i=0; i<LAYERS; i++)
{
int shade = (i+1)*(255/LAYERS);
vert[i].x = 0;
vert[i].y = 0;
vert[i].z = 0;
vert[i].color = D3DCOLOR_XRGB(shade, shade, shade);
}

LPDIRECT3DVERTEXBUFFER9 pVertBuffer = NULL;
pDevice->CreateVertexBuffer(LAYERS * sizeof(VERTEX), D3DUSAGE_WRITEONLY,
                                 D3DFVF_VERTEX, D3DPOOL_MANAGED, &pVertBuffer, NULL);
VOID* pVoid;
pVertBuffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vert, sizeof(vert));
    pVertBuffer->Unlock();

initStars();

// Start render loop
while (!GetAsyncKeyState(VK_ESCAPE))
{
pDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
pDevice->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

pDevice->BeginScene();

pDevice->SetFVF(D3DFVF_VERTEX);
pDevice->SetStreamSource(0, pVertBuffer, 0, sizeof(VERTEX));
pDevice->SetTransform(D3DTS_WORLD, &Identity);
D3DXMATRIX matTranslate;

for(int i=0; i<MAX_STARS; i++)
{
D3DXMatrixTranslation(&matTranslate,stars[i].x,stars[i].y,0.0f);
pDevice->SetTransform(D3DTS_WORLD, &matTranslate);

stars[i].x += stars[i].speed;
if(stars[i].x > (float)SCREEN_WIDTH/2)
{
stars[i].x = (float)(-SCREEN_WIDTH/2);
stars[i].y = (float)((rand()%SCREEN_HEIGHT)-SCREEN_HEIGHT/2);
}
pDevice->DrawPrimitive(D3DPT_POINTLIST, (stars[i].speed)-1, 1);
}

pDevice->EndScene();

pDevice->Present( NULL, NULL, NULL, NULL );
}

ExitProcess(0);
}

I still need to look at timers, I also plan to change the points to point sprites and (hopefully) add a pixel shader for some blurring.

Pages: [1] 2 3