OpenGL 画出雷达动态扫描效果(二) 非底图
时间: 2023-08-18 21:07:09 浏览: 50
非底图雷达扫描效果的实现方式与底图雷达扫描效果有些不同。下面是一个简单的实现思路:
1. 创建一个圆形图形,作为雷达扫描的掩膜。
2. 创建一个矩形图形,作为雷达扫描区域。
3. 将圆形图形和矩形图形合并,得到一个圆形区域内有矩形区域的图形。
4. 将上述图形作为纹理绑定到一个矩形上。
5. 使用OpenGL的着色器编程,将纹理矩形进行旋转,实现雷达扫描效果。
具体实现细节可以参考以下代码示例:
```c++
// 创建圆形图形
GLfloat circleVertices[361 * 3];
for (int i = 0; i < 361; i++)
{
circleVertices[i * 3] = cos(i * M_PI / 180.0f);
circleVertices[i * 3 + 1] = sin(i * M_PI / 180.0f);
circleVertices[i * 3 + 2] = 0.0f;
}
GLuint circleVAO, circleVBO;
glGenVertexArrays(1, &circleVAO);
glBindVertexArray(circleVAO);
glGenBuffers(1, &circleVBO);
glBindBuffer(GL_ARRAY_BUFFER, circleVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(circleVertices), circleVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// 创建矩形图形
GLfloat rectVertices[] = {
-0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f
};
GLuint rectVAO, rectVBO;
glGenVertexArrays(1, &rectVAO);
glBindVertexArray(rectVAO);
glGenBuffers(1, &rectVBO);
glBindBuffer(GL_ARRAY_BUFFER, rectVBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(rectVertices), rectVertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
// 合并图形
GLuint fbo;
glGenFramebuffers(1, &fbo);
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture, 0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(combineShaderProgram);
glBindVertexArray(circleVAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 361);
glBindVertexArray(rectVAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
// 旋转纹理
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(scanShaderProgram);
glBindTexture(GL_TEXTURE_2D, texture);
glUniform1f(glGetUniformLocation(scanShaderProgram, "time"), time);
glUniform1f(glGetUniformLocation(scanShaderProgram, "radius"), radius);
glUniform2f(glGetUniformLocation(scanShaderProgram, "center"), centerX, centerY);
glBindVertexArray(screenVAO);
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
```
其中 `combineShaderProgram` 是将圆形图形和矩形图形合并的着色器程序,`scanShaderProgram` 是旋转纹理的着色器程序,`time` 是时间参数,`radius` 是雷达扫描半径,`centerX` 和 `centerY` 是雷达扫描中心坐标。`screenVAO` 是一个屏幕大小的矩形图形。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)