View Full Version : stencil buffer question
largo_winch
16-08-2011, 14:51
hello. 1) I have last days by any chance and fixed a stencil buffer example from this board. that took over one hour to find an undesigned char! the sub a() was infected I deleted this part complete and renamed this sub new by hand into sub ax() and changed "Pointer" into "long".
stencil buffer is a limitation of a certain rendering area. you can stencil a white/black colour buffer and copy it like a clipping plane and mirror a scene with new content (like a bluebox) where nothing is drawn before. but I am asking myself if this showing example is correct and serve fully with this techniques ? perhaps somebody can check and give more closer infos about this code example!
2) Attention! This stencil example I fixed works only with an old "thinbasic_tbgl.dll" (around 271 kbyte). I found this dll ! it's old but this special example works with this "thinbasic_tbgl.dll", I am glad. now I am understanding this example! the sphere use colour stencil from second sphere, cut the half sphere and "copied" this colour inside first sphere! :)
bye, largo
Petr Schreiber
16-08-2011, 16:44
The old TBGL you link is more like experimetal one, stencil buffer was never enabled by default in TBGL. I will think about how to make possible to use stencil with in future in TBGL, if there will be enough interest.
The concept of stencil is very straightforward, but I do not remember the last time I really needed it in application, that's why it is not present at the moment.
Petr
largo_winch
16-08-2011, 20:13
if you need a working example you can help to use this little but great idea ;) a) stencil buffer as a "mirror" does function exactly reverse modus. you don't want to see a fighter (star wars) in front of a mirror shining through mirror side behind the wall. here the stencil buffer is working.
b) if you are taking a aeroplane cockpit and extract only cockpit colors, take a sky (with blue cloud) bitmap and "stencil" black-white modus out and "stencil" (copy for this way) this sky texture to cockpit you will have a new environment for you aeroplane cockpit
I add a "non-working-pure-script-example" have to run for thinbasic too, I am very sure. I have not the time to check this example, but I know this is running. take a 3d model (figure by zygote, poser or such things) and place it in front of a mirror and wall scene (alternative a dungeon area). I will try to build this example with own power
Code snippet:
[thinbasic=code] 'constant ClipPlane : array[0..3] of Double = (0, 0, -1, 0); // Löschen des Stencil-Puffers zum Beginn der Szene // Delete Stencil-Buffer at start of scene glClear(GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT or GL_STENCIL_BUFFER_BIT); glColorMask(False, False, False, False); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_ALWAYS, 1, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_REPLACE); glDisable(GL_DEPTH_TEST); glDisable(GL_TEXTURE_2D); DrawMirror; glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glColorMask(True, True, True, True); glStencilFunc(GL_EQUAL, 1, 1); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glEnable(GL_CLIP_PLANE0); glClipPlane(GL_CLIP_PLANE0, @ClipPlane); glScalef(1,1,-1) 'mirrorscene green/red with star wars fighter glDisable(GL_CLIP_PLANE0); glDisable(GL_STENCIL_TEST); '-----------------------> draw the mirror --------------> glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glColor4f(1,1,1,0.45); MirrorTex.Bind; DrawMirror; glDisable(GL_BLEND); '-----------------------> draw the mirror --------------> [/code]
bye, largo
largo_winch
18-08-2011, 17:09
hello petr. can you check if these code lines are compatible for tbgl?
Global mFloor As Long = TBGL_NewMaterial
TBGL_UseBlend %TRUE
TBGL_BlendFunc(%GL_SRC_ALPHA, %GL_ONE_MINUS_SRC_ALPHA)
TBGL_ColorAlpha(0.7, 0.0, 0.0, 0.40)
TBGL_SetMaterialDiffuse(mFloor, 0, 0, 255) '0
TBGL_SetMaterialAmbient(mFloor, 255, 255, 255)
TBGL_SetMaterialSpecular(mFloor, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mFloor, 128)
TBGL_UseBlend %FALSE
'------------------------------------------------------------------------> 'gl_Enable(GL_BLEND);
'glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
'glColor4f(0.7, 0.0, 0.0, 0.40); /* 40% dark red floor color */
'drawFloor();
'glDisable(GL_BLEND);
'------------------------------------------------------------------------>
I send a tbasic file with new material ideas.
bye, largo
Petr Schreiber
18-08-2011, 17:43
Hello!,
blending is not part of the material definition at the moment, to achieve blended torus, you should enable it in the *_Render entity callback.
I am not decided whether to add blending to material definition, but I just developed optional alpha component for diffuse/ambient/specular, so instead of TBGL_ColorAlpha you will be able to specify:
TBGL_SetMaterialDiffuse(mFloor, 0.7*255, 0.0, 0.0, 0.40*255)
in ThinBASIC 1.8.9.0, once it is released.
Petr
Petr Schreiber
18-08-2011, 18:00
To not make you wait,
improved TBGL with TBGL_SetMaterialAmbient, TBGL_SetMaterialDiffuse and TBGL_SetMaterialSpecular is available here (including updated documentation) (http://www.thinbasic.com/community/showthread.php?10909-The-latest-TBGL-version&p=81635#post81635).
Petr
largo_winch
26-08-2011, 13:13
hello petr. thanks for your last edition of "SetMaterialDiffuse" tbgl update!
1)Do you know if it's possible to create a transparent material?
'TESTING ----------------------------------------------->
Global mAlpha As Long = TBGL_NewMaterial ''mat_transparent(1)
TBGL_SetMaterialDiffuse(mAlpha, 0.7*255, 0.0, 0.0, 0.40*255)
'TBGL_SetMaterialEmission(mAlpha, 0, 0, 255)
TBGL_SetMaterialAmbient(mAlpha, 0, 0, 255)
TBGL_SetMaterialSpecular(mAlpha, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mAlpha, 64)
2) Does "TBGL_SetMaterialEmission" command is existing?
3) Sub Sphere_Render()
Dim element As TBGL_TENTITYIDENTIFIER At TBGL_CallingEntity
Dim material As Long At TBGL_EntityGetUserDataPointer(element.scene, element.entity)
TBGL_PushMaterial(material)
'TBGL_Translate -0.15, -0.15, 0.1+solidZ
'TBGL_Translate 0.15, 0.15, transparentZ
TBGL_Torus 0.2, 0.8
TBGL_PopMaterial
End Sub
How I can translate object position for this example with adding variables to z-coordinate?
that's all :)
bye, largo
largo_winch
26-08-2011, 13:26
I am sending current example (attention: only for test demo, not perfect!) and picture!
4) does 'TBGL_DepthMask %GL_FALSE (%GL_TRUE) command exists?
bye, largo
Petr Schreiber
26-08-2011, 17:31
Hi Largo,
1)
as I wrote earlier:
I am not decided whether to add blending to material definition
So short answer is - not present at the moment.
2)
Regarding TBGL_SetMaterialEmission, do you have any particular use for it? I did not used emissive property in my work so far, so I didn't add it to TBGL either.
Which material would you like to model with emission?
To check whether command exists, you can simply write it in the editor and hit F1. If you don't get help for it, than it is not existing.
If you would like to see some feature, please do not hesitate to request one item at time(ideally with description including motivation) in TBGL support section (http://www.thinbasic.com/community/project.php?projectid=3) (just click Post new issue/Feature).
3)
You can use TBGL_EntityPush to push object in local coordinates, or TBGL_EntityMove to move object in global coordinates.
The way you did is also possible (presuming solidZ, transparentZ are globals), but it is not wise from modular design point of view.
4)
I think that what you seek is called TBGL_UseDepthMask in ThinBASIC.
I hope it helps, let me know :)
Petr
largo_winch
29-08-2011, 09:54
thank you again for infos, petr! I cannot popup "new issue" for tbgl_section you linked from my pc, sorry. perhaps you can push it to right place.
please add for next tbgl and thinbasic issues:
1) //Add for light/material
'ok:) void setMaterialAmbient (GLfloat r, GLfloat g, GLfloat b);
'ok:) void setMaterialDiffuse (GLfloat r, GLfloat g, GLfloat b);
'ok:) void setMaterialSpecular (GLfloat r, GLfloat g, GLfloat b);
missing: void setMaterialEmission (GLfloat r, GLfloat g, GLfloat b);
missing: void setMaterialShininess (GLfloat s);
"setMaterialEmission" you can use for example for "candle light" material ;)
2) a) glEnable(GL_BLEND), b) glDisable(GL_BLEND)
I can wait until next release no problem. thank you for your great tbgl efforts until today!
3) general question: do you have had problems to mix different libraries (as you already have mentioned) for creation your tbgl graphic library?
bye, largo
Petr Schreiber
29-08-2011, 14:23
Hi Largo,
1)
The proposed syntax is identical to native OpenGL, so in such a case it is better to use it directly:
#include "thinBasic_gl.inc"
...
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, arrayOfParams)
' -- And so on
Regarding the emission, I just added it for you as TBGL_SetMaterialEmission. You can download the preview here (http://www.thinbasic.com/community/showthread.php?10909-The-latest-TBGL-version).
2)
If you need glEnable(GL_BLEND)/glDisable(GL_BLEND), you can use the OpenGL headers as I wrote in #1. In TBGL, the same functionality is solved via more flexible TBGL_UseBlend.
I am not adding blending to material definition yet, as it is tricky (order dependent property). I need to think about it more.
3)
TBGL internally uses just OpenGL with extensions on the rendering side, so no 3rd party libraries are used. The internal design is done in way it is possible to mix TBGL with OpenGL calls in ThinBASIC scripts. I think it should be possible to mix it with other libraries as well.
Support
May I ask which browser do you use? I tried from FireFox 6 and there was no problem to create new issue by clicking that magic button.
Petr
largo_winch
29-08-2011, 14:45
petr, thank you for little gifts! I will check new tbgl setMaterialEmission !
1) short feedback question for understanding: I can create "transparent" material or "alpha-blending" (with current tbgl power) example like I did with freebasic ? see my antique example with freeglut.dll and included last years "freeglut.bi" library for this example. Push "A" key to set alpha blending and "b" for taking back.
2) you've said: "The proposed syntax is identical to native OpenGL, so in such a case it is better to use it directly:".. these native functions are working for 100 per cent together with your tbgl commands ? I will check some new example to explore that behaviour. thank you for fast feedback and new material!
3) firefox 3.6.
bye, largo
Petr Schreiber
29-08-2011, 16:18
1)
For alpha blending, you can use:
TBGL_BlendFunc(%GL_SRC_ALPHA, %GL_ONE_MINUS_SRC_ALPHA)
TBGL_PushState(%TBGL_Blend)
...
TBGL_PopState
So here comes whole example:
Uses "TBGL"
Function TBMain()
Local hWnd As DWord
Local FrameRate As Double
' -- Create and show window
hWnd = TBGL_CreateWindowEx("TBGL script - press ESC to quit", 640, 480, 32, %TBGL_WS_WINDOWED Or %TBGL_WS_CLOSEBOX)
TBGL_ShowWindow
' -- Initialize lighting
TBGL_UseLighting(%TRUE)
TBGL_UseLightSource(%GL_LIGHT0, %TRUE)
TBGL_SetLightParameter(%GL_LIGHT0, %TBGL_LIGHT_POSITION, 10, 10, 10, 1)
' -- Setup material 1
' -- Create new materials
Global mBlue As Long = TBGL_NewMaterial
Global mGreen As Long = TBGL_NewMaterial
' -- Set properties
TBGL_SetMaterialDiffuse(mBlue, 0, 0, 255)
TBGL_SetMaterialAmbient(mBlue, 0, 0, 255)
TBGL_SetMaterialSpecular(mBlue, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mBlue, 128)
TBGL_SetMaterialDiffuse(mGreen, 0, 255, 0, 128)
TBGL_SetMaterialAmbient(mGreen, 0, 255, 0)
TBGL_SetMaterialSpecular(mGreen, 255, 255, 255)
TBGL_SetMaterialSpecularExponent(mGreen, 128)
' -- Resets status of all keys
TBGL_ResetKeyState()
' -- Main loop
While TBGL_IsWindow(hWnd)
FrameRate = TBGL_GetFrameRate
TBGL_ClearFrame
TBGL_Camera(5, 5, 10, 0, 0, 0)
' -- Transform
TBGL_PushMatrix
' -- Material
TBGL_PushMaterial mBlue
TBGL_Sphere 1
TBGL_PopMaterial
TBGL_PopMatrix
' -- Transform
TBGL_PushMatrix
TBGL_Translate 0.5, 0.5, 0
' -- Blending
TBGL_BlendFunc(%GL_SRC_ALPHA, %GL_ONE_MINUS_SRC_ALPHA)
TBGL_PushState(%TBGL_BLEND)
' -- Material
TBGL_PushMaterial mGreen
TBGL_Sphere 1
TBGL_PopMaterial
TBGL_PopState
TBGL_PopMatrix
TBGL_DrawFrame
' -- ESCAPE key to exit application
If TBGL_GetWindowKeyState(hWnd, %VK_ESCAPE) Then Exit While
Wend
TBGL_DestroyWindow
End Function
2)
You should be able to use FreeGlut primitives for example. Try it out, if you have time.
3)
I don't want to force you to do anything, but I would strongly recommend to upgrade to Firefox 5/6.
Petr
largo_winch
29-08-2011, 17:18
Well and so simple!, I can see: that was only missing important command "TBGL_PushState(%TBGL_BLEND)" I didn't know this nice little blending helpers! ;) Thank you petr for your short and interesting example! I am learning walking tbgl like a child!
As you can see in my example I have placed "TBGL_PushStateProtected" in subs I didn't want to blend. I have tried last days blending with "TBGL_BlendFunc(%GL_SRC_ALPHA, %GL_ONE_MINUS_SRC_ALPHA)" in my subs but without "applying" these blending function command (TBGL_PushState(%TBGL_BLEND) ) to specific material it couldn't work!
my correct blending example I have had in mind last days to realize with tbgl I send again. Now the result for me is very ok! :)
bye, largo
Petr Schreiber
29-08-2011, 17:21
I am happy it worked for you!
Demo ran okay here :)
Petr
largo_winch
13-09-2011, 11:19
this little example shows how to texture primitives with current example project.
@petr: How I can make a reflection effect as you did in tbgl_advanced_dungeonWithCollisionBlend ("the old tumb") example with different floor textures ? ;) btw: the old dungeon example has a little rotation problem with function "chunkCalcPositionAndDraw()"
zip folder contains all textures. bye, largo
Petr Schreiber
13-09-2011, 11:41
Hi Largo,
the reflection was made using simple trick - the floor was transparent and the scene was rendered upside down below the floor to create the illusion ;)
Petr