Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Implementing a simple skybox.

This thread is locked; no one can reply to it. rss feed Print
Implementing a simple skybox.
nshade
Member #4,372
February 2004

So the game I'm porting originally was written for a resolution of 320x200 on a 4:3 display. (The pixels were not square). I've given the game a 4x resolution boost. Now it runs at 1280x800 at a 16:10 aspect ratio now that the pixels are square. I'm beginning to add the ability to run the game native (1280x800 with a border), Zoomed(Zoom to edge and Maintain Aspect Ratio), and corner-to-corner (stretch/squish).

So now I have this big blank border to deal with. For example, My display is 1920x1080. When I run the game in "Native" 1280x800, I have a thick black border around the outside. When I run zoomed, my display is 16:9 so I have black pillars on the right and left.

I decided to fill the border with a starfield (It fits the theme of the game). I originally was going to put a scrolling tiled background, but I found a starscape generator that makes skyboxes.

Is there a very quick and dirty skybox example written in C? I doesn't even need to use OpenGL (I'd actually rather it didn't) I just need it to lazily rotate around it's center. I'm going to be pasting a 2D picture over the top of it anyway.

It kind of has to be in C as that's the language the original game was written in. Anything using vectors or just STL in general is right out. I see lots of OpenGL examples, but they are all using C++.

I don't need you guys to code up something for me, just a push in the right direction.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Several of us have been through this. Skyboxes are actually rather simple, as they are just a cube rendered centered on the player with textures.

Make a cube. Texture it. Transform it. Draw it.

You can do this with allegro and al_draw_prim or OpenGL directly.

nshade
Member #4,372
February 2004

Hmm, this is trickier than I thought...

I was able to draw a triangle using the documentation example. However, When I tried to make a quad, looks like al_draw_prim() only likes triangles. That's cool. however when I started to monkey with the z axis, my triangle disappeared.

ALLEGRO_VERTEX has a warning:
"Note that at this time, the software driver for this addon cannot render 3D primitives. "

How do I know which driver I'm using and how do I switch them if this is the case?

Also, as a extra bonus. I don't know how to do math very well. Anything diving into geometry or algebra and I'm lost. (I haven't been in high school in many, many years) so I'm not sure how transforms are going to work.

==EDIT==

Never mind, I'm aping the code from ex_camera.c I'm adding a floor to that skybox, keeping the camera roll/yaw/pitch and nuking the rest :)

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

As long as you're drawing on a video bitmap it should be accelerated.

nshade said:

Also, as a extra bonus. I don't know how to do math very well. Anything diving into geometry or algebra and I'm lost. (I haven't been in high school in many, many years) so I'm not sure how transforms are going to work.

You're going to need to learn math. You just need to. It's not so scary, honest.

Transforms allow you to move, scale, and rotate your objects in space.

They're disappearing because of the default projection matrix, which is orthogonal 2D. You need to setup your own 3D matrix for the projection and the view. After that you push and pop transforms on and off the stack for the view transform to make your models appear in different sizes, positions, and orientations.

No, Allegro doesn't support rendering QUADs. They're just two triangles to be honest. Use a TRIANGLE_FAN and pass the 4 vertices of the quad in counter clockwise order to al_draw_prim or OpenGL.

nshade
Member #4,372
February 2004

The camera example was exactly what I needed. It also in code shows me the proper matrix set up and a few useful routines. It's actually a little too verbose, but makes removing the unneeded cruft much easier.

However, I do have an issue... I seem to have seams in my box. The corners are matching up so It's not a texture issue. any ideas on how to solve something like this?

I have an example pic.....

https://i.imgur.com/Epz83mj.png

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

nshade
Member #4,372
February 2004

I stole the code form the camera example. There are functions that add vertexes to a vertex array (starfield.v). You can either do them on a per quad or per vertex basis.

The original skybox didn't have a proper top or bottom so I had to manually put the vertexes in myself. The relevant code is add_skybox() and draw_scene()

the coordinates are kind of all over the place. I may have to manually remake the cube

This are lots of unneeded variables in here I have yet to zero out or simplify. I'll be cleaning those up later.

#SelectExpand
1/* Create a skybox. This is simply 5 quads with a fixed distance to the 2* camera. 3*/ 4static void add_skybox(void) 5{ 6 Vector pos = starfield.camera.position; 7 ALLEGRO_COLOR color1 = al_color_name("black"); 8 ALLEGRO_COLOR color2 = al_color_name("blue"); 9 ALLEGRO_COLOR color3 = al_color_name("white"); 10 11 double tex_size = 0; 12 if (starfield.front) { 13 tex_size = al_get_bitmap_width(starfield.front); 14 color1 = color2 = color3; 15 } 16 17 // x1,y1,z1,u1,v1 18 // x2,y2,z2,u2,v2 19 // x3,y3,z3,u3,v3 20 // color tri, color tri2 21 22 /* Front skybox wall. */ 23 add_quad(-50, -50, -50, tex_size, 0, 24 100, 0, 0, -tex_size, 0, 25 0, 100, 0, 0, -tex_size, 26 color1, color2); 27 28 29 /* Right skybox wall. */ 30 add_quad(50, -50, -50, tex_size, 0, 31 0, 0, 100, -tex_size, 0, 32 0, 100, 0, 0, -tex_size, 33 color1, color2); 34 35 /* Back skybox wall. */ 36 add_quad(-50, -50, 50, 0, tex_size, 37 100, 0, 0, tex_size, 0, 38 0, 100, 0, 0, -tex_size, 39 color1, color2); 40 41 /* Left skybox wall. */ 42 add_quad(-50, -50, -50, 0, tex_size, 43 0, 0, 100, tex_size, 0, 44 0, 100, 0, 0, -tex_size, 45 color1, color2); 46 47 //These were added on a per-vertex basis 48 //x,y,z,u,v,color 49 50 /* Top skybox wall. */ 51 add_vertex(-50, 50, -50, 0, 0, color2); 52 add_vertex(50, 50, -50, -tex_size, 0, color2); 53 add_vertex(50, 50, 50, -tex_size, -tex_size, color2); 54 55 add_vertex(50, 50, 50, -tex_size, -tex_size, color2); 56 add_vertex(-50, 50, 50, 0, -tex_size, color2); 57 add_vertex(-50, 50, -50, 0, 0, color2); 58 59 60 /* Bottom skybox wall. */ 61 62 add_vertex(-50, -50, 50, 0, 0, color2); 63 add_vertex(50, -50, 50, -tex_size, -0, color2); 64 add_vertex(50, -50, -50, -tex_size, -tex_size, color2); 65 66 add_vertex(50, -50, -50, -tex_size, -tex_size, color2); 67 add_vertex(-50, -50, -50, 0, -tex_size, color2); 68 add_vertex(-50, -50, 50, 0, 0, color2); 69 70 71} 72 73static void draw_scene(void) 74{ 75 Camera *c = &starfield.camera; 76 /* We save Allegro's projection so we can restore it for drawing text. */ 77 ALLEGRO_TRANSFORM projection = *al_get_current_projection_transform(); 78 ALLEGRO_TRANSFORM t; 79 ALLEGRO_COLOR back = al_color_name("black"); 80 ALLEGRO_COLOR front = al_color_name("white"); 81 int th; 82 double pitch, yaw, roll; 83 84 setup_3d_projection(); 85 al_clear_to_color(back); 86 87 /* We use a depth buffer. */ 88 al_set_render_state(ALLEGRO_DEPTH_TEST, 1); 89 al_clear_depth_buffer(1); 90 91 /* Recreate the entire scene geometry - this is only a very small example 92 * so this is fine. 93 */ 94 starfield.n = 0; 95 96 add_skybox(); 97 98 /* Construct a transform corresponding to our camera. This is an inverse 99 * translation by the camera position, followed by an inverse rotation 100 * from the camera orientation. 101 */ 102 al_build_camera_transform(&t, 103 starfield.camera.position.x, starfield.camera.position.y, starfield.camera.position.z, 104 starfield.camera.position.x - starfield.camera.zaxis.x, 105 starfield.camera.position.y - starfield.camera.zaxis.y, 106 starfield.camera.position.z - starfield.camera.zaxis.z, 107 starfield.camera.yaxis.x, starfield.camera.yaxis.y, starfield.camera.yaxis.z); 108 al_use_transform(&t); 109 110 al_draw_prim(starfield.v, NULL, starfield.front, 0, 3, ALLEGRO_PRIM_TRIANGLE_LIST); //front 111 al_draw_prim(starfield.v, NULL, starfield.front, 3, 6, ALLEGRO_PRIM_TRIANGLE_LIST); //front 112 al_draw_prim(starfield.v, NULL, starfield.right, 6, 9, ALLEGRO_PRIM_TRIANGLE_LIST); //right 113 al_draw_prim(starfield.v, NULL, starfield.right, 9, 12, ALLEGRO_PRIM_TRIANGLE_LIST); //right 114 al_draw_prim(starfield.v, NULL, starfield.back, 12, 15, ALLEGRO_PRIM_TRIANGLE_LIST); //back 115 al_draw_prim(starfield.v, NULL, starfield.back, 15, 18, ALLEGRO_PRIM_TRIANGLE_LIST); //back 116 al_draw_prim(starfield.v, NULL, starfield.left, 18, 21, ALLEGRO_PRIM_TRIANGLE_LIST); //left 117 al_draw_prim(starfield.v, NULL, starfield.left, 21, 24, ALLEGRO_PRIM_TRIANGLE_LIST); //left 118 al_draw_prim(starfield.v, NULL, starfield.up, 24, 27, ALLEGRO_PRIM_TRIANGLE_LIST); //up 119 al_draw_prim(starfield.v, NULL, starfield.up, 27, 30, ALLEGRO_PRIM_TRIANGLE_LIST); //up 120 al_draw_prim(starfield.v, NULL, starfield.down, 30, 33, ALLEGRO_PRIM_TRIANGLE_LIST); //down 121 al_draw_prim(starfield.v, NULL, starfield.down, 33, 36, ALLEGRO_PRIM_TRIANGLE_LIST); //down 122 123 124 /* Restore projection. */ 125 al_identity_transform(&t); 126 al_use_transform(&t); 127 al_use_projection_transform(&projection); 128 al_set_render_state(ALLEGRO_DEPTH_TEST, 0); 129}

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

nshade
Member #4,372
February 2004

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

nshade
Member #4,372
February 2004

Sorry, didn't realize the forums had attachment abilities

I also attached the full modified version of the camera example that I've been tweaking. It's still very much ex_camera.c with junk commented out and some struct modification. It just allows for different textures for the box and you can use an optional border_skybox.png as a test texture.

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

nshade
Member #4,372
February 2004

I found a solution. I just remodeled the skybox with overlapping edges.

Here is a picture of what I'm talking about...
https://i.imgur.com/MJEECz2.png

Go to: