3

I was trying to use opengl using c. The code which I wrote leaves gaps within the lines i.e. draws dotted lined whereas the one which I found on the net draws perfectly. I don't get that, if there is no difference in the code which I wrote and the one I found on the net, Why does this happen?

I tried surfing through the sites. I also read DDA Line Drawing Algoruthm have errors however, couldn't find the solution

#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();
}

The code which I found on the net:

#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;
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 3
    Is there a reason why you’re writing in woefully outdated K&R style instead of a modern style of C? – Konrad Rudolph Apr 03 '19 at 16:58
  • 1
    No reason. I am used to that style since I learned c from the first edition of Ritchie's book –  Apr 03 '19 at 17:15
  • I don't feel that, it should be an issue, because of the way that it has been implemented. I gave int values only, when running the program –  Apr 03 '19 at 17:21
  • 1
    Why do you say that "there is no difference in the code" if they are clearly not identical? – Yakov Galka Apr 03 '19 at 17:37
  • @ybungalobill, What I meant was, there was no logical difference. I didn't know that using GL_LINES instead of GL_POINTS will cause so big a difference, of course till you pointed it out. Thanks for your help –  Apr 03 '19 at 17:52

1 Answers1

3

The difference is that your code uses GL_LINES whereas the other code uses GL_POINTS.

When you draw GL_POINTS then each drawn vertex fills one pixel (when point size is 1).

When you draw GL_LINES then every pair of vertices draws a line between them. In your code it essentially means that vertices 0 and 1 fill the 0th pixel, vertices 2 and 3 fill the 2nd pixel, etc... no pairs fill the odd pixels.

Possible solutions:

  • Use GL_POINTS as in the original code.
  • Use GL_LINE_STRIP.
  • Or the best of all, just use the OpenGL functionality of rasterizing the line for you:

    void dda(float x0, float y0, float x1, float y1)
    {
        glBegin(GL_LINES);
        glVertex2f(x0, y0);
        glVertex2f(x1, y1);
        glEnd();
    }
    
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • Nice bro. Thanks. However, if I write GL_LINES, do I need to write x += xInc and y += yInc? –  Apr 03 '19 at 17:49
  • @kesarling: I don't understand your question. You already wrote GL_LINES, so what do you mean? Just replace your `dda` function with the one In my answer and see for yourself. – Yakov Galka Apr 03 '19 at 17:55
  • Thanks. Do consider including glFlush() after glEnd() –  Apr 03 '19 at 18:10
  • @kesarling: `glFlush` shall be at the end of the frame rendering code in `renderFunction`, not in `dda`. – Yakov Galka Apr 03 '19 at 19:13