   Draw Square instead of Triangle in DirectX?
  How can I modify this code so that I can render a Square instead of a Triangle?bool InitializeObject(){   // Here we create a vertex buffer.// All the data needed to draw a triangle.   Vertex Triangle_Data[3] =   {   {-0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)}, {0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},   {0.0f, 0.3f, 0.0f, D3DCOLOR_XRGB(0,0,255)}   };if(FAILED(D3D_Device->CreateVertexBuffer(3 * sizeof(Vertex), 0, D3DFVF_D3DVertex,    D3DPOOL_DEFAULT, &Vertex_Buffer, NULL))) {    return false;  // Exit this function if there is some kind of problem. }   // This will hold the triangle's data that will be sent to Direct 3D because we   // are required to use a pointer instead of a normal object when you send the   // data to the vertex buffer.  To do so you must call the Lock() function.   // Lock will lock a range of data into the vertex buffer memory.   Vertex* Vertices;   if(FAILED(Vertex_Buffer->Lock(0, sizeof(Triangle_Data), (void**)&Vertices, 0))) return false;   // Here we copy the triangle's data into the vertex buffer.   memcpy(Vertices, Triangle_Data, sizeof(Triangle_Data));   // Unlock when your done coping data into the buffer.   Vertex_Buffer->Unlock();   return true;}
  You tell it to render as a square in your actual rendering code, not the code to put the vertices in memory.typedef enum D3DPRIMITIVETYPE{    D3DPT_POINTLIST = 1,    D3DPT_LINELIST = 2,    D3DPT_LINESTRIP = 3,    D3DPT_TRIANGLELIST = 4,    D3DPT_TRIANGLESTRIP = 5,    D3DPT_TRIANGLEFAN = 6,    D3DPT_FORCE_DWORD = 0x7fffffff,} D3DPRIMITIVETYPE, *LPD3DPRIMITIVETYPE;edit: Sorry, looks like you can't directly render as a square.   ;Quote:Original post by MercuryYou tell it to render as a square in your actual rendering code, not the code to put the vertices in memory.typedef enum D3DPRIMITIVETYPE{    D3DPT_POINTLIST = 1,    D3DPT_LINELIST = 2,    D3DPT_LINESTRIP = 3,    D3DPT_TRIANGLELIST = 4,    D3DPT_TRIANGLESTRIP = 5,    D3DPT_TRIANGLEFAN = 6,    D3DPT_FORCE_DWORD = 0x7fffffff,} D3DPRIMITIVETYPE, *LPD3DPRIMITIVETYPE;edit: Sorry, looks like you can't directly render as a square.You need to dummy it down even more than you already have.Can you just post that the new InitializeObject method would look like...or is it not that simple?;  A square is made from two triangles.John B   ;  Think about what you can make with triangles...bool InitializeObject(){   // Here we create a vertex buffer.// All the data needed to draw a triangle.   Vertex Triangle_Data[] =   { {0.5f, 0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)}, {0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)}, {-0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(0,0,255)} {-0.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)}, {-0.5f, 0.5f, 0.0f, D3DCOLOR_XRGB(0,0,255)} {0.5f, 0.5f, 0.0f, D3DCOLOR_XRGB(255,255,0)},   };if(FAILED(D3D_Device->CreateVertexBuffer(2 * 3 * sizeof(Vertex), 0, D3DFVF_D3DVertex,    D3DPOOL_DEFAULT, &Vertex_Buffer, NULL))) {    return false;  // Exit this function if there is some kind of problem. }   // This will hold the triangle's data that will be sent to Direct 3D because we   // are required to use a pointer instead of a normal object when you send the   // data to the vertex buffer.  To do so you must call the Lock() function.   // Lock will lock a range of data into the vertex buffer memory.   Vertex* Vertices;   if(FAILED(Vertex_Buffer->Lock(0, sizeof(Triangle_Data), (void**)&Vertices, 0))) return false;   // Here we copy the triangle's data into the vertex buffer.   memcpy(Vertices, Triangle_Data, sizeof(Triangle_Data));   // Unlock when your done coping data into the buffer.   Vertex_Buffer->Unlock();   return true;};Quote:Original post by BezbenThink about what you can make with triangles...*** Source Snippet Removed ***Only half of the square shows up.I understand...2 triangles on top of each other make a square.I would rather just draw 4 lines.I don't like DirectX.  They make stuff so hard, especially for newbies.   ;  Well, depending on the order that you define the vertices in, you can draw the square with four vertices, instead of six.  That's not much different than drawing four lines.  You need to specify a "triangle strip" instead of a "triangle list" in the draw call (sorry, I don't remember the exact syntax for it in C++).But basically, to draw this:v1    v2*---*|    /||   / ||  /  || /   ||/    |*---*v3    v4Instead of declaring six vertices (in the example order of: v1, v2, v3, v3, v2, v4), you would declare four vertices in the order of: v1, v2, v3, v4.That's a handy (small) optimization trick.  If you're interested in knowing more, try looking up information about triangle lists and triangle strips (and "triangle fans").But the basic principle of drawing triangles to add up to all objects you create is a fundamental problem you're going to run into regardless of what graphics API you use.  It's a fundamental concept you need to learn to understand how to draw just about any shape.  There will be a certain complexity to overcome with all of them.As for DirectX being "difficult", there are some functions that draw some basic shapes (cubes, spheres, etc.), but they aren't generally all that handy except for simple applications and for learning purposes.   ;Quote:Original post by Tom KnowltonOnly half of the square shows up.My guess would be that you've got back-face culling turned on.  Basically, when back-face culling is turned on, DirectX will ignore any triangles that are facing away from the camera (this is a good thing usually, since those triangles are generally obscured by other parts of the object, so drawing them would be a waste of time).  The difficulty is that it needs to be able to work out which side of the triangle is the 'front'.  The way it does this is based on the order in which you give it the vertices of the triangle.So, if back-face culling is the problem, the solution is either to turn it off, or to reverse the order of the vertices of the triangle that doesn't show up.There's more information about back-face culling in the DirectX documentation.John B   