Author Topic: Basics and FAQ of a Texture Generator  (Read 5312 times)

0 Members and 1 Guest are viewing this topic.

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Basics and FAQ of a Texture Generator
« on: February 12, 2008 »
Its some years ago when i started to code an own texture generator which was very slow and had only a few very simple generators (can be found somewhere on the forum here). Sadly i lost all the sources in the past due fact of a bad hd crash. Now i am very motivated to code a new one, esp. for my actually project i am working on and where i could need some nice simple and fast texture generator functions to create simple textures.

I want to code, ask you and help other guys here how and where to start a good, flexible and fast texture generator with some basic  functions (generators, operations, filters). So i will talk and ask here about some basic things, which may not be the best solution and we can hopefully discuss here to show better solutions.

You all may know Windows GDI and possible GDI+ ... Personally i think its not good to use GDI nor GDI+ for drawing 2D objects like "Line, Box, Oval, Circle, Plot", because GDI is to slow and dont support Antialiazed drawing like GDI+ where we need to install GDI+ for. Generating textures at runtime needs a lot of calculations/times and so we want to have a good, easy and fast solution instead long calculating times!

The idea is to have a hand full of own "Generators" where we are drawing needed things by using our own code/algos. Possible Generators we should have for a useable TextureGenerator can look like this:

Code: [Select]
TexGen_Flat( RGBA.l )   or    TexGen_Flat( Width.l, Height.l, RGBA.l )
TexGen_Rect( xPos.l, yPos.l, Width.l, Height.l, RGBA.l )
TexGen_Glow( xPos.l, yPos.l, Width.l, Height.l, Gamma.l, RGBA.l )
TexGen_Noise( Octaves.l, Scales.l, Seed.l, RGBA.l )

Ofcourse we could add many other generators as we want and needing. Generators are just only one part of a texture generator. Another things we need are so called Deformation Effects and Color Operations.

Code: [Select]
TexFx_RotoZoom( ...) 
TexFx_SineDistort( ... )    or  TexFX_Distort( ...) by using Red and Blue Colors as input i.e.!?
TexFx_Twirl( ... )

TexCol_Blur()
TexCol_Invert()
TexCol_Color()
TexCol_Normal()     

As you can see, you could add a lot of funtions to your generator... but dont use to much of them if you want have a small tiny generator. Possible you know the so called "SinePlasma Textures". I think you dont need to add this function to your generator, because you could create the same thing using TexGen_Glow() and TexFX_RotoZoom()... So thinking about some basic operations can save you some engine code by doing things on another way. A checkerboard texture can be created by an own Checkerboard Generator or just by using TexGen_Rect() and TexFX_RotoZoom().

Here are some small and simple generator sources:

Code: [Select]
; thanks to for the idea to create this with XOr *sweet*, which is smaller
; as my old bloody version

Procedure TexGen_Checkerboard( x_size.l, y_size.l, Color1.l, Color2.l)
    Protected Color.l, x1.l, y1.l, x2.f, y2.f
 
    For y = 0 To TextureHeight -1         
        For x = 0 To TextureWidth -1
           
        If ((x / x_size) & 1) ! ((y / y_size) & 1) = 0
            Color = Color1
        Else
            Color = Color2
        EndIf
   
        Plot( x,y, RGB(color, color, color))

        Next
    Next
                   
EndProcedure


Code: [Select]
; this is a silly function without AA and i thought about it... can be done with two TexGen_Glow() ^^
; original source from angelcode website

Procedure TexGen_CircleEx( cx.l, cy.l, r.l, thickness.l, red.f, green.f, blue.f)

  w.l = TextureWidth
  h.l = TextureHeight

  Define.l  x, y, count
 
  count = r + thickness
  If( cx - count < 0 ) : cx = cx + w : EndIf
  If( cy - count < 0 ) : cy = cy + h : EndIf
 
  x = 0;
  While( x <= count )
 
    y = 0;
    While( y <= count )

      pr.l = Int(Sqr(x*x + y*y));

      If( (pr < r + thickness/2) And (pr >= r - thickness/2) )
         Define.l  px, py
         
      px = (cx+x)%w;
      py = (cy+y)%h;
         Plot ( px, py, RGB(red,green,Blue))
         
      px = (cx-x)%w;
      py = (cy+y)%h;
         Plot ( px, py, RGB(red,green,Blue))

      px = (cx-x)%w;
      py = (cy-y)%h;
         Plot ( px, py, RGB(red,green,Blue))

      px = (cx+x)%w;
      py = (cy-y)%h;
         Plot ( px, py, RGB(red,green,Blue))

      EndIf

      y=y+1
    Wend
    x=x+1
  Wend
