I've gone a little further with my code since the last time I posted here. Everything worked fine till the moment I started drawing in an actual 3rd dimension - the results are visible at least, but they're far from what they should look like. Instead of a pyramid and a cube I get two more or less randomly combined surfaces. Tried rechecking and playing around with cords, without effects.

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

bool game;

struct plr {
       float x;
       float y;
       int dir;
       float spin;
       float saturation;
       
       //badguy's specific vars
       float speed;
};

struct plr bobby;
struct plr badguy;



void reset(void)
{
     bobby.x = 100.0f;
     bobby.y = 50.0f;
     bobby.dir = 1;
     
     badguy.x = 200.0f;
     badguy.y = 100.0f;
     badguy.speed = 0.5f;
}
     

void init(void)
{
     reset();
     glClearColor (0.0, 0.0, 0.0, 0.0);
     glShadeModel (GL_FLAT);
}

void UpdateVariables(void)
{
     bobby.spin+=1.0f;
     bobby.saturation-=0.01f;

     
     if (bobby.saturation < 0.3f) { bobby.saturation = 0.8f; } 
}

void UpdateAI(void)
{
     if (bobby.x > badguy.x) { badguy.x += badguy.speed; } else { badguy.x -= badguy.speed; }
     if (bobby.y > badguy.y) { badguy.y += badguy.speed; } else { badguy.y -= badguy.speed; }
}

void checkForUpdates(void)
{
     UpdateVariables();
     UpdateAI();
           
            if (bobby.x > 640) { bobby.x = 0; }
            if (bobby.x < 0) { bobby.x = 640; }
            if (bobby.y > 400) { bobby.y = 0; }
            if (bobby.y < 0) { bobby.y = 400; }
     
     switch (bobby.dir){
            case 1:
                 bobby.y-=1.0f;
                 glutPostRedisplay();
                 break;
            case 2:
                 bobby.y+=1.0f;
                 glutPostRedisplay();
                 break;
            case 3:
                 bobby.x-=1.0f;
                 glutPostRedisplay();
                 break;
            case 4:
                 bobby.x+=1.0f;
                 glutPostRedisplay();
                 break;
            default:
                    break;
            }

}

void DrawWorld(void)
{
     checkForUpdates();

     
     glPushMatrix();
     glTranslatef(bobby.x, bobby.y, 0.0f);
     glRotatef(bobby.spin, 1.0f, 1.0f, 1.0f);
     
     glColor3f(1.0f, 0.0f, 0.0f);
     glBegin(GL_TRIANGLES);
     
     glVertex3f(20.0, 20.0, 0.0);
     glVertex3f(40.0, 20.0, 0.0);
     glVertex3f(20.0, 40.0, 0.0);

     glVertex3f(20.0, 40.0, 0.0);
     glVertex3f(20.0, 20.0, 0.0);
     glVertex3f(30.0, 30.0, -10.0);
     
     glVertex3f(30.0, 30.0, -10.0);
     glVertex3f(20.0, 20.0, 0.0);
     glVertex3f(40.0, 20.0, 0.0);
     

     glVertex3f(40.0, 20.0, 0.0);
     glVertex3f(20.0, 40.0, 0.0);
     glVertex3f(30.0, 30.0, -10.0);
          
     glEnd();
     glPopMatrix();
     
     glPushMatrix();
     glTranslatef(badguy.x, badguy.y, 0.0f);
     glRotatef(bobby.spin, 1.0f, 1.0f, 1.0f);
     
     glColor3f(0.0f, 0.0f, 1.0f);
     glBegin(GL_QUADS);
     
     glVertex3f(20.0, 20.0, 0.0);
     glVertex3f(40.0, 20.0, 0.0);
     glVertex3f(40.0, 40.0, 0.0);
     glVertex3f(20.0, 40.0, 0.0);
     
     glVertex3f(20.0, 20.0, 0.0);
     glVertex3f(40.0, 20.0, 0.0);
     glVertex3f(40.0, 20.0, -20.0);
     glVertex3f(20.0, 20.0, -20.0);
     
     glVertex3f(20.0, 20.0, -20.0);
     glVertex3f(40.0, 20.0, -20.0);
     glVertex3f(40.0, 40.0, -20.0);
     glVertex3f(20.0, 40.0, -20.0);
     
     glVertex3f(20.0, 40.0, 0.0);
     glVertex3f(40.0, 40.0, 0.0);
     glVertex3f(20.0, 40.0, -20.0);
     glVertex3f(40.0, 40.0, -20.0);
     
     glVertex3f(40.0, 20.0, 0.0);
     glVertex3f(40.0, 40.0, 0.0);
     glVertex3f(40.0, 20.0, -20.0);
     glVertex3f(40.0, 40.0, -20.0);
     
     glVertex3f(20.0, 20.0, 0.0);
     glVertex3f(20.0, 40.0, 0.0);
     glVertex3f(20.0, 20.0, -20.0);
     glVertex3f(20.0, 40.0, -20.0);
     
     glEnd();
     glPopMatrix();
}
void display(void)
{
     glClear (GL_COLOR_BUFFER_BIT);     
     DrawWorld();
     glutSwapBuffers();

}


