GLBasic forum

Main forum => GLBasic - en => Topic started by: Qube on 2010-Apr-17

Title: iPhone 3D Batching
Post by: Qube on 2010-Apr-17
Hi :)

Is there a way to batch draw 3D models in order to reduce draw calls on the iPhone?

I use polyvector when doing 2D to draw many sprites in between startpoly / endpoly in order to reduce draw calls and that works well but I see no solution for 3D, unless I'm missing something obvious  :blink:

I've see some referral to glDrawArrays but I've no idea if that is the right thing to use or indeed how it could be used  :S

Title: Re: iPhone 3D Batching
Post by: bigsofty on 2010-Apr-20
You could use GLDrawArrays (or glDrawElements I think) but that would require Gernot to expose some of the internal GLBasic data structures he uses for storing meshes? Some sort of mesh Batching command would be a great addition in GLBasic, even good old Blitz3D has this.

Title: Re: iPhone 3D Batching
Post by: Qube on 2010-Apr-21
Quote
Some sort of mesh Batching command would be a great addition in GLBasic

I agree, a mesh batching feature would be very handy for GLB and iPhone  =D **VOTE FOR FEATURE**
Title: Re: iPhone 3D Batching
Post by: Kitty Hello on 2010-Apr-21
That's what I already am doing. I render each X_DRAWOBJ in one batch.
Do you want more in one batch? It must be the same texture, mind!
Title: Re: iPhone 3D Batching
Post by: bigsofty on 2010-Apr-21
Yes, that would be great Gernot, doing a bunch of X_DRAWOBJ calls for a single call would be a big boost.  :good:

A mesh combiner command is also something to consider too...

"X_ADDMESH mesh1, mesh2, newmesh"

Where all the vertices in "mesh1" are added to "mesh2" which would create a new mesh "newmesh". See... B3D AddMesh (http://www.blitzbasic.com/b3ddocs/command.php?name=addmesh&ref=goto)
Title: Re: iPhone 3D Batching
Post by: matchy on 2010-Apr-21
That would mean that X_SETTEXTURE would need to include the object number.
Title: Re: iPhone 3D Batching
Post by: Kitty Hello on 2010-Apr-21
No - you cannot switch texture within drawing a batch of triangles.
I'll see what I can do.
Title: Re: iPhone 3D Batching
Post by: Scott_AW on 2010-Apr-21
You could combine some textures into a 'tilemap' and manipulate the texture x,y accordingly.

That would be useful to be able to batch like textured objects.
Title: Re: iPhone 3D Batching
Post by: matchy on 2010-Apr-22
I thought I'd try it out:
8)
Code (glbasic) Select

// --------------------------------- //
// Project: texdouble
// Start: Thursday, Aprl 22, 2010
// IDE Version: 7.322
// Name: matchy
// Description: Experimental demonstration of combining inital separate texture maps for separate textured objects.

CONSTANT SPRITE_TEXTURE1=1
CONSTANT SPRITE_TEXTURE2=2
CONSTANT OBJECT_BOX1=1
CONSTANT OBJECT_BOX2=2
CONSTANT BRUSH_HORIZONTAL_LINES=1
CONSTANT BRUSH_VERTICAL_LINES=2
CONSTANT COL_WHITE=0xffffff
CONSTANT COL_RED=0x0000ff
CONSTANT COL_GREEN=0x00ff00
CONSTANT COL_BLUE=0xff0000
GLOBAL SPRITE_TEXTURE3


funStart()

FUNCTION funStart:
SETSCREEN 640,480, 0

SPRITE_TEXTURE3=GENSPRITE()
funCreateTextureSprite(SPRITE_TEXTURE1,BRUSH_HORIZONTAL_LINES, 128,128, COL_GREEN)
funCreateTextureSprite(SPRITE_TEXTURE2,BRUSH_VERTICAL_LINES,   128,128, COL_BLUE)

funCreateFlatObject(OBJECT_BOX1, -2,0,0, 1,1,1, 0.1,0.9, COL_WHITE)
funCreateFlatObject(OBJECT_BOX2,  2,0,0, 1,1,1, 0.1,0.9, COL_WHITE)

SPRITE_TEXTURE3=funAddTexture(SPRITE_TEXTURE1,SPRITE_TEXTURE2)

WHILE TRUE
X_MAKE3D 1,10, 45
X_CAMERA 0,1,10, 0,0,0

// Display separate objects with separate textures

X_MOVEMENT 0,1,0
X_SETTEXTURE SPRITE_TEXTURE1,-1
X_DRAWOBJ OBJECT_BOX1, 0
X_SETTEXTURE SPRITE_TEXTURE2,-1
X_DRAWOBJ OBJECT_BOX2, 0

// Display combined texture from original separate textures

X_MOVEMENT 0,-1,0
X_SETTEXTURE SPRITE_TEXTURE3,-1
X_DRAWOBJ OBJECT_BOX1, 0
X_SETTEXTUREOFFSET 0.5,0
X_DRAWOBJ OBJECT_BOX2, 0
SHOWSCREEN
WEND
ENDFUNCTION

FUNCTION funAddTexture: texture1,texture2
LOCAL w1,h1, w2,h2, w3,h3, texture3

GETSPRITESIZE texture1, w1,h1
GETSPRITESIZE texture2, w2,h2
w3=w1+w2
h3=h1 //+h2
CLEARSCREEN
DRAWSPRITE texture1,  0,0
DRAWSPRITE texture2, w1,0
GRABSPRITE texture3,  0,0, w3,h3
RETURN texture3
ENDFUNCTION

FUNCTION funCreateTextureSprite: sprite,brush, w,h, col
LOCAL x,y

CLEARSCREEN
SELECT brush
CASE BRUSH_HORIZONTAL_LINES
FOR y=0 TO h STEP 5
DRAWLINE 0,y, w,y, col
NEXT
CASE BRUSH_VERTICAL_LINES
FOR x=0 TO w STEP 5
DRAWLINE x,0, x,h, col
NEXT
ENDSELECT
GRABSPRITE sprite, 0,0, w,h
ENDFUNCTION

FUNCTION funCreateFlatObject: num, x,y,z, l,w,h, tw,th, col
l=l*0.5
w=w*0.5
h=h*0.5
X_OBJSTART num
X_OBJADDVERTEX x-l, y+w, z+h,  0,  0, col
X_OBJADDVERTEX x+l, y+w, z+h, tw,  0, col
X_OBJADDVERTEX x-l, y-w, z+h,  0, th, col
X_OBJADDVERTEX x+l, y-w, z+h, tw, th, col
X_OBJNEWGROUP
X_OBJEND
ENDFUNCTION