C++调用glew绘制天线水平方向图(在极坐标上)代码,极点根据世界坐标自定义修改
时间: 2024-03-04 08:52:47 浏览: 103
以下是一个使用 GLEW 库在极坐标上绘制水平方向图的 C++ 代码示例:
```c++
#include <iostream>
#include <cmath>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
const double PI = 3.14159265358979323846;
// 极坐标点结构体
struct PolarPoint {
double r; // 半径
double theta; // 角度
};
// 将极坐标转换为笛卡尔坐标
struct CartesianPoint {
double x;
double y;
};
CartesianPoint polarToCartesian(PolarPoint p, CartesianPoint origin) {
// 将角度转换为弧度
double rad = p.theta * PI / 180.0;
// 计算笛卡尔坐标
CartesianPoint result;
result.x = p.r * cos(rad) + origin.x;
result.y = p.r * sin(rad) + origin.y;
return result;
}
// 绘制天线水平方向图
void drawAntenna(std::vector<PolarPoint> data, CartesianPoint origin, double maxRadius) {
// 初始化 GLFW
if (!glfwInit()) {
std::cerr << "Failed to initialize GLFW" << std::endl;
return;
}
// 创建窗口
GLFWwindow* window = glfwCreateWindow(800, 800, "Antenna Plot", NULL, NULL);
if (!window) {
std::cerr << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return;
}
// 使窗口成为当前上下文
glfwMakeContextCurrent(window);
// 初始化 GLEW
if (glewInit() != GLEW_OK) {
std::cerr << "Failed to initialize GLEW" << std::endl;
glfwTerminate();
return;
}
// 设置视口大小
glViewport(0, 0, 800, 800);
// 设置背景颜色
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
// 创建顶点数组和缓冲区
std::vector<GLfloat> vertices;
for (int i = 0; i < data.size(); i++) {
CartesianPoint p = polarToCartesian(data[i], origin);
vertices.push_back(p.x);
vertices.push_back(p.y);
}
GLuint vbo;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
// 创建着色器程序
const GLchar* vertexShaderSource =
"#version 330 core\n"
"layout (location = 0) in vec2 position;\n"
"void main()\n"
"{\n"
" gl_Position = vec4(position.x / 400.0 - 1.0, position.y / 400.0 - 1.0, 0.0, 1.0);\n"
"}\n";
const GLchar* fragmentShaderSource =
"#version 330 core\n"
"out vec4 color;\n"
"void main()\n"
"{\n"
" color = vec4(0.0, 0.0, 1.0, 1.0);\n"
"}\n";
GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
GLuint shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
GLint success;
GLchar infoLog[512];
glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success);
if (!success) {
glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog);
std::cerr << "Failed to link shader program" << std::endl;
glfwTerminate();
return;
}
// 设置顶点属性指针
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// 渲染循环
while (!glfwWindowShouldClose(window)) {
// 处理事件
glfwPollEvents();
// 清空颜色缓冲区
glClear(GL_COLOR_BUFFER_BIT);
// 绘制数据点
glUseProgram(shaderProgram);
glDrawArrays(GL_LINE_STRIP, 0, data.size());
// 交换缓冲区
glfwSwapBuffers(window);
}
// 删除缓冲区和窗口
glDeleteBuffers(1, &vbo);
glfwTerminate();
}
int main() {
// 构造极坐标数据
std::vector<PolarPoint> data = {{10, 0}, {20, 30}, {30, 60}, {40, 90}, {50, 120}, {60, 150}, {70, 180}, {80, 210}, {90, 240}, {100, 270}, {90, 300}, {80, 330}};
// 定义极点
CartesianPoint origin = {400, 400};
// 定义最大半径
double maxRadius = 400;
// 绘制天线水平方向图
drawAntenna(data, origin, maxRadius);
return 0;
}
```
这段代码使用 GLFW 创建一个窗口,并使用 GLEW 绘制数据点。顶点数组和缓冲区存储数据点的笛卡尔坐标。着色器程序将顶点坐标从像素坐标转换为标准化设备坐标,然后将其绘制为蓝色线条。在渲染循环中,使用 `glfwPollEvents()` 处理事件,使用 `glClear()` 清空颜色缓冲区,使用 `glUseProgram()` 设置着色器程序,使用 `glDrawArrays()` 绘制数据点,使用 `glfwSwapBuffers()` 交换缓冲区。
阅读全文