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

最终效果如下所示

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

使用位图资源

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

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);
上一篇:【MVC+MySQL+EntityFramework】查询性能优化笔记


下一篇:OperationalError: (2002, “Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock' (2)”)