Archived

This topic is now archived and is closed to further replies.

Briskon

Lesson 36

Recommended Posts

Briskon    122
Lesson 36: In the EmptyTexture function, the following line is used to allocate space for an empty texture of size 128*128 width 4 color components: data = (unsigned int*)new GLuint[((128*128) * 4 * sizeof(unsigned int))]; Can someone tell me why "4*sizeof(unsigned int)" is used? Shouldn''t the new operator automatically allocate "4*sizeof(unsigned int)" bytes of memory per pixel when GLuint is specified? Or is it an error?

Share this post


Link to post
Share on other sites
Hyren    122
Briskon:

You''re right in that the new operator should automatically include the "sizeof(unsigned int)". However, you still need the 4 to allocate space for the 4 color components of the texture (RGBA). As it is it should allocated 4 times as much space as is needed (assuming that an unsigned int is 4 bytes). Take out the "sizeof(unsigned int)" and it should work just fine.

BGCJR:
If we were using malloc, you''d be correct. However, since lesson 36 uses the new operator, we don''t need to specify the number of bytes, only the number of elements in the array.

My guess is that the code may have been written in C (using malloc) orginally and that clause was simply missed when converting to C++ memory operations.



"Back to the code mines... ka-chink... ka-chink..."
Tachyon Digital - Down for the summer, be back in the fall.

Share this post


Link to post
Share on other sites
Briskon    122
Are you sure you need to use the "4" for the RGBA components?

Ie. How come you need four ints per component when the max a component can have is one byte? Therefore 4 bytes for a whole pixel which is one int.

Try and make myself clearer:
"new GLuint[128*128]" should allocate enough memory for 128*128 pixels, each with four components (RGBA). So why the need for the 4?



Share this post


Link to post
Share on other sites