GLBasic forum

Main forum => GLBasic - en => Topic started by: djtoon on 2010-Sep-26

Title: set texture not the power of 2
Post by: djtoon on 2010-Sep-26
is there a way to make a texure for a 3d object not the poer of 2
like 640 333?

1-x
Title: Re: set texture not the power of 2
Post by: Moru on 2010-Sep-26
I believe GLBasic is supposed to handle that internally by converting them to the next higher resolution if needed.
Title: Re: set texture not the power of 2
Post by: Cliff3D on 2010-Sep-26
GLBasic appears not to do that stretching automatically. For example, from the help file:

Code (glbasic) Select
Textures must be 2^n size (width or height must be 2, 4, 8, 16, 32, 65, 128, 256, 512...) Be aware that some models you may get from the internet will not feature this restriction. If this is the case with your models, then you will have to stretch the texture manually to an appropriate size - 64, 128, 256, 512 or 1024 pixels squared - otherwise ugly texture errors will occur.


I have to date been using this routine:

Code (glbasic) Select
// ------------------------------------------------------------- //
// ---  RESIZESPRITE  ---
// ------------------------------------------------------------- //
FUNCTION ResizeSprite: SourceSprite%, TargetSprite%, destWidth%, destHeight%, Smooth%
// Uses "virtual screen" 0
// These values are defined LOCAL:
// SourceSprite%,  TargetSprite%,  Width%,  Height%
//Resets smoothness to TRUE before exit

LOCAL spritewidth%, spriteheight%
GETSPRITESIZE SourceSprite%, spritewidth%, spriteheight%

CREATESCREEN 0, TargetSprite%, destWidth%, destHeight% // create temporary workscreen of desired resolution

USESCREEN 0

//Copy sprite #0 to target sprite and stretch to fit
SMOOTHSHADING Smooth%
STARTPOLY SourceSprite% // Bitmap = No.SourceSprite%
  POLYVECTOR  0,   0,  0,  0, RGB(255, 255, 255) // top left
  POLYVECTOR   0, destHeight%-1,  0, spriteheight%-1, RGB (255, 255, 255) // bottom left
  POLYVECTOR destWidth%-1, destHeight%-1, spritewidth%-1, spriteheight%-1, RGB(255, 255, 255) // bottom right
  POLYVECTOR destWidth%-1,  0, spritewidth%-1,  0, RGB(  255, 255,   255) // top right
ENDPOLY
SMOOTHSHADING TRUE
USESCREEN -1 // switch back to main screen
LOADSPRITE "", SourceSprite% // empty source sprite/release memory/resources
ENDFUNCTION // RESIZESPRITE


to resize sprites as I load them - specifying the size to resize to in main (I plan to include size calculation "any time now" but have not got to it yet).
Title: Re: set texture not the power of 2
Post by: Cliff3D on 2010-Sep-26
There's also the StretchSprite (http://wiki.just-do-i-t.com/index.php?title=STRETCHSPRITE) command. From GLBasic\Samples\Media\Stretchsprite.gbap :

Code (glbasic) Select
// StretchSprite Demo


LOADSPRITE "block.png", 0

DRAWRECT 0,0,320,240, RGB(128,128,128)
ALPHAMODE 100 // Transparent
STRETCHSPRITE 0, 0, 0, 320, 240
ALPHAMODE 0
DRAWSPRITE 0, 320, 240

SHOWSCREEN
MOUSEWAIT
Title: Re: set texture not the power of 2
Post by: djtoon on 2010-Sep-26
well that will deform my orig picture
and i need it to be the same proportion and not diformd

so i guess there isnt any answer
Title: Re: set texture not the power of 2
Post by: Cliff3D on 2010-Sep-26
Quote from: djtoon on 2010-Sep-26
well that will deform my orig picture

Not necessarily. Not if it's to be used as a 3D texture on a 3D object it won't - not in the least.

Quote from: djtoon on 2010-Sep-26and i need it to be the same proportion and not diformd

If you're using it for a 3D texture, it won't be deformed.

Quote from: djtoon on 2010-Sep-26so i guess there isnt any answer

Wrong guess! that isn't how textures are used in 3D.

Take this as an illustration - the attached screen capture is of a woman's head, where my code (listed above) has been used to resize the original 950 * 950 image to 512 * 256, to illustrate for you that the aspect ratio of a bitmap does NOT alter how the image is applied to a 3D model. Note how her lips aren't painted under her left ear, as might be the case if altering the aspect ratio of the bitmap altered how it applied to the model. I'm using UV co-ordinated to apply the image, not planar projection. Are you?

Also - if you don't like one answer, don't assume that's the ONLY answer. Even though I gave two possible solutions for you, others may have different (often better) ideas. I could even be just one lone nut who doesn't understand anything about 3D - have patience, and TRY the solutions you are offerred before declaring them unworkable.

[attachment deleted by admin]
Title: Re: set texture not the power of 2
Post by: djtoon on 2010-Sep-26
this is what im doing
im createing a 3d plane
the size is 312 x241
and loading a texture the same size

when appliing the texture it gets mess'd up
when i ajust the texture to the power or 2 it is ok
Title: Re: set texture not the power of 2
Post by: Cliff3D on 2010-Sep-26
Quote from: djtoon on 2010-Sep-26
this is what im doing
im createing a 3d plane
the size is 312 x241
and loading a texture the same size

I have highlighted the important phrase. the same size? In what way? 3D objects - like a plane - are not scaled in pixels.


Quote from: djtoon on 2010-Sep-26when appliing the texture it gets mess'd up
when i ajust the texture to the power or 2 it is ok

Your texture or sprite must be power of 2 by the time you apply it to a 3D model. Do this either in code - as I have shown - or in advance. That is all. If your plane has uv (tx,ty) co-oprdinates then aspect ratio per se is meaningless. If you have tight detail then you may need to scale up by more than "to the next power of two" in order to retain the quality/consistency of detail. But try it. try resizing a 312 * 241 texture to 512 * 512 or 512 * 256 and apply it - see how it looks. You may be surprised :)

Then try it at 4096 * 4096 and at 64 * 64. Try it with and without smoothing turned on - what your texture is of/like will determine a lot about what is the most efficient route to getting the results you want. But textures must be sized to a power of 2 by the time you use them as a texture on a 3D model.
Title: Re: set texture not the power of 2
Post by: Moru on 2010-Sep-26
Also try 64x512 just for fun :-)
Title: Re: set texture not the power of 2
Post by: Cliff3D on 2010-Sep-26
Quote from: Moru on 2010-Sep-26
Also try 64x512 just for fun :-)

:D

Mischief maker! But yes, it's all worth playing around with a few options - especially when the code to do so has been handed out on a plate :)