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;
}
The difference is that your code uses
GL_LINES
whereas the other code usesGL_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:
GL_POINTS
as in the original code.GL_LINE_STRIP
.Or the best of all, just use the OpenGL functionality of rasterizing the line for you: