Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Combining Allegro 5.1.8 with OpenGL culling problems

This thread is locked; no one can reply to it. rss feed Print
Combining Allegro 5.1.8 with OpenGL culling problems
mind78
Member #15,802
November 2014

I am using Allegro 5.1.8 with Msys2. I am trying to combine OpenGL and Allegro drawing, using Allegro routines to draw 2D elements in front of the 3D scene.

Well, so far so good, I get it drawing all right but I am getting stuck on Backface culling.

I have allegro drawing a bitmap mousepointer on top of my 3D scene (a spinning cube). I am using a single al_flip_display(), in this fashion:

setupGLstuff();

while(1)
{
drawGLstuff();
restoreALdrawing();
drawALstuff();
al_flip_display();
}

I can either have no backface culling, and my 2D overlay works, or I can have backfaceculling, and my 2D overlay disappears.

Anyone knows the best way to get this to work?

I was thinking maybe rendering 3D to a bitmap, and then disabling backface culling again, and rendering the 2D on top of that bitmap before showing it but maybe there is a better way?

Thomas Fjellstrom
Member #476
June 2000
avatar

I think you want to disable culling before drawing using allegro.

I'm doing that in my current project.

{"name":"3FAegCS.png","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/b\/fbcf87028635f4dda63ab2ea7c1bcb93.png","w":806,"h":627,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/f\/b\/fbcf87028635f4dda63ab2ea7c1bcb93"}3FAegCS.png

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

mind78
Member #15,802
November 2014

I am trying too.

No luck, for instance, if I change the above scenario to this:

setupGLstuff();

while(1)
{
drawGLstuff();
restoreALdrawing();
glDisable(GL_CULL_FACE);
drawALstuff();
al_flip_display();
}

Allegro drawing works, but the GL stuff gets drawn without culling as well.

[Edit:]

Hold on, I just think I realized how truly retarded I should feel right now. Let me test something.

Thomas Fjellstrom
Member #476
June 2000
avatar

This is basically my code:

#SelectExpand
1// main loop: 2 3 ALLEGRO_STATE state; 4 al_store_state(&state, ALLEGRO_STATE_ALL); 5 6 glClear(GL_DEPTH_BUFFER_BIT); 7 8 redraw = false; 9 al_clear_to_color(al_map_rgb(255,255,255)); 10 11 draw(); 12 13 al_restore_state(&state); 14 15 drawHud(); 16 17 al_restore_state(&state); 18 al_flip_display(); 19 20// draw(): 21 22void Renderer::draw() 23{ 24 ALLEGRO_TRANSFORM trans; 25 al_identity_transform(&trans); 26 27 setupProjection(&trans); 28 29 glm::mat4 mat = camera_.getMat(); 30 31 trans.m[0][3] = mat[0][0]; 32 trans.m[0][1] = mat[0][1]; 33 trans.m[0][2] = mat[0][2]; 34 trans.m[0][3] = mat[0][3]; 35 36 trans.m[1][0] = mat[1][0]; 37 trans.m[1][1] = mat[1][1]; 38 trans.m[1][2] = mat[1][2]; 39 trans.m[1][3] = mat[1][3]; 40 41 trans.m[2][0] = mat[2][0]; 42 trans.m[2][1] = mat[2][1]; 43 trans.m[2][2] = mat[2][2]; 44 trans.m[2][3] = mat[2][3]; 45 46 trans.m[3][0] = mat[3][0]; 47 trans.m[3][1] = mat[3][1]; 48 trans.m[3][2] = mat[3][2]; 49 trans.m[3][3] = mat[3][3]; 50 51 al_use_transform(&trans); 52 53 glEnable(GL_DEPTH_TEST); 54 // Accept fragment if it closer to the camera than the former one 55 glDepthFunc(GL_LEQUAL); 56 glEnable(GL_CULL_FACE); 57 glFrontFace(GL_CCW); 58 glEnable(GL_ALPHA_TEST); 59 60 if(!setShader(SHADER_DEFAULT)) 61 { 62 NBT_Debug("failed to set default shader"); 63 } 64 65 glBindVertexArray(vao_); 66 67 resManager_->setAtlasUniforms(); 68 69 for(auto &it: chunkData_) 70 { 71 ChunkData *cd = it.second; 72 73 ALLEGRO_TRANSFORM ctrans; 74 al_identity_transform(&ctrans); 75 al_translate_transform_3d(&ctrans, cd->x()*15.0, 0.0, cd->z()*15.0); 76 al_use_transform(&ctrans); 77 78 cd->draw(&ctrans, look_block_info_); 79 } 80 81 glBindVertexArray(0); 82 83 glDisable(GL_DEPTH_TEST); 84 glDisable(GL_CULL_FACE); 85 86 resManager_->unsetAtlasUniforms(); 87} 88 89void Renderer::drawHud() 90{ 91 if(!setShader(SHADER_ALLEGRO)) 92 NBT_Debug("failed to set allegro shader"); 93 94 if(!resManager_->getAtlas()->getSheet(0)->alBitmap()) 95 NBT_Debug("no sheet bitmap????"); 96 97 al_set_projection_transform(dpy_, &al_proj_transform_); 98 99 ALLEGRO_BITMAP *tex = resManager_->getAtlas()->getSheet(0)->alBitmap(); 100 101 al_draw_bitmap(tex, 0, 0, 0); 102 103 float x = 0.0, y = 0.0; 104 glm::vec3 camera_pos = camera_.getPos(); 105 106 const char *block_name = look_block_info_.state_name != nullptr ? look_block_info_.state_name : "unk"; 107 108 int dw = al_get_display_width(dpy_); 109 int dh = al_get_display_height(dpy_); 110 al_draw_filled_rectangle(0, dh-36, dw/2, dh, al_map_rgba(0,0,0,200)); 111 112 al_draw_textf(fnt_, al_map_rgb(255,255,255), 8, dh-26, 0, "Block: bi:%i:%i:%s x:%i, y:%i, z:%i", look_block_info_.id, look_block_info_.data, block_name, look_block_address_.x, look_block_address_.y, look_block_address_.z); 113 114 al_draw_textf(fnt_, al_map_rgb(255,255,255), 8, dh-12, 0, "Pos: x:%.0f, y:%.0f, z:%.0f", camera_pos.x, camera_pos.y, camera_pos.z); 115 116}

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

mind78
Member #15,802
November 2014

Ok, you are all allowed to mock me relentlessly :D.

I thought something looked a little of with my description there. The thing is...

setupGLstuff();

while(1)
{
setupGLstuffFrame(); <------------ Was missing
drawGLstuff();
restoreALdrawing();
drawALstuff();
al_flip_display();
}

No matter what I put in there, of course it won't work when it doesn't even gets called. Realized just now when I was reading these posts ::)

It works now anyway, I have to reenable the culling for the GL and that was in the part that didn't get called :D

Go to: