Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Line Drawing with OpenGL

Credits go to SiegeLord for helping out!
This thread is locked; no one can reply to it. rss feed Print
Line Drawing with OpenGL
DragonDePlatino
Member #16,608
December 2016

Hello again. I'm sorry to make another thread so quickly, but this is really baffling me. In my game, I have a transparent minimap that's layered over gameplay. It worked fine in Direct3D, but immediately broke when I switched to OpenGL with al_set_new_display_flags(ALLEGRO_OPENGL).

{"name":"TIABOH9.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/9\/4971d386b780e34bec199cea56f672d3.png","w":680,"h":223,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/4\/9\/4971d386b780e34bec199cea56f672d3"}TIABOH9.png

Basically, I'm doing a series of very fine pixel-perfect line drawing operations inside each cell. There's no scaling, rotating or even translations going on. Just drawing to a minimap texture then blitting that to the window with al_draw_bitmap(map_texture, 0, 0, 0). No changes were made to my code except switching to OpenGL rendering and everything else (rectangle drawing, texture blitting, pixel scaling) is working perfectly.

If you look at the lines in the north/south sides of cells sometimes they're 2-thick, 1-thick or not even drawn. Is this an issue with my code? Or a quirk with how allegro handles OpenGL?

SiegeLord
Member #7,827
October 2006
avatar

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

DragonDePlatino
Member #16,608
December 2016

Oh...I figured as such. It's weird how that happens with OpenGL but not DirectX, though. Guess I'll implement all of my line drawing with al_draw_filled_rectangle() then.

{"name":"BmDgsQs.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/a\/ea66e65a42b582a0c34907337fc633c4.png","w":340,"h":223,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/e\/a\/ea66e65a42b582a0c34907337fc633c4"}BmDgsQs.png

Thanks for your help again.

SiegeLord
Member #7,827
October 2006
avatar

You can do al_draw_line with thickness > 0 and it'll be consistent as well (assuming you set your coordinates appropriately as that link describes). There are two sources of differences between D3D and OpenGL. For thickness == 0, the drawing process is implementation dependent, so that can differ between D3D and OpenGL on the same system, and between OpenGL's on differen systems. For thickness > 0, and if you're right on the boundary (e.g. you draw a line with thickness == 1 with a coordinate of 0, 0) then which pixel will get colored will also be somewhat implementation dependent.

If you just follow the recommendations in that link, it'll work the same anywhere.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

DragonDePlatino
Member #16,608
December 2016

I think I'll just stick to the rectangle drawing. Following the advice of that link, this is how I'd draw the lines on the south sides of pits (the red tiles):

#SelectExpand
1/* Check for non-pit south of cell. */ 2if (level_solidity(pos_add(cell, dir_pos[DIR_SOUTH])) != SOL_PIT) { 3 al_draw_line( cell.x * cell_size.x + 0.5, 4 cell.y * cell_size.y + cell_size.y - 0.5, 5 cell.x * cell_size.x + cell_size.x + 0.5, 6 cell.y * cell_size.y + cell_size.y - 0.5, 7 pit_edge_color, 1 8 ); 9}

and with rectangle drawing:

#SelectExpand
1/* Check for non-pit south of cell. */ 2if (level_solidity(pos_add(cell, dir_pos[DIR_SOUTH])) != SOL_PIT) { 3 al_draw_filled_rectangle( cell.x * cell_size.x, 4 cell.y * cell_size.y + cell_size.y - 1, 5 cell.x * cell_size.x + cell_size.x, 6 cell.y * cell_size.y + cell_size.y, 7 pit_edge_color 8 ); 9}

Both of these methods work perfectly with both DirectX and OpenGL. However, the line drawing feels a little more verbose. It would be neat if allegro had macros for drawing straight pixel-perfect lines that pre-calculated all of the middle-pixel-fragment-rubbish for you. Something like:

#SelectExpand
1#define al_draw_hline(x, y, dx, color) al_draw_line(x + 0.5, y + 0.5, x + dx + 0.5, y + 0.5, color, 0) 2#define al_draw_vline(x, y, dy, color) al_draw_line(x + 0.5, y + 0.5, x + 0.5, y + dy + 0.5, color, 0)

So then the drawing operation would become...

#SelectExpand
1/* Check for non-pit south of cell. */ 2if (level_solidity(pos_add(cell, dir_pos[DIR_SOUTH])) != SOL_PIT) { 3al_draw_hline( cell.x * cell_size.x, cell.y * cell_size.y + cell_size.y - 1, 4 cell_size.x, pit_edge_color); 5}

Go to: