gpt4 book ai didi

c++ - C++ Opengl 圆函数是如何工作的

转载 作者:行者123 更新时间:2023-11-30 05:32:29 25 4
gpt4 key购买 nike

我正在阅读一本关于 opengl 的书,其中有一个画圆的函数,但我不知道如何将这个函数放入我的代码中并运行它,我也不知道我在其中输入了什么参数。

我是 opengl 的新手,我正在努力弄明白。

代码

#include <stdlib.h>
#include <GL/glut.h>
#include <cmath>

void keyboard(unsigned char key, int x, int y);
void display(void);
void drawCircle(float cx, float cy, float r, int num_segments);

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();

return EXIT_SUCCESS;
}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
//drawCircle(, , , );
glFlush();
}

void drawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for (int i = 0; i < num_segments; i++)
{
float theta = i * (2.0f * PI / num_segments); // get the current angle
float x = r * cos(theta); // calculate the x component
float y = r * sin(theta); // calculate the y component
glVertex2f(x + cx, y + cy); // output vertex
}
glEnd();
}

最佳答案

您缺少窗口创建和设置您所在的圆圈的颜色绘图:

#include <stdlib.h>
#include <gl/glut.h>
#include <math.h>
#define M_PI 3.14159265359


void keyboard(unsigned char key, int x, int y);
void display(void);
void drawCircle(float cx, float cy, float r, int num_segments);

int main(int argc, char** argv)
{
int width = 1280;
int height = 720;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowSize(width, height);
glutCreateWindow("circle");
glutKeyboardFunc(&keyboard);
glutDisplayFunc(&display);
glutMainLoop();
return EXIT_SUCCESS;
}

void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '\x1B':
exit(EXIT_SUCCESS);
break;
}
}

void display()
{
glColor3f(1.0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
gluOrtho2D(0.0, 1280, 0.0, 720);
glColor3f(1.0, 1.0, 1.0);
drawCircle(640, 360, 100, 200);
glFlush();
}

void drawCircle(float cx, float cy, float r, int num_segments)
{
glBegin(GL_LINE_LOOP);
for (int i = 0; i < num_segments; i++)
{
float theta = i * (2.0f * M_PI / num_segments); // get the current angle
float x = r * cos(theta); // calculate the x component
float y = r * sin(theta); // calculate the y component
glVertex2f(x + cx, y + cy); // output vertex
}
glEnd();
}

关于c++ - C++ Opengl 圆函数是如何工作的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35100171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com