首页 > 代码库 > OPENGL 显示BMP图片+旋转

OPENGL 显示BMP图片+旋转

VS2010/Windows 7/

1. 需包含头文件 stdio.h, glaux.h, glut.h。需要对应的lib,并添加包含路径

2. 窗口显示用glut库的函数

3. bmp图片从本地读取,再用它来生成纹理,首先用auxDIBImageLoad函数将图片数据读到AUX_RGBImageRec结构体当中。

    然后

//生成纹理数据
glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);

   然后用线性滤波函数按自己要求缩放显示

//线性滤波
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

4. 图片旋转显示的时候用

glRotatef(-90,0.0f,0.0f,1.0f);

或 

//贴图及映射(glTexCoord2f对应纹理坐标, glVertex2f对应矩形坐标,此处已做旋转处理)
glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, 1.0f);     
glTexCoord2f(0.0f, 1.0f); glVertex2f( 1.0f, 1.0f);    
glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, -1.0f); 
glTexCoord2f(1.0f, 0.0f); glVertex2f(-1.0f, -1.0f);     

前一种是旋转函数,后一种是直接在坐标映射时改坐标

附代码:

#include "stdafx.h"

#include <stdio.h>
#include <gl/glaux.h>
#include<gl/glut.h>

GLfloat xrot;
GLfloat yrot;
GLfloat zrot;

GLuint texture[1]; //存储一个纹理

//读取bmp文件数据
AUX_RGBImageRec *LoadBMP(char * Filename)
{
    FILE *File = NULL;
    if (!Filename)
        return NULL;
    File = fopen(Filename, "r");
    if (File)
    {
        fclose(File);
        return auxDIBImageLoad(Filename);
    }
    return NULL;
}

//生成纹理
int LoadGLTextures()
{
    int Status = FALSE;
    AUX_RGBImageRec *TextureImage[1];

    memset(TextureImage, 0, sizeof(void *)*1);
    if (TextureImage[0] = LoadBMP("C:\\2.bmp"))
    {
        Status = TRUE;
        //注册纹理
        glGenTextures(1, &texture[0]); 
        //绑定纹理名称
        glBindTexture(GL_TEXTURE_2D, texture[0]);
        //生成纹理数据
        glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);
        //线性滤波
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    }
    if (TextureImage[0])
    {
        if (TextureImage[0]->data)
        {
            free(TextureImage[0]->data);
        }
        free(TextureImage[0]);
    }
    return Status;
}

//调用入口
int InitGL(GLvoid)
{
    if (!LoadGLTextures())
    {
        return false;
    }
    glEnable(GL_TEXTURE_2D);
    //glShadeModel(GL_SMOOTH);
    //设置背景色为黑色
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    return TRUE;
}

//贴图绘制
void DrawGLScene(GLvoid)
{
    //清除屏幕缓存
    glClear(GL_COLOR_BUFFER_BIT);    
    //重置当前的模型观察矩阵
    glLoadIdentity();
    
    /**************************************
    * 旋转函数备用 -90表示顺时针绕Z轴旋转90度
    ***************************************/
    //glRotatef(-90,0.0f,0.0f,1.0f);                
    
    //开始绘制四边形
    glBegin(GL_QUADS);

    //贴图及映射(glTexCoord2f对应纹理坐标, glVertex2f对应矩形坐标,此处已做旋转处理)
    glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, 1.0f);     
    glTexCoord2f(0.0f, 1.0f); glVertex2f( 1.0f, 1.0f);    
    glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0f, -1.0f); 
    glTexCoord2f(1.0f, 0.0f); glVertex2f(-1.0f, -1.0f);     

    glEnd();
    glFlush();
    //glutSwapBuffers();
}

int main(int argc, char **argv)
{
    //初始化glut运行
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DEPTH|GLUT_SINGLE|GLUT_RGBA);
    glutInitWindowPosition(300,100);
    glutInitWindowSize(450, 600);
    glutCreateWindow("hello");
    
    InitGL();
    glutDisplayFunc(&DrawGLScene);
    glutMainLoop();
    return 0;
}

(完)