OpenGL 画出雷达动态扫描效果(一)

最终效果如下所示

Demo下载  http://files.cnblogs.com/xd-jinjian/Debug.zip

源代码下载 http://download.csdn.net/detail/xdjinjian/8266927

绘制扫面线部分代码如下,扫面部分可改进使用粒子系统,以增加真实感。

    glBegin(GL_TRIANGLE_FAN);
    glColor4f(0.0f, 0.0f, 0.0f,1.0f);
    glVertex2f(0.0f, 0.0f);    

    
    for(int i=0;i<N;++i)
    { 
        pos_x=cos((i/180.0)*Pi+theta)*R;
        pos_y=sin((i/180.0)*Pi+theta)*R;
        glColor4f(0.0f, (i+0.01)/N, 0.0f,0.8f);
        glVertex2f(pos_x, pos_y); 
    }
    glEnd();    
    

 

雷达界面的框架部分使用纹理贴图完成,加载位图资源代码如下

int power_of_two(int n) 
{ 
    if( n <= 0 ) 
        return 0; 
    return (n & (n-1)) == 0; 
} 

GLuint load_texture(const char* file_name) 
{ 
    GLint width, height, total_bytes; 
    GLubyte* pixels = 0; 
    GLint last_texture_ID;
    GLuint texture_ID = 0; 
 
        FILE* pFile = fopen(file_name, "rb"); 
    if( pFile == 0 ) 
        return 0; 
 
    // 读取文件中图象的宽度和高度 
    fseek(pFile, 0x0012, SEEK_SET); 
    fread(&width, 4, 1, pFile); 
    fread(&height, 4, 1, pFile); 
    fseek(pFile, BMP_Header_Length, SEEK_SET); 
 
    // 计算每行像素所占字节数,并根据此数据计算总像素字节数 
        { 
        GLint line_bytes = width * 3; 
        while( line_bytes % 4 != 0 ) 
            ++line_bytes; 
        total_bytes = line_bytes * height; 
    } 
 
    // 根据总像素字节数分配内存 
    pixels = (GLubyte*)malloc(total_bytes); 
    if( pixels == 0 ) 
            { 
        fclose(pFile); 
        return 0; 
    } 
 
    // 读取像素数据 
    if( fread(pixels, total_bytes, 1, pFile) <= 0 ) 
    { 
                free(pixels); 
        fclose(pFile); 
        return 0; 
    } 
    { 
       GLint max; 
       glGetIntegerv(GL_MAX_TEXTURE_SIZE, &max); 
       if( !power_of_two(width) 
        || !power_of_two(height) 
        || width > max 
        || height > max ) 
       { 
                       const GLint new_width = 256; 
            const GLint new_height = 256; // 规定缩放后新的大小为边长的正方形 
            GLint new_line_bytes, new_total_bytes; 
            GLubyte* new_pixels = 0; 
 
            // 计算每行需要的字节数和总字节数 
            new_line_bytes = new_width * 3; 
            while( new_line_bytes % 4 != 0 ) 
                                ++new_line_bytes; 
            new_total_bytes = new_line_bytes * new_height; 
 
            // 分配内存 
            new_pixels = (GLubyte*)malloc(new_total_bytes); 
            if( new_pixels == 0 ) 
            { 
                free(pixels); 
                fclose(pFile); 
                return 0; 
            } 

                        gluScaleImage(GL_RGB, 
                width, height, GL_UNSIGNED_BYTE, pixels, new_width, new_height, GL_UNSIGNED_BYTE, new_pixels); 
 
            // 释放原来的像素数据,把pixels指向新的像素数据,并重新设置width和height 
            free(pixels); 
            pixels = new_pixels; 
            width = new_width; 

                       height = new_height; 
       } 
   } 
   glGenTextures(1, &texture_ID); 
    if( texture_ID == 0 ) 
    { 
        free(pixels); 
        fclose(pFile); 
        return 0; 
    } 
    glGetIntegerv(GL_TEXTURE_BINDING_2D, &last_texture_ID); 
    glBindTexture(GL_TEXTURE_2D, texture_ID); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, 
    GL_BGR_EXT, GL_UNSIGNED_BYTE, pixels); 
    glBindTexture(GL_TEXTURE_2D, last_texture_ID); 
    free(pixels); 
    return texture_ID; 
} 

使用位图资源

texGround = load_texture("xxxxx.bmp");

纹理贴图

    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, texGround); 
    glBegin(GL_QUADS); 
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.5f, -0.5f,-0.0f); 
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, 0.5f,-0.0f); 
        glTexCoord2f(1.0f, 1.0f); glVertex3f(0.5f, 0.5f,-0.0f); 
        glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, -0.5f,-0.0f); 
    glEnd();

注意画图形时打开纹理混合,并关闭深度测试

    glDisable(GL_DEPTH_TEST);
    glEnable(GL_BLEND);        

 

原文地址:https://www.cnblogs.com/xd-jinjian/p/3916954.html