Thứ Hai, 24 tháng 11, 2008

Code ve co vua

// Bo qua man hinh Console
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )

#include
#include "glut.h"
#include
// kich thuoc, vi tri cua so
#define windowWidth 640
#define windowHeight 480
#define startX 0
#define startY 0
// hang so PI
#define M_PI 3.14159265358979
// macro doi tu do --> radian
#define RAD(goc) ((goc)*(M_PI/180.0))


// ham khoi tao
void Init();
// !!! ham ve (tat ca thao tac ve nam trong ham nay)
void Display();
// ham ve lai moi khi cua so thay doi kich thuoc
void Reshape(int Width,int Height);
// ham xu ly Idle
void OnIdle();

void DrawSquareBlack(int x0,int y0,int d);

void DrawSquareWhite(int x0,int y0,int d);

void DrawChessBoard(int xc,int yc);

int main(int argc, char* argv[])
{
// Khoi tap cua so OpenGL
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(startX, startY);
glutCreateWindow("Chess Board");
// Cac thao tac khoi tao
Init();
// Dang ky ham Display
glutDisplayFunc(Display);
// Dang ky ham Reshape
glutReshapeFunc(Reshape);
// Dang ky ham OnIdle
glutIdleFunc(OnIdle);
// Vong lap su kien
glutMainLoop();
// Ket thuc
return 0;
}

void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
}

void Display()
{
// Xoa de bat dau ve
glClear(GL_COLOR_BUFFER_BIT);
// !!! Cac thao tac ve
int xMax = glutGet(GLUT_WINDOW_WIDTH);
int yMax = glutGet(GLUT_WINDOW_HEIGHT);
DrawChessBoard(xMax/2,yMax/2);
// Ket xuat ra man hinh
glFlush();
glutSwapBuffers();
}

void OnIdle()
{
// Goi ham Display de ve lai
glutPostRedisplay();
}

void Reshape(int Width,int Height)
{
glViewport(0, 0 , (GLsizei)Width,(GLsizei)Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0,(GLdouble)Width,0, (GLdouble)Height);
}

void DrawSquareBlack(int x0,int y0,int d)
{
glBegin(GL_POLYGON);
glVertex2f(x0,y0);
glVertex2f(x0+d,y0);
glVertex2f(x0+d,y0-d);
glVertex2f(x0,y0-d);
glEnd();
}

void DrawSquareWhite(int x0,int y0,int d)
{
glBegin(GL_LINE_LOOP);
glVertex2f(x0,y0);
glVertex2f(x0+d,y0);
glVertex2f(x0+d,y0-d);
glVertex2f(x0,y0-d);
glEnd();
}

void DrawChessBoard(int xc,int yc)
{
GLfloat R = sqrt(140*140+140*140);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
if((i%2==0&&j%2==0)||(i%2==1&&j%2==1))
{
glColor3ub(255,255,255);
DrawSquareBlack(xc + j*35 + R*cos(RAD(135)),yc - i*35 + R*sin(RAD(135)),35);
}
glColor3ub(0,255,0);
DrawSquareWhite(xc + j*35 + R*cos(RAD(135)),yc - i*35 + R*sin(RAD(135)),35);
}
}
}

Không có nhận xét nào: