A couple of little questions

Started by
6 comments, last by Biax 19 years, 4 months ago
Ive only recently started programming primarily in C++ and using OpenGL. I've been working on my first 3D project, which is very simple. Anyways, I have a couple of questions: I want to have a box made of "glass". So I put a glass texture on it and I enabled GL_BLEND when I render it. That indeed makes it transparent, but it makes it kind of crazy glowing, like it has a light inside the box. I just want it to be flat out transparent. Plus the faces of the box cant be seen from some angles, is there a way to make OpenGL render both sides of a polygon? Now, my other question, I've made a 'skybox' that my bow of glass exists inside. I set the skybox's position to always be the position of the camera, so I get the good panaramic effect happening, and it looks perfect. But if i keep moving the camera away from my box of glass, eventually the cube is outside the skybox, so it 'disappears'. How do I go about making this not happen? Is there some kind of rendering option or should I be doing something else rather than setting the position of the sky to the camera? I appreciate any help.
Advertisement
Quote:Original post by Biax
I want to have a box made of "glass". So I put a glass texture on it and I enabled GL_BLEND when I render it. That indeed makes it transparent, but it makes it kind of crazy glowing, like it has a light inside the box. I just want it to be flat out transparent. Plus the faces of the box cant be seen from some angles, is there a way to make OpenGL render both sides of a polygon?

The glowing part. Its probably the blending mode. Some sides disapear becouse you are using z buffering and fornt face is renderd first, so the side behind doesn't gets renderd. A simple solution for both problems:
-enable src_alpha, one_minues_src_alpha blending
-set color ( r, g, b, *transparency* )
-enable front face culling
-render cube
-enable back side culling
-render cube

Quote:Original post by Biax
Now, my other question, I've made a 'skybox' that my bow of glass exists inside. I set the skybox's position to always be the position of the camera, so I get the good panaramic effect happening, and it looks perfect. But if i keep moving the camera away from my box of glass, eventually the cube is outside the skybox, so it 'disappears'. How do I go about making this not happen? Is there some kind of rendering option or should I be doing something else rather than setting the position of the sky to the camera?

Render skybox without z buffer so everything will be renderd over it.
You should never let your fears become the boundaries of your dreams.
Quote:Original post by Biax
I want to have a box made of "glass". So I put a glass texture on it and I enabled GL_BLEND when I render it. That indeed makes it transparent, but it makes it kind of crazy glowing, like it has a light inside the box. I just want it to be flat out transparent. Plus the faces of the box cant be seen from some angles, is there a way to make OpenGL render both sides of a polygon?

It sounds like you're using the wrong blend function for the job. You probably want to do glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA) to render the cube realistically. This will blend the cube into the scene by a weighting equal to the alpha value of its pixels. Other blend modes might just add the cube's colours into the existing scene, which is fine for say fire or lightning effects, but not good for glass.

The disadvantage with this blend mode is that you do need to sort the transparent polygons in the scene from back to front before rendering. If you're only drawing a single convex cube though, you can skip this. Otherwise, google for "radix sort revisited" for a very fast (linear-time) sorting algorithm.

Quote:Now, my other question, I've made a 'skybox' that my bow of glass exists inside. I set the skybox's position to always be the position of the camera, so I get the good panaramic effect happening, and it looks perfect. But if i keep moving the camera away from my box of glass, eventually the cube is outside the skybox, so it 'disappears'. How do I go about making this not happen? Is there some kind of rendering option or should I be doing something else rather than setting the position of the sky to the camera?


It sounds like you're overcomplicating things somewhat. The skybox rendering shouldn't affect the other objects in the scene. If you render it first, with depth writing and depth testing disabled, this won't be an issue. The skybox is fixed relative to the camera in terms of translation, but not rotation. So to render a skybox, do something like this:

glLoadIdentity();glDisable(GL_DEPTH_TEST); //disable depth testglDepthMask(GL_FALSE); //disable depth writingdoCameraRotation(); //multiply the modelview matrix by just the rotation part of the camera transformation (however you want to do this)drawSkybox();glDepthMask(GL_TRUE);glEnable(GL_DEPTH_TEST);glLoadIdentity();

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);

CreateSkyBox(g_Camera.Position().x, g_Camera.Position().y,
g_Camera.Position().z, 300, 300, 300);

glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);

//Transparency enabling
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

CreateTexturedBox(0, 0, 0, 50, 50, 50, GLASS_ID);

//Disable Transparency
glDisable(GL_BLEND);

-----------

Thats the code for the 2 boxes. But now, the glass box isnt transparent at all, its entirely opaque, and the skybox still eventually takes over the glass box when I'm moving away from it.
How do I render the skybox without z buffer?
Quote:Original post by Biax
Thats the code for the 2 boxes. But now, the glass box isnt transparent at all, its entirely opaque, and the skybox still eventually takes over the glass box when I'm moving away from it.

Try glColor4f(1,1,1,0.5); Obviously with alpha blending you won't see an effect if your alpha value is 1.

Quote:How do I render the skybox without z buffer?

See my post above (on disabling depth writes/depth tests)

____________________________________________________________www.elf-stone.com | Automated GL Extension Loading: GLee 5.00 for Win32 and Linux

You'll need to give the glass box an alpha value. Either use a texture with an alpha colour or call glColour4f(red, green, blue, alpha);. An alpha of 1.0f is fully opaque and an alpha of 0.0f is fully transparent. Don't forget to set the colour back afterwards with either a glColour4f(red, green, blue, 1.0f); or glColour3f(red, green, blue); (in the latter case alpha is implicitly set to 1.0f).

As to the box disappearing, this is likely due to your far clip plane. The code that benjamin bunny gave you and that your using handles rendering the skybox without updating the z-buffer. The far clip plane is set in your gluPerspective call. You could try pushing it out further, but you'll lose precision in the depth buffer, which means your application will start to lose the ability to distinguish between objects of similar depths. This is a consequence of a discrete depth value storage and the way the depth buffer works. You will always have a problem with the object disappearing at some point. One possible solution is to introduce distance fog, so that the object blends into a background colour as it approaches the far clip plane and disappears that way.

Enigma
Glass _filters_ data, it doesn't _replace_ data. Thus, your blend function should be glBlendFunc(GL_DST_COLOR,GL_ZERO). If you want the cloud reflections, you add an environment map, in additive mode, in a separate pass.
enum Bool { True, False, FileNotFound };
Thanks alot that "clears things up" nicely. ;). Glass box is transparent now, and yeh, its the clipping plane. I'll just set a max distance you can travel from the glass box. Easy as that. :)

This topic is closed to new replies.

Advertisement