Photos Blog Random Things Links Guest Book Resume

AndyC.org : #Lecture

D3D8 - Intro to Vertex and Index Buffers


Session Start: Wed Jan 08 00:44:06 2003
[00:44] DCC Chat connection established
-
[00:44] <Error404> Sorry, I was reading logs.
[00:44] <RedBeard> gimme a min
[00:45] <Error404> ok.
[00:45] <RedBeard> i recommend you go through the code and find every single Direct3D function call, and look it up in the d3d docs to figure out what all the parameters are for
[00:46] <Error404> Ok.
[00:47] <Error404> msdn or just the ones that came with d3d8?
[00:47] <Error404> I have the ones open that came with the SDK.
[00:47] <Error404> They are usually open whenever i attempt to code dx
[00:47] <RedBeard> the d3d docs that come with the sdk
[00:48] <Error404> completely start over my d3d class?
[00:48] <RedBeard> not yet
[00:48] <RedBeard> lemme modify your initd3d function
[00:48] <RedBeard> uh, initvb
[00:49] <Error404> Ok.
[00:50] <RedBeard> bool cDXanadu::InitVB()
[00:50] <RedBeard> {
[00:50] <RedBeard> StartLog("cDXanadu::InitVB");loc;
[00:50] <RedBeard> if(FAILED( pd3dDevice->CreateVertexBuffer(50*3*sizeof(CUSTOMVERTEX), 0,
[00:50] <RedBeard> D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,
[00:50] <RedBeard> &vb)))
[00:50] <RedBeard> return false;
[00:50] <RedBeard> CUSTOMVERTEX * pVertices;
[00:50] <RedBeard> if(FAILED( vb->Lock(0, 0,(BYTE**)&pVertices,D3DLOCK_DISCARD)))
[00:50] <RedBeard> return false;
[00:50] <RedBeard> DWORD i;
[00:50] <RedBeard> FLOAT theta, theta2;
[00:50] <RedBeard> for(i=0;i<50;i+=2)
[00:50] <RedBeard> {
[00:50] <RedBeard> theta = (2*D3DX_PI*i)/(50-1);
[00:50] <RedBeard> theta2= (2*D3DX_PI*(i+1))/(50-1);
[00:50] <RedBeard> pVertices[3*i+0].position = D3DXVECTOR3 (sinf(theta), -1.0f,cosf(theta));
[00:50] <RedBeard> pVertices[3*i+1].position = D3DXVECTOR3 (sinf(theta), 1.0f,cosf(theta));
[00:50] <RedBeard> pVertices[3*i+2].position = D3DXVECTOR3 (sinf(theta2),-1.0f,cosf(theta2));
[00:50] <RedBeard> pVertices[3*i+3].position = D3DXVECTOR3 (sinf(theta), 1.0f,cosf(theta));
[00:50] <RedBeard> pVertices[3*i+4].position = D3DXVECTOR3 (sinf(theta2), 1.0f,cosf(theta2));
[00:50] <RedBeard> pVertices[3*i+5].position = D3DXVECTOR3 (sinf(theta2),-1.0f,cosf(theta2));
[00:50] <RedBeard> }
[00:50] <RedBeard> vb->Unlock();
[00:50] <RedBeard> return true;
[00:50] <RedBeard> }
[00:51] <RedBeard> no idea if that will work properly
[00:52] <RedBeard> i can't compile the app here, dont have certain headers
[00:52] <RedBeard> then in DoFrame, use this line in place of what's there
[00:52] <RedBeard> pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST,0,2*50-2);
[00:54] <Error404> Err
[00:54] <Error404> Well.. it animated
[00:54] <Error404> but the cylinder is weird
[00:54] <Error404> and faces are everywhere
[00:54] <RedBeard> hrmm
[00:55] <Error404> Do I have to know the equation to design a cynlinder in a for loop to be a good dx programmer?
[00:55] <Error404> cylinder.
[00:55] <RedBeard> heh not really
[00:55] <RedBeard> but you need to know how the geometry is arranged
[00:56] <RedBeard> so that it makes a surface
[00:56] <RedBeard> i'd like to get you started on an index buffer method, but it would be a bit complicated to add that on-the-fly
[00:56] <Error404> I can rewrite the entire direct x class quick like
[00:56] <Error404> I've rewritten it over and over for the past week now.
[00:57] <RedBeard> heh hold on a sec
[00:57] <RedBeard> give it an IDirect3DIndexBuffer8 * ib
[00:57] <Error404> The current or new class?
[00:58] <RedBeard> whichever you're working with
[00:58] <RedBeard> mebbe start over with a sample app that works
[00:58] <RedBeard> and modify that
[00:58] <RedBeard> or sample code
[00:58] <Error404> Ok let me go back a few tutorials.
[00:58] <RedBeard> wait, lemme see if i can compile this stuff
[00:59] <Error404> I went back to the first file set I sent you.
[00:59] <Error404> It was working, it just draws a triangle with diff vertex shades.
[01:00] <RedBeard> mm ok
[01:00] <RedBeard> send me those again?
[01:00] <RedBeard> and i'll try to help you expand it
[01:01] <Error404> Ok.
[01:01] <Error404> If its possible can we open a DCC with the other computer, it would allow me to chat and code at the same time.
[01:01] <Error404> Actually nevermind.
[01:02] <Error404> This works.
[01:02] <Error404> Helps for copynpaste

-- switch from one DCC to another --

