1. Trang chủ
  2. » Công Nghệ Thông Tin

Tài liệu Lập trình đồ họa bằng visual C++ pptx

8 2,4K 36
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Lập trình đồ họa bằng Visual C++
Chuyên ngành Lập trình đồ họa
Năm xuất bản 2008
Định dạng
Số trang 8
Dung lượng 45 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

// Khoi tap cua so OpenGLglutInit&argc, argv; glutInitDisplayModeGLUT_SINGLE | GLUT_RGB; glutInitWindowSizewindowWidth, windowHeight; glutInitWindowPositionstartX, startY; glutCreateWind

Trang 1

Muốn lập trình đồ họa bằng visual C++ 6.0 thì cần phải có các thư viện 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[])

{

Trang 2

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

Trang 3

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

}

Trang 4

Đâ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);

Trang 5

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()

Trang 6

// 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);

Trang 7

DrawSquareWhite(xc + j*35 + R*cos(RAD(135)),yc - i*35 + R*sin(RAD(135)),35);

}

}

}

hondacodonbk

02-10-2008, 11:40 PM

anh finalfantasy có tài liệu lập trình đồ họa C++ sử dụng OpenGL hông,gửi cho em với,tại đang học về đồ họa dùng C++ và OpenGL

mà anh cho em xin nịck đc hông,để có gì dễ trao đổi,code của anh vẽ bàn cờ rất hay,em đang bí

về cái đó,may mà có code của anh tham khảo nên cũng đã hiểu phần nào

thank

==============================

==============================

cho em hỏi mấy chỗ trong cái code về vẽ bàn cờ,em vẫn chưa hiểu ý tại sao nó lại như vậy ? 1

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(); // -> tai sao phai co dong lenh nay ?????

}

2

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

{

glBegin(GL_POLYGON); // Tai sao cai nay phai la POLYGON ?

glVertex2f(x0,y0);

glVertex2f(x0+d,y0);

glVertex2f(x0+d,y0-d);

glVertex2f(x0,y0-d);

glEnd();

glFlush();

}

Trang 8

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

{

glBegin(GL_LINE_LOOP); // Tai sao cai nay phai la LINES_LOOP ????

glVertex2f(x0,y0);

glVertex2f(x0+d,y0);

glVertex2f(x0+d,y0-d);

glVertex2f(x0,y0-d);

glEnd();

glFlush();

}

3

void DrawChessBoard(int xc,int yc)

{

GLfloat R = sqrt(140*140+140*140); // 140 > where ?????

for(int i=0;i<8;i++)

{

for(int j=0;j<8;j++)

{

if((i+j)%2 == 0){

glColor3f(1.0,1.0,1.0);

DrawSquareBlack(xc + j*35 ,yc - i*35 ,35); // cong thuc nay o dau ????? voi 35 la canh cua 1 o vuong ?????

}

glColor3f(0.0,1.0,0.0);

DrawSquareWhite(xc + j*35 ,yc - i*35 ,35);

}

}

}

Ngày đăng: 13/12/2013, 00:15

TỪ KHÓA LIÊN QUAN

w