⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 d3dsphr.c

📁 国外游戏开发者杂志1997年第九期配套代码
💻 C
📖 第 1 页 / 共 3 页
字号:
           	n++;
        }
        dy += ddy;
    }

	
/*	
   m = 0; // first vertex in current ring,begins at 1 to skip top point
   n  = 0; //  triangle being generated, skip the top cap 
  for (i = 0; i < num_rings; i++) {
        for (j = 0; j < num_sections; j++)  {
            lptri[n].v1 = m + j;
            lptri[n].v2 = m + num_sections + j;
            lptri[n].v3 = m + num_sections + ((j + 1) % num_sections);
            lptri[n].wFlags = D3DTRIFLAG_EDGEENABLETRIANGLE;
            lptri[n].wFlags = D3DTRIFLAG_STARTFLAT(1);
            lptri[n].wFlags |= D3DTRIFLAG_EDGEENABLE1 |
                                      D3DTRIFLAG_EDGEENABLE2;
            
			lptri[n + 1].v1 = lptri[n].v1;
            lptri[n + 1].v2 = lptri[n].v3;
            lptri[n + 1].v3 = m + ((j + 1) % num_sections);
            
            lptri[n + 1].wFlags = D3DTRIFLAG_EVEN;
            lptri[n + 1].wFlags |= D3DTRIFLAG_EDGEENABLE2 |
                                   D3DTRIFLAG_EDGEENABLE3;
			n += 2;
        }
        m += num_sections;
    }
*/
return TRUE;
}

/*****************************************************************************/
BOOL 
GenerateTorus(float tor_big_r, float tor_small_r,
			           int num_rings,  int num_sections, 
					   float sx, float sy, float sz, 
					   LPD3DVERTEX* plpv, LPD3DTRIANGLE* plptri, 
					   int* pnum_v, int* pnum_tri)
{

    float theta, phi;    /* Angles used to sweep around sphere */
    float dtheta, dphi;  /* Angle between each section and ring */
    float x, y, z, v; /* Temporary variables */
    float a, b, bcostheta;
	float  a_pls_bcosv, cos_v , sin_v, cos_u ,sin_u , Nx, Ny ,Nz ,Nsize;
	int i, j, n ;//, m;      /* counters */
    int num_v, num_tri;  /* Internal vertex and triangle count */
    LPD3DVERTEX lpv;     /* Internal pointer for vertices */
    LPD3DTRIANGLE lptri; /* Internal pointer for trianlges */

    /*
     * Check the parameters to make sure they are valid.
     */
    if ((tor_big_r <= 0) || (num_rings < 1) || (num_sections < 3) ||
        (sx <= 0) || (sy <= 0) || (sz <= 0))
        return FALSE;
    /*
     * Generate space for the required triangles and vertices.
     */
     num_tri = (num_rings ) * (num_sections) * 2;
	 num_v  = (num_rings + 1) * (num_sections + 1);
    *plpv    = (LPD3DVERTEX) malloc(sizeof(D3DVERTEX) * num_v);
    // *plptri = (LPD3DTRIANGLE) malloc(sizeof(D3DTRIANGLE) * num_tri);
    
	 lpv = *plpv;
     lptri = *plptri;
     *pnum_v = num_v;
    *pnum_tri = num_tri;
	
	a = tor_big_r;
	b = tor_small_r;
	
     /*
     * Generate vertex points for rings
     */
    dtheta = (float)(2.0 * PI / (double)(num_rings ));
    dphi    = (float)(2.0 * PI / (double) num_sections);
    n = 0; /* vertex being generated, begins at 1 to skip top point */
    theta = 0.0f;
    for (i = 0; i <= num_rings; i++) {
        y = tor_small_r * (float)sin(theta); /* y is the same for each ring */
        v = theta / (2.0f * PI);		    /* v is the same for each ring */
        bcostheta = b * (float)cos(theta);
        phi = (float)0.0;
        for (j = 0; j <=  num_sections; j++) {
            x = (a + bcostheta) * (float)cos(phi);
            z = (a + bcostheta) * (float)sin(phi);
            
			
			lpv[n].x = D3DVAL(sx * x );
            lpv[n].z = D3DVAL(sz * z * -1.0f);
            lpv[n].y = D3DVAL(sy * y);
            
			// normal calculation
			cos_v  = (float)cos(theta); sin_v = (float)sin(theta);
			cos_u  = (float)cos(phi);    sin_u = (float)sin(phi);
			a_pls_bcosv = a + b * cos_v;
            
			Nx   =  (a_pls_bcosv  * b  * cos_v * cos_u);
		    Ny   =  (a_pls_bcosv  * b  * sin_v  );
		    Nz   =  -1.0f * (a_pls_bcosv  * b  * cos_v * sin_u);
            /*
			Nx   =  (lpv[n].x - ((a + b)  * cos_u));
		    Ny   =  (lpv[n].y) ;
		    Nz   =  (lpv[n].z - ((a + b)  * sin_u) );
			*/
			Nsize = (float)sqrt(Nx * Nx + Ny * Ny + Nz * Nz);
            lpv[n].nx = D3DVAL(Nx / Nsize);
            lpv[n].ny = D3DVAL(Ny / Nsize);
            lpv[n].nz = D3DVAL(Nz / Nsize);
            /*
			if( (v < 0.15f) || (v > 0.85f) ) {
			lpv[n].x += lpv[n].nx;
            lpv[n].z += lpv[n].nz;
            lpv[n].y += lpv[n].ny;
			}
			*/
			lpv[n].tv = D3DVAL(v);
            lpv[n].tu = D3DVAL((float)(1.0 - phi / (2.0 * PI)));
            
			
			
			phi += dphi;
            ++n;
        }
        theta += dtheta;
    }
    /*
     * Generate triangles for the rings
     */
    /*
	m = 0; // first vertex in current ring,begins at 1 to skip top point
    n = 0;  // triangle being generated, skip the top cap 
    for (i = 0; i < num_rings; i++) {
        for (j = 0; j < num_sections; j++) {
            lptri[n].v1 = m + j;
            lptri[n].v2 = m + (num_sections +1) + j;
            lptri[n].v3 = m + (num_sections + 1) + (j + 1);
			
			lptri[n + 1].v1 = lptri[n].v1;
            lptri[n + 1].v2 = lptri[n].v3;
            lptri[n + 1].v3 = m + (j + 1);
            
			n += 2;
        }
        m = m + (num_sections +1);
    }
   */
    return TRUE;
}
/*****************************************************************************/
BOOL 
GenerateEllipsoid(float a, float b, float c,
			           int num_rings,  int num_sections, 
					   LPD3DVERTEX* plpv, LPD3DTRIANGLE* plptri, 
					   int* pnum_v, int* pnum_tri)
{

    float theta, phi;    /* Angles used to sweep around sphere */
    float dtheta, dphi;  /* Angle between each section and ring */
    float x, y, z, v; /* Temporary variables */
	float  cos_v , sin_v, cos_u ,sin_u , Nx, Ny ,Nz ,Nsize;
	int i, j, n;      /* counters */
    int num_v, num_tri;  /* Internal vertex and triangle count */
    LPD3DVERTEX lpv;     /* Internal pointer for vertices */
    LPD3DTRIANGLE lptri; /* Internal pointer for trianlges */

    /*
     * Check the parameters to make sure they are valid.
     */
    if ((num_rings < 1) || (num_sections < 3) ) return FALSE;
    /*
     * Generate space for the required triangles and vertices.
     */
     num_tri = (num_rings + 1) * (num_sections  + 1) * 2;
	 num_v  = (num_rings + 1) * (num_sections + 1);
    *plpv    = (LPD3DVERTEX) malloc(sizeof(D3DVERTEX) * num_v);
    *plptri = (LPD3DTRIANGLE) malloc(sizeof(D3DTRIANGLE) * num_tri);
    
	 lpv = *plpv;
     lptri = *plptri;
     *pnum_v = num_v;
    *pnum_tri = num_tri;
	
     /*
     * Generate vertex points for rings
     */
    dtheta = (float)(2.0 * PI / (double)(num_rings ));
    dphi    = (float)(2.0 * PI / (double) num_sections);
    n = 0; /* vertex being generated, begins at 1 to skip top point */
    phi = 0.0f;
    for (i = 0; i <= num_rings; i++) {
        z = c  * (float)cos(phi);           /* z is the same for each ring */
        v = phi / (2.0f * PI);	   /* v is the same for each ring */
        theta = 0.0f;
        for (j = 0; j <=  num_sections; j++) {
            x = a * (float)(cos(theta)*sin(phi));
            y = b * (float)(sin(theta) *sin(phi));
            lpv[n].x = D3DVAL( x);
            lpv[n].z = D3DVAL( -1.0f * z);
            lpv[n].y = D3DVAL( y);
            // normal calculation
            cos_v  = (float)cos(theta); sin_v = (float)sin(theta);
			cos_u  = (float)cos(phi);    sin_u = (float)sin(phi);
            
			Nx   = - (b  * c * cos_v * sin_u * sin_u);
		    Ny   = - (a  * c * sin_v  * sin_u * sin_u);
		    Nz   = - (a  * b * cos_u * sin_u);
            Nsize = (float)sqrt(Nx * Nx + Ny * Ny + Nz * Nz);
            lpv[n].nx = D3DVAL(Nx / Nsize);
            lpv[n].ny = D3DVAL(Ny / Nsize);
            lpv[n].nz = D3DVAL(Nz / Nsize);
            lpv[n].tv = D3DVAL(v);
            lpv[n].tu = D3DVAL((float)(1.0 - theta / (2.0 * PI)));
            theta += dtheta;
            ++n;
        }
        phi += dphi;
    }

    return TRUE;
}

