본문으로 바로가기

2020_1014 Sphere

category DX11 3D Game Programming 2020. 10. 14. 17:32

201014_DX3D_Kim.zip
5.78MB

 

 

1. Sphere class

더보기
#pragma once

class Sphere : public Transform
{
private:
	typedef VertexUVNormal VertexType;
	//
	Material* material;
	Mesh* mesh;
	//
	vector<VertexType> vertices;
	vector<UINT> indices;
	//
	UINT slices; // 위도
	UINT stacks; // 경도
	float radius; // 반지름
	///////////////////////////////////
public:
	Sphere(float radius = 1.0f, UINT slices = 8, UINT stacks = 8);
	~Sphere();
	//
	void Update();
	void Render();
	///////////////////////////////////
private:
	void Create();
};
///////////////////////////////////////////////////////////////////////////////////////////////
#include "Framework.h"

Sphere::Sphere(float radius, UINT slices, UINT stacks)
	: radius(radius), slices(slices), stacks(stacks)
{
	material = new Material(L"Diffuse");
	material->SetDiffuseMap(L"Textures/test.jpg");
	//
	Create(); // Create Mesh
}

Sphere::~Sphere()
{
	delete mesh;
	delete material;
}

void Sphere::Update()
{
	UpdateWorld();
}

void Sphere::Render()
{
	mesh->IASet();
	SetWorldBuffer();
	material->Set();

	DC->DrawIndexed((UINT)indices.size(), 0, 0);
}

void Sphere::Create()
{
    float fRadius = 1.0f;
    UINT uSlices = 8;
    UINT uStacks = 8;
    UINT cFaces = 2 * (uStacks - 1) * uSlices;
    UINT cVertices = (uStacks - 1) * uSlices + 2;

    VertexType* verticesArr = new VertexType[cVertices];


    UINT i, j;

    const int CACHE_SIZE = 240 * 2;

    // Sin/Cos caches
    float sinI[CACHE_SIZE], cosI[CACHE_SIZE];
    float sinJ[CACHE_SIZE], cosJ[CACHE_SIZE];

    for (i = 0; i < uSlices; i++) {
        sinI[i] = sinf((float)(2.0f * PI * i / uSlices));
        cosI[i] = cosf((float)(2.0f * PI * i / uSlices));
        // sincosf( 2.0f * D3DX_PI * i / uSlices, sinI + i, cosI + i );
    }
    for (j = 0; j < uStacks; j++) {
        sinJ[j] = sinf((float)(PI * j / uStacks));
        cosJ[j] = cosf((float)(PI * j / uStacks));
        // sincosf( D3DX_PI * j / uStacks, sinJ + j, cosJ + j );
    }

    // Generate verticesArr
    VertexType* pVertex = verticesArr;

    // +Z pole
    pVertex->position = XMFLOAT3(0.0f, 0.0f, fRadius);
    pVertex->normal = XMFLOAT3(0.0f, 0.0f, 1.0f);
    pVertex++;

    // Stacks
    for (j = 1; j < uStacks; j++)
    {
        for (i = 0; i < uSlices; i++)
        {
            XMFLOAT3 norm(sinI[i] * sinJ[j], cosI[i] * sinJ[j], cosJ[j]);
            XMFLOAT3 pos;
            pos.x = norm.x * fRadius;
            pos.y = norm.y * fRadius;
            pos.z = norm.z * fRadius;

            pVertex->position = pos; //norm * fRadius;
            pVertex->normal = norm;

            pVertex++;
        }
    }

    // Z- pole
    pVertex->position = XMFLOAT3(0.0f, 0.0f, -fRadius);
    pVertex->normal = XMFLOAT3(0.0f, 0.0f, -1.0f);
    pVertex++;

    UINT* indicesArr = new UINT[cFaces * 3];

    // Generate indicesArr
    UINT* pwFace = indicesArr;

    // Z+ pole
    UINT uRowA = 0;
    UINT uRowB = 1;

    for (i = 0; i < uSlices - 1; i++)
    {
        pwFace[0] = (WORD)(uRowA);
        pwFace[1] = (WORD)(uRowB + i + 1);
        pwFace[2] = (WORD)(uRowB + i);
        pwFace += 3;
    }

    pwFace[0] = (WORD)(uRowA);
    pwFace[1] = (WORD)(uRowB);
    pwFace[2] = (WORD)(uRowB + i);
    pwFace += 3;

    // Interior stacks
    for (j = 1; j < uStacks - 1; j++)
    {
        uRowA = 1 + (j - 1) * uSlices;
        uRowB = uRowA + uSlices;

        for (i = 0; i < uSlices - 1; i++)
        {
            pwFace[0] = (WORD)(uRowA + i);
            pwFace[1] = (WORD)(uRowA + i + 1);
            pwFace[2] = (WORD)(uRowB + i);
            pwFace += 3;

            pwFace[0] = (WORD)(uRowA + i + 1);
            pwFace[1] = (WORD)(uRowB + i + 1);
            pwFace[2] = (WORD)(uRowB + i);
            pwFace += 3;
        }

        pwFace[0] = (WORD)(uRowA + i);
        pwFace[1] = (WORD)(uRowA);
        pwFace[2] = (WORD)(uRowB + i);
        pwFace += 3;

        pwFace[0] = (WORD)(uRowA);
        pwFace[1] = (WORD)(uRowB);
        pwFace[2] = (WORD)(uRowB + i);
        pwFace += 3;
    }

    // Z- pole
    uRowA = 1 + (uStacks - 2) * uSlices;
    uRowB = uRowA + uSlices;

    for (i = 0; i < uSlices - 1; i++)
    {
        pwFace[0] = (WORD)(uRowA + i);
        pwFace[1] = (WORD)(uRowA + i + 1);
        pwFace[2] = (WORD)(uRowB);
        pwFace += 3;
    }

    pwFace[0] = (WORD)(uRowA + i);
    pwFace[1] = (WORD)(uRowA);
    pwFace[2] = (WORD)(uRowB);
    pwFace += 3;

	mesh = new Mesh(verticesArr, sizeof(VertexType), cVertices,
		indicesArr, cFaces * 3);

	indices.resize(cFaces * 3);
	vertices.resize(cVertices);
	memcpy(&indices[0], &indicesArr[0], sizeof(UINT) * cFaces * 3);
	memcpy(&vertices[0], &verticesArr[0], sizeof(VertexType) * cVertices);
    //
    delete[] verticesArr;
    delete[] indicesArr;
}

 

'DX11 3D Game Programming' 카테고리의 다른 글

2020_1020 Terrain Edit ( Terrain Brush )  (0) 2020.10.20
2020_1019 Terrain Edit ( Save & Load )  (0) 2020.10.19
2020_1015 Light - Diffuse & Specular  (0) 2020.10.15
2020_1008 SolarSystem  (0) 2020.10.12
2020_1007 Render Cube  (0) 2020.10.07