Tin học Đồ họa 3D với Graph c++

trà nguyễn hữu nghĩa

Cựu Mod Vật Lí |Cây bút Thơ|Thần tượng VH
Thành viên
14 Tháng năm 2017
3,974
7,619
744
21
Phú Yên
Trường THPT Lương Văn Chánh
[TẶNG BẠN] TRỌN BỘ Bí kíp học tốt 08 môn
Chắc suất Đại học top - Giữ chỗ ngay!!

ĐĂNG BÀI NGAY để cùng trao đổi với các thành viên siêu nhiệt tình & dễ thương trên diễn đàn.

Một đoạn code khá hay mà mình xem mấy anh nước ngoài viết....chả hiểu gì nhưng cứ viết ra thì nó được thế này (Người ta đẹp hơn nữa, mình dùng thư viện Graph nên hơi hãm một tí)upload_2019-10-17_23-34-48.png.
Và đây là code tham khảo. Code này chỉ có giá trị tham khảo đồ họa 3D, không có ý nghĩa gì về Game nha. Và nhớ cài thư viện winbgim.h trước khi copy code :D
Mã:
#include<iostream>
#include<math.h>
#include<winbgim.h>
#include<vector>
#include <windows.h>

using namespace std;

int bkc = 3; //cyan
int height = 500;
int width = 500;
int roadW = 2000;
int seg = 400;
float camD = 0.84;
struct Line
{
    int x,y,z;
    int X,Y,W;
    float scale, curve;
    Line(){curve = x = y = z = 0;}
    void project(int camX, int camY, int camZ)
    {
        scale = camD/(z - camZ);
        X = (1 + scale*(x-camX))*width/2;
        Y = (1 - scale*(y-camY))*height/2;
        W = scale*roadW*width/2;
    }
};

void Clear(int y1,int y2)
{
    setfillstyle(1,bkc);
    bar(0,y1,width,y2);
}

void DrawQuad(int c,int x1,int y1,int w1,int x2,int y2,int w2)
{
    if (y1 > height+100) return;
    int arr[] = {x1-w1,y1,x1+w1,y1,x2+w2,y2,x2-w2,y2};
    setfillstyle(1,c);
    setcolor(c);
    fillpoly(4,arr);
}
int main()
{
    initwindow(width,height);
    setbkcolor(bkc);cleardevice();
    setcolor(4);
    settextstyle(0,0,3);
    outtextxy(50,100,"3D RACING GAME");
    bool esc = false;
    bool kp = false;
    std::vector<Line> lines;
    for(int i = 0;i < 2000; i++)
    {
        Line line;
        line.z = i*seg;
        if ((i > 100 && i < 300) || (i > 600 && i < 800)) line.curve = 2;
        if (i > 300) line.y = sin((i-300)/30.0)*1500;
        lines.push_back(line);
    }
    int N = lines.size();
    int pos = 300;
    int PlayerX = 0;
    while (!esc)
    {
    delay(20);
    kp = false;
    if( GetAsyncKeyState( VK_UP ) & 0x8000 ) {pos += 200;kp = true;}
    if( GetAsyncKeyState( VK_DOWN ) & 0x8000 ) {pos -= 200;kp = true;}
    if( GetAsyncKeyState( VK_RIGHT ) & 0x8000 ) {PlayerX += 200;kp = true;}
    if( GetAsyncKeyState( VK_LEFT ) & 0x8000 ) {PlayerX -= 200;kp = true;}
    
    int startPos = pos/seg;
    int camH = 1500 + lines[startPos].y;
    int x = 0, dx = 0;
    int maxy = height;
    if (kp)
    {
        for(int n = startPos;n < startPos+75;n++)
        {
            Line &l = lines[n%N];
            l.project(PlayerX-x,camH,pos);
            x += dx;
            dx += l.curve;
            if (l.Y >= maxy + 1638 + height) continue;
            maxy = l.Y + 1638;
        
            Line p = lines[(n-1)%N];
            int grass = 10;
            int road = 8;
            int rumble = 4 +  (n%2)*11;
            DrawQuad(grass,0,p.Y,p.X-p.W*1.2,0,l.Y,l.X-l.W*1.2);
            DrawQuad(grass,width,p.Y,width-p.X-p.W*1.2,width,l.Y,width-l.X-l.W*1.2);
            DrawQuad(rumble,p.X-p.W*1.1,p.Y,p.W*0.1,l.X-l.W*1.1,l.Y,l.W*0.1);
            DrawQuad(rumble,p.X+p.W*1.1,p.Y,p.W*0.1,l.X+l.W*1.1,l.Y,l.W*0.1);
            DrawQuad(road,p.X,p.Y,p.W,l.X,l.Y,l.W);
            if (n == startPos + 74) Clear(lines[n%N].Y,lines[n%N].Y-50);
        }
    }
}
    closegraph();
    return 0;
}
 
Top Bottom