OpenGL不会绘制,但是算法正确

问题描述 投票:0回答:1

你好,我正在用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编码

c++ opengl graphics opengl-compat
1个回答
0
投票

顶点坐标必须在[-1.0,1.0]范围内。左下角的坐标为(-1,-1),向右为(1,1)。

如果要以像素为单位指定顶点坐标,则必须通过glOrtho设置正交投影。例如:

glOrtho
© www.soinside.com 2019 - 2024. All rights reserved.