Drawing a quad with al_draw_prim not appearing
Edgar Reynaldo

I can't get this code to work. I've tried drawing the same texture 3 different times, and the texture coords are painted on the screen after it, but none of the al_draw_prim calls work. They don't display anything. I know the texture loaded successfully because I display it first.

WorldWrap.7z Binary + src + data

#SelectExpand
1void DrawWorldWrapped() { 2 // this won't work because al_draw_bitmap_region clips the source bitmap 3/// al_draw_bitmap_region(tex , texX , texY , viewwidth , viewheight , viewcenterx - viewwidth/2 , viewcentery - viewheight/2 , 0); 4 5 double texlx = (viewcenterx - viewwidth/2)/(double)texwidth; 6 double texrx = (viewcenterx + viewwidth/2)/(double)texwidth; 7 double texty = (viewcentery - viewheight/2)/(double)texheight; 8 double texby = (viewcentery + viewheight/2)/(double)texheight; 9 10 ALLEGRO_COLOR white = al_map_rgb_f(1,1,1); 11/** 12 ALLEGRO_VERTEX quad[4] = {// counterclockwise from top left 13 MakeAllegroVertex(viewdrawx , viewdrawy , 0.0 , white , texlx , texty), 14 MakeAllegroVertex(viewdrawx , viewdrawy + viewheight , 0.0 , white , texlx , texby), 15 MakeAllegroVertex(viewdrawx + viewwidth , viewdrawy + viewheight , 0.0 , white , texrx , texby), 16 MakeAllegroVertex(viewdrawx + viewwidth , viewdrawy , 0.0 , white , texrx , texty) 17 }; 18//*/ 19/** 20 ALLEGRO_VERTEX quad2[4] = {// clockwise from top left 21 MakeAllegroVertex(viewdrawx , viewdrawy , 0.0 , white , texlx , texty), 22 MakeAllegroVertex(viewdrawx + viewwidth , viewdrawy , 0.0 , white , texrx , texty), 23 MakeAllegroVertex(viewdrawx + viewwidth , viewdrawy + viewheight , 0.0 , white , texrx , texby), 24 MakeAllegroVertex(viewdrawx , viewdrawy + viewheight , 0.0 , white , texlx , texby) 25 }; 26//*/ 27//* 28 ALLEGRO_VERTEX quad[4] = {// counterclockwise from top left 29 MakeAllegroVertex(0 , 0 , 0.0 , white , texlx , texty), 30 MakeAllegroVertex(0 , 300 , 0.0 , white , texlx , texby), 31 MakeAllegroVertex(400 , 300 , 0.0 , white , texrx , texby), 32 MakeAllegroVertex(400 , 0 , 0.0 , white , texrx , texty) 33 }; 34//*/ 35//* 36 ALLEGRO_VERTEX quad2[4] = {// counterclockwise from top left 37 MakeAllegroVertex(400 , 300 , 0.0 , white , texlx , texty), 38 MakeAllegroVertex(400 , 600 , 0.0 , white , texlx , texby), 39 MakeAllegroVertex(800 , 600 , 0.0 , white , texrx , texby), 40 MakeAllegroVertex(800 , 300 , 0.0 , white , texrx , texty) 41 }; 42//*/ 43//** 44 ALLEGRO_VERTEX quad3[4] = {// clockwise from top left 45 MakeAllegroVertex(viewdrawx , viewdrawy , 0.0 , white , texlx , texty), 46 MakeAllegroVertex(viewdrawx + viewwidth , viewdrawy , 0.0 , white , texrx , texty), 47 MakeAllegroVertex(viewdrawx + viewwidth , viewdrawy + viewheight , 0.0 , white , texrx , texby), 48 MakeAllegroVertex(viewdrawx , viewdrawy + viewheight , 0.0 , white , texlx , texby) 49 }; 50//*/ 51 al_draw_prim(quad , NULL , tex , 0 , 3 , ALLEGRO_PRIM_TRIANGLE_FAN); 52 al_draw_prim(quad2 , NULL , tex , 0 , 3 , ALLEGRO_PRIM_TRIANGLE_FAN); 53 al_draw_prim(quad3 , NULL , tex , 0 , 3 , ALLEGRO_PRIM_TRIANGLE_FAN); 54 55//void al_draw_textf(const ALLEGRO_FONT *font, ALLEGRO_COLOR color, 56// float x, float y, int flags, 57// const char *format, ...) 58 59 al_draw_textf(font , white , 50 , 50 , 0 , "lx = %f , rx = %f" , texlx , texrx); 60 al_draw_textf(font , white , 50 , 100 , 0 , "ty = %f , by = %f" , texty , texby); 61 62/// al_draw_textf(font , white , 50 , 150 , 0 , "lx = %lf , rx = %lf" , texlx , texrx); 63/// al_draw_textf(font , white , 50 , 200 , 0 , "ty = %lf , by = %lf" , texty , texby); 64 65 al_draw_textf(font , white , 50 , 250 , 0 , "vlx = %i , vrx = %i" , viewdrawx , viewdrawx + viewwidth); 66 al_draw_textf(font , white , 50 , 300 , 0 , "vty = %i , vby = %i" , viewdrawy , viewdrawy + viewheight); 67 68}

Arthur Kalliokoski

I see this
{"name":"608139","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/a\/6a37e65e5fb247256b1938b38027cf3d.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/6\/a\/6a37e65e5fb247256b1938b38027cf3d"}608139

What's it supposed to look like?

BTW, you forgot to include mybackground.png, so I substituted a random image.

[EDIT]

Upon further examination, the x's and y's are the same, so you're only showing one pixel of Map.BMP, which is black.

Waiting for charter.com to reconnect so I can hit the submit button. >:(
Maybe the new roomie's laptop is riddled with spyware and it's overloading the data cap with spam emails or something.

[EDIT 2]

I added a new vertex, and copied the first to the 5th and now it shows a rectangle, scrolling along with the arrow keys eventually changes the color due to enormous pixels much larger than the window, fiddling with the numbers hasn't helped yet.

{"name":"608140","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/3\/d3155a721a90088e2d470545b2a427fe.png","w":800,"h":600,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/d\/3\/d3155a721a90088e2d470545b2a427fe"}608140

Edgar Reynaldo

Sorry, forgot to specify, you can specify. :P

Usage :
WorldWrap.exe ImageFileName.png

It's supposed to be drawing 3 quads, one in the top left quadrant, one in the bottom right, and one in the center. It doesn't draw anything for me.

Oh, sorry, didn't update the binary. Gotta wait til I get home now.

Arthur Kalliokoski

Use an image program to provide a crude example of what that would look like, please. I'm multitasking too hard and my dual-cell brain is thrashing badly.

SiegeLord
  • u, v need to be in pixels.

  • end argument is last valid index + 1 (i.e. 4 not 3).

  • Your bitmap needs to have its dimensions be a power of 2.

Edgar Reynaldo

Has to be a power of 2? So how would I wrap a texture around that is not a power of 2?

Arthur Kalliokoski

how would I wrap a texture around that is not a power of 2?

You mean how to force a texture to be a power of 2? Either stretch it with a good graphic program to the desired dimensions, or copy the image and paste it to a larger image that does have power of two dimensions.

Edgar Reynaldo

But I don't want it to be POT. Then I would have to scale it. The whole idea is that it can be any size and you can wrap the edges for a continuously scrolling background.

SiegeLord

The answer then is a shader or abandoning al_draw_prim and just calling al_draw_bitmap_region 4 times (assuming your bitmap is larger than the screen).

Edgar Reynaldo

Okay, I fixed what you said, and it's drawing now, but allegro is using a POT texture with zero alpha in the gaps and wrapping that. My source image is 1024x768 and the texture is 1024x1024 I believe. How do I avoid scaling? I don't want to change the aspect ratio either. You said draw 4 bitmaps, what if the view size is larger than the wrapped background? I might have to draw more. Isn't there an easy way to tile?

And I don't really know how to write a shader. Any good tutorials? Websites?

WorldWrap.7z Bin + src + dat

WorldWrap.exe HarperVR2.BMP

{"name":"608151","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/f\/8f76d9ce132caae530147f4db5ba57a4.png","w":812,"h":632,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/8\/f\/8f76d9ce132caae530147f4db5ba57a4"}608151

SiegeLord

It's a simple piece of code to draw a grid of bitmaps to fill the screen.

For shaders you need to use A5.1, obviously. I think just looking at A5 examples will be sufficient for such a simple shader. I'd just grab the default ones and modify them a bit (untested):

#SelectExpand
1static const char *glsl_pixel_source = 2 #ifndef ALLEGRO_CFG_OPENGLES 3 "#version 120\n" /* Must be first so we can't use #ifndef GL_ES. */ 4 #endif 5 "#ifdef GL_ES\n" 6 "precision mediump float;\n" 7 "#endif\n" 8 "uniform sampler2D " ALLEGRO_SHADER_VAR_TEX ";\n" 9 "uniform bool " ALLEGRO_SHADER_VAR_USE_TEX ";\n" 10 "varying vec4 varying_color;\n" 11 "varying vec2 varying_texcoord;\n"
12 "uniform vec2 bitmap_size_frac;\n"
13 "void main()\n" 14 "{\n" 15 " if (" ALLEGRO_SHADER_VAR_USE_TEX ")\n"
16 " gl_FragColor = varying_color * texture2D(" ALLEGRO_SHADER_VAR_TEX ", fract(varying_texcoord / bitmap_size_frac) * bitmap_size_frac );\n"
17 " else\n" 18 " gl_FragColor = varying_color;\n" 19 "}\n"; 20 21static const char *hlsl_pixel_source = 22 "bool " ALLEGRO_SHADER_VAR_USE_TEX ";\n" 23 "texture " ALLEGRO_SHADER_VAR_TEX ";\n" 24 "sampler2D s = sampler_state {\n" 25 " texture = <" ALLEGRO_SHADER_VAR_TEX ">;\n" 26 "};\n"
27 "float2 bitmap_size_frac; \n"
28 "\n" 29 "float4 ps_main(VS_OUTPUT Input) : COLOR0\n" 30 "{\n" 31 " if (" ALLEGRO_SHADER_VAR_USE_TEX ") {\n"
32 " return Input.Color * tex2D(s, frac(Input.TexCoord / bitmap_size_frac) * bitmap_size_frac);\n"
33 " }\n" 34 " else {\n" 35 " return Input.Color;\n" 36 " }\n" 37 "}\n";

Then you'll need to set the bitmap_size_frac variable in your code like so:

int true_w, true_h;
al_get_opengl_texture_size(tex, &true_w, &true_h);
float bitmap_size_frac[2] = {(float)al_get_bitmap_width(tex) / true_w, (float)al_get_bitmap_height(tex) / true_h};
al_set_shader_float_vector("bitmap_size_frac", 2, bitmap_size_frac, 1);

Thread #613603. Printed from Allegro.cc