• Advertisement
Sign in to follow this  

New to Direct X needs some help please

This topic is 1589 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey everyone!

 

I'm new to direct X and needs some help. I have a few questions based around some code I wrote to draw triangles to make up multiple quads.

 

Here is the code for the render and some inits on direct X

http://pastebin.com/QFXgvAW3

 

I was wondering if anyone can tell me:

 

1. If I turn off culling mode it draws my 4 triangles (2 quads). Why does this work only if culling mode is off? Am I doing something wrong?

2. If I load a texture will I be able to draw the texture properly? I mean will the texture be handled correctly by the triangles?

3. Is this the right way to draw quads? Should I call draw using trianglelist based on a vertexBuffer that has all my vertexes I need or something else? In the end I would like to use the quads as sprites for a 2d game.

 

Share this post


Link to post
Share on other sites
Advertisement

1. That seems like wrong winding order. The default culling mode is D3DCULL_CCW, which means that Direct3D takes triangles with counter-clockwise winding order (the order of verticles) as front facing and renders them. Your verticles are ordered in the other way. To render them you don't have to set D3DCULL_NONE, you can either set D3DCULL_CW or swap any two verticles in each triangle.

2. Not directly, because all your texture coordinates (u, v) are zero at this moment. Your verticles are ready for it, you just have to fill proper values. The proper values of (u, v) for the verticles in each quad are: topLeft - topRight - bottomLeft - bottomRight  (0, 0) - (1, 0) - (0, 1) - (1, 1)

3. This is a right way, but you also can use index buffers. In your case you need 6 verticles for each quad, while 2 are doubled. With indices, you would need just 4 vertices for each quad, plus 6 indices. Try to find something about index buffers in the documentation.

Edited by Tom KQT

Share this post


Link to post
Share on other sites


1. If I turn off culling mode it draws my 4 triangles (2 quads). Why does this work only if culling mode is off? Am I doing something wrong?

 

your triangles are facing the wrong way.  triangles have a "front" and a "back",  defined by winding order.  vertices are listed in counter clockwise order when viewed from the "front" of the triangle.   when cull is on, it  it only draws triangles facing the camera.

 


2. If I load a texture will I be able to draw the texture properly? I mean will the texture be handled correctly by the triangles?

 

once you setup the pipeline for texturing (if you haven't) and your U,V coords, it should work.

 


3. Is this the right way to draw quads? Should I call draw using trianglelist based on a vertexBuffer that has all my vertexes I need or something else? In the end I would like to use the quads as sprites for a 2d game.

 

indexed is faster - therefore usually preferred, although a bit more complex.

 

but for 2d, i would think you'd want to be using the ID3DXSprite interface.  it handles all the quad drawing for you. you just specify a texture, a center, a scale, and an optional transform (for rotated sprites, etc).

Share this post


Link to post
Share on other sites
Thanks a lot guys, this is really helpful!

Does the sprite interface have limitations compared to using quads and making a sprite batcher? Eg performance or dynamic lighting / vertex and pixel shaders

Also, if I had a texture atlas would I just change the u and v variables of my vertexs accordingly to get the image I want?

Share this post


Link to post
Share on other sites

Also I am having trouble with the textures. I'm just trying to draw a single quad made from 2 triangles with a texture on top. But the texture is distorted instead of displaying a circle like it should. It is just displaying a thick black line in the middle like if the circle was being folded in two.

 

What am I doing wrong? Here all of my code

http://pastebin.com/c4sFHpTK

 

Here is also the image I'm trying to display

[attachment=17854:imgTest.png]

Share this post


Link to post
Share on other sites

Hm, the texture coordinates of the first quad look fine to me. Are you drawing the first quad (verts[0] to verts[5])? The second quad doesn't have texcoords yet.

Please check this and then maybe post a screenshot?

Btw, are the vertex colors working? You have different colors in verticles, does it render the colors properly? (Just to make sure that your vertex definition is compatible with the FVF, I'm not familiar with FVF very much.)

Share this post


Link to post
Share on other sites

How was D3DFVF_TLVERTEX defined in your code before? That's not a standard DirectX constant or macro and using Google I found just these definitions:

#define D3DFVF_TLVERTEX D3DFVF_XYZRHW|D3DFVF_DIFFUSE|D3DFVF_TEX1 

So I assumed it had color (diffuse). If it didn't then it's absolutely clear why it didn't work, because you have also a color value in your vertex definition and I guess it was between the position and texture coordinates (you didn't include declaration of the TLVERTEX struct in your code) - so Direct3D probably took the bits belonging to color and used them as if they represented texture coordinates.

 

But if you already had D3DFVF_DIFFUSE in the FVF definition, then it doesn't make sense :D

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement