Skip to main content
GameDev.net gamedev.net
🔒 Locked

Loading a texture into a GLbyte array

Started by rubberkidney Mar 19, 2003 at 4:27 AM 3 replies 650+ views
Original Post
rubberkidney
rubberkidney
Im loading a texture into a GLbyte array the code for that is: int index = 0; for( int i = 0; i < h; i++ ) { int k = 0; for( int j = 0; j < w; j+=4 ) { tmp = texture.GetPixel( i, k ); img[index + 0] = tmp.r; img[index + 1] = tmp.g; img[index + 2] = tmp.b; img[index + 3] = tmp.alpha; index += 4; k++; } } when I get to glTexImage2D I get an access error, im not sure why, the only reason that i can think of is that im loading it into the array incorrectly...my call to glTexImage2D looks like: glTexImage2D( GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, img ); any ideas? also if someone can tell me how to post code blocks that would be helpful : )
rubberkidney
rubberkidney
Also, I was going steping through the loop and when it is setting the values in img[index + 0] = tmp.r; the value for tmp.r will be something like 244 but the copied value is usually a negative number like -23, dont know if that has something to do with it
kronq
kronq
Your ''i'' is stepping through height, while your ''k'' is stepping through width. So ''i'' corresponds to the ''y'' (or t) axis and ''k'' corresponds to ''x'' (or s), but you do GetPixel( i, k ) which corresponds to GetPixel( y, x ). Should this be GetPixel( k, i )? That could cause an access violation if you fetch outside the bounds of your texture.

Also, your ''j'' loop only goes w/4 times due to j+=4 -- I suspect this is not what you meant to do... You could do j++ and eliminate ''k''
rubberkidney
rubberkidney
I cant just get rid of the k because of the way it needs to be incremented, if i just used j then it would be read incorrectly because i need to get the pixel value at x,y and i also need to set the next 4 elements of the array within that loop, so i have to increment it by 4, if im only incrementing j by one, not only am i writing over values that have already been set, im also grabbing every forth pixel, instead of the next one. If anyone has any other ideas please let me know! This is part of the last project for my class. Thanks
kronq
kronq
quote:
Original post by rubberkidney
so i have to increment it by 4, if im only incrementing j by one, not only am i writing over values that have already been set, im also grabbing every forth pixel, instead of the next one.


your 'index += 4' takes care of properly incrementing the output buffer.

In your current 'j' loop you are only processing the first 1/4 of the pixels in the y direction.

For a quick proof, cut and paste this code:

for ( int index = 0, i = 0; i < h; i++ )
{
for ( int j = 0; j < w; j++, index+=4 )
{
tmp = texture.GetPixel( j, i );
img[index+0] = tmp.r;
img[index+1] = tmp.g;
img[index+2] = tmp.b;
img[index+3] = tmp.alpha;
}
}


[edited by - kronq on March 20, 2003 6:40:32 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.