Author Topic: create and usescreen with make3d  (Read 2837 times)

Offline djtoon

  • Dr. Type
  • ****
  • Posts: 290
    • View Profile
create and usescreen with make3d
« on: 2011-Nov-22 »
is it working together?
cant seem to draw anything 3d in my virtual screen

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10816
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: create and usescreen with make3d
« Reply #1 on: 2011-Nov-22 »
it really should. I'll try later. Oh dear.

Offline MrPlow

  • Prof. Inline
  • *****
  • Posts: 1785
    • View Profile
Re: create and usescreen with make3d
« Reply #2 on: 2011-Nov-22 »
How does one use the virtual screen...

Its these little tricks I need to learn... O_O
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

Offline mentalthink

  • Prof. Inline
  • *****
  • Posts: 3375
  • Integrated Brain
    • View Profile
Re: create and usescreen with make3d
« Reply #3 on: 2011-Nov-22 »
DjToon, yes these works!!!

MrPlow
(this it´s a example from the help)
Quote
// CREATESCREEN
PRINT "Monitor", 0,0
GRABSPRITE 1, 0,0,32,32

// Screen 0, Sprite 13
CREATESCREEN 0, 13, 128,128
// Use screen for all output
USESCREEN 0
   PRINT "Offscreen", 0,0
   DRAWSPRITE 1,0,32
// Switch back to the window
USESCREEN -1

// Draw the screen
DRAWSPRITE 13, 100,100
SHOWSCREEN
MOUSEWAIT


The mode of use it´s very easy (like all) :) ,

You create a virtual Screen, give you an ID to this screen, and the second parameter it´s like similar to grabsprite, after use "usescreen" whit the id of create Screen, and you can see what you made in this virtual Screen...

I think in the forum you can search some examples or a explanation better than mine, only make a quick search... good luck.

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
Re: create and usescreen with make3d
« Reply #4 on: 2011-Nov-22 »
I'm afraid to confirm that glb now crashes or no draw when rendering 3D after a USESCREEN is used though I suspect it's not directly related to 3d. Tested on old and new code.  :'(

I use this often, for example; to render a series of frame animated in 3D to 2D sprites or rendering the whole screen so that it can be rotated.


Feel free to correct my code if need be!
Code: (glbasic) [Select]
// --------------------------------- //
// Project: 3dtestscreen
// Start: Wednesday, November 23, 2011
// IDE Version: 10.159 (same on 10.179)



TYPE _sprite
id
num
typ
screen
width
height
width2
height2
file_name$
ENDTYPE

TYPE _screen
width
height
ENDTYPE

GLOBAL screen AS _screen
GLOBAL sprite[] AS _sprite
GLOBAL sprite_a
GLOBAL mesh_a


main()

FUNCTION main:
init_mesh()
init_sprite()
animate_scene()
ENDFUNCTION

FUNCTION init_sprite:
GETSCREENSIZE screen.width, screen.height
sprite_a = add_sprite(screen.width, screen.height)
ENDFUNCTION

FUNCTION add_sprite: width, height
LOCAL spr AS _sprite
LOCAL sprites = BOUNDS(sprite[], 0)

spr.id = sprites
spr.num = GENSPRITE()
spr.width = width
spr.height = height
spr.screen = spr.num
CREATESCREEN spr.screen, spr.num, spr.width, spr.height
USESCREEN spr.screen
// CLEARSCREEN // possible crash
USESCREEN -1
spr.width2 = INTEGER(spr.width / 2)
spr.height2 = INTEGER(spr.height / 2)

REDIM sprite[sprites + 1]
sprite[sprites] = spr
RETURN sprites
ENDFUNCTION

FUNCTION animate_scene:
WHILE TRUE
render_view()
// render_3d() // direct
SHOWSCREEN

WEND
ENDFUNCTION

FUNCTION render_view:
USESCREEN sprite[sprite_a].screen
render_3d()
USESCREEN -1
DRAWSPRITE sprite[sprite_a].num, 0, 0
ENDFUNCTION

FUNCTION render_3d:
LOCAL angle = SIN(GETTIMERALL()/10) * 180

