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;
}