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?
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a mini-project for Computer Graphics, the idea of the project is simulation of gravity on different planets. I tried to find a tutorial for creating different viewports, each viewport has its own code individually so I can customize each viewport separately but I couldn't find it, most of the tutorials or examples about having different viewports of the same scene but from different angle of view.
The main vision in my mind is to split the screen into 3 parts, each part have a falling object where the acceleration of the falling object simulates the gravity acceleration on that planet.
Usually you have one glViewport call and your render you scene afterwards. To render two different scenes you just do that twice:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // this clears the whole window
glViewport(0, 0, 100, 100);
render_scene_zero();
glViewport(100, 0, 100, 100);
render_scene_one();
Here render_scene_zero and render_scene_one are responsible for drawing the respective scene as-if it was the only scene visible. They can draw completely different things, e.g. drawing a cube in scene zero and a sphere in scene one.
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.
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);
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 7 years ago.
Improve this question
I am trying to texture a glutSolidTorus().
Here is my code:
glColor3f(1.0f, 1.0f, 1.0f);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tiring);
glutSolidTorus(.55, 1.8, 25, 25);
glDisable(GL_TEXTURE_2D);
But it does not work. How can I texture glutSolidTorus()?
Except for glut*Teapot() none of the GLUT geometry primitives provide texture coordinates:
11 Geometric Object Rendering
GLUT includes a number of routines for generating easily recognizable 3D geometric objects. These routines reflect functionality available in the aux toolkit described in the OpenGL Programmer's Guide and are included in GLUT to allow the construction of simple GLUT programs that render recognizable objects. These routines can be implemented as pure OpenGL rendering routines. The routines do not generate display lists for the objects they create.
The routines generate normals appropriate for lighting but do not generate texture coordinates (except for the teapot).
You have several options:
Fixed-function texcoord generation
Shader-based texcoord generation
Reimplement glutSolidTorus() to add texture coordinates
This question already has an answer here:
OpenGl coordinate system is not at -1 to 1
(1 answer)
Closed 9 years ago.
Fixed: I am trying to create a basic game in C++ using OpenGL. I can make a window with a square in it, and make the square move around. However, I am having trouble getting the window to be the correct size.
If I try and make the window to be 800 by 600 then the window and borders will be this size, not just the bit inside the border. Is there a way I can make it so that the bit inside the border is the size I define?
I pasted the code in http://pastebin.com/jxd5YhHa.
After skimming over your code I saw a lot of the typical newbie mistakes – this time low level Win32 API style.
First and foremost: Do yourself a favor and please don't use the naked Win32 API. There are so many nice OpenGL frameworks, which have the added benefit of making your program cross plattform for free. In your case I'd recommend SDL, available from http://www.libsdl.org
Now the main problem with your program is, that it scattered some vital OpenGL operations all over the place. Most importantly setting up viewport and matrices. Your drawing functions should always follow the following scheme
void draw(…)
{
glDisable(GL_SCISSOR_TEST);
glClearColor(…);
glClearDepth(…);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
for(subview in views) {
glViewport(…);
#ifdef USE_FIXED_FUNCTION_PIPELINE
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
projection_setup();
for(model in scene) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
modelview_setup();
#else
glUseProgram(…);
setup_uniforms();
#endif
draw_stuff();
}
}
SwapBuffers();
}
The key here to your problem is, to setup viewport, projection and modelview in the drawing code right when you need it to what you need it to be.