以下两个实现之间的基本区别是什么?

What's the basic difference between the following two implementations?

我试图通过 c 使用 opengl。我写的代码在线条内留下了空隙,即绘制了虚线,而我在网上找到的代码绘制得非常完美。我不明白,如果我写的代码和我在网上找到的代码没有区别,为什么会这样?

我尝试浏览网站。我也看了DDA Line Drawing Algoruthm have errors 然而,找不到解决方案

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
//comment the following line when not on windows
//#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>

void Init(void);                //used to initialize the stuff such as ortho2D and matrix mode;
void renderFunction(void);          //this function is called in the glutDisplayFunc
void drawAxes(void);                //used to draw the axes
void dda(const float, const float, const float, const float);       //the actual implementation of the algorithm

main(argc, argv)
char** argv;
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(300,300);
    glutInitWindowPosition(100,100);
    glutCreateWindow(argv[1]);
    Init();
    glutDisplayFunc(renderFunction);
    glutMainLoop();
return EXIT_FAILURE;
}

void Init(void) {
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 1.0, 1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(-500, 500, -500, 500);
}

void renderFunction(void) {
    drawAxes();
    int x0, y0, x1, y1;
    while(1) {
        printf("ENTER THE CO-ORDINATES OF THE FIRST POINT: ");
        scanf("%d %d",&x0 ,&y0);
        printf("ENTER THE CO-ORDINATES OF THE SECOND POINT: ");
        scanf("%d %d",&x1 ,&y1);
        dda(x0, y0, x1, y1);
    }
}

void drawAxes(void) {
    dda(-500, 0, 500, 0);
    dda(0, -500, 0, 500);
}

void dda(x0, y0, x1, y1)
const float x0, y0, x1, y1;
{
    float dx = x1 - x0;
    float dy = y1 - y0;
    int steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
    float xInc = dx/(float)steps;
    float yInc = dy/(float)steps;
    float x = x0;
    float y = y0;
    glBegin(GL_LINES);
        int i = 0;
        for(i = 0; i < steps; i++) {
            glVertex2i((int)x, (int)y);
            x += xInc;
            y += yInc;
        }
    glEnd();
    glFlush();
}

网上找到的代码:

#include<stdio.h>
#include<math.h>
#include<GL/freeglut.h>
#include<GL/gl.h>

void dda(int x0, int y0, int x1, int y1){
    int steps;
    float Xinc; float Yinc; float X,Y;
//DDA Calculation start
    int dx = x1-x0;
    int dy = y1-y0;
    steps = abs(dx) > abs(dy) ? abs(dx) : abs(dy);
    Xinc = dx/(float)steps;
    Yinc = dy/(float)steps;
    X=x0;
    Y=y0;
//DDA Calculation end
    int i;
    glColor3f(0.0, 0.0, 0.0);
//  glOrtho(-1.0,1.0,-1.0, 1.0,1.0,-1.0);
    glBegin(GL_POINTS);
    for(i=0 ; i<steps ; i++){
        glVertex2i((int)X,(int)Y);
        X+=Xinc;
        Y+=Yinc;
    }
    glEnd();
}

void axis(){
    dda(-750,0,750,0);
    dda(0,-750,0,750);
}

void renderF(void){
    gluOrtho2D(750,-750,750,-750);
    axis();

    //Diagonal Vertex 1
    int x1 = 500;
    int y1 = 500;
    //Diagonal Vertex 2
    int x2 = -500;
    int y2 = -500;

    int v = x1;
    int u = v/2;

    dda(-v,v,-v,-v);


    glFlush();
}

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Hello");

    glClearColor(1.0, 1.0, 1.0, 0.0);
    glClear(GL_COLOR_BUFFER_BIT);
    glutDisplayFunc(renderF);

    glutMainLoop();
    return 0;
}

区别在于您的代码使用 GL_LINES 而其他代码使用 GL_POINTS.

绘制时 GL_POINTS 每个绘制的顶点填充一个像素(当点大小为 1 时)。

当您绘制 GL_LINES 时,每对顶点都会在它们之间绘制一条线。在您的代码中,它本质上意味着顶点 0 和 1 填充第 0 个像素,顶点 2 和 3 填充第二个像素,等等......没有对填充奇数像素。

可能的解决方案:

  • 使用GL_POINTS 和原来的代码一样.
  • 使用GL_LINE_STRIP.
  • 或者最重要的是,只需使用 OpenGL 功能为您栅格化线条:

    void dda(float x0, float y0, float x1, float y1)
    {
        glBegin(GL_LINES);
        glVertex2f(x0, y0);
        glVertex2f(x1, y1);
        glEnd();
    }