forums.ps2dev.org Forum Index forums.ps2dev.org
Homebrew PS2, PSP & PS3 Development Discussions
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Adjust alpha values in an image

 
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development
View previous topic :: View next topic  
Author Message
AlphaDingDong



Joined: 21 Mar 2008
Posts: 29
Location: The interwebs

PostPosted: Sat Apr 19, 2008 7:50 am    Post subject: Adjust alpha values in an image Reply with quote

Okay. I've got everything installed and set up. I've completed the tutorials at psp-programming.com and all the C I learned in college is starting to trickle back into my brain after 10 long years of neglect. I've created a small demo project to start myself out and it's going well. I've been easily able to look up any information I've needed either on google or in one of my programming books from college.

However, I do seem to be stuck one one small detail.

I've been using a graphics package that I downloaded for lesson 4 of the tutorials (link) and it seems to handle alpha values. I'm only working with .png images, using zlib and libpng from this site's svn server.

svn://svn.pspdev.org/psp/trunk/zlib
svn://svn.pspdev.org/psp/trunk/libpng

There's a function in it to draw text to an image and of course a function to alpha-blit an image to the screen.

What I'm trying to do is make some text that will slowly fade out. I figure if I create an empty image and draw the text to it and then lower the image's alpha by 1 each frame until it hits 0 then it should look pretty nice and use less cpu than redrawing the text every frame (the alpha for the text drawing doesn't seem to work anyway).

The problem is that I don't know how to adjust the alpha values of an image. I've looked through graphics.c a few times and I can't make head nor tail of the blit functions.

Could anyone give me some advice? I would have thought I could just sweep through the image with some nested while()'s and set the alpha for each pixel. Can this be done?

Many thanks for everyone's time.
Back to top
View user's profile Send private message
Insert_witty_name



Joined: 10 May 2006
Posts: 376

PostPosted: Sat Apr 19, 2008 8:17 am    Post subject: Reply with quote

Hi ADD.

What you need to do cannot be done using the graphics.c file from that tutorial.

I would suggest starting to learn GU.

Using GU, all you need to do is change a few init params.

Code:
sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
sceGuBlendFunc(GU_ADD, GU_SRC_ALPHA, GU_FIX, 0xffffffff, 0xffffffff);


Then when you build your vert array, ensure it has a color member, and use this to set the alpha you need.

Code:
vertices[0].color = GU_RGBA(255, 255, 255, alpha);


Then when you draw using something like:

Code:
sceGuDrawArray(GU_SPRITES, GU_COLOR_8888|GU_TEXTURE_32BITF|GU_VERTEX_32BITF|GU_TRANSFORM_2D, 2, 0, vertices);


The texture will appear with alpha that you have specified.

Looping through the texture and altering it's data to give you the required alpha is not good practice.

There are plenty of GU samples in the SDK, and even a set of 11 simple tutorials aimed at a beginner here: http://www.psp-programming.com/code/doku.php?id=c:tutorials

I really would recommend to learn GU, instead of using the graphics.c supplied in the tutorial.

Hope this helps.
Back to top
View user's profile Send private message
AlphaDingDong



Joined: 21 Mar 2008
Posts: 29
Location: The interwebs

PostPosted: Sat Apr 19, 2008 1:56 pm    Post subject: Reply with quote

Awesome. I will definitely check out the tuts there. Thanks, man. I did sort of have an uneasy feeling about manually adjusting the pixels, but I figured it would be okay since the image was so small. Much better if the GU can handle it, though.

Thanks again.
Back to top
View user's profile Send private message
moonlight



Joined: 26 Oct 2005
Posts: 567

PostPosted: Sat Apr 19, 2008 6:37 pm    Post subject: Reply with quote

Alternatively, you can use a simple sceGuColor, that is what sce tends to do (for whatever reason, they never set the color in vertex).

The blitAlphaImageToScree function with the parameter "alpha" added to the end would be like:

Code:
void blitAlphaImageToScreen(int sx, int sy, int width, int height, Image* source, int dx, int dy, u32 alpha)
{
   if (!initialized) return;

   sceKernelDcacheWritebackInvalidateAll();
   guStart();
   sceGuColor((alpha << 24) | 0xFFFFFF);
   sceGuTexFunc(GU_TFX_MODULATE, GU_TCC_RGBA);
   sceGuTexImage(0, source->textureWidth, source->textureHeight, source->textureWidth, (void*) source->data);
   float u = 1.0f / ((float)source->textureWidth);
   float v = 1.0f / ((float)source->textureHeight);
   sceGuTexScale(u, v);
   
   int j = 0;
   while (j < width) {
      Vertex* vertices = (Vertex*) sceGuGetMemory(2 * sizeof(Vertex));
      int sliceWidth = 64;
      if (j + sliceWidth > width) sliceWidth = width - j;
      vertices[0].u = sx + j;
      vertices[0].v = sy;
      vertices[0].x = dx + j;
      vertices[0].y = dy;
      vertices[0].z = 0;
      vertices[1].u = sx + j + sliceWidth;
      vertices[1].v = sy + height;
      vertices[1].x = dx + j + sliceWidth;
      vertices[1].y = dy + height;
      vertices[1].z = 0;
      sceGuDrawArray(GU_SPRITES, GU_TEXTURE_16BIT | GU_VERTEX_16BIT | GU_TRANSFORM_2D, 2, 0, vertices);
      j += sliceWidth;
   }
   
   sceGuFinish();
   sceGuSync(0, 0);
}


And a main code sample:

Code:
#include <pspsdk.h>
#include <pspdisplay.h>
#include <pspgu.h>
#include "graphics.h"

PSP_MODULE_INFO("ModuleName", 0, 1, 0);
PSP_MAIN_THREAD_ATTR(PSP_THREAD_ATTR_USER);

int main()
{
   initGraphics();

   Image* image = createImage(200, 8);
   clearImage(0xFF000000, image);

   printTextImage(0, 0, "This is a red text", 0xFF0000FF, image);

   int alpha = 0xFF;

   while (1)
   {
      clearScreen(0);
      blitAlphaImageToScreen(0, 0, 200, 8, image, 180, 100, alpha&0xFF);
      sceDisplayWaitVblankStart();
      flipScreen();

      if (--alpha < 0)
         alpha = 0xFF;
   }


   return 0;
}
Back to top
View user's profile Send private message
Art



Joined: 09 Nov 2005
Posts: 647

PostPosted: Sun Apr 20, 2008 9:04 pm    Post subject: Reply with quote

Thanks for that moonlight :) It might be handy.
Now AlphaDingDong will have to change his nick to: (the next problem he has)DingDong.
_________________
If not actually, then potentially.
Back to top
View user's profile Send private message
AlphaDingDong