/******************************************************************************/
BOOL 
GenerateHyperboloid(float a, float b, float c,
			           int num_rings,  int num_sections, 
					   float sx, float sy, float sz, 
					   LPD3DVERTEX* plpv, LPD3DTRIANGLE* plptri, 
					   int* pnum_v, int* pnum_tri)
{

    float theta, phi;    /* Angles used to sweep around sphere */
    float dtheta, dphi;  /* Angle between each section and ring */
    float x, y, z, v; /* Temporary variables */
	float  cos_v , sin_v, cos_u ,sin_u , Nx, Ny ,Nz ,Nsize;
	int i, j, n;      /* counters */
    int num_v, num_tri;  /* Internal vertex and triangle count */
    LPD3DVERTEX lpv;     /* Internal pointer for vertices */
    LPD3DTRIANGLE lptri; /* Internal pointer for trianlges */

    /*
     * Check the parameters to make sure they are valid.
     */
    if ((a <= 0) || (num_rings < 1) || (num_sections < 3) ||
        (sx <= 0) || (sy <= 0) || (sz <= 0))
        return FALSE;
    /*
     * Generate space for the required triangles and vertices.
     */
     num_tri = (num_rings + 1) * (num_sections  + 1) * 2;
	 num_v  = (num_rings + 1) * (num_sections + 1);
    *plpv    = (LPD3DVERTEX) malloc(sizeof(D3DVERTEX) * num_v);
    *plptri = (LPD3DTRIANGLE) malloc(sizeof(D3DTRIANGLE) * num_tri);
    
	 lpv = *plpv;
     lptri = *plptri;
     *pnum_v = num_v;
    *pnum_tri = num_tri;
	
     /*
     * Generate vertex points for rings
     */
    dtheta = (float)(2.0 * PI / (double)(num_rings ));
    dphi    = (float)(2.0 * PI / (double) num_sections);
    n = 0; /* vertex being generated, begins at 1 to skip top point */
    phi = -3.141592653589793f;
    for (i = 0; i <= num_rings; i++) {
        y = b  * (float)sinh(phi);           /* z is the same for each ring */
        v = (phi + PI) / ((float)2.0 * PI);	   /* v is the same for each ring */
        theta = 0.0f;
        for (j = 0; j <=  num_sections; j++) {
            x = a * (float)(cos(theta)*cosh(phi));
            z = c * (float)(sin(theta) *sinh(phi));
            lpv[n].x = D3DVAL(sx * x);
            lpv[n].z = D3DVAL(sz * z);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -