1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
|
struct Vertex
{
float x, y, z;
};
struct MyColor
{
float c1, c2, c3;
};
void GameScene(Vertex v[], const int count, MyColor& c)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(c.c1, c.c2, c.c3);
for (int n = 0; n < count; ++n)
glVertex3f(v[n].x, v[n].y, v[n].z);
glEnd();
glutSwapBuffers(); // does this need to be called outside of this function?
}
void Keys(unsigned char key, int x, int y,
Vertex* head, const int head_count, MyColor& c1,
Vertex* leg, const int leg_count, MyColor& c2)
{
switch(key)
{
case 27 : exit(0); break;
case 'a':
{
for (int n = 0; n < head_count; ++n)
(*head++).x--;
for (int n = 0; n < leg_count; ++n)
(*leg++).x++;
break;
}
case 'd':
{
for (int n = 0; n < head_count; ++n)
(*head++).x++;
for (int n = 0; n < leg_count; ++n)
(*leg++).x--;
break;
}
}
}
void SpecialKeys(int key, int x, int y)
{
switch(key)
{
}
}
void Timer(int value)
{
glutTimerFunc(50, Timer, value);
glutPostRedisplay();
}
void changeSize(int w, int h)
{
float ratio = 1.0 * w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(45, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void main(int argc, char **argv)
{
srand(time(NULL));
GLUTInit(&argc, argv);
MyColor head_color = {1.0, 1.0, 1.0};
MyColor leg_color = {1.0, 0.0, 0.0};
Vertex head[] =
{{-0.5, 3.0, 0.0},
{0.5, 3.0, 0.0},
{0.5, 2.0, 0.0},
{-0.5, 2.0, 0.0}};
Vertex leg[] =
{{-0.6, -2.0, 0.0},
{-0.4, -2.0, 0.0},
{-0.4, -3.0, 0.0},
{-0.6, -3.0, 0.0}};
GameScene(head, sizeof(head)/sizeof(Vertex), head_color);
GameScene(leg, sizeof(leg)/sizeof(Vertex), leg_color);
// etc...
}
|