簡體   English   中英

在OpenGL中繪制N-Pointed Star - C ++

[英]Drawing N-Pointed Star in OpenGL - C++

我編寫了一個輸入n並繪制N-point星的代碼,

就像這個:

當n = 5並提交

n = 7或8或16或25時的問題......

我在星圖中遇到問題它變成這樣:

當n = 7並填充時

這是我的代碼:

#include <iostream>
#include <ctime>
#include <vector>
#include <glut.h>

using namespace std;

float starCenterX, starCenterY, starRadius;
int numPoints;
bool bDrawFill = false;

void DrawStar (float cx, float cy, float radius, int numPoints);
void DrawStarFilled (float cx, float cy, float radius, int numPoints);


float width, height;    // global variables to store window width and height

// render text
void renderBitmapString (float x, float y, float z, void* font, const char* string) 
{
    const char *c;
    glRasterPos3f (x, y,z);
    for (c = string; *c != '\0'; c++)
        glutBitmapCharacter (font, *c);
}

void init ()
{
    glClearColor (1.0, 1.0, 1.0, 0.0);  // set display-window color to white
}

void reshape (int width, int height)
{
    ::width = width;
    ::height = height;

    glViewport (0, 0, width, height);

    glMatrixMode (GL_PROJECTION);       // set projection parameters
    glLoadIdentity ();
    gluOrtho2D (0.0, width, 0.0, height);

    glMatrixMode (GL_MODELVIEW);        // set projection parameters
    glLoadIdentity ();
}

void display ()
{
    glClear (GL_COLOR_BUFFER_BIT);      // clear display window

    glColor3f (0, 0, 1);
    renderBitmapString (10, height - 20, 0, GLUT_BITMAP_TIMES_ROMAN_24, "Name   : Saif Badran");
    renderBitmapString (10, height - 50, 0, GLUT_BITMAP_TIMES_ROMAN_24, "ID         : 0142852");
    renderBitmapString (10, height - 80, 0, GLUT_BITMAP_TIMES_ROMAN_24, "Section : 2");

    DrawStar(starCenterX,starCenterY,starRadius,numPoints);

    if(bDrawFill)
        DrawStarFilled(starCenterX,starCenterY,starRadius,numPoints);

    glFlush ();         // process all openGl routines as quickly as possible
}

void processNormalKeys (unsigned char key, int x, int y) 
{
    if(key=='w' || key=='W')
        starCenterY+=4;
    else if(key=='z' || key=='Z')
        starCenterY-=4;
    else if(key=='a' || key=='A')
        starCenterX-=4;
    else if(key=='d' || key=='D')
        starCenterX+=4;
    else if(key=='f' || key=='F')
        bDrawFill = (bDrawFill==1?0:1);
}

void mouseClick (int button, int state, int x, int y)
{
    if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
    {
        starCenterX = x;
        starCenterY = height - y;
    }
}

void activeMouseMotion (int x, int y)
{
    starRadius = abs(starCenterX-x);
}


void main (int argc, char** argv)
{
    cout<<"Enter number of points : ";
    cin>>numPoints;
    numPoints = (numPoints < 2) ? 2 : numPoints;

    glutInit (&argc, argv);                         // initialize GLUT
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   // set display mode
    glutInitWindowPosition (20, 20);        // set top left display window position
    glutInitWindowSize (600, 600);          // set display window width and height
    glutCreateWindow ("Homework#2 : Star Drawing"); // create display window

    init ();                                // execute initialization function

    glutKeyboardFunc (processNormalKeys);
    glutMouseFunc (mouseClick);
    glutMotionFunc (activeMouseMotion);
    glutReshapeFunc (reshape);
    glutDisplayFunc (display);              // send graphics to display window
    glutIdleFunc (display);
    glutMainLoop ();                        // dispaly everthing and wait
}

void DrawStar (float cx, float cy, float radius, int numPoints)
{

    const float DegToRad = 3.14159 / 180;
    glColor3f(1.0,0.0,0.0);
    glBegin (GL_POINTS);
    int count = 1;
        for (int i = 0; i <= 360; i+=360/(numPoints*2)) {
            float DegInRad = i * DegToRad;
            if(count%2!=0)
                glVertex2d (cx + cos (DegInRad) * radius, cy + sin (DegInRad) * radius);
            else
                glVertex2d ((cx + cos (DegInRad) * radius/2), (cy + sin (DegInRad) * radius/2));
        count++;
        }
    glEnd();

    glBegin (GL_LINE_LOOP);
    count = 1;
        for (int i = 0; i <= 360; i+=360/(numPoints*2)) {
            float DegInRad = i * DegToRad;
            if(count%2!=0)
                glVertex2d (cx + cos (DegInRad) * radius, cy + sin (DegInRad) * radius);
            else
                glVertex2d ((cx + cos (DegInRad) * radius/2), (cy + sin (DegInRad) * radius/2));
        count++;
        }
    glEnd();
}

void DrawStarFilled (float cx, float cy, float radius, int numPoints)
{
    const float DegToRad = 3.14159 / 180;

    glBegin (GL_TRIANGLE_FAN);
    int count = 1;
                glVertex2f(starCenterX, starCenterY);
        for (int i = 0; i <= 360; i+=360/(numPoints*2)) {
            float DegInRad = i * DegToRad;
            if(count%2!=0)
                glVertex2d (cx + cos (DegInRad) * radius, cy + sin (DegInRad) * radius);
            else
                glVertex2d ((cx + cos (DegInRad) * radius/2), (cy + sin (DegInRad) * radius/2));
        count++;
        }

    glEnd();
}

問題出在這一行:

for (int i = 0; i <= 360; i+=360/(numPoints*2)) {

對於numPoints = 5 ,對於每個步驟, i將以360/(2*5) = 36遞增。

對於numPoints = 7 ,對於每個步驟, i將以360/(2*7) = 25 (整數除法,截斷25.714...2525.714... 因此,每一步都有0.714..度的損失。 Cummulated,這是: 360 - 14 * 25 = 10度。 這可以在輸出圖片上看到。

為了解決這個問題,我們可以使用浮點變量作為步進計數器,並使用從浮點除法獲得的浮點值來遞增它,例如使用360.0作為分子。 (實際上360.0存儲為double ,將其存儲為單個精度float ,應該是360.0f )。

for (float i = 0; i <= 360; i+=360.0/(numPoints*2)) {

但是這樣做,我們可能在i <= 360比較時遇到麻煩,浮點運算導致量化誤差( i可能比“數學”值略小或大)。 因此,最好保留循環的整數計數器,然后執行浮點運算。 這段代碼部分:

    for (int i = 0; i <= 360; i+=360/(numPoints*2)) {
        float DegInRad = i * DegToRad;

然后會改為:

    for (int i = 0; i <= numPoints*2; i++) {
        float DegInRad = i * 360.0/(numPoints*2) * DegToRad;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM