Solved

OpenGL Help with drawLine Function

Posted on 2011-09-28
2
602 Views
Last Modified: 2013-12-06
Hello Everyone,

I am writing a program in which I must implement the mid-point line algorithm to draw a line.  I have the algorithm working but I am unable to figure out why the line is not displaying to the screen.  BTW I can only use glVertex to plot each pixel. Below is the drawLine function.
void drawLine(GLfloat x1, GLfloat y1, GLfloat x2, GLfloat y2)
{
	GLfloat pk;
	GLfloat pk1;
	GLfloat color = 1.23234;
	Point2D vertex;
	Point2D slope;
	slope.x = x2 - x1;
	slope.y = y2 - y1;

	pk = ((2*slope.y) - slope.x);

	vertex.x = x1;
	vertex.y = y1;

	glColor3f(5.0f, 3.0f, 2.0f);

	for(; vertex.x <= x2; vertex.x++)
	{
		if(pk < 0 )
		{
			//vertex.x++;
			glBegin(GL_POINTS);
			glVertex2f(vertex.x, vertex.y);
			glEnd();
			vertex.round();
			//mySetPixel(vertex.x, vertex.y, &color);
			pk1 = pk + 2 * (slope.y);
			pk = pk1;
		}
		else
		{
			//vertex.x++;
			vertex.y++;
			glBegin(GL_POINTS);
			glVertex2f(vertex.x, vertex.y);
			glEnd();
			pk1 = pk + 2 * (slope.y) - 2 * (slope.x);
			pk = pk1;
		}
	}

Open in new window

0
Comment
Question by:brich744
2 Comments
 
LVL 16

Accepted Solution

by:
George Tokas earned 250 total points
ID: 36813269
Why don't you use the line functions?
glBegin(GL_LINES);
glVertex3f(vertex1.x, vertex1.y,vertex1.z);
glVertex3f(vertex2.x, vertex2.y,vertex2.z);
glEnd();
The certex declaration will be for 3 dimentions where z = 0.
Change the width of the line with: glLineWidth() and don't forget for antialiasing the GL_LINE_SMOOTH parameter.

George Tokas.

0
 
LVL 12

Assisted Solution

by:HappyCactus
HappyCactus earned 250 total points
ID: 36813743
What is the GL_POINT_SIZE query result? what about setting the glpointsize() value?
Are the gl context correctly setup? try to draw a simple triangle, does it display correctly? if not, maybe your problem is not how the line is drawn, but how your GL environment is setup.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
factorial example challenge 10 115
C++ mouse_event mouse look 7 96
c++, dynamic object by json 1 53
Embarcadero C++ builder XE10.1 Berlin TRegistry declaration 1 39
Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question