opengl画线 虚线 折线(OpenGL画线虚线折线)
opengl画线 虚线 折线(OpenGL画线虚线折线) * OpenGL graphics, dotted line, various shapes
#include "windows.h""
#include
#include
#include
Void myinit (void);
Void DrawMyObjects (void);
Void myReshape (GLsizei, W, GLsizei, H);
Void display (void);
Void myinit (void)
{
GlClearColor (0.0,0.0,0.0,0.0);
GlClear (GL_COLOR_BUFFER_BIT);
GlShadeModel (GL_FLAT);
}
Void myReshape (GLsizei, W, GLsizei, H)
{
GlViewport (0,0, W, H);
GlMatrixMode (GL_PROJECTION);
GlLoadIdentity ();
If (w<=h)
GlOrtho (-20.0,20.0, -20.0* (GLfloat), h/ (GLfloat), W, 20.0*
(GLfloat), h/ (GLfloat), W, -50.0,50.0);
Else
GlOrtho (-20.0* (GLfloat), h/ (GLfloat), W, 20.0* (GLfloat),
h/ (GLfloat), W, -20.0,20.0, -50.0,50.0);
GlMatrixMode (GL_MODELVIEW);
GlLoadIdentity ();
}
Void display (void)
{
GlColor3f (1.0,1.0,0.0);
DrawMyObjects ();
GlFlush ();
}
Void DrawMyObjects (void)
{
GlPointSize (5); / / set point size
GlBegin (GL_POINTS); / / painting
GlColor3f (1.0,0.0,0.0);
GlVertex2f (-10.0,11.0);
GlColor3f (1.0,1.0,0.0);
GlVertex2f (-9.0,10.0);
GlColor3f (0.0,1.0,1.0);
GlVertex2f (-8.0,12.0);
GlEnd ();
GlLineWidth (1); / / set the line size
GlBegin (GL_LINES); / / painting line
GlColor3f (1.0,1.0,0.0);
GlVertex2f (-11.0,8.0);
GlVertex2f (-7.0,7.0);
GlColor3f (1.0,0.0,1.0);
GlVertex2f (-11.0,9.0);
GlVertex2f (-8.0,6.0);
GlEnd ();
GlLineWidth (1); / / set the closed line size
GlBegin (GL_LINE_STRIP); / / not closed line
GlVertex2f (-3.0,9.0);
GlColor3f (0.0,1.0,0.0);
GlVertex2f (2.0,6.0);
GlColor3f (0.0,0.0,1.0);
GlVertex2f (3.0,8.0);
GlColor3f (1.0,0.0,1.0);
GlVertex2f (-2.5,6.5);
GlEnd ();
GlLineWidth (1); / / set the closed line size
GlBegin (GL_LINE_LOOP); / / draw closed line
GlColor3f (0.0,1.0,1.0);
GlVertex2f (7.0,7.0);
GlVertex2f (8.0,8.0);
GlVertex2f (9.0,6.5);
GlVertex2f (10.3,7.5);
GlVertex2f (11.5,6.0);
GlVertex2f (7.5,6.0);
GlEnd ();
GlEnable (GL_LINE_STIPPLE); / / draw line start
GlLineWidth (1);
GlLineStipple (1,0x00FF); / / the first parameter is the ratio
of second is 16 hexadecimal number
GlBegin (GL_LINES);
GlColor3f (1.0,1.0,0.0);
GlVertex2f (-11.0,8.0);
GlVertex2f (-3.0,7.0);
GlEnd ();
GlLineStipple (1,0x00FF);
GlBegin (GL_LINES);
GlColor3f (1.0,0.0,1.0);
GlVertex2f (-15.0,9.0);
GlVertex2f (-9.0,6.0);
GlEnd ();
GlDisable (GL_LINE_STIPPLE); / / the end of line painting
GlPolygonMode (GL_FRONT, GL_LINE); / / front line
/ / the first parameter is the front and back GL_FRONT, GL_BACK,
GL_FRONT_AND_BACK, second GL_LINE GL_FILL, GL_POINT parameters.
GlBegin (GL_POLYGON); / / drawing convex polygon
GlColor3f (0.5,0.3,0.7);
GlVertex2f (-7.0,2.0);
GlVertex2f (-8.0,3.0);
GlVertex2f (-10.3,0.5);
GlVertex2f (-7.5, -2.0);
GlVertex2f (-6.0, -1.0);
GlEnd ();
GlPolygonMode (GL_FRONT, GL_LINE); / / front line
GlPolygonMode (GL_BACK, GL_FILL); / / back filling
GlBegin (GL_TRIANGLES); / / draw a triangle
GlColor3f (0.2,0.5,0.7); / / counterclockwise, positive
GlVertex2f (-10.0, -5.0);
GlVertex2f (-12.3, -7.5);
GlVertex2f (-8.5, -6.0);
GlColor3f (0.2,0.7,0.5); / / clockwise, back
GlVertex2f (-8.0, -7.0);
GlVertex2f (-7.0, -4.5);
GlVertex2f (-5.5, -9.0);
GlEnd ();
GlBegin (GL_TRIANGLE_STRIP); / / draw linear triangular group
GlVertex2f (-1.0, -8.0);
GlVertex2f (-2.5, -5.0);
GlColor3f (0.8,0.8,0.0);
GlVertex2f (1, -7.0);
GlColor3f (0.0,0.8,0.8);
GlVertex2f (2, -4.0);
GlColor3f (0.8,0.0,0.8);
GlVertex2f (4, -6.0);
GlEnd ();
GlBegin (GL_TRIANGLE_FAN); / / fan painting
GlVertex2f (8, -6.0);
GlVertex2f (10, -3.0);
GlColor3f (0.8,0.2,0.5);
GlVertex2f (12.5, -4.5);
GlColor3f (0.2,0.5,0.8);
GlVertex2f (13, -7.5);
GlColor3f (0.8,0.5,0.2);
GlVertex2f (10.5, -9.0);
GlEnd ();
GlBegin (GL_QUADS); / / draw quadrilateral
GlColor3f (0.7,0.5,0.2);
GlVertex2f (0.0,2.0);
GlVertex2f (-1.0,3.0);
GlVertex2f (-3.3,0.5);
GlVertex2f (-0.5, -1.0);
GlColor3f (0.5,0.7,0.2);
GlVertex2f (3.0,2.0);
GlVertex2f (2.0,3.0);
GlVertex2f (0.0,0.5);
GlVertex2f (2.5, -1.0);
GlEnd ();
GlBegin (GL_QUADS); / / draw continuous filling quadrilateral
GlVertex2f (6, -2.0);
GlVertex2f (5.5,1.0);
GlVertex2f (8, -1.0);
GlColor3f (0.8,0.0,0.0);
GlVertex2f (9.0,2.0);
GlVertex2f (11, -2.0);
GlColor3f (0.0,0.0,0.8);
GlVertex2f (11.0,2.0);
GlVertex2f (13, -1.0);
GlColor3f (0.0,0.8,0.0);
GlVertex2f (14.0,1.0);
GlEnd ();
}
Int main (int, argc, char**, argv)
{
GlutInit (&argc, argv);
GlutInitDisplayMode (GLUT_SINGLE GLUT_RGB |);
GlutInitWindowSize (500, 500);
GlutInitWindowPosition (0, 0);
GlutCreateWindow ("Draw");
Myinit (); //init ();
GlutReshapeFunc (myReshape);
GlutDisplayFunc (display);
//glutKeyboardFunc (keyboard);
GlutMainLoop ();
Return 0;
}