如何使用此代码作为起点在opengl中绘制一个较小的圆?

时间:2010-11-16 12:59:40

标签: c opengl

我正在尝试使用opengl在c中绘制一个圆圈,它比显示的那个小。问题是我似乎无法找到如何减小它的大小......任何人都可以帮助我吗?

#define GLUT_DISABLE_ATEXIT_HACK
#include <GL/gl.h>
#include <GL/glut.h>
#include <stdio.h>

#include <math.h>
#define PI 3.1415926535898
GLint circle_points =100;
 // This is the draw function.
void draw()
{

glClear(GL_COLOR_BUFFER_BIT);
double angle = 2*  PI/circle_points ;
glPolygonMode( GL_FRONT, GL_FILL );
glColor3f(0.2, 0.5, 0.5 );
glBegin(GL_POLYGON);
    double angle1=0.0;
    glVertex2d( cos(0.0) , sin(0.0));
    int i;
    for ( i=0 ; i< circle_points ;i++)
    {
        printf( "angle = %f \n" , angle1);
        glVertex2d(cos(angle1),sin(angle1));
        angle1 += angle ;
    }
glEnd();
glFlush();
}

void init()
{
glClearColor(0.0,0.0,0.0,0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
}

void keyboard (unsigned char key , int x, int y)
{
exit(0);

}

void main( int argc,char **argv)
 {
     glutInit(&argc,argv);
 glutInitDisplayMode(GLUT_SINGLE |GLUT_RGB);
 glutInitWindowSize(250,250);
 glutInitWindowPosition(100,100);
 glutCreateWindow("ch06");
 init();
 glutKeyboardFunc(keyboard);
 glutDisplayFunc(draw);
        glutMainLoop();
 }

2 个答案:

答案 0 :(得分:5)

您可以更改以下行:

    glVertex2d(cos(angle1),sin(angle1)); 

为:

    glVertex2d(0.25f * cos(angle1),0.25f * sin(angle1)); 

这将绘制一个半径为0.25而不是1的圆圈。

编辑:正如Dasen发现的那样,您还需要将0.25f *添加到该行:

    glVertex2d(cos(0.0),sin(0.0)); 

答案 1 :(得分:1)

您当然也可以在下面添加绘图代码:

glScalef(0.25f, 0.25f, 1.0f);

这会改变你的模型视图矩阵,所以最好还是使用矩阵堆栈保存它:

glPushMatrix();
glScalef(0.25, 0.25f, 1.0f);
/* drawing code goes here. */
glPopMatrix();