Opengl surface rendering issue - c++

I just started loading some obj files and render it with opengl. When I render these meshes I get this result (see pictures).
I think its some kind of depth problem but i cant figure it out by myself.
Thats the parameters for rendering:
// Dark blue background
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
// Enable depth test
glEnable( GL_DEPTH_TEST );
// Cull triangles which normal is not towards the camera
glEnable(GL_CULL_FACE);
I used this Tutorial code as template. https://code.google.com/p/opengl-tutorial-org/source/browse/#hg%2Ftutorial08_basic_shading

The problem is simple, you are doing FRONT or BACK culling.
And the object file contains CCW(Counter-Clock-Wise) or CW (Clock-Wise) cordinates, so written from left to right or right to left.
Your openGL code is expecting it in the other way round, so it hides the surfaces which you are looking backward on.
To check this solves your problem, just take out the glEnable(GL_CULL_FACE);
As this exactly seems to be producing the problem.
Additionally you can use glCullFace(ENUM); where ENUM has to be GL_FRONT or GL_BACK.
If you don't in at least one of both cases can't see your mesh (means in both cases: GL_FRONT or GL_BACK your just seeing the partial mesh) , thats a problem with your code of interpreting the .obj. or the .obj uses not strict surface vectors. (A mix of CCW and CW)

I am actually unsure what you mean, however glEnable(GL_CULL_FACE); and then GL_CULL_FACE(GL_BACK); will cull out or remove the back face of the object. This greatly reduces the lag while rendering objects, and only makes a difference if you are inside or "behind" the object.
Also, have you tried glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); before your render code?

Related

Is there any way to make front face of gluCylinder() transparent?

I am using gluCylinder() to create a cylinder in openGL and then plotting points inside the cylinder with Depth Test On .
When i see the front view of the cylinder, the points inside the cylinder are obstructed by front face.
To make front face of the cylinder translucent i am using Blending.
I am using below functions.
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
But whatever coloring or alpha value i assign to the cylinder the front face is not looking transparent due to its back face.
Tell whether it is possible to do with blending only or else i need to introduce lighting for both the faces of Cylinder.Here it clearly visible the change in the color of front face and back face of cylinder. And the points inside the cylinder are not visible due to being obstructed by front face of cylinder.
You should be able to accomplish this by drawing the cylinder twice, while culling the front faces the first time, and culling the back faces the second time. This way, you can draw the front and back parts differently, e.g. by making the front part transparent.
The code sequence could look like this:
// Draw back part of cylinder, opaque.
glEnable(GL_CULL_FACE);
glCullFace(GL_FRONT);
gluCylinder(...);
// Draw points.
// Draw front part of cylinder, transparent.
glCullFace(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
gluCylinder(...);
glDisable(GL_BLEND);
If I understand you right then no, you can't do it with blending alone.
If the cylinder's normals all point outward then you also won't be able to see the cylinder's internal parts no matter what you do.
I do something similar to show characters behind walls and it goes like this - render your scene normally and save it all to a framebuffer. Then render what you want shown behind with the buffer contents on top, using a custom shader to make a bubble of transparency around the thing you want shown behind.
Not sure if I am explaining it well or not but it unfortunately requires multiple steps to get the results you want.
Your problem is still a bit unclear to me despite the image but I will attempt to answer based on my perception of your issue.
You are drawing a cylinder and have geometry (lines or other models) inside the cylinder. You want the cylinder to look translucent so the inner objects are visible. Here is one way to do it. Assuming your render functions are drawCylinder() and drawPoints().
init()
{
...
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
...
}
drawScene()
{
glDepthMask(GL_FALSE);
glEnable(GL_BLEND);
drawCylinder();
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
drawPoints();
}
doing so will make sure that the points are drawn regardless of the cylinder. Try using lower values of alpha for your cylinder color.
Please note this is one way to do it. I suggest using shaders to have more control over blending as well as exploring fragment/pixel discard options.

OpenGL: Able to see inside of an object despite the fact that transparency is set to 1.0

I am trying to check transparency and how it works.
I have created a solidsphere and a solidcube in it.
I have enabled
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
The transparency works as expected using color4f.
The problem is that when I am setting the transparency of the sphere to 1.0, the cube still appears.
I am also using these:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glClearDepth(1);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
How can I make the cube visible only when the transparency of the sphere is != 1.0 ?
Is there a reason you've set glFrontFace(GL_CW); ? The default is GL_CCW, so you'll be culling front faces, and rendering back faces. With the front of the sphere missing, you'll see inside it.
To elaborate:
When rendering translucent objects using a normal (src_alpha , 1-src_alpha) blend, the translucent surfaces need to be rendered in depth order, back to front.
If you render only the back-surfaces of an object, it may (if it's more or less symmetrical) appear normal, but it is in fact inside-out, and thus cannot occlude objects correctly which are actually inside it.
So for this to work, it is important both that the front-surfaces are not culled, and that the opaque or more distant objects are rendered first.
If you want both front and back of the sphere to render translucently, and correctly, you would need to render the back before the front. This could be done with polygon sorting, but for a convex object it would suffice to render it twice, with the backface culling inverted - so render back faces first, and then the front faces, in two seperate passes.

Visible lines beetween textures in SkyBox?

I made a skybox in opengl using five textures on five quads, the problem is that the lines(boarders) between the textures are visible:
How do you get of those lines?
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0.0f, 1.0f);
GL11.glVertex3f(-15.0f,-14.0f,-15.0f);
GL11.glTexCoord2f(1f, 1.0f);
GL11.glVertex3f(15.0f,-14.0f,-15.0f);
GL11.glTexCoord2f(1f, 0f);
GL11.glVertex3f(15.0f,16.0f,-15.0f);
GL11.glTexCoord2f(0.0f, 0f);
GL11.glVertex3f(-15.0f,16.0f,-15.0f);
GL11.glEnd();
This is one quad of the skybox, should it be done differently?
I had the same problem in OpenSceneGraph and adapted the OpenGL solution, which should be pretty straight forward. Basically just use glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS) when your program starts before you start your rendering loop and the lines should disappear.
There was some ambiguity in my research, and it is possible that the enum is actually GL_TEXTURE_CUBEMAP_SEAMLESS, but one or the other should work for you. It is a bit more complicated in OSG because you need to #define GL_TEXTURE_CUBE_MAP_SEAMLESS 0x884F then enable seamless cubemapping by using state->setMode(GL_TEXTURE_CUBE_MAP_SEAMLESS, osg::StateAttribute::ON), where state is a state set from the root of your scene graph.
I hope this helps, it worked for me.

opengl - How to draw square pixel with GL_POINTS

I try to use the following code to draw a square-shaped pixel with opengl
glPointSize(5.0f);
glBegin(GL_POINTS);
glVertex3f(1.0f, 1.0f, 1.0f);
glEnd();
However, the final result is a circle-shaped pixel.
Please take a look the reference http://risknfun.com/compform/w1.html
See the "Problem 4. A Grid". On the right side, the display image has square-shaped pixel.
It's partly up to the OpenGL implementation (i.e., it can vary with your graphics driver), but with a bit of luck, you can turn this on or off with glEnable(GL_POINT_SMOOTH); or glDisable(GL_POINT_SMOOTH); With point smoothing turned on, you'll normally get round points, but with it turned off you'll get square points.
You can also try to tell OpenGL not to spend time making GL_POINTS nice and round by calling:
glHint(GL_POINT_SMOOTH_HINT, GL_FASTEST);
But keep in mind that's just an hint. The OpenGL driver has ultimately the last word.

Why does my colored cube not work with GL_BLEND?

My cube isn't rendering as expected when I use GL_BLEND.
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE);
I'm also having a similar problem with drawing some semi-opaque vertices in front, which could well be related.
Related: Why do my semi-opaque vertices make background objects brighter in OpenGL?
Here's what it's supposed to look like:
Normal cube http://img408.imageshack.us/img408/2853/normalcube.png
And here's what it actually looks like:
Dark cube http://img7.imageshack.us/img7/7133/darkcube.png
Please see the code used to create the colored cube, and the code used to actually draw the cube.
The cube is being drawn like so:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glLoadIdentity();
// ... do some translation, rotation, etc ...
drawCube();
glPopMatrix();
// ... swap the buffers ...
You could try disabling all lighting before drawing the cube:
glDisable(GL_LIGHTING);
It looks like you have lighting enabled on the second one,
try with a glShadeModel( GL_FLAT ) before drawing,
This has me stomped. What it looks like is that some vertices have some alpha values that are non-opaque. However the code you posted has all 1. for alpha. So... in order to debug more, did you try to change your clear color to something non-black ? Say green ?
From the code, I doubt lighting is turned on, since no normals were specified.
Last comment, offtopic... You should really not use glBegin/glEnd (2 function calls per vertex + 2 per primitive is really not a good usage of the recent developments in OpenGL). Try glDrawElements with QUAD_LIST, or even better, TRIANGLE_LIST. You already have the data nicely laid out for that.