animated toroid in OpenB3D

Post your FreeBASIC source, examples, tips and tricks here. Please don’t post code without including an explanation.
Post Reply
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

animated toroid in OpenB3D

Post by angros47 »

Just for fun, and to show how to use an isosurface

Code: Select all

#include "openb3d.bi"


function torus cdecl(a as single, b as single, c as single) as single

	dim q as single
	q=sqr(a*a+b*b)-2*abs(sin(timer))
	return 1-(q*q+c*c)
end function


screen 18,  32, , &h10002 
	
Graphics3d 640,480,32,1,1




var camera=createcamera(0)
var fluid=createfluid
entitycolor fluid,128,255,128

fluidfunction fluid, @torus

moveentity camera,0,0,-5

'entityfx fluid,4

var light=createlight()
positionentity light,5,5,-5

dim key as string, w as integer
do

	key=inkey
        if key=chr(255)+"H" then turnentity camera,1,0,0,0
        if key=chr(255)+"P" then turnentity camera,-1,0,0,0
        if key=chr(255)+"M" then turnentity camera,0,-1,0,0
        if key=chr(255)+"K" then turnentity camera,0,1,0,0
        if key="a" then moveentity camera,0,0,1
        if key="z" then moveentity camera,0,0,-1
        if key=" " then w=1-w:	wireframe w
	updateworld 


	renderworld
	sleep 1
	flip
loop until key=chr(27)
Last edited by angros47 on Aug 27, 2019 12:51, edited 2 times in total.
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: animated toroid in OpenB3D

Post by Gunslinger »

Sorry, that demo gives me a error
Aborting due to runtime error 12 ("segmentation violation" signal) in Toroid.bas::()
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: animated toroid in OpenB3D

Post by angros47 »

Sorry, I developed it under Linux, and I forgot one thing:

Code: Select all

function torus cdecl(a as single, b as single, c as single) as single
Under Linux C calling convention is the default, under Windows it isn't, so it must be specified (OpenB3D expects C calling convention in callbacks)
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: animated toroid in OpenB3D

Post by Gunslinger »

Yes, it works now with a warning.
warning 3(1): Passing different pointer types, at parameter 2 of FLUIDFUNCTION()
I'm not sure why because i don't get whats going on in the FluidFunction that is a sub with a function in it?

Code: Select all

declare sub FluidFunction cdecl alias "FluidFunction" (byval fluid as any ptr, ScalarField as Function(x as single, y as single, z as single) as single) 
It also had 2 times flip in code that made screen flashing black randomly.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: animated toroid in OpenB3D

Post by angros47 »

The double flip can be removed, it's a leftover of a previous version.
About the warning, I figured that under Windows the "as Function" expects a different calling convention.
In OpenB3d.bi, you can change the line to:

Code: Select all

declare sub FluidFunction cdecl alias "FluidFunction" (byval fluid as any ptr, ScalarField as Function cdecl(x as single, y as single, z as single) as single) 
to get rid of the warning
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: animated toroid in OpenB3D

Post by BasicCoder2 »

Horrible flickering display?
Can it work with screenres 640,480,32?
I tried to capture the screen image with the keyboards [prt sc] key but all that showed was a blank window?
Why does it have to keep coming and going in size?
Can you make a still version that just changes size or whatever with a single tap of a key?
What about rotation?

It would be neat to have a program that could generate a 3d image from a list of connected faces.
A simple example might be a house. One you could move around inside and outside.
Paul Doe seemed to understand the mechanics involved.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: animated toroid in OpenB3D

Post by angros47 »

Flickering is caused by the double flip, I have fixed it (on some OpenGL implementations, flipping twice ruins the image, while other implementations seem to be unaffected)

It could work with screenres 640,480,32,,&h10002 (you need to set the OpenGL flag)

It changes in size because of the "sin(timer)", to demonstrate how the function can also be dynamic

It is possible to create a 3d image from scratch by creating a surface, adding some vertices with AddVertex, and connecting them with AddTriangle.
Also, Constructive Solid Geometry can be used, providing a perhaps easier way to create custom 3d
Gunslinger
Posts: 103
Joined: Mar 08, 2016 19:10
Location: The Netherlands

Re: animated toroid in OpenB3D

Post by Gunslinger »

I was thinking is it possible to add texture to the moving fluid like this.
Seem kind of difficult to do.

Has to scale en remap the model?
Or do you know a way angros47
Map the texture on x and y only?
I don't have any experience with texture.
angros47
Posts: 2323
Joined: Jun 21, 2005 19:04

Re: animated toroid in OpenB3D

Post by angros47 »

You can add a texture to a moving fluid. Mapping is related to space coordinates, not to vertices, of course, since vertices are dynamically created and removed, so it's not possible to access them. And as you correctly guessed, it maps only on x and y. For regular 2d textures, this is the simplest solution.

For different kind of textures, things are more interesting: on fluids, the textures that work better are cubemap textures, that can look like a mirror surface. Also matcap textures (that are computationally lighter) are often used with fluid surfaces (all demos I saw online used matcap textures). OpenB3D doesn't support matcap textures directly, but they can be used with the expansion OpenB3dPlus
Post Reply