首页 > 代码库 > 《OpenGL 超级宝典(Super Bible)第五版》 有关 PBO 的 Example

《OpenGL 超级宝典(Super Bible)第五版》 有关 PBO 的 Example


代码即关键注释如下:

static GLfloat vGreen[] = { 0.0f, 1.0f, 0.0f, 1.0f };
static GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f };
static GLfloat vLightPos[] = { 0.0f, 3.0f, 0.0f, 1.0f };

GLsizei	 screenWidth;			// Desired window or desktop width
GLsizei  screenHeight;			// Desired window or desktop height

GLboolean bFullScreen;			// Request to run full screen
GLboolean bAnimated;			// Request for continual updates

GLShaderManager		shaderManager;			// Shader Manager
GLMatrixStack				modelViewMatrix;		// Modelview Matrix
GLMatrixStack				projectionMatrix;		// Projection Matrix
M3DMatrix44f				orthoMatrix;
GLFrustum					viewFrustum;				// View Frustum
GLGeometryTransform	transformPipeline;		// Geometry Transform Pipeline
GLFrame						cameraFrame;				// Camera frame

GLTriangleBatch			torusBatch;
GLBatch						floorBatch;
GLBatch						screenQuad;

GLuint							textures[1];
GLuint							blurTextures[6];
GLuint							pixBuffObjs[1];
GLuint							curBlurTarget;
bool								bUsePBOPath;
GLfloat							speedFactor;
GLuint							blurProg;
void								*pixelData;
GLuint							pixelDataSize;

//void MoveCamera(void);
void DrawWorld(GLfloat yRot, GLfloat xPos);
bool LoadBMPTexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode);

void SetupBlurProg(void);

// returns 1 - 6 for blur texture units
// curPixBuf is always between 0 and 5
void AdvanceBlurTaget()
{
    curBlurTarget = ((curBlurTarget + 1) % 6);
}
GLuint GetBlurTarget0()
{
    return (1 + ((curBlurTarget + 5) % 6));
}
GLuint GetBlurTarget1()
{
    return (1 + ((curBlurTarget + 4) % 6));
}
GLuint GetBlurTarget2()
{
    return (1 + ((curBlurTarget + 3) % 6));
}
GLuint GetBlurTarget3()
{
    return (1 + ((curBlurTarget + 2) % 6));
}
GLuint GetBlurTarget4()
{
    return (1 + ((curBlurTarget + 1) % 6));
}
GLuint GetBlurTarget5()
{
    return (1 + ((curBlurTarget) % 6));
}

void UpdateFrameCount()
{
    static int iFrames = 0;						// Frame count
    static CStopWatch frameTimer;     // Render time

    // Reset the stopwatch on first time
    if(iFrames == 0)
    {
        frameTimer.Reset();
        iFrames++;
    }
    // Increment the frame count
    iFrames++;

    // Do periodic frame rate calculation
    if (iFrames == 101)
    {
        float fps;

        fps = 100.0f / frameTimer.GetElapsedSeconds();
        if (bUsePBOPath)
            printf("Pix_buffs - Using PBOs  %.1f fps\n", fps);
        else
            printf("Pix_buffs - Using Client mem copies %.1f fps\n", fps);

        frameTimer.Reset();
        iFrames = 1;
    }
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
// Load in a BMP file as a texture. Allows specification of the filters and the wrap mode
bool LoadBMPTexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode)
{
    GLbyte *pBits;
    GLint iWidth, iHeight;

    pBits = gltReadBMPBits(szFileName, &iWidth, &iHeight);
    if(pBits == NULL)
        return false;

    // Set Wrap modes
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode);

    // Do I need to generate mipmaps?
    if(minFilter == GL_LINEAR_MIPMAP_LINEAR || minFilter == GL_LINEAR_MIPMAP_NEAREST || minFilter == GL_NEAREST_MIPMAP_LINEAR || minFilter == GL_NEAREST_MIPMAP_NEAREST)
        glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, pBits);
    return true;
}

///////////////////////////////////////////////////////////////////////////////
// OpenGL related startup code is safe to put here. Load textures, etc.
void SetupRC(void)
{
    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        /* Problem: glewInit failed, something is seriously wrong. */
        fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
    }

    // Initialze Shader Manager
    shaderManager.InitializeStockShaders();
    glEnable(GL_DEPTH_TEST);

    // Black
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    gltMakeTorus(torusBatch, 0.4f, 0.15f, 35, 35);

    GLfloat alpha = 0.25f;
    floorBatch.Begin(GL_TRIANGLE_FAN, 4, 1);
    floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
    floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f);
    floorBatch.Normal3f(0.0, 1.0f, 0.0f);
    floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f);

    floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
    floorBatch.MultiTexCoord2f(0, 10.0f, 0.0f);
    floorBatch.Normal3f(0.0, 1.0f, 0.0f);
    floorBatch.Vertex3f(20.0f, -0.41f, 20.0f);

    floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
    floorBatch.MultiTexCoord2f(0, 10.0f, 10.0f);
    floorBatch.Normal3f(0.0, 1.0f, 0.0f);
    floorBatch.Vertex3f(20.0f, -0.41f, -20.0f);

    floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha);
    floorBatch.MultiTexCoord2f(0, 0.0f, 10.0f);
    floorBatch.Normal3f(0.0, 1.0f, 0.0f);
    floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f);
    floorBatch.End();

    glGenTextures(1, textures);
    glBindTexture(GL_TEXTURE_2D, textures[0]);
    LoadBMPTexture("marble.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT);

    // Create blur program
    blurProg =  gltLoadShaderPairWithAttributes("blur.vs", "blur.fs", 2,
                GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "texCoord0");

    // Create blur textures
    glGenTextures(6, blurTextures);

	// ☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆☆
    // XXX I don‘t think this is necessary. Should set texture data to NULL
    // Allocate a pixel buffer to initialize textures and PBOs
	// 计算 像素数据的字节数
    pixelDataSize = screenWidth * screenHeight * 3 * sizeof(unsigned int); // XXX This should be unsigned byte
	// 可以直接用 NULL 初始化 Texture
    //void *data = http://www.mamicode.com/(void *)malloc(pixelDataSize);>


执行效果图:

技术分享



相关阅读:OpenGL深入探索——像素缓冲区对象 (PBO)

























《OpenGL 超级宝典(Super Bible)第五版》 有关 PBO 的 Example