X_MAKE3D 0.01, 10, 45
X_CAMERA 0.0, 0.0, 0.0,  0.0, 0.0, 3.0
// X_AMBIENT_LT 1, RGB(255, 255, 255)
X_MOVEMENT 0.0, 0.0, 3.0
X_ROTATION angle, 0, 1, 0
X_DRAWOBJ mesh_a, 0
ENDFUNCTION

FUNCTION init_mesh:
mesh_a = GENX_OBJ()
X_OBJSTART mesh_a
make_sphere(0.0, 0.0, 0.0, 8, 1, RGB(255, 255, 255))
X_OBJEND
ENDFUNCTION

FUNCTION make_sphere: x, y, z, faces, radius, color
LOCAL i, j, theta1, theta2, theta3, pi
LOCAL n = faces
LOCAL r = radius
LOCAL c = color

pi = ACOS(0) * 2
IF r < 0 THEN r = -r
IF n < 3 THEN n = 3
FOR j = 0 TO n / 2 - 1
  theta1 = j * 2 * pi / n - pi/2
  theta2 = (j + 1) * 2 * pi / n - pi / 2
  FOR i = 0 TO n
     theta3 = i * 2 * pi / n
      c = RGB(RND(255), RND(255), RND(255)) // test
     X_OBJADDVERTEX (r * COS(theta2) * COS(theta3)) + x, r * SIN(theta2) + y, (r * COS(theta2) * SIN(theta3)) + z, i / n, 2 * (j + 1) / n, c
     X_OBJADDVERTEX (r * COS(theta1) * COS(theta3)) + x, r * SIN(theta1) + y, (r * COS(theta1) * SIN(theta3)) + z, i / n, 2 *  j      / n, c
  NEXT
  X_OBJNEWGROUP
NEXT
ENDFUNCTION
« Last Edit: 2011-Nov-23 by matchy »

MrTAToad

  • Guest
Re: create and usescreen with make3d
« Reply #5 on: 2011-Nov-22 »
Yes - CREATESCREEN cant be used with 3D...

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10816
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: create and usescreen with make3d
« Reply #6 on: 2011-Nov-23 »
Can you try-as a hotfix-the setscreen after x_make3d again?

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
Re: create and usescreen with make3d
« Reply #7 on: 2011-Nov-23 »
Yes, SETSCREEN after X_MAKE3D works fine for now.

Debug note: works first time after PC cold boot and editor run. Second execution, black screen. Third time, crash. (for 10.159 & 10.179)

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10816
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: create and usescreen with make3d
« Reply #8 on: 2011-Nov-25 »
USESCREEN, not setscreen. So sorry. I'll have a peek this weekend.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10816
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: create and usescreen with make3d
« Reply #9 on: 2011-Nov-25 »
Folks - I think this is working...

Code: (glbasic) [Select]
// Simple 3D
LOCAL phi

DRAWRECT 0,0,64,64,RGB(255,255,0)
DRAWRECT 10,10,23,23,RGB(0,0,255)
PRINT "GLBASIC", 0,32
GRABSPRITE 0, 0,0,64,64




// simple polygon shaped 3D object
X_OBJSTART 1
X_OBJADDVERTEX   40,  0, -50,  1,1, RGB(255,255,255)
X_OBJADDVERTEX   40, 40, -50,  1,0, RGB(  0,255,  0)

X_OBJADDVERTEX   30,  0, -20,  0,1, RGB(255,255,255)
X_OBJADDVERTEX   30, 40, -20,  0,0, RGB(  0,255,  0)

X_OBJADDVERTEX   20,  0, 0,  0,1, RGB(255,255,255)
X_OBJADDVERTEX   20, 40, 0,  0,0, RGB(  0,255,  0)
X_OBJADDVERTEX  -20,  0, 0,  1,1, RGB(  0,  0,255)
X_OBJADDVERTEX  -20, 40, 0,  1,0, RGB(  0,  0,255)
X_OBJEND






CREATESCREEN 0, 100, 500,500

WHILE TRUE
phi=GETTIMERALL() * 0.10

USESCREEN 0

CLEARSCREEN // or DRAWRECT 0,0,999,999,0 - framebuffers are transparent

// X_CLEAR_Z // Z buffer on screen might be dirty!

PRINT "Test before", 0,0
X_MAKE3D 1, 2500, 45
X_CAMERA 0,0,120, 0,0,0

