簡體   English   中英

OpenGL 不畫,但算法正確

[英]OpenGL doesn't paint, but the algorithm is correct

您好,我正在使用 openGl 實現 bresenham 算法。 計算公式是正確的,它不能在屏幕上正確繪制。 問題是什么? 即使將值放入硬編碼中,也不會繪制它。 它編譯得很好。

#include <iostream>
#include <GLUT/glut.h>

void sp(int x, int y){
    glBegin(GL_POINTS);
    glVertex2i(x,y);
    glEnd();
}

void bsh_al(int xs,int ys,int xe,int ye){
    int x,y=ys,W=xe-xs,H=ye-ys;
    int F=2*H-W,dF1=2*H, dF2=2*(H-W);

    for(x=xs;x<=xe;x++){
        sp(x,y);
        std::cout << "x : "<< x << " | y : "<< y << std::endl;
        if(F<0)
            F+=dF1;
        else{
            y++;
            F+=dF2;
        }
    }  
}

void Draw() {
    bsh_al(1,1,6,4);
    glFinish();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("OpenGL");
    glutDisplayFunc(Draw);
    glutMainLoop();

    return 0;
}

用 Xcode 編碼

頂點坐標必須在 [-1.0, 1.0] 范圍內。 左下坐標為 (-1, -1),右下坐標為 (1, 1)。

如果要以像素為單位指定頂點坐標,則必須通過glOrtho設置正交投影。 例如:

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutCreateWindow("OpenGL");
    glutDisplayFunc(Draw);

    int width = glutGet(GLUT_WINDOW_WIDTH);
    int height = glutGet(GLUT_WINDOW_HEIGHT);

    glMatrixMode(GL_PROJECTION);
    glOrtho(0, width, height, 0, -1, 1);

    glMatrixMode(GL_MODELVIEW);

    glutMainLoop();

    return 0;
}

暫無
暫無

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

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