Perspective

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Perspective

Post by Ginto8 »

szdarkhack answered your question, even if you didn't realize it.

OpenGL's coordinate system is not quite the same as how you'd think of a camera's coordinate system; with a camera, a higher z means it's farther away. But in OpenGL it's actually quite the opposite: the higher the z value, the closer something is to the camera. The reason gluPerspective works when you have 0 as an angle is because it doesn't really do anything when the angle is 0. Otherwise, it catches that you're drawing something that's actually behind the camera, and disregards it. The solution to all this? Negate all your z values. If you make them

Code: Select all

glTranslatef(0, 0,-translationAmount);
and

Code: Select all

gluPerspective(45.0f,2.0f,-0.1f,-100.0f);
your problems should disappear, and you should be able to have angles other than 0.

He also brought up the fact that you have an entire function defined in your header file. In this case, there's no point in having the header file, and if you try to use it like you would a normal header file you can have tons of problems, primarily multiple include and multiple definition problems. The point of a header is to separate interface and implementation. Think about the buttons in an elevator: they all pretty much look the same, no matter which elevator your in. The same is true with a lot of programming libraries: it doesn't matter which version they are or what internal tweaks they have, they all look exactly the same. In the case of your header, you haven't separated the implementation from the interface whatsoever, and honestly, in this case, there's no reason to have a header file at all. Are you expecting to call initialize_it() from a different file? Because if so, you didn't approach it correctly. There are two main features that all good header files have: include guards, and declarations without implementations.

Include guards are pretty simple to do; just format your header like this:

Code: Select all

#ifndef MYFILE_H
#define MYFILE_H

/* other stuff goes here */

#endif
MYFILE_H can be anything, but the convention is the filename in all caps with a _ separating the name and file suffix. If you'd like to know how these work, Wikipedia has a pages on both include guards and the C preprocessor.

Declarations without implementations are also simple. Just declare a function in your header:

Code: Select all

void foo();
and implement it in your source file:

Code: Select all

void foo() {
    /* content goes here */
}
and voila, separated interface and implementation.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

OK.
Thanks guys.

I've never taken an official class on programming. I've mostly looked through books and online to learn a lot of the basics, so I guess I just assumed a header file was just an extra file for when you want to keep your cpp shorter and everything in order. Didn't know I wasn't supposed to define a function there.

Well thanks, guys.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

Still doesn't show a thing.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: Perspective

Post by Pornomag »

Ginto8 wrote:...

Code: Select all

gluPerspective(45.0f,2.0f,-0.1f,-100.0f);
your problems should disappear, and you should be able to have angles other than 0.
last 2 parameters MUST be positive ;)
http://www.opengl.org/sdk/docs/man/xhtm ... ective.xml

Oh and Ben, post your code (hope you don't mind me calling you Ben S:)
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

I did post my code.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: Perspective

Post by Pornomag »

OK, well I searched for your code and I found it (hopefully it's the right one). I'm not *that* experienced with OpenGL but I do know a fair amount of it.

First things first we'll start off how your initializing your scene.

Code: Select all

void initialize_it(void)   //sets up graphic stuff.
{
   glClearColor(0.0, 0.0, 0.0, 0.0);
   glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
   glutInitWindowSize(400,200);
   glutInitWindowPosition(100,50);
   glutCreateWindow("Moving Square");
   glMatrixMode(GL_PROJECTION);
   glLoadIdentity();
   glOrtho(0.0,1.0,0.1,1.0,-1.0,1.0);
   gluPerspective(0.0f,2.0f,0.1f,100.0f);
   glEnable(GL_DEPTH_TEST);
   glDepthFunc(GL_LEQUAL);
   glMatrixMode(GL_MODELVIEW);
   glLoadIdentity();
}
First thing I want to mention is the glutInitDisplayMode() function, you're not using Double Buffering therefore you don't need to swap buffers AND there is a chance when your doing animation that it WILL flicker.

Look at line 6, where you are setting the matrix mode to the projection matrix, you then load the identity matrix which resets the matrix to the identity matrix (which was explained in a previous post). You then call glOrtho(), which makes the Projection matrix ORTHOGRAPHIC, which means that it will not care how far/close an object is, it will stay the same, therefore delete that function call. Then you try and setup the projection matrix to be a perspective view with gluPerspective(), now what you're doing there is making the Field of View 0 degrees, which basically means you wont be able to see anything.

The field of view really depends on how far your sitting away from the display (monitor), I'd set it around 60-70 degrees. Play around with it and you'll see the effect. The 2nd parameter of the gluPerspective is the ASPECT RATIO of the display, which is width/height (so it should be (float)400/200). Just make sure that the last 2 parameters of gluPerspective is positive and you should be OK. Everything else seems fine in that function (you shouldn't really need to change the glDepthFunc() but you can if you want to).

NOTE:
If you play Minecraft you can set the field of view from 69(Normal)-110(Quake Pro).

Now for your rendering code:

Code: Select all

void rect_display(void)   
{
   glClearColor(0, 0, 0, 0);   
   glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
   glMatrixMode(GL_MODELVIEW);
   
   
   glPushMatrix();
   
   glTranslatef(0, 0,translationAmount);
   translationAmount+=0.0001;
   glBegin(GL_TRIANGLES);
        
        glColor3f(0.0, 0.0, 1.0);  //front.
        
        glVertex3f(0.3, 0.8, 0.2);
        glVertex3f(0.3, 0.2, 0.2);
        glVertex3f(0.8, 0.8, 0.2);
   
   glEnd();
   
   glPopMatrix();
   
   glFlush();
   glutSwapBuffers();
   glutPostRedisplay();  
}
First off, you don't really need to set the clearing colour again, as you already did it beforehand, after you push on the matrix stack, you *might* want to reset to the identity matrix (glLoadIdentity()), unless you are wanting to do something o:).

The z-axis is pointing towards you in the positive direction (therefore you should make your glTranslate(0, 0, NEGATIVE_VALUE); and make all your glVertex3f to have negative values (or zero) for the 3rd parameter (z-axis)).

Last but not least, unless your using that function for the idle function you have no need to call glutPostRedisplay(), also seeing as your using single buffering, you don't need to tell glut to swap the buffers (so delete glutSwapBuffers()), but if you were using double buffering then you shouldn't use glFlush().

I hope this helped a bit, if this still doesn't work, repost your code and I or someone will try and help O:! Goodluck.

P.S: Sorry for my terrible grammar skills.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

Still not seeing the shape.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: Perspective

Post by Pornomag »

Post your current code then!
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Perspective

Post by Ginto8 »

Benjamin100 wrote:I've never taken an official class on programming.
And I have yet to take a programming class that really teaches me things that I haven't already taught myself. There's so much information out there, why wait for someone to organize it into a curriculum and spoon-feed it to you? That said, formal programming classes have helped me go outside my typical comfort zone (using Java for instance), strengthen some things (sorting algorithms and data structures), and kept me sharp when my other schoolwork has been too high-load to do any programming outside of class. It's very helpful to take a programming class, but infinitely more helpful to take the initiative yourself and learn things.

But like pornomag said, post your code; trying to solve problems without code to look at is like trying to fix busted plumbing when the only thing you know about is the shit that is suddenly covering the bathroom walls.

If you'd like, you can come on the IRC channel to chat, and I or someone else can help you in real time. If you don't know how to join it, download xchat2 from http://www.silverex.org/download/, then join the channel #elysian_shadows on the server freenode.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

My Xchat trial expired.

It is the SAME code as before, except no "glOrtho" And I changed the z values to negative.
There isn't much else to it.
That is all I changed.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Perspective

Post by qpHalcy0n »

Did you read any of the posts? They solve all of the problems relevant to the code that you POSTED.
If there is OpenGL code that you HAVE NOT posted, then you likely have violated the current render state at which point we can't be sure wtf is happening.

1) Your FOV (first parameter to gluPerspective) is not useful. 45-95 degrees are good values to get you started.
2) You do not have a view matrix, therefore the vertices are untransformed by that stage (except to be translated). Since the z axis is negative INTO the scene by default, the z-values for your vertices must pass from -0.1 to -100 (the absolute value of the near and far clip planes). If they do not, then they will be clipped.
3) Be sure "translationAmount" decreases from 0. Not increases, because the geometry will not pass through the view frustum otherwise.
4) You do not "swap buffers" with single buffering. Switch to GLUT_DOUBLE or do not call glutSwapBuffers

All of these things must happen. You only indicate that you've done one or two. We can't help you further until you do this.
Pornomag
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 41
Joined: Tue Jun 21, 2011 5:39 am
Programming Language of Choice: C++

Re: Perspective

Post by Pornomag »

OK I honestly just tried to get your code working (which I did), it does work with 0 degrees field of view, it just doesn't shrink the triangle for some reason, use 60 degrees and just move the triangle in and out. The only other problem(s) you should be having are the following, you've set your field of view to 60 degrees and not fixed your coordinates to draw the triangle (the coordinates should be a bit bigger).

When your initialising glut, you weren't initialising it with depth, like so:

Code: Select all

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
Your not loading the identity matrix after you push on the stack (this didn't seem to effect it, but it could be a possible solution):

Code: Select all

glPushMatrix();
glLoadIdentity(); 
You are translating too far in/out, try changing your glTranslate to a constant like so:

Code: Select all

#define TRANSLATE_Z_VALUE -5.f
// ...

glTranslatef(0, 0, TRANSLATE_Z_VALUE);
Or if you want... Change it interactively like so:

Code: Select all


float z_move = -5.f;

void key(unsigned char key, int, int)
{
    switch (key)
    {
        case '1':
            z_move -= 0.1f;
            break;
        case '2':
            z_move += 0.1f;
            break;
    }
    
    glutPostRedisplay();
}


int main(int argc, char* argv[])
{
    // Init glut
    glutInit(&argc, argv);
    
    // Init the window/OpenGL
    initialize_it();
    
    // Set the keyboard function to key
    glutKeyboardFunc(key);
    
    // Enter the main loop of glut
    glutMainLoop();
    
    
	return 0;
}
You have to initialise glut before you call any OpenGL specific functions, I noticed you called glClearColor() before glutInit, you must do that after you have setup glut. (I think this was mentioned earlier, but oh well)

Code: Select all

// Setup glut
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutInitWindowPosition(100,50);
glutCreateWindow("Moving Square");
glutDisplayFunc(rect_display);
    
// Setup OpenGL state-variables and etc.
glClearColor(0.0, 0.0, 0.0, 0.0);
After you've done all that, stuff should work, if perhaps you can't figure it out, I may as well just post what I did to get it working and you can compare (if you can't figure it out yourself). Oh by the way, I haven't taken any programming classes or what-not (I'm still in high school :) and I'm 16 :p), I really recommend getting a book of some sort, like the OpenGL Redbook to learn OpenGL, you *can* find it online, but it is old legacy code but still thats what your using right now :D. Edition 8 of it should be out soon (I think) and it will be updated :o.

Oh and here's some lectures I found before which were very useful to me, although they don't go into OpenGL code much, but they teach you all the concepts you need for Computer Graphics.

http://itunes.apple.com/WebObjects/MZSt ... =428958018
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Perspective

Post by Ginto8 »

Benjamin100 wrote:My Xchat trial expired.
And? I wasn't giving you the "shareware" xchat, I was giving you a link to the linux xchat project (completely free to use, "free" as in both free beer and free market) compiled for windows, which has no expiration date. Its informal names are xchat2 and ychat; but ychat is also a Yahoo! chat client, so don't search for ychat.
Benjamin100 wrote: It is the SAME code as before, except no "glOrtho" And I changed the z values to negative.
There isn't much else to it.
That is all I changed.
Well so far each and every problem you could be having has been addressed. If your problem has not been addressed, you haven't given us the code you're actually using. If you go and read each of our posts, we go into a fair amount of detail about a) what the problem could be, b) why that could be a problem and c) how to fix it, so you have no reason to still have a problem unless you have not given the correct code.

Long story short, come on IRC, use pastebin to give us a complete copy of your code; don't leave anything out; and we will be able to fix your problem in a fraction of the time I've spent typing up responses for this thread.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

I'd appreciate knowing what I left out.
Benjamin100
ES Beta Backer
ES Beta Backer
Posts: 250
Joined: Tue Jul 19, 2011 9:37 pm

Re: Perspective

Post by Benjamin100 »

EDITED FOR REPETITIVENESS.
Last edited by Benjamin100 on Thu Jan 19, 2012 1:08 am, edited 1 time in total.
Post Reply