Click here to Skip to main content
15,896,727 members
Articles / Multimedia / OpenGL

Drawing nearly perfect 2D line segments in OpenGL

Rate me:
Please Sign up or sign in to vote.
4.80/5 (29 votes)
18 Jul 2011CPOL5 min read 246K   8.4K   87  
With premium quality anti- aliasing, color, thickness, and minimum CPU overhead.
/*This file must be used with FLTK1.3 with gl enabled
 * config.h is generated by fltk on your system
 * compile with: fltk-config --use-gl --compile test8.cpp
*/
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <FL/Fl.H>
#include <FL/gl.h>
#include <FL/Fl_Window.H>
#include <FL/Fl_Gl_Window.H>
#include "../include/vase_rend_draft_2.h"

#define MATH_PI 3.141592654

class Gl_Window : public Fl_Gl_Window
{
	void draw();
	int handle(int);

	public:
	Gl_Window(int x,int y,int w,int h,const char *l=0)
		: Fl_Gl_Window(x,y,w,h,l) {}
	Gl_Window(int w,int h,const char *l=0)
		: Fl_Gl_Window(w,h,l) {}
};

void Gl_Window::draw()
{
	if (!valid())
	{
		glMatrixMode(GL_PROJECTION);
			glLoadIdentity();
			glOrtho( 0,w(),h(),0,0.0f,100.0f);
			glClearColor( 1.0,1.0,1.0,1.0f);
			glClearDepth( 0.0f);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		//glEnable(GL_LINE_SMOOTH);
		//glHint(GL_LINE_SMOOTH_HINT,  GL_NICEST/*GL_FASTEST*/);
	}
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);
	
	float background_vertex[]=
	{
		0  ,  0,
		806,  0,
		0  ,194,
		806,194
	};
	float background_color[]=
	{
		1.0,1.0,1.0,
		1.0,1.0,1.0,
		0.6,0.6,0.6,
		0.6,0.6,0.6
	};
	glVertexPointer(2, GL_FLOAT, 0, background_vertex);
	glColorPointer(3, GL_FLOAT, 0, background_color);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	
	for ( int i=0; i<20; i++)
	{
		line ( 5+29.7*i,187, 35+29.7*i,8, //coordinates
			0.3*(i+1),		//thickness in px
			0.5, 0.0, 1.0, 1.0,	//color RGBA
			0,0,			//not used
			true);			//enable alphablend
	}
	
	for ( double ag=0; ag<=2*MATH_PI; ag+=MATH_PI/20.0)
	{
		double tx2=90*cos(ag);
		double ty2=90*sin(ag);
		double tx1=0.0;
		double ty1=0.0;
		line ( 706+tx1,97-ty1, 706+tx2,97-ty2, //coordinates
			1.0,			//thickness in px
			0.5, 0.0, 1.0, 1.0,	//color RGBA
			0,0,			//not used
			true);			//enable alphablend

		//hair_line ( 706+tx1,97-ty1, 706+tx2,97-ty2, true);
	}
	
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_COLOR_ARRAY);
}

int Gl_Window::handle(int e)
{
	return Fl_Gl_Window::handle(e);
}

int main(int argc, char **argv)
{
	Gl_Window gl_wnd( 606+200,194,"test8 [fltk-opengl][vertex array]");
	gl_wnd.position( 170,360);
	gl_wnd.show();
	gl_wnd.redraw();
	return Fl::run();
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Hong Kong Hong Kong
Chris H.F. Tsang
tyt2y3@gmail.com

Comments and Discussions