OpenGL depth issue [closed] - opengl

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I draw a 3D shape with both fill mode and lines mode "wire frame", I get intersection between triangles and lines.
I enable the depth buffer before I renderGL.Enable(EnableCap.DepthTest); , this is the code of drawing the model:
GL.BindVertexArray(VAO);
Vector4 color;
int colorLoc = GL.GetUniformLocation(programID, "color");
if (BorderThickness > 0)
{
color = new Vector4((float)BorderColor.R / 255, (float)BorderColor.G / 255, (float)BorderColor.B / 255, (float)BorderColor.A / 255);
GL.Uniform4(colorLoc, color.X, color.Y, color.Z, color.W);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Line);
GL.LineWidth(BorderThickness);
GL.DrawArrays(PrimitiveType.LineStrip, 0, _positions.Count);
}
color = new Vector4((float)FillColor.R / 255, (float)FillColor.G / 255, (float)FillColor.B / 255, (float)FillColor.A / 255);
GL.Uniform4(colorLoc, color.X, color.Y, color.Z, color.W);
GL.PolygonMode(MaterialFace.FrontAndBack, PolygonMode.Fill);
GL.DrawArrays(PrimitiveType.Triangles, 0, _positions.Count);
GL.BindVertexArray(0);
I got the following result:
The result after adding glPolygonOffset
It fix the issue but the lines are not clear.

If the vertex that is far away is at a distance of 1100, then it is unlikely that you need a close plane at 0.1f precision.
If we look at the model, all the points seem to be in a similar scale. This kind of issue looks a lot like a Z-fighting issue.
Try
projection = Matrix4.CreatePerspectiveFieldOfView(MathHelper.PiOver4, (float)width / (float)height, 10f, 2000f)
You will be significantly more precise at the scale you are working. You'll get clipping if your points are less then 10 away from the camera, but considering how big the mesh is, it shouldn't be an issue.
Since Z-buffer's precision is logarithmic, the close you are to the far plane, the lower your precision will be and the more Z-fighting you'll get. Considering your points are very close to the far plane, this would explain the issue we are seeing. By bringing the close plane to an order of magnitude a bit closer to the one of your vertices the issues should disappear.

Related

How to draw a image with alpha texture without changing the draw order [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
if i first draw a image with alpha channel at z depth 0.1 and after that i draw a rectangle at z depth 0.0.
The result is the following image where the transparent part of the image becomes black.
I can correct this by first drawing the rectangle and than drawing the image.
Since image is in front of the rectangle in z is their a way i can first draw the image and than draw the rectangle without having the transparent part of image becoming black.
After discarding the transparent fragments this is the result.

glClearColor(1.0, 1.0, 1.0, 1.0) makes the whole screen white, instead of just the background [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
When I draw shapes on a black background with glClearColor(0.0, 0.0, 0.0, 1.0);, everything gets drawn perfectly on a black background, like this:
Now when I increase the RGB closer to white (0.8, 0.8, 0.8, 1.0), things get fade out, like this:
When it's white, everything is completely fade out:
Changing the alpha to 0.0 doesn't make a difference either. I'm calling these two functions at the beginning of each frame (and their order doesn't make a difference):
glClearColor(clear_color.x, clear_color.y, clear_color.z, clear_color.w);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
What I want is obvious: to be able to see my blue arrows on white background.
The behavior described in the question can only happen when blending is enabled while drawing the arrows. Disable blending before drawing and everything should work as expected:
glDisable(GL_BLEND);

Texture transparency with P3D [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I’m currently coding a “Doom” clone, using P3D. I’m not using any external library.
I’m having a weird artifact that happens when I’m using a masked texture. I’ve tried several methods to fix it, but to no avail :(. DISABLE_DEPTH_MASK suppresses the artifact, but then my sprite get sorted all wrong.
If anyone can point me in the right direction, I would really appreciate it! I’m so close to having a functional engine!
(disregard the “doom” face and soldier sprites, of course, they are just temporary assets I’m using…)
artifact1As you can see, eventhough the mask is working (see the feet of the soldier) it’s leaving a huge black (background color) artifact behind.
artifact2When walking behind the quad, the artifact isn't too bad but still present.
You have to use/enable Blending.
This technique is used if you want ot render a sprite which is not completely opaque , but has partly or completely transparent areas
The soldier has be rendered at the end, after the environment of the scene is finished. The depth test has to be enabled.
Enable blending before you render soldier sprite (texture) with the alpha channel.
gl.glEnable(GL.GL_BLEND);
gl.glBlendFunc(GL.GL_SRC_ALPHA,GL.GL_ONE_MINUS_SRC_ALPHA);
And disable blending after:
gl.glDisable(GL.GL_BLEND);
Note, the "black" background of the soldier sprite has to have an alpha channel of 0.0 (completely transparent) and the alpha channel of the soldier itself has to be 1.0 (completely opaque). In common PNG images fulfill this.
Explanation:
At the point when the soldier is rendered, then the background has already been drawn and the background color (color of the environment) has been stored in the framebuffer.
The blending function is:
dest.rgba = dest.rgba * (1 - src.a) + src.rgba * src.a
where dest.rgba is the color in the framebuffer and src.rgba is the color of the sprite.
If the alpha channel of the sprite is 1.0 (src.a = 1.0; opaque), then
dest.rgba = dest.rgba * (1.0 - 1.0) + src.rgba * 1.0
dest.rgba = src.rgba
If the alpha channel of the sprite is 0.0 (src.a = 0.0; transparent), then
dest.rgba = dest.rgba * (1.0 - 0.0) + src.rgba * 0.0
dest.rgba = dest.rgba

glut sphere appear stretched when using non-square resolutions [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I have a code:
void kula(void)
{
glColor3f(1, 0, 0);
glutSolidSphere(0.2, 100.0, 100.0);
glFlush();
}
Next:
void Display()
{
..
kula();
..
}
When the window size is 600x600 the sphere is ok. But when the window size is not a square, for example 600x800 instead of the sphere is flattened egg. How do I deal with it?
It's a little hard to know without seeing your complete initialisation process, but I'll take a guess.
You are probably not setting up your projection matrix according your resolution, the gluPerspective can help you with that. You should call it whatever there's a windows resize event, like this:
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluPerspective( field_of_view, (double)w/h, zner, zfar );
Those links can help you
glutPerspective
How can I prevent the viewport from stretching/distorting?

Rubik's cube rotation [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I created a rubik's cube with 26 squares(not 27 since you cant see the middle one). I am trying to rotate the cube. I thought about using Pushstack Popstack, but I couldnt find any good examples I could look at.
I was wondering what is a good way to rotate. I used 26 of the following code to create the cubes
glBegin(GL_POLYGON);
//glColor3f( 1.0, 0.0, 1.0 );
glColor3f( 1.0, 0.0, 0.0 );
glVertex3f( 1.5, -0.5, -0.5 );
glVertex3f( 1.5, 0.5, -0.5 );
glVertex3f( 1.5, 0.5, 0.5 );
glVertex3f( 1.5, -0.5, 0.5 );
glEnd();
Your code produces a face, not a whole cube. But you will need 26 complete small cubes in order to render it correctly. Otherwise, if you rotate a cube, there will be holes.
You can do something like the following:
Organize the big cube as a 3x3x3 grid. Each grid cell contains a small cube. Each small cube consists of its geometry data and its rotation information. You can store the geometry data as a vertex buffer or as a display list or as a method that generates the geometry on the fly. Just as you wish. So if you don't have any rotations yet and you render the pure geometry, you should get the whole cube well-aligned.
For the rotations it is reasonable to use a quaternion for each small cube. However, you could use matrices as well, but those are a bit trickier to handle. Actually, I would store two quaternions for each cube. One that describes the target rotation and one that describes the current rotation (for animation purposes). When updating the current rotation towards the target rotation, you can do something like this:
interpolationVariable = c ^ timeStep //to allow a fluid and continuous animation.
//c is usually between 0.99 and 1, depending on the desired animation smoothness
currentRotation := interpolationVariable * currentRotation + (1 - interpolationVariable ) * targetRotation
currentRotation.normalize()
This is actually an infinite adjustment. You should introduce a threshold for the difference of currentRotation and targetRotation to set currentRotation to targetRotation and update only if currentRotation != targetRotation.
Now we have the rotation specified as a quaternion. In order to render the cube, you can apply the quaternion as a model transformation (after the conversion to a matrix) and render the geometry.
To rotate a cube slice, you simply have to apply a rotation transformation to the according cubes. E.g. if you want to rotate about the x-axis:
quat = QuaternionRotation( (1,0,0) /* axis */ , Pi / 2 /* angle */ )
for each affected cube
cube.targetRotation = cube.targetRotation * quat
next
// Update the grid
And the slice will rotate nicely to the target position. If you have the geometry well-alignes (around the origin), you don't need any translation because all rotations will be about the origin (or an axis through the origin).