Removing polygons from the screen (OpenGL)? - opengl

Assuming the code is:
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glBegin(GL_TRIANGLES);
glVertex3f( 0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f,-1.0f, 0.0f);
glVertex3f( 1.0f,-1.0f, 0.0f);
glEnd();
glLoadIdentity();
//Drawing another object...
How would I change the code to erase the object? I know that commenting out glTranslatef() will erase the triangle, but is that the formal way to do it?

If you put glClear at the start of the draw function (draw function is usually in a loop) you can simply choose not to redraw the triangle, drawing like that will leave no reference to your triangle.
Also, glTranslatef() wont remove your triangle, glTranslatef() is just a function to move the current matrix (in your case the matrix with your triangle is being moved into the camera view)
glClear()
http://www.khronos.org/opengles/documentation/opengles1_0/html/glClear.html

If you're asking how to make the triangle go away in subsequent frames, there's no need. Every frame time you're responsible for redrawing everything. OpenGL will not remember your triangle.

Putting an if around glBegin...glVertex...glEnd would be the most straightforward way.

Related

Why does this OpenGL rotation also translate my object?

I'm trying to rotate an object (an arrow) which has it's default position as pointing to the right. I've been looking around and I'm using the glLoadIdentity() and glPushMatrix() and glPopMatrix() as a way to only rotate the object in my glBegin function and not the whole scene:
glLoadIdentity();
glPushMatrix();
glRotatef(5, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex2f(xx2-0.01, yy2);
glVertex2f(xx2-0.06, yy2+0.03);
glVertex2f(xx2-0.06, yy2-0.03);
glEnd();
glPopMatrix();
However, it also translates my arrow, instead of only rotating it. Do I need to translate the offset back to it's original position? Or am I doing something wrong?
SOLVED:
glLoadIdentity();
glPushMatrix();
glTranslatef(xx2, yy2, 0);
glRotatef(45, 0.0f, 0.0f, 1.0f);
glTranslatef(-xx2, -yy2, 0);
glBegin(GL_TRIANGLE_FAN);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex2f(xx2-0.01, yy2);
glVertex2f(xx2-0.06, yy2+0.03);
glVertex2f(xx2-0.06, yy2-0.03);
glEnd();
glPopMatrix();
Need to translate back to center (0, 0, 0), apply the rotation and then back to it's original position (xx2, yy2, 0)
All rotations are around the origin.
So yes, if you want to rotate around some other point, you must translate from that point to the origin, rotate, and then translate back again.
Your code is correct, but you are applying your operations out of order. Specifically, you need to apply the rotation first, then apply the translation.

opengl rotate an object to set its correct face before render on screen

I am loading an object through an obj file in opengl using GLM Library but it comes out on the screen upside down. Also i am providing a capability to user so he can rotate all the objects accordingly by mouse too. e.g. zoom and zoom in and rotate with y axis.
Problem is i dont know how to rotate the object first to make its face according to what i need. After that i want to draw this object and offcourse at that time mouse can play its role to rotate it.
my draw function contain the following code
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glTranslatef(m_fPosX, m_fPosY,-m_fZoom);
glRotatef(m_fRotX, 1.0f, 0.0f, 0.0f);//these two for mouse movement
glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);
glRotated(180,0.0f, 0.0f, -1.0f);
glDisable(GL_TEXTURE_2D);
glColor3f(0.90f,0.90f,0.90f);
glmDraw(m_p3dModel,GLM_SMOOTH | GLM_MATERIAL);
glPopMatrix();
here rotate functions are all dependent on mouse movement but if it get load upside down i dont know how to first make my object face right direction and then allow it in this draw function...Which means i need to set its face before calling this draw function.
glRotatef(m_fRotY, 1.0f, 0.0f, 0.0f);
should be
glRotatef(m_fRotY, 0.0f, 1.0f, 0.0f);
copy/paste is the mother of many evils :o)
For transformation / projection / model view tutorial : check link

OpenGL basics : drawing a multi-color square