EndProcedure

i will check my old backup DVDs and will post some other procedures if i find some of them...

As we know now, we will use our own drawing algos/procedures which are faster by drawing directly in the memory (on the texture) without using GDI/GDI+.... This makes the TexGen functions (Generators, filters....)  OS independent and portable to other systems too!

I am not really sure what is the best way to start with. Using an Array like TexSize * TexSize * 4 bytes for RGBA values is silly and still slow i think... So i think allocating a memoryblock TexWidth * Texheight * 4 bytes for RGBA seems to be a better and faster solution. I am not really sure if we just only need to alloc this memory block size (without BMP header for example)... or using WinAPI structures too...

Code: [Select]
Structure BITMAPINFOHEADER
  biSize.l
  biWidth.l
  biHeight.l
  biPlanes.w
  biBitCount.w
  biCompression.l
  biSizeImage.l
  biXPelsPerMeter.l
  biYPelsPerMeter.l
  biClrUsed.l
  biClrImportant.l
EndStructure

Structure BITMAPFILEHEADER
  bfType.w
  bfSize.l
  bfReserved1.w
  bfReserved2.w
  bfOffBits.l
EndStructure

So it would be nice to know from the real texture generations gurus here (hello mind, rbraz ;) what the best way and how/where to start if you want to load the generated texture with DX9 from memory ^^ thanks

« Last Edit: February 12, 2008 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Basics of a Texture Generator
« Reply #1 on: February 12, 2008 »
i would like to know how to code this generator procedure to have an AA flat circle and how to handle all the stuff.. just only something like:

Code: [Select]
*pTexture1 = TexGen_Glow( ... ) 
*pTexture2 = TexGen_Rect( ... )
*pTexture3 = Add/Mul/Sub Both together

This way?? Or how is it possible to have just only the engine where we can do something like this, by giving each operator an ID number to identify...

Code: [Select]
Procedure Engine_CreateTexture( ?LabelOfTextureData)
    ProcedureReturn *pPointerOfFinalTexture
EndProcedure

*Texture_Grass = Engine_CreateTexture( ?Dat1 )
*Texture_Grass = Engine_CreateTexture( ?Dat2 )

Dat1:
  IncludeBinary "DataFile1.bin"

Dat2:
  IncludeBinary "DataFile2.bin"

Things i am very interested in, how they works and how to code them... would be nice have here a nice friendly disucssion with some codesnips and so on...

Code: [Select]
TexGen_Glow( xPos.l, yPos.l, Width.l, Height.l, Gamma.l, RGBA.l )   ; based on x*x + y*y but then...? :P
TexFx_RotoZoom( CenterX.l, CenterY.l, Rotate.l, Zoom.l )         

Images == Alpha value: 48, 16, 8, 0
« Last Edit: February 12, 2008 by va!n »
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Basics and FAQ of a Texture Generator
« Reply #2 on: February 13, 2008 »
mhhhh.... no reply? nobody who can help and give tips how and where to start a good and fast texturegen? ;)
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won:

Offline Stonemonkey

  • Pentium
  • *****
  • Posts: 1315
  • Karma: 96
    • View Profile
Re: Basics and FAQ of a Texture Generator
« Reply #3 on: February 13, 2008 »
I am definitely no texture generating guru but generally I have a structure with graphics buffer info such as dimensions and a pointer that points to the colour data. Since I am usually software rendering all my graphics buffers, textures, sprites, images whatever all use the same structure so I can use all the same drawing/rendering functions on any image buffer.