void reshape (int w, int h)
{
     if (h==0)
     {
              h=1;
     }
     
	 glViewport(0,0,w,h);	
     glMatrixMode (GL_PROJECTION);
     glLoadIdentity();
     
     glOrtho(0.0f,w,h,0.0f,-1.0f,1.0f);	
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
}


void keyboard (unsigned char key, int x, int y)
{

 
   switch (key) {
      case 's':
           bobby.dir=2;

         break;
      case 'w':
           bobby.dir=1;

         break;
      case 'a': // lewo = -
           bobby.dir=3;

           break;
      case 'd':
           bobby.dir=4;

           break;
      default:
         break;
   }
}

int main(int argc, char** argv)
{

    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowSize (640, 400);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init();

    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

First: I believe you still did not get what is meant by glOrtho. It sets the dimensions of your 3d Cordinate System. You would like to have it good long, wide and deep, so that you are comfortable drawing a moving shape in it. The sytanx ist:

void glOrtho(	GLdouble  	left, 
 	GLdouble  	right, 
 	GLdouble  	bottom, 
 	GLdouble  	top, 
 	GLdouble  	nearVal, 
 	GLdouble  	farVal);

Thus if you want to draw a cube that is about 30 wide and 30 long and 30 deep and also move it around within the cordinate system up to 640 in x and 400 in top

glOrtho(0, 640, 0, 400, -50.0f, 50.0f);

Now, the big difference to your posted code are the last two values. They specify the near Value and far Value of your cordinate system.

Nearvar, Farvar specify the distances to the nearer and farther depth clipping planes. These values are negative if the plane is to be behind the viewer.

Thus, if you do not plan to anim your shape in z direction, its range should be at least as big as the shape's dimension, otherwise it will be clipped. And that is what exactly happened in your code. Since zou use negative z values, you also need to specify negative nearVal.

Point 2: Your shapes are still a little screwed. Play around once you have set your new glOrtho. It will work then.

Point 3: You need Color for your every shape, as they would appear meshed, once you have set your shapes right.

Point Four: Get used to init and clear your depth buffer, since you are using now near and far values, otherwise your anim could be choppy. Replace and add those lines. They do the same for 3d as your current code does for 2d.

glClearDepth(10.0f);
glEnable(GL_DEPTH_TEST);

glutInitDisplayMode (GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB);

glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);

hope that helps!

&Cheers,

poliet

If you want 3d the glOrtho is the wrong choice. glOrtho is for
2d. Use gluPerspective(45.0,width/(float)height,1.0,1000.0f); for 3d

your reshape functions should look something like this :

void reshape (int w, int h)
{
     if( !h) h = 1;
 
     glViewport(0,0,w,h);	
     glMatrixMode (GL_PROJECTION);
     glLoadIdentity();
 
     gluPerspective(45.0, //angle
                              (float)width/height, //ratio
                               0.001f, //znear
                               1000.0f //zfar
                             )
     glMatrixMode(GL_MODELVIEW);
     glLoadIdentity();
}

Look at the documentation about gluPerspective

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.