C ++ Opengl循环函数如何工作 [英] How does C++ Opengl circle function work
本文介绍了C ++ Opengl循环函数如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在读一本书 opengl
,并且有一个函数来绘制一个圆,但我不知道如何将这个函数放在我的代码中,并运行它,我也不知道这个参数是什么。
我是新的 opengl
<$ c $ c> #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{
float theta = i *(2.0f * PI / num_segments); //获取当前角度
float x = r * cos(theta); //计算x分量
float y = r * sin(theta); //计算y分量
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 {
float theta = i *(2.0f * M_PI / num_segments); //获取当前角度
float x = r * cos(theta); //计算x分量
float y = r * sin(theta); //计算y分量
glVertex2f(x + cx,y + cy); // output vertex
}
glEnd();
}
I am reading a book of opengl
and there is a function to draw a circle but I don't know how to put this function in my code and run it and also I don't know what parameter I put in this.
I am new to opengl
and I am trying to figure it out.
Code
#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();
}
解决方案
You're missing window creation and a setting the color of the circle you are drawing:
#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 ++ Opengl循环函数如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文