I want to ask a question about transformation.
glPushMatrix();
glTranslatef(0.0, -10, 0.0);
glScalef(5000.0, 10.0, 5000.0);
glPushMatrix();
glColor3f(0,0,0);
glutSolidCube(1);
glColor3f(0.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
glPopMatrix();
The above code is the one I wrote to create a cube. In this case, I have understood the centre of the cube will be on y = -5. Did I understand correctly?
One of the great things about computer graphics is that you can always run the program and see what happens. And then swap the order of the translate and scale calls and run it again. You'll figure it out.
In this case, you may have difficulties because there's no reference to compare the cube to. I often use a little drawAxes() function, and recommend it to students, that just draws three lines from 0,0,0 to each of X=1, Y=1, Z=1. (Helps to draw the X line in red, Y in green, Z in blue.) Draw the axes as the very first thing you do so you've got a reference point, draw them again anytime you're not sure where a series of transformations will end up.
Related
I'm doing this OpenGL project for my Computer Graphics class, where I display an object and I rotate it and stuff, the thing is that at the beginning of the project we used glOrtho() and it looked really great.
But now the teacher said that we have to use glFrustum() for perspective and if I use that function, the object is drawn like this and I really don't know why does this happens:
This is my code from the init() function where everything changes:
void init (void)
{
/* select clearing (background) color */
glClearColor (0.0, 0.0, 0.0, 0.0);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100.0, 100.0, -60.0, 160.0, -100.0, 100.0);
//glFrustum(-100, 100 ,-100 ,100 ,1 , 40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(90,0,1,0);
}
I'd appreciate your help.
EDIT: If I use glFrustum(-100, 100, -100, 100, 20, 200) it looks like this, like I'm getting closer but what about the left, right, top and bottom parameters? Are they okay with that values?
It's hard to be certain without more information. Perhaps the model could give some insight. But I suspect it may have to do with your clipping planes (nearVal and farVal as described here) arguments passed to glFrustum (1, 40). Perhaps try setting them to a broader range like your glOrtho call: 1, 150 (Note: neither nearVal or farVal can be negative when passed to glFrustum).
This all depends on the scale of the model and how it is positioned relative to the camera. If part of the model falls outside of the clipping planes, then it will be, well... clipped.
I'm following Computer Graphics Through OpenGL, 2nd Edition by Sumanta Guha second edition, and in the page 35, it says that
Raising the first vertex of (the original) square.cpp from glVertex3f(20.0, 20.0, 0.0) to glVertex3f(20.0, 20.0, 1.5) causes the square – actually, the new figure which is not a square any more – to be clipped. If, instead, the second vertex is raised from glVertex3f(80.0, 20.0, 0.0) to glVertex3f(80.0, 20.0, 1.5), then the figure is clipped too, but very differently from when the first vertex is raised. Why? Should not the results be similar by symmetry?
Hint: OpenGL draws polygons after triangulating them as so-called triangle fans with the first vertex of the polygon the center of the fan. For example, the fan in Figure 2.16 consists of three triangles around vertex v0.
where the corresponding code looks like
glVertex3f(20.0, 20.0, 0.0);
glVertex3f(80.0, 20.0, 0.0);
glVertex3f(80.0, 80.0, 0.0);
glVertex3f(20.0, 80.0, 0.0);
if I set only the z-axis of the first vertex to 1.5f, I get such an output,
And if I set only the z-axis of the second vertex to 1.5f, I get the following output
In the latter case, I can understand why I get that output because of the clipping, but I don't understand why I get that output in the former case.
You are drawing two triangles: A,B,C and A,C,D.
If you change one z of one of the vertices, both triangles will not lie in the same plane any more.
In the first case, you change A which affects both trianges. In the second case, you are changing B which will affect only the second triangle.
Be warned that the code you are using is horribly outdated, and will not work in a modern core profile of OpenGL, where "modern" means: since a decade.
I am trying to draw a line along the point 0.5,-0.5,0.0 to 0.5,-0.5,-0.5 using GL_LINES in the z direction .
Intialization of the window :
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH|GLUT_RGB);
Setup in the display function.
glClearColor(1.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
However, the line is not displayed on the screen. Please help as to how to display a line going along the z direction.
You should probably share the piece of code where you actually attempt to draw the line using GL_LINES. Without it I must assume that you don't know how to do it properly. The correct way to draw the line after the setup is:
glBegin(GL_LINES);
glVertex3f(0.5f, -0.5f, 0.0f);
glVertex3f(0.5f, -0.5f, -0.5f);
glEnd();
Have you tried it that way? Also, if you use double buffering, don't forget to swap buffers after rendering, using glutSwapBuffers() when using glut or SwapBuffers(hdc) when not using it.
Edit:
Additionally you need to setup your camera correctly and move it slightly to actually see the line that you draw (it is possible that it's outside of the view area)
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45,1,0.1,100); //Example values but should work in your case
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
This piece of code should setup your projection correctly. Now OpenGL by default looks in the negative direction of Z axis so if you want to see your line you need to move tha camera towards the positive end of Z axis using the code (in fact the code moves the whole world, not just your camera, but it doesn't matter):
glTranslate(0.0f,0.0f,-1.0f);
Use this before glBegin and you should be good to go.
I have an object which is at (0.0, 0.0, -39.0) and never moves, the rest of the scene moves relative to this object; I want the camera to look at this object and at the same direction. When I do this:
gluLookAt(0.0, 120.0, 10.0, 0.0, 0.0, -39.0, 0.0, 0.0, 1.0);
drawobject();
glTranslatef(0.0, y, 0.0);
glRotatef(angle, 0.0, 0.0, 1.0);
drawScene();
instead of scene rotating around object (actually around world z-axis), it rotates around its center. If I change the order of translation and rotation then it rotates around object correctly, but it translates in the direction of scene y-direction (I want it translate towards world y-direction). I think I know why this happens, but I don't know how to fix it. I guess I have made myself confused XD.
I should add that I use arrow keys to get input from user to change "y" and "angle". So, basically if this object is the player, I want the camera look at its back at all times, and player can move around using the keyboard. However, instead of moving the player, I decided to move the scene. I am not sure if this is the standard approach though.
I'm working on interactive scenes for a computer graphics course. I've set up a program which will generate color cubes, and let me rotate them with the keyboard. However they're getting cut open by the near clip plane of my camera:
I've tried to use gluPerspective, but the OpenGL documentation doesn't give any examples of its use. I found it being used in an example program online, and semi-replicated their code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 65, 1, 0.01, 100 );
glMatrixMode(GL_MODELVIEW);
Any thoughts?
UPDATE:
As suggested in the comments below, I tried using glFrustum instead, with the following code:
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum( -0.5, 0.5, -0.5, 0.5, 0.1, 100 );
glMatrixMode(GL_MODELVIEW);
Again, there was no difference. Am I not pushing the resulting matrices correctly or something?
Perhaps you need to move your objects a little farther from the Camera. Right now it seems that they are closer than 0.0.
Considering your update "I moved the cubes one whole unit away from the camera, and now as they rotate they get clipped by both the near and the far clip planes" your cubes may be too large for your clipping depth (100 - 0.1). Move cubes away from the camera by 50 and set your clipping planes to 0.1 .. 1000 to make sure everything fits.
If the problem remains we might need to look at your matrices code.