// cullmodes -1; 0; +1
FOR cmode=-1 TO 1
IF cmode
X_SETTEXTURE 0, -1
ELSE
X_SETTEXTURE -1, -1
ENDIF
X_CULLMODE cmode
X_MOVEMENT cmode * 50, 0, 0
X_ROTATION phi, 0, 1, 0
X_DRAWOBJ 1, 0
NEXT

// draw an user-created animated object.
X_CULLMODE 0
X_MOVEMENT 0,-30,0
X_DRAWANIM 2, 0, 2, FMOD(GETTIMERALL()/2000., 1.0), TRUE

X_MAKE2D
PRINT "test after", 0,20

USESCREEN -1
DRAWSPRITE 100,0,SIN(GETTIMERALL()/10) * 32

SHOWSCREEN
WEND

Offline matchy

  • Prof. Inline
  • *****
  • Posts: 1545
    • View Profile
Re: create and usescreen with make3d
« Reply #10 on: 2011-Nov-25 »
I'm now discovering that GENSPRITE is returning 65503, which crashes.

MrTAToad

  • Guest
Re: create and usescreen with make3d
« Reply #11 on: 2011-Nov-25 »
Folks - I think this is working...

Code: (glbasic) [Select]
// Simple 3D
LOCAL phi

DRAWRECT 0,0,64,64,RGB(255,255,0)
DRAWRECT 10,10,23,23,RGB(0,0,255)
PRINT "GLBASIC", 0,32
GRABSPRITE 0, 0,0,64,64




// simple polygon shaped 3D object
X_OBJSTART 1
X_OBJADDVERTEX   40,  0, -50,  1,1, RGB(255,255,255)
X_OBJADDVERTEX   40, 40, -50,  1,0, RGB(  0,255,  0)

X_OBJADDVERTEX   30,  0, -20,  0,1, RGB(255,255,255)
X_OBJADDVERTEX   30, 40, -20,  0,0, RGB(  0,255,  0)

X_OBJADDVERTEX   20,  0, 0,  0,1, RGB(255,255,255)
X_OBJADDVERTEX   20, 40, 0,  0,0, RGB(  0,255,  0)
X_OBJADDVERTEX  -20,  0, 0,  1,1, RGB(  0,  0,255)
X_OBJADDVERTEX  -20, 40, 0,  1,0, RGB(  0,  0,255)
X_OBJEND






CREATESCREEN 0, 100, 500,500

WHILE TRUE
phi=GETTIMERALL() * 0.10

USESCREEN 0

CLEARSCREEN // or DRAWRECT 0,0,999,999,0 - framebuffers are transparent

// X_CLEAR_Z // Z buffer on screen might be dirty!

PRINT "Test before", 0,0
X_MAKE3D 1, 2500, 45
X_CAMERA 0,0,120, 0,0,0

// cullmodes -1; 0; +1
FOR cmode=-1 TO 1
IF cmode
X_SETTEXTURE 0, -1
ELSE
X_SETTEXTURE -1, -1
ENDIF
X_CULLMODE cmode
X_MOVEMENT cmode * 50, 0, 0
X_ROTATION phi, 0, 1, 0
X_DRAWOBJ 1, 0
NEXT

// draw an user-created animated object.
X_CULLMODE 0
X_MOVEMENT 0,-30,0
X_DRAWANIM 2, 0, 2, FMOD(GETTIMERALL()/2000., 1.0), TRUE

X_MAKE2D
PRINT "test after", 0,20

USESCREEN -1
DRAWSPRITE 100,0,SIN(GETTIMERALL()/10) * 32

SHOWSCREEN
WEND

Yes, that works

MrTAToad

  • Guest
Re: create and usescreen with make3d
« Reply #12 on: 2011-Nov-25 »
I'm now discovering that GENSPRITE is returning 65503, which crashes.
It starts at 65503 and works backwards.

This works fine here :

Code: (glbasic) [Select]
LOCAL x%

x%=GENSPRITE()
LOADSPRITE "Media/icon.png",x%
DRAWSPRITE x%,0,0
SHOWSCREEN
KEYWAIT

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10816
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Re: create and usescreen with make3d
« Reply #13 on: 2011-Nov-25 »
I changed that so it will use the highest free slot to avoid collisions.