我正在读一本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();
}
答案 0 :(得分:1)
您缺少窗口创建和设置圆的颜色 附图中:
#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();
}