OpenGL Issue with Camera(?) - c++

I'm learning OpenGL and I have a problem with my program where I'm supposed to make the solar system.
First of all here's the code I use to setup my ModelView Matrix:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(20, 1, 0, 0);
glTranslatef(0, -20, -60);
And then I draw the orbits using line loops and the sun is a gluSphere:
glPushMatrix();
glColor3f(1, 0.4f, 0);
glTranslatef(0, -2, 0);
gluSphere(gluNewQuadric(), 4, 30, 30);
glPopMatrix();
And here's the result:
But then, when I "zoom in" using this code:
if (key=='w')
{
glTranslatef(0, 1, 2.4);
}
else if (key=='s')
{
glTranslatef(0, -1, -2.4);
}
this happens:
the lines stay in front of the sphere. I know it's probably something dumb I'm doing but I'm just starting to learn and this is really slowing me down..
Thanks!

You probably don't have the depth test turned on.
glEnable(GL_DEPTH_TEST);
You may also need to fiddle with the depth test parameters, though usually the default setting is sufficient.
glDepthfunc(GL_LESS);
I'd also like to take this time to strongly recommend that you stop using OpenGL's Immediate Mode and OpenGL's Fixed Function Pipeline, and learn Modern OpenGL.

Related

Programming an EFIS for X-Plane

I am trying to develop a working EFIS display written in C++, OpenGL and the X-Plane SDK for an aircraft in X-Plane. I do not have very much experience with C++ and OpenGL. However, I do know X-Plane fairly well and I know how to use the X-Plane data for moving each element. What I do not know is how to code the EFIS display to draw all of the elements in an efficient way. I only know the very basics of drawing an OpenGL GL_QUAD and binding a texture to it however this seems to be a very low-level way of doing things.
What I would like to be able to do is create the GUI for the EFIS in a more efficient way as there are a lot of texture elements that need to be drawn.
This is an example of what I would like to build for X-Plane:
Here is the code I have currently written that loads in 1 image texture and binds it to a GL_QUAD.
static int my_draw_tex(
XPLMDrawingPhase inPhase,
int inIsBefore,
void* inRefcon)
{
// Note: if the tex size is not changing, glTexSubImage2D is faster than glTexImage2D.
// The drawing part.
XPLMSetGraphicsState(
0, // No fog, equivalent to glDisable(GL_FOG);
1, // One texture, equivalent to glEnable(GL_TEXTURE_2D);
0, // No lighting, equivalent to glDisable(GL_LIGHT0);
0, // No alpha testing, e.g glDisable(GL_ALPHA_TEST);
1, // Use alpha blending, e.g. glEnable(GL_BLEND);
0, // No depth read, e.g. glDisable(GL_DEPTH_TEST);
0); // No depth write, e.g. glDepthMask(GL_FALSE);
//---------------------------------------------- HORIZON -----------------------------------------//
glPushMatrix();
// Bind the Texture
XPLMBindTexture2d(texName[HORIZON], 0);
glColor3f(1, 1, 1);
glBegin(GL_QUADS);
// Initial coordinates for the horizon background
int arry[] = { 838, 465, 838, 2915, 2154, 2915, 2154 ,465 };
// Coordinates for the image
glTexCoord2f(0, 0); glVertex2f(arry[0], arry[1]);
glTexCoord2f(0, 1); glVertex2f(arry[2], arry[3]);
glTexCoord2f(1, 1); glVertex2f(arry[4], arry[5]);
glTexCoord2f(1, 0); glVertex2f(arry[6], arry[7]);
glEnd();
/*glDisable(GL_SCISSOR_TEST);*/
glPopMatrix();
return 1;
}
If someone could help with this, I would greatly appreciate it.

Drawing a sphere in a room opengl

I have the code for a room provided to me, I need to add a sphere in the room. It goes like this:
PushMatrix();
//draw floor,walls,ceilings
PushMatrix();
//draw some boxes on front wall
PopMatrix();
PushMatrix();
//drawing sphere;
glLoadIdentity();
glColor3f(1, 0, 0);
glTranslatef(0, ypos, 0);
glutSolidSphere(2, 20, 20);
PopMatrix();
PopMatrix();
But all the walls etc turn red (and no sphere) when I do this. Why does that happen even after pushing another matrix?
Calling glColor*() sets the current color. This color doesn't change before you call glColor*() again. In other words glPushMatrix() and glPopMatrix() has no effect on the current color.
Thus if you in //draw some boxes on front wall don't call glColor*(). Then due to you setting the color to red by calling glColor3f(1, 0, 0) then everything is going to be red from that point on.
Considering:
glMatrixMode(GL_PROJECTION);
gluPerspective(40, 1, 1, 40);
glMatrixMode(GL_MODELVIEW);
Then:
glPushMatrix();
glTranslatef(0, 0, -10);
glColor3f(1, 0, 0);
glutSolidSphere(2, 20, 20);
glPopMatrix();
By doing the above you should see a sphere.
Thus if you before didn't see the sphere at all. Then it's probably due to ypos being outside your view. If you were referring to the whole screen being red, then I'm assuming ypos to be around 0, which means that it would be filling the whole screen (Again assuming you don't translate the view in any other way).

OpenGL object not rotating

So, I've been trying to rotate a single object in an OpenGL/GLUT environment.
After going through several questions on SO and the like, I've written what appears to be correct code, but no dice. Does anyone know how to make this work?
PS: I've tried changing the GLMatrixmode to Projection, but that just shows a black screen. Same thing happens if I use glLoadIdentity().
Here's my rendering code
void display()
{
preProcessEvents();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(Camera::position.x, Camera::position.y, Camera::position.z,
Camera::position.x+Math::sind(Camera::rotationAngles.x)*Math::cosd(Camera::rotationAngles.y),
Camera::position.y+Math::cosd(Camera::rotationAngles.x),
Camera::position.z+Math::sind(Camera::rotationAngles.x)*Math::sind(Camera::rotationAngles.y),
0.0, 1.0, 0.0);
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(-1, 0,-3);
glColor3f(0, 1, 0);
glVertex3f(0.0f, 2.0f,-3);
glColor3f(0, 0, 1);
glVertex3f(1.0f, 0.0f,-3);
glEnd();
glBindTexture(GL_TEXTURE_2D, tex->textureID);
glBegin(GL_QUADS);
glColor3f(1, 1, 1);
glTexCoord2f(100, 100);
glVertex3f(100,0,100);
glTexCoord2f(-100, 100);
glVertex3f(-100,0,100);
glTexCoord2f(-100,-100);
glVertex3f(-100,0,-100);
glTexCoord2f(100,-100);
glVertex3f(100,0,-100);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
object1.draw();
glTranslatef(-10.0, 10.0, 0.0);
glBindTexture(GL_TEXTURE_2D, tex2->textureID);
gluQuadricTexture(quad,1);
gluSphere(quad,10,20,20);
glBindTexture(GL_TEXTURE_2D, 0);
//RELEVANT CODE STARTS HERE
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(190, 0.0, 0.0, 1.0);
glPopMatrix();
glutSwapBuffers();
}
Are you aware what glPushMatrix and glPopMatrix do? They save and restore the "current" matrix.
By enclosing your rotation in that and then doing no actual drawing operation before restoring the matrix the entire sequence of code beginning with //RELEVANT CODE STARTS HERE is completely pointless.
Even if you did not push/pop, your rotation would only be applied the next time you draw something. Logically you might think that would mean the next time you call display (...), but one of the first things you do in display (...) is replace the current matrix with an identity matrix (line 3).
In all honesty, you should consider abandoning whatever resource you are currently using to learn OpenGL. You are using deprecated functionality and missing a few fundamentals. A good OpenGL 3.0 tutorial will usually touch on the basics of transformation matrices.
As for why changing the matrix mode to projection produces a black screen, that is because the next time you call display (...), gluLookAt operates on the projection matrix. In effect, you wind up applying the camera transformation twice. You should really add glMatrixMode (GL_MODELVIEW) to the beginning of display (...) to avoid this.
You do the rotation (and reset it with glPopMatrix) after you draw, do the rotation code before the glBegin/glEnd calls.
Or just move to the shader based pipeline and manage you own transformation matrices.

openGL transformation and gluLookAt()

I have a studying project which represents simple 3D scene. I want to draw sphere in some non-origin point. Later I'm going implement this as separate function or method.
I'm setting point of view using gluLookAt() then I'm transforming model-view matrix using glTranslatef() with little offset and drawing sphere. Unfortunately, the sphere isn't shown. Am I right with model-view matrix approaching?
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1, 0 ,1, 0, 0, 0, 0, 1, 0);
glColor3b(197, 96, 63);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.1, 0, 0);
glutWireSphere(0.2, 20, 10);
glPopMatrix();
glFlush();
}
void reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho ((float)w/(float)h, (float)-w/(float)h, -1, 1, 0.8, 100);
glMatrixMode(GL_MODELVIEW);
}
No, you aren't.
gluLookAt(1, 0 ,1, 0, 0, 0, 0, 1, 0);
glColor3b(197, 96, 63);
glPushMatrix();
glLoadIdentity(); // why it should be there?
By zeroing the view matrix there, you are drawing your object relatively to the origin coordinates, not taking your glLookAt into account. The call to it is effectively ignored. It should be coded as:
Set up the "camera matrix"
Push the matrix on the stack
Translate to the object's position
Draw the object
Pop and go back to 2.
So if you want to set up hypothetical "camera", you have to combine positions of objects with the camera matrix itself.
Your approach doesn't look that unreasonable. The problem is here:
glPushMatrix();
glLoadIdentity();
glTranslatef(0.1, 0, 0);
The pushing (and later popping) is a good idea, but by setting the matrix to identity before the translation, you loose any transformations done before, in particular the viewing transformations established with gluLookAt. So just remove this glLoadIdentity to properly concatenate the individual transformations.
Always keep in mind that all the matrix transformation functions, like glTranslate, glOrtho, or gluLookat always modify the currently selected (with glMatrixMode) matrix and don't just replace it. This is also the reason why you do a glLoadIdentity before the calls to glOrtho and gluLookAt.

GL_POLYGON not filled properly?

Why are those lines appearing in my shape?
I'm initializing OpenGL like this:
glDisable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glEnable(GL_POLYGON_SMOOTH);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0, 0, 0, 0);
And drawing the shape like this:
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1, 0, 0);
glBegin(GL_POLYGON);
glVertex2f(-5, -5); // bottom left
glVertex2f(5, -5); // bottom right...
glVertex2f(6, 0);
glVertex2f(5, 5);
glVertex2f(-5, 5);
glEnd();
Doesn't matter if it's clockwise or CCW.
I think disabling GL_POLYGON_SMOOTH would fix that, but you'd lose the antialiasing. FSAA would work as an alternative, but it'd be slower.
Edit: looking around, there are a lot of examples out there using glBlendFunc( GL_SRC_ALPHA_SATURATE, GL_ONE );
GL_POLYGON_SMOOTH is an antiquated and slow method of polygon anti-aliasing. It also results in the problem you see above.
Using the Multisample buffer extension is the best way to perform fast anti-aliasing on modern machines. Read more here.