Thickness of lines..

Started by
7 comments, last by yanuart 19 years, 10 months ago
Can we change the thickness of the lines when we draw lines primitives ?? How ?
Ride the thrill of your life
Play Motorama
Advertisement
Simple: you can''t. At least not with DirectX (I don''t knowwhether that''ll be the case with DirectX9, but it surely is so with all versions up to 8.1). OpenGL can render variable width lines AFAIK.

Of course, there is a workaround for this inconvenience in DX; it''s actually the way to render textured lines that can be also used in projects such as lightning bolts, laser beams, et al.

The trick is simple: instead of defining two certices in space and rendering them as a linelist or a linestrip, define a quad (two triangular polygons, sharing one edge - two vertices, that is), which is equivalent to four vertices (or 6 if you use triangle lists; however this is less efficient than using a triangelist or a trianglestrip - do pay attention to the (counter)clockwiseness of the vertices :D). Then you render the quad at the correct dimensions in order to resemble a line.

In essence, you create a square that is so stretched that it is no longer a square, but rather a thich line.

Try it out; should you come up with any code samples, I''d like to see them.

GL
Spyros

-----------<<>>-----------
Flareman (a.k.a Ga1adaN)
Primus ante Adain
flareman@freemail.gr
-----------<<>>------------
-----------<<>>-----------Flareman (a.k.a Ga1adaN)Primus ante Adain[email=flareman@freemail.gr]flareman@freemail.gr[/email]-----------<<>>------------
Maybe there''s some way using vertex shaders...

I realy don''t know a lot of vertex shaders but if they are
the renderers they should do it...

P.S. I don''t know if there''s really a way to do that, just
trying to give help...
quote:Original post by Kamikaze15
Maybe there''s some way using vertex shaders...

I realy don''t know a lot of vertex shaders but if they are
the renderers they should do it...

P.S. I don''t know if there''s really a way to do that, just
trying to give help...


Vertex shaders have nothing to do with this.

Don''t listen to me. I''ve had too much coffee.
Whatever you do , a lines width will always be equal to 1 pixel.Remember this , not matter where ur camera is.The only solution to creating thick lines is to make more than 1 and position them next to each other
-Da Mr.RaSt3RiZah-
I''m sorry, I thought it could do that...
Actually, you can emulate line thickness (to an extent) using vertex shaders. I remember somewhere (I think it was the cg browser) had an example.
Where ?? How ??
Ride the thrill of your life
Play Motorama
Although this thread is a couple years old, it helped me get line drawing (at least in my app) working. Special thanks to "flareman".

If you are working with DirectDraw (using pixel coordinates and such), the simplest way to draw a lines is to use the "D3DXCreateLine" function to get a "ID3DXLine". Then you can do something like this (code might not be perfect)

D3DXVECTOR2 verts[] = { { startX, startY }, { endX, endY } };LPD3DXLINE* ppLine;D3DXCreateLine(g_youd3ddevice, ppLine);ppLine.SetWidth(10);ppLine.Begin();// draw function takes array, number of verts, and color// in this case color should be redppLine.Draw(verts, 2, 0xffff0000);ppLine.End();  


Simple, huh? This only works if you to make a 2d program. The line only accepts "D3DXVECTOR2" types, which contain only two members: x and y. These members are also pixel positions, not world coordinates (i.e. 0,0 is the top left of the window).

The 3d version is more complicated. You basically make two triangles using four vertices (using a triangle strip). Something like this:

HRESULT drawLine(){	int numVerts = 4;	float startX, startY, endX, endY;	startX = 0.5;	startY = -0.5;	endX = 30.5;	endY = -0.5;    float thickness = 0.25;	DWORD color = D3DCOLOR_ARGB(255,255,0,0);        // the first three vertices (0,1,2) define the first triangle        // the next triangle is defined by using the the last two vertices from        // the first triangle, and the next vertex in the list (3), so         // the second triangle is defined by (1,2,3)	createVertex(0, startX, startY + thickness, color);	createVertex(1, startX, startY - thickness, color);	createVertex(2, endX, endY + thickness, color);	createVertex(3, endX, endY - thickness, color);	// Direct X calls follow    if( FAILED( g_pd3dDevice->CreateVertexBuffer( numVerts*sizeof(CUSTOMVERTEX),                                                  0, D3DFVF_CUSTOMVERTEX,                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )    {        return E_FAIL;    }    // Fill the vertex buffer.    VOID* pVertices;    if( FAILED( g_pVB->Lock( 0, 0, (void**)&pVertices, 0 ) ) )        return E_FAIL;    memcpy( pVertices, g_verts, numVerts*sizeof(CUSTOMVERTEX) );    g_pVB->Unlock();	// Render the vertex buffer contents	g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) );	g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );	g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 );	g_pVB->Release();		return S_OK;}  


"createVertex" fills up a globally defined vertex array called g_verts. Note that this line has no depth, it's just a flat line.

Hope this helps someone.



[edited by - jashugan on May 31, 2004 1:27:40 PM]

This topic is closed to new replies.

Advertisement