What I want to do is to draw a square where each vertex is supposed to have a different color.
This should lead into a nice gradient within the square.
Here's the code I'm using:
glBegin(GL_QUADS);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(((float)(winWidth-redLineWidth))/2.f,((float)(winHeight-redLineWidth))/2.f);
glColor3f(0.0f, 0.0f, 0.0f);
glVertex2f(((float)(winWidth+redLineWidth))/2.f,((float)(winHeight-redLineWidth))/2.f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(((float)(winWidth+redLineWidth))/2.f,((float)(winHeight+redLineWidth))/2.f);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex2f(((float)(winWidth-redLineWidth))/2.f,((float)(winHeight+redLineWidth))/2.f);
glEnd();
Please ignore the variables used.
I get a rectangle painted, but it has a solid color.
Where's the error here?
I'm using GLUT on Mac OS X.
It seems unlikely that you've changed this, but you might try to add a glShadeModel(GL_SMOOTH) call before your drawing code. The default behavior should do as you expected though, so the problem is likely elsewhere.

How to rotate model not camera in OpenGL?

I have the same qustion as in the title :/
I make something like:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0f, 0, -zoom);
glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
//Here model render :/
And in my app camera is rotating not model :/
Presumably the reason you believe it's your camera moving and not your model is because all the objects in the scene are moving together?
After rendering your model and before rendering other objects, are you resetting the MODELVIEW matrix? In other words, are you doing another glLoadIdentity() or glPopMatrix() after you render the model you're talking about and before rendering other objects?
Because if not, whatever transformations you applied to that model will also apply to other objects rendered, and it will be as if you rotated the whole world (or the camera).
I think there may be another problem with your code though:
glTranslatef(0.0f, 0, -zoom);
glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
//Here model render :/
Are you trying to rotate your model around the point (0, 0, -zoom)?
Normally, in order to rotate around a certain point (x,y,z), you do:
Translate (x,y,z) to the origin (0,0,0) by translating by the vector (-x,-y,-z)
Perform rotations
Translate the origin back to (x,y,z) by translating by the vector (x,y,z)
Draw
If you are trying to rotate around the point (0,0,zoom), you are missing step 3.
So try adding this before you render your model:
glTranslatef(0.0f, 0, zoom); // translate back from origin
On the other hand if you are trying to rotate the model around the origin (0,0,0), and also move it along the z-axis, then you will want your translation to come after your rotation, as #nonVirtual said.
And don't forget to reset the MODELVIEW matrix before you draw other objects. So the whole sequence would be something like:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
#if you want to rotate around (0, 0, -zoom):
glTranslatef(0.0f, 0, -zoom);
glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 0, zoom); // translate back from origin
#else if you want to rotate around (0, 0, 0):
glRotatef(yr*20+moveYr*20, 0.0f, 1.0f, 0.0f);
glRotatef(zr*20+moveZr*20, 1.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 0, -zoom);
#endif
//Here model render :/
glLoadIdentity();
// translate/rotate for other objects if necessary
// draw other objects
You use GL_MODELVIEW when defining the transformation for your objects and GL_PROJECTION to transform your viewpoint.
I believe it has to do with the order in which you are applying your transformations. Try switching the order of the call to glRotate and glTranslate. As it is now you are moving it outward from the origin, then rotating it about the origin, which will give the appearance of the object orbiting around the camera. If you instead rotate it while it is still at the origin, then move it, it should give you the desired result. Hope that helps.

OpenGL lighting problem when rotating the camera

I draw buildings in my game world, i shade them with the following code:
GLfloat light_ambient[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat light_position[] = {135.66f, 129.83f, 4.7f, 1.0f};
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glColorMaterial(GL_FRONT, GL_AMBIENT);
It works nicely.
But when i start flying in the world, the lighting reacts to that as if the world was an object that is being rotated. So the lights changes when my camera angle changes.
How do i revert that rotation? so the lighting would think that i am not actually rotating the world, and then i could make my buildings have static shading which would change depending on where the sun is on the sky.
Edit: here is the rendering code:
int DrawGLScene()
{
// stuff
glLoadIdentity();
glRotatef(XROT, 1.0f, 0.0f, 0.0f);
glRotatef(YROT, 0.0f, 1.0f, 0.0f);
glRotatef(ZROT, 0.0f, 0.0f, 1.0f);
glTranslatef(-XPOS, -YPOS, -ZPOS);
// draw world
}
http://www.opengl.org/resources/faq/technical/lights.htm
See #18.050
In short, you need to make sure you're defining your light position in the right reference frame and applying the appropriate transforms each frame to keep it where you want it.
Edit:
With the removal of the fixed function pipeline in OpenGL 3.1 the code and answer here are deprecated. The correct answer now is to pass your light position(s) into your vertex/fragment shaders and perform your shading using that position in world space. This calculation varies based on the type of lighting you're doing (PBR, phong, deferred etc).
I believe your problem arises from the fact that OpenGL Lights are specified in World Coordinates, but stored internally in Camera Coordinates. This means that once you place a light, whenever you move your camera, your light will move with it.
To fix this, simply reposition your lights whenever you move your camera.