Joined: 21 Mar 2008
Posts: 29
Location: The interwebs

PostPosted: Wed Apr 23, 2008 1:39 pm    Post subject: Reply with quote

XD

Thanks, guys. It's been a busy week, but I'm sitting down in a few minutes to start learning GU and get cookin' on this.

Oh, and lol (@art). :)
Back to top
View user's profile Send private message
AlphaDingDong



Joined: 21 Mar 2008
Posts: 29
Location: The interwebs

PostPosted: Fri Apr 25, 2008 11:59 am    Post subject: Reply with quote

Sorry to double but it's been a couple days.

GU Lesson 1 - Things I don't understand:

1) There are some variables declared like so:

u32 tickResolution;
u64 fpsTickNow;

Is this unsigned ints of 32 bits and 64 bits respectively?

2) The vertex structs are created with this thingy in the declaration:

__attribute__((aligned(16)))

The tut specifies that this means the data will be 16 bit aligned. Does that mean that the properties of the struct will be stored in 16 bits even if their value only needs 8 bits? IOW, would 0x2F be stored as 0x002F? All hex hacking of RAM for video games I've ever done has had variables stored in this way, so I'm guessing that's what it means.

3) I understand the properties of the vertex struct. It a simply stores RGBA and XYZ. However, the tut author used a notation I'm not familiar with: 1.0f. I'm assuming this means float 1.0, but is that really necessary, since the XYZ are already floats, and how does it work with the RGBA being unsigned ints?

4) I noticed that in both vertex arrays the author used an alpha value of 0.0f, but the objects still appeared onscreen. I thought the alpha may be reversed, so I set it to 1.0f but there was no change. Then I thought maybe it only applies to colors, so I changed to color to red, but with the alpha at 1.0f and 0.0f there was no difference. Am I missing something here?

5) Apparently sceGumDrawArray() needs one of the following types:

Point, Line, Line Strip, Triangles, Triangle Strip, Triangle Fan or Sprites.

What is the difference between these types? I messed around with them a little, but I don't really get what each one does specifically. I _think_ triangle strip uses the first 2 points to create a line, then the next point makes a second line and the third line is automatically between the start and endpoint of the 2 lines, then the next point makes a new line and the whole thing sort of gets shifted down so that line 2 from the previous triangle becomes line 1 and the new line becomes line 2, etc. Whereas I _think_ triangle fan shifts the points instead of the lines such that 2 points is a line and then the third point makes a triangle but then the next point makes the old point 3 become point 2 but keeps the same point 1....

Holy crap, did that make any sense at all?

I think that's all for now. I'm gonna move on to further lessons and see if it makes any more sense. For now all I can do is screw with this guy's code and make my triangle and square do some really uncomfortable things (for polygons). I get really uneasy when I don't understand 80% of the lines in a program that's supposedly mine. :(

Help?

P.S. -- I just realized that inadvertently used my wall-of-text attack there asking for favors and I know none of you owe me a damn thing. Sorry about that.

I'm really eager to learn this stuff because I've always only dealt with 2D sprite-based apps in the past and usually in scripting languages on PC platforms. Now I find myself suddenly able to code for an actual game-device, and not only that, but the possibility of learning to work with 3D models!

So I just want to say that I really do appreciate any and all help that I get from anyone here and I appreciate that there's a place where I can go to ask these questions. Thanks, y'all. Sorry if I get ahead of myself and seem presumptuous.
Back to top
View user's profile Send private message
Tinnus



Joined: 29 Jul 2006
Posts: 67

PostPosted: Fri Apr 25, 2008 12:48 pm    Post subject: Reply with quote

You might try taking a look at the NeHe OpenGL tutorials ( nehe.gamedev.net ) or something to learn more about the basics of 3D programming, like those primitive types. Just ignore the OpenGL bits (or better yet, read them too so you learn even more :) )
_________________
Let's see what the PSP reserves... well, I'd say anything is better than Palm OS.
Back to top
View user's profile Send private message
AlphaDingDong



Joined: 21 Mar 2008
Posts: 29
Location: The interwebs

PostPosted: Fri Apr 25, 2008 3:18 pm    Post subject: Reply with quote

I'll take a look. Thanks. Know anything about that other stuff?
Back to top
View user's profile Send private message
Jim



Joined: 02 Jul 2005
Posts: 487
Location: Sydney

PostPosted: Fri Apr 25, 2008 3:32 pm    Post subject: Reply with quote

Quote:
Sorry to double but it's been a couple days.

GU Lesson 1 - Things I don't understand:

1) There are some variables declared like so:

u32 tickResolution;
u64 fpsTickNow;

Is this unsigned ints of 32 bits and 64 bits respectively?

Yes.

Quote:

2) The vertex structs are created with this thingy in the declaration:

__attribute__((aligned(16)))

The tut specifies that this means the data will be 16 bit aligned. Does that mean that the properties of the struct will be stored in 16 bits even if their value only needs 8 bits? IOW, would 0x2F be stored as 0x002F? All hex hacking of RAM for video games I've ever done has had variables stored in this way, so I'm guessing that's what it means.

It just changes the address the object is stored at to be a multiple of 16 BYTES not bits. It doesn't change the size of the object. Some of the PSP custom hardware needs this address alignment.

Quote:

3) I understand the properties of the vertex struct. It a simply stores RGBA and XYZ. However, the tut author used a notation I'm not familiar with: 1.0f. I'm assuming this means float 1.0, but is that really necessary, since the XYZ are already floats, and how does it work with the RGBA being unsigned ints?

1.0f is a float, 1.0 is a double. floats are way faster than doubles because they are handled in hardware, and they 32bit instead of 64

Quote:

4) I noticed that in both vertex arrays the author used an alpha value of 0.0f, but the objects still appeared onscreen. I thought the alpha may be reversed, so I set it to 1.0f but there was no change. Then I thought maybe it only applies to colors, so I changed to color to red, but with the alpha at 1.0f and 0.0f there was no difference. Am I missing something here?

If alpha blending is turned off it doesn't matter the value.

Quote:

5) Apparently sceGumDrawArray() needs one of the following types:

Point, Line, Line Strip, Triangles, Triangle Strip, Triangle Fan or Sprites.

What is the difference between these types?

http://books.google.com.au/books?id=dJoDkRK5BJkC&pg=PA60&lpg=PA60&dq=Point,+Line,+Line+Strip,+Triangles,+Triangle+Strip,+Triangle+Fan&source=web&ots=du-8uaB_aC&sig=aXKZpBxtYw0lQZxYU_N4Q8ZIcgE&hl=en#PPA62,M1

Jim[/url]
_________________
http://www.dbfinteractive.com
Back to top
View user's profile Send private message Visit poster's website
sallyxi



Joined: 25 Apr 2008
Posts: 3

PostPosted: Fri Apr 25, 2008 10:02 pm    Post subject: Reply with quote

Any guys who can display the way to solve it will be highly appeciated.
Back to top
View user's profile Send private message Visit poster's website
AlphaDingDong



Joined: 21 Mar 2008
Posts: 29
Location: The interwebs

PostPosted: Sun Apr 27, 2008 1:11 pm    Post subject: Reply with quote

Thanks again, Jim.

I've figured out the primitives now (except the sprite one but I'll get there). My brain hurts at the idea of coding something similar to Crisis Core or another game like it. X|

lol

I think I'll try to figure out the rest of this stuff a piece at a time and then once I've got a bit of a grasp on it I'll start messing with sprite primitives.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.ps2dev.org Forum Index -> PSP Development All times are GMT + 10 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group