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

📄 ogl_benchmark_sphere.cpp

📁 一个VC编写的显示地球小程序员,可随鼠标移动转动
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	glEnable( GL_TEXTURE_2D );
	glEnable( GL_DEPTH_TEST );

    loadTexture();

	glMatrixMode( GL_PROJECTION );
	glLoadIdentity();
	gluPerspective( 45.0f, 640.0f / 480.0f, 0.1f, 100.0f );

    //
    // Create the first sphere...
    //

    // Inform the user of the current mode
    cout << "Render Method: Immediate Mode" << endl;

    createSphereGeometry( 0.0f, 0.0f, 0.0f, 1.5f, g_nPrecision );
}

//-----------------------------------------------------------------------------
// Name: shutDown()
// Desc: 
//-----------------------------------------------------------------------------
void shutDown( void )	
{
    glDeleteTextures( 1, &g_textureID );
        
	if( g_hRC != NULL )
	{
		wglMakeCurrent( NULL, NULL );
		wglDeleteContext( g_hRC );
		g_hRC = NULL;							
	}

	if( g_hRC != NULL )
	{
		ReleaseDC( g_hWnd, g_hDC );
		g_hDC = NULL;
	}
}

//-----------------------------------------------------------------------------
// Name: renderSphere()
// Desc: Create a sphere centered at cy, cx, cz with radius r, and 
//       precision p. Based on a function Written by Paul Bourke. 
//       http://astronomy.swin.edu.au/~pbourke/opengl/sphere/
//-----------------------------------------------------------------------------
void renderSphere( float cx, float cy, float cz, float r, int p )
{
    const float PI     = 3.14159265358979f;
    const float TWOPI  = 6.28318530717958f;
    const float PIDIV2 = 1.57079632679489f;

    float theta1 = 0.0;
    float theta2 = 0.0;
    float theta3 = 0.0;

    float ex = 0.0f;
    float ey = 0.0f;
    float ez = 0.0f;

    float px = 0.0f;
    float py = 0.0f;
    float pz = 0.0f;

    // Disallow a negative number for radius.
    if( r < 0 )
        r = -r;

    // Disallow a negative number for precision.
    if( p < 0 )
        p = -p;

    // If the sphere is too small, just render a OpenGL point instead.
    if( p < 4 || r <= 0 ) 
    {
        glBegin( GL_POINTS );
        glVertex3f( cx, cy, cz );
        glEnd();
        return;
    }

    for( int i = 0; i < p/2; ++i )
    {
        theta1 = i * TWOPI / p - PIDIV2;
        theta2 = (i + 1) * TWOPI / p - PIDIV2;

        glBegin( GL_TRIANGLE_STRIP );
        {
            for( int j = 0; j <= p; ++j )
            {
                theta3 = j * TWOPI / p;

                ex = cosf(theta2) * cosf(theta3);
                ey = sinf(theta2);
                ez = cosf(theta2) * sinf(theta3);
                px = cx + r * ex;
                py = cy + r * ey;
                pz = cz + r * ez;

                glNormal3f( ex, ey, ez );
                glTexCoord2f( -(j/(float)p) , 2*(i+1)/(float)p );
                glVertex3f( px, py, pz );

                ex = cosf(theta1) * cosf(theta3);
                ey = sinf(theta1);
                ez = cosf(theta1) * sinf(theta3);
                px = cx + r * ex;
                py = cy + r * ey;
                pz = cz + r * ez;

                glNormal3f( ex, ey, ez );
                glTexCoord2f( -(j/(float)p), 2*i/(float)p );
                glVertex3f( px, py, pz );
            }
        }
        glEnd();
    }
}

//-----------------------------------------------------------------------------
// Name: createSphereDisplayList()
// Desc: Build Sphere Display List
//-----------------------------------------------------------------------------
void createSphereDisplayList()
{
    glDeleteLists( g_sphereDList, 0 );

    static bool firstPass = true;

    if( firstPass )
    {
        g_sphereDList = glGenLists(1);
        firstPass     = false;
    }

    if( g_sphereDList != 0 )
    {
        glNewList( g_sphereDList, GL_COMPILE );
        // Cache the calls needed to render a sphere
        renderSphere( 0.0f, 0.0f, 0.0f, 1.5f, g_nPrecision );
        glEndList();
    }
}