[01:02] <Error405> thanks
[01:03] <Error405> In the clean up I can just do if(!vb) right?
[01:03] <Error405> Since NULL is 0.
[01:04] <RedBeard> yes
[01:04] <RedBeard> it's technically correct, although some people like you to explicitly compare against NULL
[01:04] <RedBeard> i say don't worry about that unless it's not clear that you're using a pointer
[01:05] <Error405> Yeah. that makes since.
[01:05] <Error405> sense
[01:05] <RedBeard> using NULL makes it more readable when using pointers
[01:05] <Error405> So people aren't guessing what i'm doing. No 'clever code'.
[01:06] <RedBeard> right
[01:06] <RedBeard> don't try to outwit the compiler, it knows what it's doing
[01:06] <RedBeard> oh and another thing, don't EVER use the incrementer operator inside other code
[01:06] <RedBeard> it causes much pain when used as such
[01:06] <Error405> I try to force good coding practice on myself so I wont write bad code.
[01:06] <Error405> inside other code?
[01:06] <RedBeard> array[i++] = 12;
[01:07] <Error405> Oh.
[01:07] <RedBeard> instead, do: array[i] = 12; i++;
[01:07] <Error405> ah i see.
[01:07] <RedBeard> it's clearer and less bug-prone
[01:09] <RedBeard> alright, so what are ya gonna do?
[01:09] <Error405> Well if you think it'd be easier to restart, I can do that and remake the class.
[01:09] <Error405> Or I can use the class I gave you, which is really simple I think.
[01:10] <RedBeard> nah it's not worth tearing it down
[01:11] <RedBeard> IDirect3DIndexBuffer8 *ib; <== add that
[01:11] <Error405> got that
[01:11] <RedBeard> now you gotta create and initialize the vb to be used as being indexed
[01:11] <RedBeard> with a single triangle, that's no different
[01:11] <RedBeard> so now, create and init the index buffer
[01:12] <RedBeard> CreateIndexBuffer(...)
[01:12] <Error405> alright.
[01:12] <Error405> ok.. umm.
[01:12] <RedBeard> 3 indices, D3DUSAGE_WRITEONLY
[01:12] <Error405> Do I change the &vb to ib>?
[01:13] <RedBeard> you need a vertex buffer AND an index buffer, so you're making both still, right?
[01:13] <Error405> oh i do?
[01:13] <RedBeard> so in calls dealing with the index buffer, use "ib"
[01:13] <Error405> ah ok
[01:13] <RedBeard> yes, the index buffer just stores integer (short, 16-bit) indices
[01:14] <RedBeard> and the vertices are still stored in the vertex buffer
[01:14] <RedBeard> D3DFMT_INDEX16
[01:14] <RedBeard> default pool
[01:15] <Error405> ok.. ah please slow.. lol.. For the createindexbuffer do I keep it the same as it was just change the second variable to D3DUSAGE_WRITEONLY correct.. Ok I got that.
[01:15] <RedBeard> look it up in teh docs to see what im talking about with these values
[01:16] <Error405> yeah that is where i found out where to put writeonly
[01:16] <Error405> lol
[01:16] <Error405> Ok and I'm keeping the current vertice setup the same.
[01:16] <Error405> with the triangle.
[01:16] <Error405> So I still keep the 3*sizeof(customvertex)
[01:17] <RedBeard> right
[01:17] <Error405> Ok, now I leave the rest alone and set up the indexbuffer?
[01:17] <RedBeard> put in another thing there, make a new member variable num_tris
[01:17] <Error405> so all of this is happening in InitVB?
[01:17] <RedBeard> and make it num_tris*3*sizeof(customvertex)
[01:17] <RedBeard> yes
[01:18] <RedBeard> oh, and for the size of the index buffer...
[01:18] <Error405> just leave num_tris 1 for now?
[01:18] <RedBeard> sizeof(short)*num_tris
[01:18] <RedBeard> yes
[01:18] <RedBeard> actually, make another member var, num_verts
[01:19] <Error405> 3?
[01:19] <RedBeard> and the index buffer will deal with num_tris, and the vertex buffer will deal with num_verts
[01:19] <RedBeard> yes
[01:19] <RedBeard> remember, member var, not local var
[01:19] <Error405> Oh member vart
[01:19] <Error405> var
[01:19] <Error405> lemme move them
[01:20] <Error405> ok for the vertex buffer it isn't num_tris*3*sizeof.. it's num_verts
[01:20] <RedBeard> yes
[01:20] <RedBeard> and take out the 3
[01:20] <Error405> Ok when I call CreateIndexBuffer I should do it before or after the Lock?
[01:21] <RedBeard> the index and vertex buffers are completely independent
[01:21] <RedBeard> you have to create it before you lock it, but you can create and lock one, then create and lock the other
[01:21] <RedBeard> doesnt matter
[01:21] <Error405> alright
[01:22] <Error405> ok index buffer is..err.. damn... num_tris*3*sizeof(customvertex)?
[01:22] <Error405> oh you rtold me that
[01:22] <Error405> sorry
[01:23] <RedBeard> sizeof(short)*num_tris
[01:23] <Error405> oo short
[01:23] <Error405> .. why a short?
[01:23] <RedBeard> because you're using 16-bit indices
[01:23] <RedBeard> they're smaller, and you're using less than 65,536 vertices
[01:24] <RedBeard> also, your hardware probably doesn' support 32-bit indices
[01:24] <Error405> ok i understand that, but hmm. It's 1 triangle. I don't need 3 anywhere?
[01:24] <RedBeard> my gf2 doesnt
[01:24] <RedBeard> oh wait yeah my bad
[01:24] <RedBeard> sizeof(short)*num_tris*3
[01:24] <Error405> ah ok
[01:24] <RedBeard> :P
[01:25] <RedBeard> so, that's created. now to lock it and fill values in
[01:25] <Error405> ok the index buffer's usage, it isn't writeonly right?
[01:25] <RedBeard> yes, it is
[01:25] <RedBeard> you don't need to read values back out of it
[01:25] <Error405> oh.. ok
[01:25] <RedBeard> if you ever think you'll need to re-read the values in a vertex or index buffer, or texture, or anythng, just keep a copy of it around in your code
[01:25] <RedBeard> don't read stuff back through the API
[01:26] <Error405> it's slow right?
[01:26] <RedBeard> very
[01:27] <RedBeard> we can take this back to #silicon3d
[01:27] <RedBeard> since i'm not pasting stuff anymore
[01:27] <Error405> ah ok

-- switch to #silicon3d --

