[OpenGL] 랜덤 스폰

gest·2026년 5월 8일

OpenGL

목록 보기
8/11

이번엔 서바이벌게임답게 아몬드를 맵 랜덤으로 생성시키려고 한다.


랜덤 생성

{
    const int   numAlmonds = 20; // 아몬드 갯수
    const float halfExtent = 64.0f;     //terrain.gridSize * cellSize / 2
    const float inset      = 5.0f;
    const float minXZ = -halfExtent + inset;
    const float maxXZ =  halfExtent - inset;

    //고정 시드 사용 — 매 실행마다 같은 위치에 나오게. 디버깅 편의.
    //랜덤 위치를 원하면 std::random_device 등으로 대체 가능.
    std::mt19937 rng(42);
    std::uniform_real_distribution<float> distXZ(minXZ, maxXZ);

    for (int i = 0; i < numAlmonds; ++i)
    {
        float x = distXZ(rng);
        float z = distXZ(rng);
        float y = terrain->getHeightAt(x, z);

        Almond* a = new Almond(mouseShader, glm::vec3(x, y, z));
        a->setShadowMap(depthMap);
        almonds.push_back(a);
    }

    std::cout << "Spawned " << almonds.size() << " almonds" << std::endl;
}

원리는 간단하다. 랜덤으로 xz값을 생성하면 terrian->getHeightAt(x,z)로 높이를 구해서 지형위에 생성시킨다.

참고로 vs, fs는 mouse로 대체했다.


결과

0개의 댓글