-4
#include<gl/glut.h>
#include<stdio.h>
GLfloat vertices[][3]={
{0.0,0.0,0.0},{0.0,0.4,0.0},{0.4,0.4,0.0},{0.4,0.0,0.0} };
GLfloat color[2][3]={ {1.0,0.0,0.0},{0.0,1.0,0.0}};
void board()
{
glColor3f(0.0,0.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex3f(-0.8,-0.8,0.0);
glVertex3f(-0.8,0.80,0.0);
glVertex3f(0.80,0.80,0.0);
glVertex3f(0.8,-0.80,0.0);
glEnd();
}
void cube()
{
glBegin(GL_LINE_LOOP);
glColor3f(1.0,0.0,0.0);
glVertex3fv(vertices[0]);
glVertex3fv(vertices[1]);
glVertex3fv(vertices[2]);
glVertex3fv(vertices[3]);
glEnd();
}
void display()
{
glClearColor(1.0,1.0,1.0,1.0);
glViewport(0,0,600,600);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
cube();
board();
glFlush();
}
void mykey(int key,int x,int y)
{
switch(key){
case GLUT_KEY_LEFT: glTranslatef(-0.4,0.0,0.0); break;
case GLUT_KEY_RIGHT: glTranslatef(0.4,0.0,0.0); break;
case GLUT_KEY_UP: glTranslatef(0.0,0.4,0.0); break;
case GLUT_KEY_DOWN: glTranslatef(0.0,-0.3,0.0); break;
}
glutPostRedisplay();
}
void myinit()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,200,0,400,-1,1);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(600,600);
glutCreateWindow("hello");
glutDisplayFunc(display);
glutSpecialFunc(mykey);
glutMainLoop();
return 0;
}
感謝。 ..... :) –