//-----------------------------------------------------------------------------
// Name: setVertData()
// Desc: Helper function for createSphereGeometry()
//-----------------------------------------------------------------------------
void setVertData( int index,
                 float tu, float tv, 
                 float nx, float ny, float nz, 
                 float vx, float vy, float vz )	
{
    (g_pSphereVertices+index)->tu = tu;
    (g_pSphereVertices+index)->tv = tv;
    (g_pSphereVertices+index)->nx = nx;
    (g_pSphereVertices+index)->ny = ny;
    (g_pSphereVertices+index)->nz = nz;
    (g_pSphereVertices+index)->vx = vx;
    (g_pSphereVertices+index)->vy = vy;
    (g_pSphereVertices+index)->vz = vz;
}

//-----------------------------------------------------------------------------
// Name: createSphereGeometry()
// Desc: Creates a sphere as an array of vertex data suitable to be fed into a 
//       OpenGL vertex array. The sphere will be centered at cy, cx, cz with 
//       radius r, and precision p. Based on a function Written by Paul Bourke. 
//       http://astronomy.swin.edu.au/~pbourke/opengl/sphere/
//-----------------------------------------------------------------------------
void createSphereGeometry( float cx, float cy, float cz, float r, int p )	
{
    const float PI = 3.14159265358979f;
    const float TWOPI = 6.28318530717958f;
    const float PIDIV2 = 1.57079632679489f;

    float theta1 = 0.0;
    float theta2 = 0.0;
    float theta3 = 0.0;

    float ex = 0.0f;
    float ey = 0.0f;
    float ez = 0.0f;

    float px = 0.0f;
    float py = 0.0f;
    float pz = 0.0f;

    float tu  = 0.0f;
    float tv  = 0.0f;

    //-------------------------------------------------------------------------
    // If sphere precision is set to 4, then 20 verts will be needed to 
    // hold the array of GL_TRIANGLE_STRIP(s) and so on...
    //
    // Example:
    //
    // total_verts = (p/2) * ((p+1)*2)
    // total_verts = (4/2) * (  5  *2)
    // total_verts =   2   *  10
    // total_verts =      20
    //-------------------------------------------------------------------------

    g_nNumSphereVertices = (p/2) * ((p+1)*2);

    if( g_pSphereVertices != NULL )
    {
        delete []g_pSphereVertices;
        g_pSphereVertices = NULL;
        g_pSphereVertices = new Vertex[g_nNumSphereVertices];
    }
    else
    {
        g_pSphereVertices = new Vertex[g_nNumSphereVertices];
    }

    // Disallow a negative number for radius.
    if( r < 0 )
        r = -r;

    // Disallow a negative number for precision.
    if( p < 4 ) 
        p = 4;

    int k = -1;

    for( int i = 0; i < p/2; ++i )
    {
        theta1 = i * TWOPI / p - PIDIV2;
        theta2 = (i + 1) * TWOPI / p - PIDIV2;

        for( int j = 0; j <= p; ++j )
        {
            theta3 = j * TWOPI / p;

            ex = cosf(theta2) * cosf(theta3);
            ey = sinf(theta2);
            ez = cosf(theta2) * sinf(theta3);
            px = cx + r * ex;
            py = cy + r * ey;
            pz = cz + r * ez;
            tu  = -(j/(float)p);
            tv  = 2*(i+1)/(float)p;

            ++k;
            setVertData( k, tu, tv, ex, ey, ez, px, py, pz );

            ex = cosf(theta1) * cosf(theta3);
            ey = sinf(theta1);
            ez = cosf(theta1) * sinf(theta3);
            px = cx + r * ex;
            py = cy + r * ey;
            pz = cz + r * ez;
            tu  = -(j/(float)p);
            tv  = 2*i/(float)p;

            ++k;
            setVertData( k, tu, tv, ex, ey, ez, px, py, pz );
        }
    }
}