[01:34] <Error405> aight
[01:34] <RedBeard> ok, so now you have the index buffer, lock it
[01:35] <RedBeard> and then treat the locked data as an array of unsigned shorts
[01:35] <Error405> i'm trying to figure out what to memcpy to it.. one moment.
[01:35] <RedBeard> uint16 might work
[01:35] <RedBeard> and fill in 1,2,3
[01:35] <RedBeard> no wait... 0,1,2
[01:35] <Error405> wait
[01:35] <Error405> i don't have to memcpy
[01:35] <Error405> ok
[01:36] <RedBeard> you can do the same thing with the vertex buffer if you want, casting the pointer to another CUSTOMVERTEX pointer
[01:36] <RedBeard> and treating it as an array
[01:36] <RedBeard> but the initializer thingy is easy to do
[01:36] <RedBeard> so keep it as-is
[01:38] <Error405> ok. sorry but I'm having trouble figuring out what to pass in for the size of the index buffer to lock
[01:38] <Error405> would it still be g_vertices?
[01:39] * Quits: Exellon (guest123@68.112.207.33) (Today is a good day to chat.)
[01:40] <RedBeard> passing 0 locks the whole thing
[01:40] <Error405> so i do want to lock the entire thing
[01:41] <Error405> do I want to do the same with the vertexbuffer since it's already the correct size.
[01:41] <RedBeard> but you wanna lock the full size, so you can either use 0 or sizeof(short)*3*num_tris
[01:44] <Error405> ok so i'm completely guestimating this, but i'm hoping i'm right, do I memcpy(pVertices,g_Vertices,sizeof(g_Vertices))?
[01:44] <Error405> I have a feeling I need to do something else
[01:44] <Error405> something dealing with uint16 like you said
[01:45] <Error405> damnit I wish I knew exactly what the hell I was doing, I hate being clueless.
[01:45] <RedBeard> i recommend you use sizeof(CUSTOMVERTEX)*num_verts
[01:45] <Error405> do I still use g_Vertices?
[01:45] <RedBeard> that's for the vertex buffer
[01:46] <RedBeard> for the index buffer, makea uint16 pointer
[01:46] <Error405> ok...
[01:46] <Error405> unsigned short?
[01:46] <Error405> or uint16 (is confused, thought they were the same)
[01:47] <RedBeard> same thing
[01:48] <Error405> holy flippin god i'm confused now.
[01:48] <Error405> Ok i have this pointer to an ushort
[01:48] <RedBeard> unsigned short * pIndices;
[01:48] <Error405> yeah made that
[01:49] * Quits: masterg (~asd24fid@ip68-8-22-175.sd.sd.cox.net) (night)
[01:50] <RedBeard> if(FAILED( ib->Lock(0, sizeof(short)*3*num_tris, (BYTE**)&pIndices,0)))
[01:50] <RedBeard>         return false;
[01:50] * Joins: Washu[AWAY] (direwolf@adsl-64-161-27-214.dsl.sntc01.pacbell.net)
[01:50] <X2> [Washu[AWAY]] <nps> masterg: what are you doing this weekend? <A*sassin> HOT SEX ON HIS PARENTS' BED! <A*sassin> uh, i mean... nothing <Masterg> omg, shhh!
[01:50] * X2 sets mode: +v Washu[AWAY]
[01:50] <RedBeard> that's your lock call
[01:50] <Error405> yeah I figured that out
[01:50] <Error405> after much thought and hair pulling.
[01:50] <RedBeard> now you can go... pIndices[0] = 0
[01:50] <RedBeard> and same for 1 and 2
[01:50] <Error405> whoa
[01:51] <Error405> That's different.
[01:51] <RedBeard> well, i wouldnt be teaching you anything if there wasnt thought involved :P
[01:51] <Error404> pIndices[0] = 0;pIndices[1] = 1;pIndices[2] = 2;
[01:51] <RedBeard> yes, you can treat the memory as an array
[01:52] <Error404> ?
[01:52] <RedBeard> pretty neat huh
[01:52] <Error404> yes.
[01:52] <Error404> This entire time I've been trying to figure out what the hell i'm memcpy'in
[01:52] <RedBeard> that's essentially what a vertex and index buffer store
[01:52] <RedBeard> an array of vertices or indices
[01:52] <RedBeard> and if you treat it in the correct manner, you can use it as such
[01:53] <Error404> awesome... now is that the line after ->lock?
[01:53] <RedBeard> yeah
[01:53] <RedBeard> and then you can unlock
[01:53] <RedBeard> i think that's all for that function
[01:53] <Error404> yay!
[01:53] <Error404> Ok wait.
[01:54] <Error404> I need to make sure I didn't write anything that will format my computer (j)
[01:54] <Error404> if(FAILED( pd3dDevice->CreateIndexBuffer(sizeof(short)*num_tris*3,D3DUSAGE_WRITEONLY,D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ib)))
[01:55] <Error404> is to create it right?
[01:55] <RedBeard> just a slight optimization thing...
[01:55] <Error404> hm?
[01:55] <RedBeard> im not sure if MSVC is smart enough to resolve the syntax tree when constants are on opposite sides of a variable
[01:55] <RedBeard> ie, sizeof(short)*num_tris*3
[01:55] <RedBeard> so put constants together
[01:55] <RedBeard> just as a coding style thin
[01:56] <RedBeard> sizeof(short)*3*num_tris
[01:56] <Error405> ah thank you...
[01:56] <RedBeard> that works out to 6*num_tris
[01:56] <RedBeard> which is 1 less multiply
[01:56] <Error405> so the compiler will do it beforehand since they are constants.
[01:56] <RedBeard> im not sure if msvc will optimize it properly the otherway
[01:56] <Error405> neat, didn't know that.
[01:57] <Error404> ok.. i'm renaming this function, InitVB seems a little wrong now
[01:57] <RedBeard> heh
[01:58] <RedBeard> InitGeometry perhaps
[01:58] <Error404> InitBuffers is what i got... but Geometry seems smarter
[01:58] <Error404> lol
[01:58] <RedBeard> buffers works too
[01:58] <RedBeard> not a big deal
[01:59] <Error405> The compiler cares!
[01:59] <Error405> hehe ah well ok what's next?
[01:59] <Error405> wait wait lemme review this function
[01:59] <RedBeard> next, you have to modify your drawing code to use the index buffer
[02:00] <RedBeard> make VB, make IB, lock VB, lock IB, fill in vertex data, fill in index data, unlock both
[02:00] <RedBeard> roughly in that order
[02:01] <Error405> is it ok if you have lock vb, unlock, lock ib, unlock?
[02:01] <Error405> with fills between?
[02:01] <RedBeard> yeah no problem
[02:01] <Error405> ah ok good
[02:02] <Error405> ok now change drawing function to use the ib lemme see if i can't figure this out
[02:02] <RedBeard> it involves SetIndices
[02:02] <RedBeard> just as a starter hint
[02:03] <Error405> err.. ok lemme find that
[02:04] <Error405> Ok the call would be pd3dDevice->SetIndices(ib,0) but it says something about making memory leaks. so would I set it to pd3dDevice->SetIndices(NULL,NULL) afterwards?
[02:05] <RedBeard> possibly
[02:05] <RedBeard> not really a problem
[02:05] <RedBeard> just do that on program shutdown
[02:05] <Error405> it's not?
[02:05] <Error405> oh.
[02:05] <Error405> ok
[02:05] <RedBeard> because if you keep using the same index buffer, or even different ones, there's no need to set it to NULL all the time
[02:06] <Error405> ok lets see. so i set the indices then set the streamsource?
[02:06] <RedBeard> yeah, don't have to do that in any particular order
[02:06] <RedBeard> just set both before calling DrawIndexedPrimitive
[02:07] <Error405> ah another hint
[02:07] <Error405> one moment
[02:07] <RedBeard> heh, i hope im helping without bludgeoning you with the stuff
[02:08] <RedBeard> just it's a lot easier when you know which functions you need, it's much less tedious
[02:08] <Error405> i would have never figured it out
[02:08] <Error405> I don't know the difference lol
[02:09] <Error405> ok.. i see how this works but I'm a little confused with one thing.
[02:09] <Error405> I understand that i'm using the IndexBuffer now, but how do the coordinate information get rendered if the ib was just fed 0,1,2?
[02:09] <RedBeard> what are you giving to SetStreamSource?
[02:10] <Error404> d3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,num_verts,0,num_tris);
[02:10] <Error404> pd3dDevice->SetStreamSource(0,ib,sizeof(CUSTOMVERTEX));
[02:10] <Error404> should i keep it vb>
[02:10] <Error404> i thikn i should eh?
[02:10] <RedBeard> yes
[02:10] <RedBeard> streams are used for vertex data
[02:10] <RedBeard> indices get their own function
[02:11] <Error404> ah.
[02:11] <RedBeard> no need to set the same data more than once
[02:11] <Error404> ok so what is thedifference?
[02:11] <RedBeard> make sense?
[02:11] <Error404> I see how it works, somewhat. But I don't see the reasoning behind it.
[02:11] <RedBeard> so now you're giving the card vertices through SetStreamSource
[02:11] <RedBeard> and you're giving it a set of indices through SetIndices
[02:12] <RedBeard> now the card goes through the index array, and collects them in sets of 3 (triangle list)
[02:12] <RedBeard> and for each 3, it goes and grabs the vertices at the index indicated
[02:12] <Error404> So in this case I could make lots of triangles?
[02:12] <RedBeard> so in this case, it grabs vertex 0, vertex 1, vertex 2
[02:12] <RedBeard> and makes a triangle from them
[02:12] <RedBeard> then it renders that triangle
[02:13] <RedBeard> you can make 2 triangles by adding 1 vertex and 3 indices
[02:13] <Error404> Just increase num_tris and then transform it then I'd have lots of triangles using only one vertex buffer with 3 coords?
[02:13] <Error404> transform each on that is..
[02:13] <Error404> If that works.
[02:13] <Error404> (is hoping thats a reason)
[02:13] <RedBeard> well, if you only have 3 coords then you can only make 1 triangle
[02:14] <Error404> o
[02:14] <Error404> Ok. so it just seperates the data flow?
[02:14] <Error404> Oh wait I think I get it.
[02:14] <RedBeard> and you can't set the transformation matrices more than once per DP call (DrawPrimitve)
[02:15] <RedBeard> dont worry about transforms just yet though
[02:15] <Error404> Ok.. so what that one DrawIndexedPrimitive the right call?
[02:15] <Error404> d3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0,num_verts,0,num_tris);
[02:16] <Error404> i'm drawing a triangle list, with 0 min indeces (i don't know why), with 3(num_verts), 0 as the starting, with 1(num_tris)?
[02:17] <RedBeard> ok
[02:17] <Error404> I used 0 in SetIndices, so I would use 0 in DIP as well?
[02:17] <RedBeard> and you're initializing num_tris to 3 and num_verts to 3*num_tris, right?
[02:17] <RedBeard> ah, those are a bit different
[02:17] <RedBeard> just use 0 for now, i can explain what they do if you want
[02:17] <Error404> Err I thought num_tris was 1 and num_verts was 3?
[02:18] <RedBeard> uhm yes, num_tris to 1, num_verts to 3*num_tris
[02:18] * RedBeard is a bit tired
[02:19] <Error404> is num_verts just 3 or 3*num_tris
[02:19] <RedBeard> 3*num_tris
[02:19] <Error404> Cause I know there are 3 vertices in a triangle..
[02:19] <Error404> Ah.. hrm ok.
[02:19] * Joins: Stinger (tcoleman@210.50.48.33)
[02:19] * X2 sets mode: +v Stinger
[02:19] <Error404> hm.. ah hmm. oh I see why.
[02:19] <Error404> Ah!@
[02:19] <Error404> ok
[02:19] <RedBeard> hrmm wait
[02:20] <RedBeard> no no, im talking rot
[02:20] <RedBeard> just make it 3
[02:20] <Error404> So if I increase the number of triangles then it'd increase it.
[02:20] <Error404> It makes sense
[02:20] <RedBeard> number of verts and number of tri's aren't really related
[02:20] <Error404> It does, really.
[02:20] <Error404> oh
[02:20] <Error404> damn
[02:20] * Error404 hits head
[02:20] <RedBeard> you can have 4 verts and 2 tris
[02:20] <RedBeard> using 6 indices
[02:20] <Error404> o
[02:20] <RedBeard> number of indices = 3*num_tris
[02:21] <Error404> ..
[02:21] <Error404> one moment, computing
[02:21] <RedBeard> but since that is always true, you can get away with only storing num_tris
[02:21] <RedBeard> but num_verts is stored separately
[02:21] <RedBeard> because it's an independent value
[02:21] <Error404> num_tris = 1;
[02:21] <Error404>     num_verts = 3;
[02:21] <RedBeard> yeah
[02:21] <Error404> ok.
[02:21] <RedBeard> see if it compiles
[02:21] <Error404> alrighty
[02:22] <Error404> err some errors, lemme debug
[02:23] <Error404> compiled.
[02:23] <Error404> .. i guess i run it eh?
[02:23] <RedBeard> yeah give it a go
[02:24] <Error404> I HAVE A FRICKEN TRIANGLE!!
[02:24] <Error404> MWHAHA
[02:24] <RedBeard> a word of caution when using index buffering:
[02:24] <Error404> hm?
[02:24] <RedBeard> it is entirely possible to fill in a bad index value
[02:24] <RedBeard> like 5637
[02:24] <RedBeard> or whatever
[02:24] <RedBeard> and it is likely that your computer will bluescreen
[02:25] <RedBeard> so take the utmost care when building index buffers
[02:25] <Error404> fill in bad index value? like putting in a wrong number in the array?
[02:25] <RedBeard> yes
[02:25] <Error404> ah, sorta if you for-loop it and it overwrites?
[02:25] <RedBeard> make sure you dont put in values that will try to reference non-existent vertices
[02:25] <Error404> Ok so lemme try to get this
[02:26] <Error404> The vertices i set up with err.
[02:26] <Error404> g_Vertices corisponds to the indeces
[02:26] <RedBeard> uh kinda yeah
[02:26] <Error404> So however many vertices i place in g_vertices there will be that many indeces?
[02:26] <RedBeard> the indices index into the vertex buffer
[02:27] <RedBeard> not quite
[02:27] <Error404> please explain?
[02:27] <RedBeard> you just want to make sure that you don't put values into your index buffer that are greater than the number of vertices you're using
[02:27] <RedBeard> ie, don't go pIndices[whatever] = 3452; if you only have 10 vertices
[02:27] * Error404 feels like making a class with error checking
[02:28] <RedBeard> yeah i'd recommend having some kind of error check in debug mode if you're wary of such things
[02:28] <RedBeard> but if you're just hardcoding things for now, it's not a big problem
[02:28] <RedBeard> just keep it in mind - if your program causes a bluescreen, it's likely an indexing problem
[02:28] <Error404> Ok can you tell me what the 0 does in SetIndices and DrawIndexedPrimitive?
[02:29] <RedBeard> i'll explain the DIP one first
[02:29] <Stinger> uh sdk?
[02:29] <Error404> alright.
[02:29] <RedBeard> MinIndex
[02:29] <RedBeard> and NumIndices
[02:29] <RedBeard> basically, that's just an optimization thing
[02:29] <Error404> NumVertices?
[02:30] <Error405> just making sure we're looking at the same function
[02:30] <RedBeard> if the indices that you're using are in the range from 10-20, and don't reference any vertices in the 0-9 range, then you can use MinIndex of 10
[02:30] <Error405> Oh!
[02:30] <Error405> sorta like ofsetting during a lock?
[02:30] <RedBeard> so the driver only has to load the vertices it will actually use
[02:30] <RedBeard> not quite, but similar
[02:31] <RedBeard> note that it's the minimum index VALUE, not the minimum index index
[02:31] <RedBeard> the starting index is different
[02:31] <Error405> ok what do you mean the value?
[02:31] <RedBeard> so if you're drawing a quad using index values (10,11,12),(12,11,13), then you can use MinIndex of 10
[02:31] <Error405> because..err.. wait
[02:32] <RedBeard> because you aren't using any vertices below that index value
[02:32] <Error404> so if I had many shapes within the vertex buffer I could choose which ones to draw?
[02:32] <Error404> pIndices[0] = 0;    pIndices[1] = 1;    pIndices[2] = 2;
[02:33] <Error404> are those the values that you're talking about?
[02:33] <RedBeard> yes
[02:33] <RedBeard> so in that case, your minimum index is 0
[02:33] <RedBeard> so use 0
[02:33] <Error404> And those corispond to the vertex buffer right?
[02:33] <Error404> { 150.0f,     50.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(255,255,255,255) },
[02:33] <Error404>         { 250.0f,    250.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(0,0,0,128) },
[02:33] <Error404>         { 50.0f,    250.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(255,0,0,64) },
[02:33] <RedBeard> right, they index into the vbuffer
[02:33] <Error404> g_Vertices[0]-2
[02:33] <Error404> Awesome
[02:34] <Error404> It makes sense now.
[02:34] <RedBeard> StartIndex
[02:34] <RedBeard> you can store lots of indices into an index buffer, and some might pertain to different objects
[02:34] <Error404> Is the actual index to start out?
[02:34] <Error404> err with
[02:35] <RedBeard> so you can use 12 indices for making a set of 4 triangles, and then another 12 for another set
[02:35] <RedBeard> but you want to change some state between the renders
[02:35] <RedBeard> say, change the texture
[02:35] * Quits: SGT-T (SGT-T@66-218-193-35.dialup.fidalgo.net) (Read error to SGT-T[66-218-193-35.dialup.fidalgo.net]: Connection reset by peer)
[02:35] <RedBeard> you can store all the indices into the same index buffer, and then give it an offset to start at
[02:35] * Joins: Kicknfast (SGT-T@66-218-193-35.dialup.fidalgo.net)
[02:35] * X2 sets mode: +v Kicknfast
[02:35] <RedBeard> so the first time you'd use 0, and the 2nd time you'd use 12
[02:35] <RedBeard> make sense?
[02:36] <Error404> So if i have a triangle in the vb in the index 2-4 then another in 50-52 i'd use StartIndex of 50 to make that triangle?
[02:36] <Error404> of course I could just set the value to 50-52 when initializing right?
[02:36] <RedBeard> uhm, i think so
[02:37] <RedBeard> NumVertices i think is used in a similar fashion to MinIndex
[02:37] <RedBeard> essentially, it indicates the MaxIndex
[02:37] <RedBeard> except it's relative to MinIndex
[02:37] <Error404> so if the IndexBuffer was technically initialized with for(int i,i<50,i++)pIndices[i]=i I would just keep MinIndex and StartIndex the same right?
[02:37] <RedBeard> so NumVertices = MaxIndex - MinIndex
[02:38] <RedBeard> no, MinIndex and StartIndex are different things entirely
[02:38] <Error404> oh
[02:38] <RedBeard> for the time being, you can just use 0 for MinIndex and num_verts for NumVertices, in all your calls
[02:38] <Error404> lemme reread what you said.
[02:39] <RedBeard> but StartIndex corresponds to which index in your index buffer you want to start with
[02:39] <RedBeard> so, using indices=(0,1,2, 3,4,5)
[02:39] <RedBeard> you can draw the triangle 3,4,5 by setting StartIndex=3
[02:40] <Error404> Oh.
[02:40] <RedBeard> that's a kinda bad example, coz the numbers match
[02:40] <Error404> So really if I had just had a huge friken list of coords in the vertex buffer.
[02:40] <Error404> I could join ones generally next to each other in value
[02:40] <RedBeard> so, using indices=(12,15,22, 22,6,80) you still use StartIndex=3 to draw the 2nd triangle (22,6,80)
[02:40] <Error404> even if they don't make a shape or whatnot, just for example.
[02:41] <RedBeard> yes, that's a part of state-change optimization
[02:41] <Error404> Yeah it's the index.
[02:41] <RedBeard> ok, so now... add another vertex
[02:41] <Error404> Ok.
[02:42] * Quits: Stinger (tcoleman@210.50.48.33) (Ping timeout for Stinger[210.50.48.33])
[02:42] <Error404> I'm trying to remember how the coords look lol
[02:42] <Error404> It's in LHS.. right?
[02:43] <RedBeard> { 150.0f,    450.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(0,255,0,64) },
[02:43] <RedBeard> and add 3 more indices
[02:43] <RedBeard> (2,1,3) in that order
[02:44] <Error404> huh.. oh
[02:44] <Error404> oh. OH
[02:44] * Error404 hits head
[02:44] <Error404> Now it makes sense
[02:44] <Error404> It's the order you're drawing the polygon
[02:44] * Error404 kicks self
[02:45] <RedBeard> note that i'm using a clockwise ordering, since that's what the original sample you sent me uses
[02:45] <Error404> Do i change num_tris and num_verts..
[02:46] <Error404> I put num_verts to 4..
[02:46] <RedBeard> you could turn on culling... D3DCULL_CCW
[02:46] <RedBeard> since you're using clockwise winding, you can cull counter-clockwise faces
[02:46] <RedBeard> dont worry about culling just yet though
[02:47] <Error404> pIndices[0] = 0;    pIndices[1] = 1;    pIndices[2] = 2;
[02:47] <Error404>     pIndices[3] = 2;    pIndices[4] = 1;    pIndices[5] = 3;
[02:47] <Error404> I've read the entire section of d3d in Jim Adam's book, i just couldn't implement any of the knowledge.
[02:47] <Error404> is that about right?
[02:47] <RedBeard> looks good
[02:47] <RedBeard> and remember to set num_tris to 2
[02:47] <Error404> heh got that.
[02:47] <Error404> should be a quad
[02:47] <RedBeard> kinda
[02:48] <Error404> Now i understand what you said up there.
[02:48] <Error404> I changed the coords
[02:48] <Error404> to make a square
[02:48] <Error404> hopefully
[02:48] <Error404> YAR!!
[02:48] <Error404> I gots square!
[02:48] <RedBeard> :)
[02:48] <RedBeard> ok, now try turning on culling and see what it looks like
[02:49] <Error404> oh it was on
[02:49] <RedBeard> ok, flip it to use D3DCULL_CW
[02:49] <Error404> Or at least I thikn it was
[02:49] <Error404> I gots no square!
[02:49] <Error404> haha
[02:49] <RedBeard> alright
[02:49] <RedBeard> now change the indices to be 1,2,3 instead of 2,1,3
[02:49] <RedBeard> and then try both culling modes
[02:50] <Error404> ha that's cool
[02:51] <RedBeard> that's backface culling
[02:51] <RedBeard> handled by the video card
[02:51] * Error404 pats his gf
[02:51] <Error404> this is awesome
[02:51] <Error404> i feel like coding again
[02:51] * RedBeard wishes there were someone around to teach him the things he wants to know
[02:52] <Error404> so with pd3dDevice->SetIndices(ib,0); i can sorta make a triangle out of that vb? right?
[02:52] <Error404> Dude if you need help with guitar I'd help anyday
[02:52] <RedBeard> heh i meant programming stuff
[02:52] <Error404> Oh yeah you haven't told me what that does
[02:52] <RedBeard> dont have me a gi-tar
[02:52] <RedBeard> heh
[02:52] <Error405> HRESULT DrawIndexedPrimitive(
[02:52] <Error405> D3DPRIMITIVETYPE Type,
[02:52] <Error405> UINT MinIndex,
[02:52] <Error405> UINT NumVertices,
[02:52] <RedBeard> ok, the 2nd param of SetIndices
[02:52] <Error405> oops
[02:52] <Error405> yeah
[02:53] <Error405> that one
[02:53] <Error405> i hate copying on one computer then pasting on the other
[02:53] <RedBeard> that's the "index offset"
[02:53] <Error405> damn clipboard should be shared damnit
[02:53] <RedBeard> BaseVertexIndex
[02:53] <RedBeard> and essentially, it increases the value of all your indices
[02:53] <RedBeard> take note that it is a 32-bit value
[02:53] <RedBeard> not 16-bit
[02:53] <Error405> hm?
[02:53] <Error405> yeah
[02:53] <Error405> unit
[02:54] <Error405> int
[02:54] <RedBeard> you can only draw 65,546 things at a time
[02:54] <RedBeard> 65536
[02:54] <Error404> really?
[02:54] <Error404> doesn't seem like that much =(
[02:54] <RedBeard> heh, it is
[02:54] <RedBeard> that's per DP call
[02:55] <RedBeard> but you can store more than that many things into a vertex buffer
[02:55] <RedBeard> so if you want to access the vertex at element 65,537 let's say
[02:55] <Error404> ohh
[02:55] <RedBeard> you can put an index of 65535 into your index buffer
[02:55] <RedBeard> and then use 2 in BaseVertexIndex
[02:55] <Error404> and then my other code will stil work
[02:55] <RedBeard> or vice versa
[02:55] <Error404> if they were all triangles for instance
[02:56] <RedBeard> yeah
[02:56] <Error404> ok, so lets say that i only want to draw three of these vertices here to make a triangle instead of a quad.
[02:56] <RedBeard> so you can use the same index buffer, and index into separate parts of the same vertex buffer using the same indices
[02:57] <RedBeard> so you have your quad-drawing index buffer (0,1,2, 2,1,3) right?
[02:57] <RedBeard> you can put 8 vertices into the vertex buffer, grouped in 4's
[02:57] <Error404> and an index buffer can only have 65546 indices?
[02:57] <RedBeard> 65536
[02:57] <Error404> yeah?
[02:57] <Error404> ah 36
[02:57] <RedBeard> on a gf2 thats the max you can draw
[02:58] <RedBeard> you can use StartIndex to go to a higher index in the buffer
[02:58] <RedBeard> so with your 8 vertices in the vbuffer, you have 2 quads
[02:58] <RedBeard> you can then go... SetIndices(ib,0); DIP(...); SetIndices(ib,4); DIP(...);
[02:58] <RedBeard> and the DIP calls will be the same
[02:59] <Error404> ah yeah and it would draw both
[02:59] <RedBeard> pretty cool huh
[02:59] <Error404> yeah that is cool.
[02:59] <RedBeard> so you don't have to rearrange your vertices or your indices, you just set 1 value in a function call
[02:59] <Error404> is there any way to choose which vertices you're drawing?
[03:00] <RedBeard> like i said before, d3d enforces a pretty optimized method of doing things
[03:00] <Error404> say I want to make one very large quad instead of two smaller ones?
[03:00] <RedBeard> one large quad?
[03:00] <Error404> Yeah assuming that those two quads are next to each other
[03:00] <Error404> and the same size.
[03:00] <Error404> I could get the far corners and then just make one extremely large quad instead?
[03:01] <RedBeard> the positions of the vertices are in the vertex buffer, if you adjust the vertex data then the polygons will reflect the change
[03:01] <RedBeard> yes
[03:01] <RedBeard> but you'd have to change either the index buffer or the vertex buffer
[03:01] <Error404> which is slow right?
[03:01] <RedBeard> can be
[03:01] <RedBeard> if you do it as a preprocessing step then it's no big deal
[03:01] <RedBeard> just try to steer clear of doing it every frame
[03:02] <Error404> reminds me of LOD
[03:02] <RedBeard> in the case of individual quads, you won't see a big hit
[03:02] <RedBeard> but if you're talking about dynamically building index buffers for LOD, that can get slow
[03:02] <Error404> So it's good to try to draw as many things with one DP call as you can right?
[03:02] <RedBeard> better to adjust the indices than the vertices though, since the indices are 2 bytes each and the vertices are like 32 bytes each
[03:02] <RedBeard> BIG difference
[03:03] <RedBeard> yes, put as much stuff into one DP as you can
[03:03] <Error404> wouldn't that mean it's all one big arse triangle list?
[03:03] <RedBeard> yeah, there's a limit to how much you can do in one call
[03:04] <Error404> Do they all have to be connected, or is that just with tristrips and stuff?
[03:04] <RedBeard> all the triangles drawn must be consecutive in the index list
[03:04] <Error404> Ohh
[03:04] <RedBeard> they can be totally separate
[03:04] <Error404> yeah that's what I was talking about
[03:04] <RedBeard> each triangle in a list is drawn independently
[03:04] <Error404> Like drawing from index 1, then index 4,
[03:04] <Error404> Now i see why you can't
[03:04] <Error404> It has to be consecutive
[03:04] <RedBeard> there's also a trick when using strips to do the same thing
[03:04] <Error404> hm?
[03:05] <RedBeard> "degenerate triangles" are triangles that are actually lines or points
[03:05] <RedBeard> they have no area, and thus can't be drawn
[03:05] <Error404> Ooo
[03:05] <Error404> so you draw a line to the other triangles
[03:05] <RedBeard> now, to make a degenerate tri, you just give it the same vertex more than once
[03:06] <Error404> that's a neat trick
[03:06] <Error404> it's fast right?
[03:06] <RedBeard> you have to do 2 degenerate tri's for each "jump" though
[03:06] <Error404> 2?
[03:06] <RedBeard> the video hardware is relatively quick to reject them
[03:07] <RedBeard> yes, 2
[03:07] <Error404> what's the reason for needing two?
[03:08] <Washu[AWAY]> two get it to have the proper order for rendering the next triangle in the sequence
[03:08] <RedBeard> when dealing with a tristrip, the "current triangle" is composed of the vertices at the current, current+1, and current+2 positions
[03:08] <Washu[AWAY]> shouldn't that be current, current-1 and current-2
[03:08] <RedBeard> and because of culling, it flip-flops back and forth between CW and CCW culling on each triangle
[03:09] <Error404> ah that's right
[03:09] <Error404> I remember reading that
[03:09] <RedBeard> so to jump and keep the right culling mode, you need to jump twice
[03:09] <RedBeard> not if you start at 0
[03:09] <RedBeard> ^ washu
[03:09] * Error404 nods
[03:09] <Washu[AWAY]> that's right...sorry, its late
[03:09] <Washu[AWAY]> why would you start at 0 though when you can start at 2
[03:10] <Error404> So hmm, if I wanted to make a cube i'd have 8 vertices??
[03:10] <Washu[AWAY]> alright, I'm out
[03:10] <Error404> I'd also have to do a matrix thingy right?
[03:10] <Washu[AWAY]> Latz red, Err, other people that I can't see
[03:10] * Quits: Washu[AWAY] (direwolf@adsl-64-161-27-214.dsl.sntc01.pacbell.net) (I think we need to learn to recieve the entire buffer before closing the socket...!)
[03:10] <RedBeard> yes 8 vertices
[03:10] <RedBeard> how many indices (using a list)
[03:11] <Error404> lemme think
[03:11] <RedBeard> btw, the benefit of using tristrips is largely done away with when using indexing
[03:11] <RedBeard> which is why im getting you to use indexing with lists - more flexible, and still fast
[03:11] <Error404> indices are just triangles right? because d3d can't handle primitive quads?
[03:11] <RedBeard> yes, for almost everything, a "primitive" is a triangle
[03:11] <Error404> ogl can take quads right?
[03:11] <RedBeard> it's a little different when using n-patches, but i havent used those
[03:12] <RedBeard> uh it can, but it breaks them into triangles internally
[03:12] <Error404> so one could easily design directx to do it as well.
[03:12] <Error404> soif there are 6 sides.
[03:12] <Error404> there are 2 tris per side
[03:12] <Error404> 12 indices?
[03:13] <RedBeard> 12 faces
[03:13] <RedBeard> 12 tri's
[03:13] <RedBeard> 36 indices
[03:13] * Error404 blikns
[03:13] <RedBeard> 6 sides, 12 tri's
[03:13] <Error404> so index != tri
[03:13] <RedBeard> 36 indices
[03:13] <RedBeard> in a list, tri = 3*index
[03:13] <Error404> Oh yeah that's right!
[03:14] <Error404> Becuase you draw seperate tri's for each
[03:14] <RedBeard> right
[03:14] <RedBeard> lemme quickly explain why indexing is so good
[03:14] <RedBeard> before i head to bed
[03:14] <Error404> ok
[03:14] <RedBeard> the video card has a "vertex cache" which is different from an L2 cache
[03:14] <RedBeard> it also haas an L2 kinda thing
[03:14] * Error404 reminds to look up L2
[03:15] <RedBeard> the difference is that L2 is just memory
[03:15] * Error404 writes that down
[03:15] <RedBeard> and vertex cache is post-transform vertices
[03:15] * Quits: kOOktrOOp (kook@c17345.mirnd1.nsw.optusnet.com.au) (Read error to kOOktrOOp[c17345.mirnd1.nsw.optusnet.com.au]: Connection reset by peer)
[03:15] <RedBeard> so if you're applying a complex vertex shader, and you go back and use an index again in a 2nd triangle, it can grab the already-transformed vertex from vertex cache without running it through the shader again
[03:16] <Error404> oo
[03:16] <RedBeard> ie, (0,1,2, 2,1,3) uses 1 and 2 twice
[03:16] <RedBeard> so you only compute 4 vertex positions and use them multiple times
[03:16] <Error404> i'm assuming that is much faster then
[03:16] <Error404> becuase you aren't reading from the vb
[03:16] <RedBeard> it can be up to three times faster
[03:16] <Error404> yeah that is fast.
[03:17] <RedBeard> "did you just say 'three times faster'?"
[03:17] <RedBeard> Yes.
[03:17] <Error404> haha
[03:17] <Error404> that's awesome.
[03:17] <RedBeard> remember that the vertex cache is quite small though, around 10 vertices on a gf2
[03:17] <Error404> I wonder how many are on gf3
[03:17] <RedBeard> i think 14 or 18
[03:17] <Error404> why so small?
[03:18] <RedBeard> takes a lot of transistors, and it's integrated into the core of the chip
[03:18] <RedBeard> = expensive
[03:18] <Error404> oh that makes sense then
[03:18] <RedBeard> and there are things in d3dx to optimize a mesh for a certain chip, based on the vertex cache size, so that it uses a vertex as many times as possible before it drops out of the cache
[03:18] <Error404> so is it smart enough to do all the computations on the available vertices before shoving them away and getting other thins?
[03:18] <Error404> ah just what you said
[03:18] <Error404> lol
[03:19] <RedBeard> that kind of mesh optimization is a freakin complex process
[03:19] <RedBeard> because once it drops out of cache, you want to delay using that vertex again until you can get a batch of uses together again
[03:19] <RedBeard> while keeping track of all the other vertices at the same time
[03:20] <Error404> sounds like it would be impossible to use with tristrips
[03:20] <RedBeard> i havent seen a paper that discusses how to do that yet, i might look for one tomorrow
[03:20] <Error404> as you're always borrowing at least one vertex from somewhere else.
[03:20] <RedBeard> not really, but tristrips make it harder to jump around to use vertex cache well
[03:21] <Error404> yeah that makes sense.
[03:21] <RedBeard> you don't have to use the vertex that you used on the previous triangle
[03:21] <RedBeard> you just have to use it while it's still in cache
[03:21] <RedBeard> so you are free to jump around a little
[03:21] <Error404> ah.
[03:21] <RedBeard> tristrips dont let you jump around, they force you to keep the last 2 vertices
[03:22] <RedBeard> so that's another reason to use lists
[03:22] <Error404> there isn't much of a diff though right?
[03:22] <Error404> Other than the extra drawing and more storage space
[03:22] <RedBeard> not a huge difference, but you might be surprised how much performance is gained by avoiding cache misses
[03:23] <RedBeard> basically the only difference between indexed tri lists and strips is the amount of memory used
[03:23] <RedBeard> and when you have textures taking up 4MB of memory, and index buffers that take up 50K, that's nothing to worry about
[03:23] <Error404> heh yeah
[03:24] <Error404> and with more and more cards going over 100mb of VRAM and more people getting over 1gb of system ram it's going to be less of a prob
[03:24] <Error404> i guess
[03:24] <RedBeard> yep
[03:24] <Error404> Thank you man, this really helped me a lot.
[03:24] <RedBeard> glad to hear it
[03:25] <Error404> I've been trying to get someone to help me, but no one seemed to want to.
[03:25] <Error404> even the great mittens..
[03:25] <RedBeard> hope i've given enough info for you to start exploring some more stuff
[03:25] * Error404 kicks trent.
[03:25] <RedBeard> .k mittens[smeep]
[03:25] * mittens[smeep] was kicked by X2 (<RedBeard> You're outta-here, mittens[smeep]!)
[03:25] * Joins: mittens[smeep] (narf@userd204.torchlake.com)
[03:25] * X2 sets mode: +o mittens[smeep]
[03:25] <RedBeard> heheh
[03:25] <Error404> Yeah I'm going to try to make a cube now, and read up on the other functions i called in the class.
[03:25] <RedBeard> good
[03:25] <Error404> I know more about DIP and SetIndices than any other.
[03:26] <Error404> I like knowing all the possibilities.
[03:26] <RedBeard> yep
[03:26] <RedBeard> im gonna get some sleep

-- the header file, DXanadu.h --

_***font face=Courier New***_
#include <d3d8.h>

#ifndef _DXANADU_
#define _DXANADU_


class cDXanadu
{
public:
cDXanadu();
~cDXanadu();

bool InitD3D(HWND hWnd);
bool InitFrame();

bool DoFrame();

void CleanUp();

private:
struct CUSTOMVERTEX
{
float x,y,z,rhw;
DWORD color;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

bool InitBuffers();
IDirect3D8 *pD3D;
IDirect3DDevice8 *pd3dDevice;
IDirect3DVertexBuffer8 *vb;
IDirect3DIndexBuffer8 *ib;
DWORD num_tris;
DWORD num_verts;

};

#endif
_***/font***_

-- the source file, DXanadu.cpp --

_***font face=Courier New***_
#include "DXanadu.h"

cDXanadu::cDXanadu()
{
pD3D = NULL;
pd3dDevice = NULL;

}
cDXanadu::~cDXanadu()
{
}

bool cDXanadu::InitD3D(HWND hWnd)
{
if(NULL == (pD3D = Direct3DCreate8 (D3D_SDK_VERSION)))
return false;

D3DDISPLAYMODE d3ddm;
if(FAILED (pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
return false;

D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp,sizeof(d3dpp));
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;

if(FAILED (pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pd3dDevice)))
return false;
pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_CCW );
return true;
}

bool cDXanadu::InitFrame()
{
if(!InitBuffers())
return false;
return true;
}

//make VB, make IB, lock VB, lock IB, fill in vertex data, fill in index data, unlock both
bool cDXanadu::InitBuffers()
{
num_tris = 2;
num_verts = 4;
CUSTOMVERTEX g_Vertices[] =
{
{ 150.0f,     50.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(255,255,255,255) },
{ 250.0f,     50.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(0,0,0,128) },
{ 150.0f,    150.0f,    0.5f,    1.0f,    D3DCOLOR_RGBA(255,0,0,64) },
{ 250.0f,    150.0f, 0.5f,    1.0f,    D3DCOLOR_RGBA(0,255,0,255) }
};
if(FAILED( pd3dDevice->CreateVertexBuffer(num_verts*sizeof(CUSTOMVERTEX), D3DUSAGE_WRITEONLY, D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT, &vb)))
return false;
VOID * pVertices;
if(FAILED( vb->Lock(0, 0, (BYTE**)&pVertices, 0)))
return false;
memcpy(pVertices,g_Vertices,sizeof(g_Vertices));
vb->Unlock();

// Creating the Index Buffer
if(FAILED( pd3dDevice->CreateIndexBuffer(sizeof(short)*3*num_tris, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &ib)))
return false;
unsigned short * pIndices;
if(FAILED( ib->Lock(0, 0, (BYTE**)&pIndices, 0)))
return false;
pIndices[0] = 0;    pIndices[1] = 1;    pIndices[2] = 2;
pIndices[3] = 2;    pIndices[4] = 1;    pIndices[5] = 3;
ib->Unlock();

return true;
}

bool cDXanadu::DoFrame()
{
pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,128,255), 1.0f, 0);
// Begin the scene
pd3dDevice->BeginScene();

// Render Stuff
pd3dDevice->SetIndices(ib, 0);
pd3dDevice->SetStreamSource(0, vb, sizeof(CUSTOMVERTEX));
pd3dDevice->SetVertexShader(D3DFVF_CUSTOMVERTEX);


pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, num_verts, 0, num_tris);

// End the scene
pd3dDevice->EndScene();

// Show the backbuffer
pd3dDevice->Present(NULL,NULL,NULL,NULL);
return true;
}

void cDXanadu::CleanUp()
{
pd3dDevice->SetIndices(NULL,NULL);
if(vb != NULL)
vb->Release();
if(ib != NULL)
ib->Release();
if(pd3dDevice != NULL)
pd3dDevice->Release();
if(pD3D != NULL)
pD3D->Release();

}
_***/font***_


Text file last modified on April 28, 2007, 10:28:21 PM, MST.

All content Copyright © 2025 Andy Campbell.
Permission to reproduce content, in any format,
by explicit written permission of author only.
If you can't learn to do it well, learn to enjoy doing it badly.