PDA

View Full Version : A few Texture-Questions



ReneMiner
13-11-2012, 18:51
I want to create some own UI for my next program using just TBGL because that weird callback-stuff drives me nuts and with TBGL I can see at least some progress if I program something and it gives me the feeling to keep everything under my control.

So I thought of using Sprites, which might display for example a file-list-box with lots of little pictures (folders etc.) and quite some text. Now I wonder how to draw text and smaller bitmaps/tga's onto a texture in memory so I have to redraw just the sprites texture if necessary and display the whole file-list-box as a sprite on screen. It would shorten the time that it needs to draw all buttons, lists, textboxes or whatever if I had them stored on a texture in memory and redraw them just if changed instead of drawing all single elements each and every frame.

I tried already the TBGL_OffscreenRendering-Example but it does not run (shows a small, black window for maybe 100 milliseconds and immediately stops again - so it doesn't answer my questions because I don't see a result).

-Is there a possibility to draw (plain 2d) images and text onto a texture in memory using TBGL?

-Can I add texture-layers? So a material can have maybe up to 8 translucent different texture-layers?

-Can I scroll textures? Or do I have to change UV for all vertices to get texture-scrolling-effect?

Btw. TBGL_Help says for

TBGL_SpriteDrawAt "Draw a sprite at its internal stored position" , guess that's a mistake?

ReneMiner
13-11-2012, 21:01
sorry John,
I'm just a basic-user and I have no idea about c++ and can't help translating or porting something and i don't know how to deal with all that stuff - thats what i need an "include#" or "uses" for, so I don't have to worry about all that complicated stuff.
I intend to create some 3d-app using a basic language and thinBasic appears currently the best what I can get in the whole wide world to get the stuff, that i like to create, done. I tried blitz, dark, pure, small etc. - seems all overloaded or crippled crap to me, and even if it appears that i am the only user that asks questions here - maybe other users don't dare to ask, have no questions or didn't discover thinBasic yet - I want to program something using thinBasic now.
And I was just asking a question how to draw onto a texture in memory since the combination of TBGL_GetWindowBMP, TBGL_MakeTexture and TBGL_SpriteSetTexture is a little cumbersome and might get unwanted stuff - like a self-made mousepointer-sprite or another "always-on-top"- window - displayed on my sprite then.

I tried already using invisible canvas and this way:


pixelBuffer = Canvas_BitmapGet(width, height)
TBGL_MakeTexture pixelBuffer, %TBGL_FILE_BMP, width, height, myBufferID, %TBGL_TEX_LINEAR

but that results in a module-specific error (unsupported Format-Version)

Petr Schreiber
14-11-2012, 16:59
Hi Rene,

you were on right track with FBO - but it is advanced feature.
What you could use is safe render to texture approach, which is intuitively offered by TBGL_RenderToTexture command.

So you basically render what you need (for example nice image with text below) and capture it to texture without anything from this process being necessarily visible on the screen.


Petr

ReneMiner
15-11-2012, 14:53
some other question, but it's about textures too:

help says there would be sample for freeImage-module/library but there isn't. So I can not find out if it's possible to use *.png, *.tiff or even *.jpeg as texture this way
- by mentioning FreeImage, I think they got a new version of it, just downloaded one from Oct 2012 at:
http://freeimage.sourceforge.net/download.html

Now I try using TImage-Module but same issue as described above where I tried to use invisible canvas as a buffer.
Is there a way to load some compressed images and convert them internally to TBGL_usable format somehow?
- at least *.tga or *.png would be great (*.jpeg is not really usable as a texture)

Petr Schreiber
15-11-2012, 17:51
FreeImage works with TBGL without bigger issues, see here (http://www.thinbasic.com/community/showthread.php?10096-FreeImage-TBGL&p=76547&viewfull=1#post76547).


Petr

ReneMiner
15-11-2012, 18:27
thanks once more :)

works fine with compressed .tga and also .jpg.

.png and .tiff load, but were not drawn correctly (I took care of POT)

Petr Schreiber
15-11-2012, 18:43
You could try this experimental unit. Should work for PNGs. How to use?


#include "fileformat_IMAGE.tBasicU"
...

Long lWidth, lHeight
String sTexture = IMAGE_GetRGBA("C:\myImage.bmp", lWidth, lHeight)

TBGL_MakeTexture sTexture, %TBGL_DATA_BGRA, lWidth, lHeight, MyTextureIndex, %TBGL_TEX_LINEAR


I think it returned the image upside down, but that can be easily arranged by texture coordinates.


Petr

ReneMiner
16-11-2012, 10:21
I already built both into my code. Can load almost any texture-files now :)

but I don't like the upside-down aspect - no good idea to flip UV - user might exchange texture later and meshes shall be loaded from other tb-apps also. Can't I just flip the strings-value - is it four bytes per pixel, multiplied by width till the end? Or are there some stopBits?

so I tried like this:



'string strTextureData is actual value in your Function IMAGE_GetRGBA()
' I added this:

Local i as Long
Local sLine(imageHeight) as String
Local lLineLen as Long = 4 * imageWidth

For i = 1 To imageHeight
sLine(i) = RIGHT$(strTextureData, lLineLen)
strTextureData = LEFT$(strTextureData, Len(strTexturedata) - lLineLen)
Next i
For i = 1 To imageHeight
strTextureData = strTextureData + sLine(i)
Next i

Function = strTextureData


but image is still flipped upside down! How comes?

Btw. On Articles-Page of this forum the most recent threads do not appear :( lots of my questions stay unanswered

Petr Schreiber
17-11-2012, 00:08
The solution would be to cut the string to rows of pixels (blocks of length width*4), reverse their order, and join again to mega string.


Petr

ReneMiner
17-11-2012, 00:15
The solution would be to cut the string to rows of pixels (blocks of length width*4), reverse their order, and join again to mega string.


Petr

ain't that what I did?
or do you mean I have to reorder the sLine()-Strings from left to right? Are them bitmaps stored asides?

Michael Clease
18-11-2012, 01:09
Declare Function GdipImageRotateFlip Lib "GDIPLUS.DLL" Alias "GdipImageRotateFlip" ( _
ByVal pImage As DWord _ ' __in GpImage *image
, ByVal rfType As Long _ ' __in RotateFlipType rfType
) As Long ' GpStatus


' enum RotateFlipType


%RotateNoneFlipNone = 0
%Rotate90FlipNone = 1
%Rotate180FlipNone = 2
%Rotate270FlipNone = 3


%RotateNoneFlipX = 4
%Rotate90FlipX = 5
%Rotate180FlipX = 6
%Rotate270FlipX = 7


%RotateNoneFlipY = %Rotate180FlipX
%Rotate90FlipY = %Rotate270FlipX
%Rotate180FlipY = %RotateNoneFlipX
%Rotate270FlipY = %Rotate90FlipX


%RotateNoneFlipXY = %Rotate180FlipNone
%Rotate90FlipXY = %Rotate270FlipNone
%Rotate180FlipXY = %RotateNoneFlipNone
%Rotate270FlipXY = %Rotate90FlipNone


sFile = Ucode$(sFile)
hStatus = GdipLoadImageFromFile(StrPtr(sFile), pImage)
If hStatus = %S_OK Then
hStatus = GdipImageRotateFlip(pImage, %Rotate180FlipY)
If hStatus = %S_OK Then




Hello Rene,

Try the above I don't the code to try it but it should do what you want, let me if it does the job.

Mike C.

ReneMiner
18-11-2012, 02:44
thanks Mike, but there are missing a few words, lines... endifs... and I have the string *.png-Texture already converted to TBGL-usable format in memory. It's just the re-ordering like described above needs a little long - so it's no fun browsing some directory since the textures are displayed instantly when mouse points at it in my "explorer".
But I'm already happy that it does compressed *.tga so there's at least one alpha-channel-format that I can load.
At the moment (see "own class"-thread) there's some other thing that keeps brain 1.0 busy - I need some dynamic structure to store and edit meshes i.e. arranged by Entities - I have no idea how to arrange memory and i fear that data gets lost, messed up or leaks if i use pointers and poke them somewhere to memory :unguee:
btw.
I think I read somewhere that I can use Win-API-functions in tB without declaring them - is that wrong or right?

Petr Schreiber
18-11-2012, 15:10
Mikes suggestion was the best :drink:,

here is new version which loads the PNG file in correct orientation.


Petr

ReneMiner
18-11-2012, 15:35
You're the best too, anyway - how about building this into TBGL? :D Would be nice just to tell TBGL_LoadTexture()...

Petr Schreiber
18-11-2012, 21:03
Hehe,

it will be done, but only once ThinBASIC drops support for Win95/98/ME, because GDI+ is only available on XP and newer Windows...


Petr

ReneMiner
18-11-2012, 21:38
Maybe it's easier if just the TBGL-Module drops below XP support.
Somebody has to start going a step forward, the future is about to come

Who uses ME? People that have a small notebook with 192 MB Ram, which can't handle XP, and those things have 2MB-Graphics-Chip onBoard- that's not enough to even open TBGL-Window! All other win95/98/ME-Users are offices, factories and bureaus. They won't need TBGL at work and they still have Version 1.9.0.1. what they can use :roll:

ReneMiner
25-11-2012, 13:03
Can't I just create an empty texture somehow?
Currently I load black rectangular bitmaps in desired size and draw on them, but I would like to create textures on the fly since that seems just a waste of HD-space because I use different sizes and have now maybe 20 empty templates in some subfolder and it's growing...

How can I fill a string correct to match the requirements since I want to use an invisible color/alpha channel?

I tried already like this

sPixel = chr$(0)+chr$(0)+chr$(0) + chr$(255)
sString = Repeat$(sPixel, Width * Height)

put 255 also in front, used 0... %TBGL_DATA_RGBA, %TBGL_DATA_BGRA but somehow it won't get translucent/transparent

Petr Schreiber
25-11-2012, 15:48
Hi Rene,

I think your problem is that you pass wrong parameters to repeat$ function, first goes the number of repetitions, please see the manual.
Textures cannot be automagically transparent, you can make them appear so by enabling alpha blending and/or alpha testing first.

Here is basic example, where you can test the transparency nicely (just change the transparency variable to anything between 0 (invisible) to 255(fully visible):


'
' Creating texture from string
' Petr Schreiber, 2012
'

Uses "TBGL"

Function TBMain()
Local hWnd As DWord
Local FrameRate As Double

' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL script - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow

Long nWidth = 256
Long nHeight= 256
Long transparency = 128 ' -- 0 = fully transparent, 255 = fully solid
String sTextureData = Repeat$(nWidth*nHeight, Chr$(0, 0, 0, 255))

TBGL_MakeTexture sTextureData, %TBGL_DATA_RGBA, nWidth, nHeight, 1, %TBGL_TEX_LINEAR

TBGL_BackColor 255, 255, 255, 255
' -- Resets status of all keys
TBGL_ResetKeyState()

' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate

TBGL_ClearFrame
TBGL_Camera 0, 0, 2, 0, 0, 0

' -- Use texturing and blending on textured quad
TBGL_PushState %TBGL_TEXTURING Or %TBGL_BLEND
TBGL_PushTexture 1
TBGL_PushBlendFunc %GL_SRC_ALPHA, %GL_ONE_MINUS_SRC_ALPHA

TBGL_BeginPoly %GL_QUADS
TBGL_TexCoord2D 0, 0
TBGL_Vertex -0.5, -0.5

TBGL_TexCoord2D 1, 0
TBGL_Vertex 0.5, -0.5

TBGL_TexCoord2D 1, 1
TBGL_Vertex 0.5, 0.5

TBGL_TexCoord2D 0, 1
TBGL_Vertex -0.5, 0.5
TBGL_EndPoly

TBGL_PopBlendFunc
TBGL_PopTexture
TBGL_PopState

TBGL_DrawFrame

' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While

Wend

TBGL_DestroyWindow
End Function



Petr

ReneMiner
25-11-2012, 15:59
grr... yeah... my mistake wasn't Repeat$ (i did it right in script) but the size! since it's zero-based texture I messed something up a little bit - used 255 and 127 as width/height :suicide:

But- is there a way to retrieve ID of an empty Texture-Slot? Like "TBGL_EntityGetFreeID"


Would be real helpful since my program consists of different files and it's just somehow cumbersome if I use constants for objects like a menu or list and that lists or menus ID needs a typed array to store properties etc. So I have one constant for the array and a different one for the texture... I would like to store those ID's to the items subset like I can do with entities already.

With sprites won't work either see here http://www.thinbasic.com/community/project.php

Petr Schreiber
25-11-2012, 21:55
Hi Rene,

as I explained in other topic, although it might seem trivial, adding native free texture id generator would mean deep redesign of TBGL for which I would need longer time block to implement and test.

In the meantime you could use approach I use:


Function Texture_GetNewID() As Long
Static id As Long

id += 1

If id > 1024 Then
MsgBox 0, "You reached the TBGL limit for texture index"
Return 0

Else
Return id

End If

End Function



Petr

ReneMiner
26-11-2012, 15:56
OK, thanks. Now that's what I got so far and the answer to lot of questions in this thread:

(TBGL)-Texture_Handling , to be used as an include-file or can be copied into code.

Minimal OS: Windows XP Sp2,
uses TBGL, needs FreeImage.dll

-handle enumerating -(has to be done exclusively by this script in order to work correctly)
-load textures from different formats
-create empty textures
-delete textures, single or in a range

works like this:


myTextureExists = TEXTURE_Exists(ID_to_Check)

TEXTURE_Kill(First_to_Delete [, Last_to_Delete])


myNewTextureID = TEXTURE_GetFromFile( 0, "c:\myTexture.dib")
' (where the 0 is can be any value below 1)
'or
success = TEXTURE_GetFromFile( %TextureID, "c:\myTexture.png" )
' ( where %TextureID is any number from 1 to 1024)
myNewTextureID = TEXTURE_GetEmpty( 0, width, height, [R, G, B, A] )
'or
success = TEXTURE_GetEmpty( %TextureID, width, height, [R, G, B, A] )


(user also can retrieve unused IDs by TEXTURE_GetNewID)

ReneMiner
09-04-2013, 09:42
Another small question related to textures:

Since I use some external texture-loading routines the textures that are not loaded with default TBGL_LoadTexture have no TBGL_GetTextureName to return.

Can I assign some name directly to the texture-slot that was created through TBGL_MakeTexture or do I have to keep track about this in an additional texturenames-array then?

Petr Schreiber
09-04-2013, 16:35
Hi Rene,

nice ideas in this thread. I wish I major window of free time would appear and I could rewrite the texturing stuff from scratch.
For now, I would recommend to store data using Dictionary (pair of texture slot and its "name").


Petr