MiniB3d for FreeBasic

Headers, Bindings, Libraries for use with FreeBASIC, Please include example of use to help ensure they are tested and usable.
Post Reply
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: MiniB3d for FreeBasic

Post by ITomi »

I tried to attach the weapon entity to the camera, but when I use the "turnentity" command at the camera, the weapon is disappear (when I use "moveentity", the weapon moves correctly with it ahead and backward):

Code: Select all

var weapon=createsprite(camera)
scalesprite weapon,0.5,0.5
entitytexture weapon,weapon1tex
positionentity weapon,entityx#(camera),entityy#(camera),entityz#(camera)+1

Do
        'TurnEntity skydome,0.1,0,0
        
        if multikey(sc_up) then moveentity camera,0,0,0.1
        if multikey(sc_down) then moveentity camera,0,0,-0.1
        if multikey(sc_left) then turnentity camera,0,0.1,0
        if multikey(sc_right) then turnentity camera,0,-0.1,0
        
        updateworld
        renderworld
        Flip
Loop Until MultiKey(sc_q)
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

The solution is simple: replace "entityz#(camera)+1" with "entityz#(camera)+1.1".

The camera range, by default, is 1 to 1000, so objects that are at a distance from the camera smaller than 1 are not rendered. And you put the sprite at a distance of 1, that is the limit: when you turn the camera, smallest rounding errors makes the sprite to be closer than 1, and so it is not rendered any more, and it disappears.
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: MiniB3d for FreeBasic

Post by ITomi »

Thank you, Anngros47, I didn't know it, but works!
And one more question: is there a background color to the sprite entities in 3d mode, that I will make it partially transparent? E.g. the colors around the weapon.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

If the image is a png, it can have a alpha channel, that makes it partially transparent
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: MiniB3d for FreeBasic

Post by ITomi »

I tried with .jpg, .bmp and .png formats, but the pixels are remain around the weapon. Where is the background color and which is? The bottom-left corner or other?
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

The image must be in png, must have transparency set, and must be loaded with the masked flag (4)
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: MiniB3d for FreeBasic

Post by ITomi »

OK, I load with the masked flag:

Code: Select all

var weapontex=loadtexture("images\weapons\pistol.png",4)
but how and where can I set its transparency? In a drawing program (e.g. Paint) or with a command?
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

In a drawing program (it is sometimes called alpha channel)
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: MiniB3d for FreeBasic

Post by ITomi »

Huhhh, thanks, Angros47, it succesfully done! :-)
(It seems that Windows7's Paint is not a suitable program for this, but I found better tools.)
ITomi
Posts: 154
Joined: Jul 31, 2015 11:23
Location: Hungary

Re: MiniB3d for FreeBasic

Post by ITomi »

How can I check if an entity is in front of the camera within a certain distance?
I would like check the collisions and I can compute the distance between two 3d objects, so on the basis of it I can stop the movement, but I can't check whether an entity is in the range of the camera or beside or behind of it.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

EntityPick does what you need, look at this:

http://www.blitzbasic.com/b3ddocs/comma ... entitypick

Also, EntityInView and EntityDistance can help
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

Version 1.20 is online

Added:
-Custom shaders now can also use the entity textures
-Experimental post processor

Fixed:
-Bug in isosurfaces when using custom shaders
-Bug in RotateEntity and PointEntity, that caused issue with the scale of an entity when it was in a nested structure
-Bug in calculating light in geospheres
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

An example of postfx (gaussian blur)

Code: Select all

#include "openb3d.bi"

screen 18,  32, , &h10002 

	
Graphics3d 640,480,32,1,1

var camera=createcamera(0)
cameraclscolor camera,0,0,255

var shader=loadshader("gauss","gaussian.vert","gaussian.frag")
setinteger shader, "RenderTex",0
setinteger shader, "Width",640
setinteger shader, "Height",480

var fx=createpostfx(camera,2)
hideentity camera

AddRenderTarget fx,0,1,1
Postfxbuffer fx,0,0,1,0
AddRenderTarget fx,1,1,0
Postfxbuffer fx,1,1,1,0
PostFXShader fx,0,shader
PostFXShaderPass fx,0,"PassNum",1
PostFXShader fx,1,shader
PostFXShaderPass fx,1,"PassNum",2


var cube=createcube()
entitycolor cube,255,0,0

var plane=createplane()
moveentity plane,0,-1.4,15


var light=createlight()
moveentity light,10,10,-3
pointentity light,cube
moveentity camera,-2,0,-5

createshadow cube

dim key as string
do

	key=inkey
        if key=chr(255)+"H" then turnentity cube,1,0,0,0
        if key=chr(255)+"P" then turnentity cube,-1,0,0,0
        if key=chr(255)+"M" then turnentity cube,0,-1,0,0
        if key=chr(255)+"K" then turnentity cube,0,1,0,0
	updateworld 1
	renderworld
sleep 1
	flip
loop until key=chr(27)

gaussian.vert

Code: Select all

#version 120

varying vec3 Position;
varying vec3 Normal;
varying vec2 TexCoord;


void main()
{
	TexCoord = gl_MultiTexCoord0.st;
	Normal = normalize(gl_NormalMatrix * gl_Normal);
	Position = vec3(gl_ModelViewMatrix * gl_Vertex);
	
	gl_Position = ftransform();
}
gaussian.frag

Code: Select all

#version 120

uniform int PassNum;

uniform sampler2D RenderTex;

uniform int Width;
uniform int Height;

varying vec3 Position;
varying vec3 Normal;
varying vec2 TexCoord;

float PixOffset[7] = float[] (0.0,1.5,3.0,4.5,6.0,7.5,9.0);
//float PixOffset[5] = float[] (0.0,1.5,3.0,4.5,6.0);
//float PixOffset[5] = float[] (0.0,0.1,0.2,0.3,0.4);
//float Weight[5] = float[] (0.75,1.0,1.5,1.0,0.75);
float Weight[5] = float[] (0.05,0.14,0.2,0.14,0.05);
//float Weight[5] = float[] (0.001,0.02,0.23,0.02,0.001);
//float Weight[7] = float[] (0.015625,0.09375,0.234375,0.3125,0.234375,0.09375,0.015625);
//float Weight[7] = float[] (0.01562,0.0937,0.2343,0.3125,0.2343,0.0937,0.0156);

vec4 Pass1()
{
	float dy = 1.0 / float(Height);
	vec4 sum = texture2D(RenderTex,TexCoord) * Weight[0];
	for ( int i = 1; i < 5; i++)
	{
		sum += texture2D(RenderTex, TexCoord + vec2(0.0,PixOffset[i]) * dy ) * Weight[i];
		sum += texture2D(RenderTex, TexCoord - vec2(0.0,PixOffset[i]) * dy ) * Weight[i];
	}
	return sum;
}

vec4 Pass2()
{
	float dx = 1.0 / float(Width);
	vec4 sum = texture2D(RenderTex, TexCoord) * Weight[0];
	for ( int i = 1; i < 5; i++)
	{
		sum += texture2D(RenderTex, TexCoord + vec2(PixOffset[i],0.0) * dx ) * Weight[i];
		sum += texture2D(RenderTex, TexCoord - vec2(PixOffset[i],0.0) * dx ) * Weight[i];
	}
	return sum;
}


void main()
{
	if (PassNum == 1)
	{
		gl_FragColor = Pass1();
	}
	if (PassNum == 2)
	{
		gl_FragColor = Pass2();
	}
}
Kronos
Posts: 9
Joined: Dec 10, 2012 19:53

Re: MiniB3d for FreeBasic

Post by Kronos »

Hi Angros,

Are you aware of any issues with the Animate function?
I can LoadAnimMesh a .b3d model and everything looks correct, but when I set the Animate function , model is animating but appears to be rotated 180 degrees around the Y axis. Any lighting on the model is also flipped 180 degrees.

The only commands I am using are

LoadAnimMesh("D:\Models\goblin.b3d");
Animate(goblin,1,1.5f,0,0);
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: MiniB3d for FreeBasic

Post by angros47 »

The issue is caused by the fact that the model has been rotated by the animation program, so, when it is not animated, it is in a position, but when is animating, all frames of the animation have it rotated. Unfortunately, OpenB3D does not update the normals, when performing an animation (usually, the difference is minimal, and in complex model updating normals would double the time for animation, and memory occupation), so, if the model is suddenly rotated by 180, all the normals will be inverted, and this will affect the lighting. The animated mesh has been made by you? Or converted by you in b3d format? If so, perhaps you should check your export settings
Post Reply