//-----------------------------------------------------------------------------
// Name: doBenchmark()
// Desc: 
//-----------------------------------------------------------------------------
void doBenchmark()
{
    timeb start;
    timeb finish;
    float  fElapsed = 0.0f;
    int    nFrames  = 1000;

    ftime( &start );  // Get the time

    while( nFrames-- ) // Loop away
        render();

    ftime( &finish ); // Get the time again

    fElapsed  = (float)(finish.time - start.time);                // This is accurate to one second
    fElapsed += (float)((finish.millitm - start.millitm)/1000.0); // This gets it down to one ms

    cout << endl;
    cout << "-- Benchmark Report --" << endl;

    if( g_nCurrentMode == IMMEDIATE_MODE )
        cout << "Render Method:     Immediate Mode" << endl;
    if( g_nCurrentMode == DISPLAY_LIST )
        cout << "Render Method:     Display List" << endl;
    if( g_nCurrentMode == VERTEX_ARRAY )
        cout << "Render Method:     Vertex Array" << endl;

    cout << "Frames Rendered:   1000" << endl;
    cout << "Sphere Resolution: " << g_nPrecision << endl;
    cout << "Primitive Used:    GL_TRIANGLE_STRIP" << endl;
    cout << "Elapsed Time:      " << fElapsed << endl;
    cout << "Frames Per Second: " << 1000.0/fElapsed << endl;
    cout << endl;
}

//-----------------------------------------------------------------------------
// Name: render()
// Desc: 
//-----------------------------------------------------------------------------
void render( void )
{
	// Clear the screen and the depth buffer
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode( GL_MODELVIEW );
    glLoadIdentity();
    glTranslatef( 0.0f, 0.0f, -5.0f );
    glRotatef( -g_fSpinY, 1.0f, 0.0f, 0.0f );
    glRotatef( -g_fSpinX, 0.0f, 1.0f, 0.0f );

    if( g_bRenderInWireFrame == true )
        glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
    else
        glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );

    //
    // Render test sphere...
    //

    glBindTexture( GL_TEXTURE_2D, g_textureID );

    if( g_nCurrentMode == IMMEDIATE_MODE )
    {
        // Render a textured sphere using immediate mode

        // To be fair to immediate mode, we won't force it incur the overhead 
        // of calling hundreds of math subroutines to generate a sphere each 
        // frame, instead, we'll use the same array that we would use for 
        // testing the vertex array, but we'll make the immediate mode calls 
        // ourselves. This is more typical of how a real app would use 
        // immediate mode calls.

        glBegin( GL_TRIANGLE_STRIP );
        {
            for( GLuint i = 0; i < g_nNumSphereVertices; ++i )
            {
                glNormal3f( (g_pSphereVertices+i)->nx,
                            (g_pSphereVertices+i)->ny,
                            (g_pSphereVertices+i)->nz );

                glTexCoord2f( (g_pSphereVertices+i)->tu,
                              (g_pSphereVertices+i)->tv );

                glVertex3f( (g_pSphereVertices+i)->vx,
                            (g_pSphereVertices+i)->vy,
                            (g_pSphereVertices+i)->vz );
            }
        }
        glEnd();
    }

    if( g_nCurrentMode == DISPLAY_LIST )
    {
        // Render a textured sphere as a display list
        glCallList( g_sphereDList );
    }

    if( g_nCurrentMode == VERTEX_ARRAY )
    {
        // Render a textured sphere using a vertex array
        glInterleavedArrays( GL_T2F_N3F_V3F, 0, g_pSphereVertices );
        glDrawArrays( GL_TRIANGLE_STRIP, 0, g_nNumSphereVertices );
    }

	SwapBuffers( g_hDC );
}

⌨️ 快捷键说明

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