Mun lp trình đ ha bng visual C++ 6.0 thì cn phi có các thư vin b sung sau:
Nh n vào đây đ down v (http://www.fileden.com/files/2008/1/19/1707613/ glut.rar)
Gi i nén và chép t t c vào trong th m c project c a b n đang làm vi c ư
Bài 1: L p trình ch ng trình v hình vuôngươ
// Bo qua man hinh Console
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#include <stdio.h>
#include "glut.h"
#include "iostream.h"
#include "afxwin.h"
#include <math.h>
// 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))
// cau truc 1 thanh phan mau theo RGB
struct colorentry {
unsigned char red;
unsigned char green;
unsigned char blue;
colorentry(int r, int g, int b):red(r),green(g), blue(b){};
};
// 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 ban phim
void OnKey(unsigned char key,int x, int y);
// ham ve duong thang
void DrawLine(int x1, int y1, int x2,int y2, colorentry c);
// ham ve diem
void PutPixel(int x, int y, colorentry c);
// ham ve hinh vuong
void DrawSquare(int x0, int y0, int w, colorentry c);
int main(int argc, char* argv[])
{
// Khoi tap cua so OpenGL
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(windowWidth, windowHeight);
glutInitWindowPosition(startX, startY);
glutCreateWindow("Draw Square Demo");
// Cac thao tac khoi tao
Init();
// Dang ky ham Display
glutDisplayFunc(Display);
// Dang ky ham Reshape
glutReshapeFunc(Reshape);
// Dang ky ham OnKey
glutKeyboardFunc(OnKey);
// Vong lap su kien
glutMainLoop();
// Ket thuc
return 0;
}
void Init()
{
glClearColor(0.0,0.0,0.0,0.0);
}
int w=100;
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);
DrawSquare(xMax/2,yMax/2,w,colorentry(255,255,0));
PutPixel(xMax/2,yMax/2,colorentry(255,0,0));
// Ket xuat ra man hinh
glFlush();
}
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 OnKey(unsigned char key,int x, int y)
{
switch(key)
{
case '+':
w+=10;
break;
case '-':
w=w-10;
break;
case 27: // (ESC)
exit(0);
break;
}
glutPostRedisplay();
}
void DrawLine(int x1, int y1, int x2,int y2, colorentry c)
{
glBegin(GL_LINES);
glColor3ub(c.red,c.green,c.blue);
glVertex2f(x1,y1);
glVertex2f(x2,y2);
glEnd();
}
void PutPixel(int x, int y, colorentry c)
{
glPointSize(2.0);
glBegin(GL_POINTS);
glColor3ub(c.red,c.green,c.blue);
glVertex2f(x,y);
glEnd();
}
void DrawSquare(int x0, int y0, int w, colorentry c)
{
int pos[][2] = { {-w,w},{w,w},{w,-w},{-w,-w},{-w,w} };
for(int i=0;i<4;i++)
{
int x1 = x0 + pos[i][0];
int y1 = y0 + pos[i][1];
int x2 = x0 + pos[i+1][0];
int y2 = y0 + pos[i+1][1];
DrawLine(x1,y1,x2,y2,c);
}
}
Đây là toàn b soure code đ v hình vuông trong VC++ 6.0. T t c đ u có chú thích đ y đ .
quangtp
30-04-2008, 01:44 AM
anh oi , cho em pass gi i nén đ c ko ? anh co code v c bàn c vua ko cho e xin m i ah .em ượ
đang c n g p . có gì anh g i vào hòm th cho e : bu_ra_ti_no_5678@yahoo.com.vn ư
finalfantasy
30-04-2008, 09:40 AM
Sr em nha. Pass đ gi i nén là www.binhphu.info. Còn v code c a bàn c vua thì anh ch a có. ư
Anh s c g ng v trong th i gian nhanh nh t đ post lên cho em t i v . Mà ch c n v bàn
c vua thôi ph i ko em? Đâu có v m y con c trên đó ph i hem?:D
finalfantasy
30-04-2008, 01:36 PM
Đây là source code c a bàn c vua nè.
// Bo qua man hinh Console
#pragma comment( linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"" )
#include <stdio.h>
#include "glut.h"
#include <math.h>
// 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();