猫史档案馆


C++3D

用户:imgainary_numberimgainary_number查看:1 回复:5 评论:1 创建时间:2024-07-09T17:55:03


#include <iostream>
#include <cmath>
#include <cstring>
#include <conio.h> 

const int screenWidth = 80;
const int screenHeight = 40;
const float fov = 100;
const float depth = 16.0;

int dirx, diry;
int 喵, vy, vz;

struct Vec3 {
    float x, y, z;
};

Vec3 cameraPosition = { 0, 0, -15 };


Vec3 vertices[] = {
    {-1, -1, -1},
    {1, -1, -1},
    {1, 1, -1},
    {-1, 1, -1},
    {-1, -1, 1},
    {1, -1, 1},
    {1, 1, 1},
    {-1, 1, 1}
};

int edges[][2] = {
    {0, 1},
    {1, 2},
    {2, 3},
    {3, 0},
    {4, 5},
    {5, 6},
    {6, 7},
    {7, 4},
    {0, 4},
    {1, 5},
    {2, 6},
    {3, 7}
};

char screenBuffer[screenHeight][screenWidth];
char backBuffer[screenHeight][screenWidth];

void clearScreen() {
    for (int y = 0; y < screenHeight; ++y) {
        for (int x = 0; x < screenWidth; ++x) {
            screenBuffer[y][x] = ' ';
        }
    }
}

void ry(int x, int z, int dir) {
    float s = sinf(0 - dir);
    float c = cosf(0 - dir);
    喵 = s * z + c * x;
    vz = c * z - s * x;
}

void draw(int x, int y, char c) {
    if (x >= 0 && x < screenWidth && y >= 0 && y < screenHeight) {
        screenBuffer[y][x] = c;
    }
}

Vec3 project(Vec3 point) {
    float dx, dy, dz;
    point.x -= cameraPosition.x;
    point.y -= cameraPosition.y;
    point.z -= cameraPosition.z;
    dx = point.x;
    dy = point.y;
    dz = point.z;

    ry(dx, dz, diry);

    point.x = 喵;
    point.y = dy;
    point.z = vz;

    Vec3 projected;
    if (point.z > 0) {
        projected.x = point.x * (fov / point.z) + screenWidth / 2;
        projected.y = -point.y * (fov / point.z) + screenHeight / 2;
    }
    return projected;
}

void render() {
    clearScreen();

    for (auto edge : edges) {
        Vec3 v0 = vertices[edge[0]];
        Vec3 v1 = vertices[edge[1]];

        Vec3 projectedV0 = project(v0);
        Vec3 projectedV1 = project(v1);

        int x0 = static_cast<int>(projectedV0.x);
        int y0 = static_cast<int>(projectedV0.y);
        int x1 = static_cast<int>(projectedV1.x);
        int y1 = static_cast<int>(projectedV1.y);

        int dx = abs(x1 - x0), sx = x0 < x1 ? 1 : -1;
        int dy = -abs(y1 - y0), sy = y0 < y1 ? 1 : -1;
        int err = dx + dy, e2;

        while (true) {
            draw(x0, y0, '#');

            if (x0 == x1 && y0 == y1) break;

            e2 = 2 * err;

            if (e2 >= dy) {
                err += dy;
                x0 += sx;
            }

            if (e2 <= dx) {
                err += dx;
                y0 += sy;
            }
        }
    }

    for (int y = 0; y < screenHeight; ++y) {
        for (int x = 0; x < screenWidth; ++x) {
            std::cout << screenBuffer[y][x];
        }
        std::cout << std::endl;
    }
}

int main() {
    char running = 'y';
    while (running == 'y') {
            system("cls");
            render();
            if (_kbhit()) {
                char key = _getch();
                switch (key) {
                case 'q':
                    running = 'n';
                    break;
                case 'w':
                    cameraPosition.z += 1.0f;  
                    break;
                case 's':
                    cameraPosition.z -= 1.0f;   
                    break;
                case 'a':
                    cameraPosition.x -= 1.0f; 
                    break;
                case 'd':
                    cameraPosition.x += 1.0f;  
                    break;
                case 'z':
                    cameraPosition.y -= 1.0f;  
                    break;
                case 'x':
                    cameraPosition.y += 1.0f;  
                    break;
                case 'j':
                    diry-=1.0f;
                    break;
                case 'l':
                    diry+=1.0f;
                    break;
                default:
                    break;
                }
            }
        
    }

    return 0;
}

center_image


回复

上一页1 页 / 共 1下一页
em1100em1100

NB

点赞0


评论


authorizationauthorization

l

点赞0


评论


authorizationauthorization

very good

点赞0


评论


「Origin、拾柒」「Origin、拾柒」

《喵》

点赞0


评论


贺鸣Herman贺鸣Herman

《“喵”》

点赞0


评论