Put whatever fields into your struct that you think you'll require and it's easy enough to add or remove fields later (as long as you're not accessing them with inline asm code)

As for functions for generating the textures, it depends on whether you're wanting to build some sort of library of functions so that you have them ready for some other time or whether you're after something specific that you want right now but you could start off with stuff like points/lines/rects/tris/circles, filled or not or with gradients and antialiased, I wrote some code somewhere for antialiased circles, I'll have a look for it.

Other things to think about could be perlin noise, different types of blur, blending images together with different blending modes.

EDIT:
Anti aliased circles code I wrote here, anti_circle.zip attached to one of my posts:

http://dbfinteractive.com/index.php?topic=2735.0

Perhaps not the best method, it works out the approximate coverage of each pixel and uses the result for blending.
« Last Edit: February 13, 2008 by Stonemonkey »

Offline Shockwave

  • good/evil
  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 17414
  • Karma: 498
  • evil/good
    • View Profile
    • My Homepage
Re: Basics and FAQ of a Texture Generator
« Reply #4 on: February 13, 2008 »
Definately what Stonemonkey said.

You need to decide on a structure for your textures so that your other functions can use them easily.

Once you decide on the storage method and texture sizes, I guess that it will be straight forward to bolt generation functions onto the program.

You have all the possibilities in the world when deciding on what texture patterns to generate.

I'd advise you to make the whole thing as modular as possible so that you can add things and take things out as easily as you can.
Shockwave ^ Codigos
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Basics and FAQ of a Texture Generator
« Reply #5 on: February 13, 2008 »
-> vain, those circle textures can all be generated by

intensity = distance from centre of texture * falloff rate

where you change falloff rate between 0 and 1 to get the patterns from top to bottom.  distance is 0-1 based on size of texture map.  You can clamp, or mask that to get circles or circular effects.

Jim



Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Basics and FAQ of a Texture Generator
« Reply #6 on: February 13, 2008 »
@vain: I'm far from being a "texture guru", the only real guru here is Mind  :)

Anyway, you can try and check this tutorials at in4k, is a bit old but still very useful.
http://in4k.untergrund.net/html_articles/hugi%2018%20-%20cogentex.htm
http://in4k.untergrund.net/html_articles/hugi%2019%20-%20pxgen.htm

There's some good advice to how you can setup your texture framework and all basic algorithms for you to start it.

Challenge Trophies Won:

Offline mind

  • Texture-San
  • DBF Aficionado
  • ******
  • Posts: 2324
  • Karma: 85
    • View Profile
    • boredom is a beatiful thing.
Re: Basics and FAQ of a Texture Generator
« Reply #7 on: February 14, 2008 »
first of all, think really hard about what kind of system you want to use and write your texgen based on that.. for example, i went for the standard photoshop layer style texgen, where you generate shit on 1 layer, modify that, generate stuff on next layer etc.. personally i regret doing that, since i'd rather use a stacking system where you just stack operators ontop of each other and can modify them independantly etc.. makes for much more powerful editing.. but oh well, too late now i guess :D

as far as memory layout, i always prefered and will always recommend beeing able to work on different channels independantly so i set up a structure, lets call it "chan", the size of 1 chan, size*size, in my case 256*256=65536, then i set up another struct of 4xchan, lets call that "layer" and last i set up a struct for each texture(in my case i had 6 layers to work on, and 2 temp buffers) so 8xlayer, lets call that "texture".. so when i wanted to draw something on the red channel of layer n(since im slack i declared a few constants, like r=1, g=2,b=3,a=4 etc), i would just go texture[n][r]^[offset]=value; (dont mind the ^, its a pointer in pascal/delphi)

but basically, as long as you work with n^n textures you can easily make your filters work with any size by going(for example, if you do a map distort, assuming dx is the pixel you want to distort your source pixel by) "(x+dx) & n" where n is any power of 2.

about the AA rings, i sent you a few links..

i can probaly come up with more things, but im tired and drunk.. and going to bed :D
Challenge Trophies Won:

Offline relsoft

  • DBF Aficionado
  • ******
  • Posts: 3303
  • Karma: 47
    • View Profile
Re: Basics and FAQ of a Texture Generator
« Reply #8 on: February 14, 2008 »
How about texture splatting/blending?

http://rel.betterwebber.com/junk.php?id=38

Don't mind the 3d engine. It's the texture combiner that works.
Challenge Trophies Won:

Offline va!n

  • Pentium
  • *****
  • Posts: 1435
  • Karma: 109
    • View Profile
    • http://www.secretly.de
Re: Basics and FAQ of a Texture Generator
« Reply #9 on: February 19, 2008 »
thanks a lot guys... i will take later a closer look to this part again, because i need it for my actual project until eastern. but now i am trying to code/finish some other important parts.
- hp EliteBook 8540p, 4 GB RAM, Windows 8.1 x64
- Asus P5Q, Intel Q8200, 6 GB DDR2, Radeon 4870, Windows 8.1 x64
http://www.secretly.de
Challenge Trophies Won: