After seeing a lot of instances,
Prepare to learn OpenGL along the OpenGL Programming Guide (seventh edition).
Operating Environment:
(1) Windows 7
(2) Codeblocks
(3) GLUT
(4) Author:lane
2014-12-02
1. Black Background plus white rectangle
#include < windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include <cstdlib># Include <cstdio> #include <cmath>void display (void) {glclear (gl_color_buffer_bit); glcolor3f (1.0,1.0,1.0);//White Glbegin (Gl_polygon); glvertex3f (0.25,0.25,0.0); glvertex3f (0.75,0.25,0.0); glvertex3f (0.75,0.75,0.0); glvertex3f (0.25,0.75,0.0); Glend (); Glflush ();//refresh, buffer data output}void init (void) {Glclearcolor (0.0,0.0,0.0,0.0);//Black background glmatrixmode (gl_projection); Glloadidentity (); Glortho (0.0,1.0,0.0,1.0,-1.0,1.0);} int main (int argc,char** argv) {glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowposition (100,100);//Window Position glutinitwindowsize (250,250);//Window size Glutcreatewindow ("Hello");//Window naming init () ; Glutdisplayfunc (display); Glutmainloop (); return 0;}
2. Click the white rectangle that triggers the rotation event, and the mouse wheel clicks stop
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include < cstdlib> #include <cstdio> #include <cmath>static glfloat spin = 0.0;void init (void) {Glclearcolor (0.0,0. 0,0.0,0.0);//Black background glshademodel (Gl_flat);} void display (void) {glclear (gl_color_buffer_bit); Glpushmatrix (); Glrotatef (spin,0.0,0.0,1.0); glcolor3f (1.0,1.0,1.0);//White GLRECTF ( -25.0,-25.0,25.0,25.0); Glpopmatrix (); Glutswapbuffers ();//swap Buffer}//cpu Leisure time execute this function void Spindisplay (void) {spin = spin + 2.0; if (Spin > 360.0) spin-= 360.0; Glutpostredisplay ();//mark the current window to redraw}void reshape (int w,int h) {Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity ();//restore initial coordinate glortho ( -50.0,50.0,-50.0,50.0,-1.0,1.0); Glmatrixmode (Gl_modelview); Glloadidentity ();//restore initial coordinates}void mouse (int button,int state,int x,int y) {switch (button) {case glut_left_button:// Click the event if (sTate = = Glut_down) glutidlefunc (spindisplay);//trigger rotation event break; Case glut_middle_button://Hub Click event if (state = = Glut_down) glutidlefunc (NULL);//Remove rotation event B Reak; Default:break; }}int Main (int argc,char** argv) {glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_double| GLUT_RGB);//double-buffered glutinitwindowsize (250,250); Glutinitwindowposition (100,100); Glutcreatewindow (Argv[0]); Init (); Glutdisplayfunc (display); Glutreshapefunc (reshape);//Set deformation function glutmousefunc (mouse);//Set mouse event Glutmainloop (); return 0;}
2014-12-03
3. Dashed line Drawing
Core function Gllinestipple (1,0x0101);
The first parameter, factor, can be interpreted as a multiple.
The second parameter is written out in binary 0000 0001 0000 0001.
1 represents the need to draw, 0 for ignore. The order is the inverse of the binary.
That is, draw a point, ignore 7 points, draw a point, and ignore 7 points.
If the factor is 2.
That is, drawing 2 points, ignoring 14 points, drawing 2 points, ignoring 14 points.
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include < cstdlib> #include <cstdio> #include <cmath> #define Drawoneline (x1,y1,x2,y2) glbegin (gl_lines); GLVERTEX2F ((x1), (y1)); glvertex2f ((x2), (y2)); Glend (); void init (void) {Glclearcolor (0.0,0.0,0.0,0.0);//Black background Glshademodel (Gl_flat);} void display (void) {int i; Glclear (gl_color_buffer_bit);//Set Background glcolor3f (1.0,1.0,1.0); Glenable (gl_line_stipple);//Set Enable dashed gllinestipple (1,0x0101); Drawoneline (50.0,125.0,150.0,125.0); Gllinestipple (1,0X00FF); Drawoneline (150.0,125.0,250.0,125.0); Gllinestipple (1,0X1C47); Drawoneline (250.0,125.0,350.0,125.0); Gllinewidth (5.0); Gllinestipple (1,0x0101); Drawoneline (50.0,100.0,150.0,100.0); Gllinestipple (1,0X00FF); Drawoneline (150.0,100.0,250.0,100.0); Gllinestipple (1,0X1C47); Drawoneline (250.0,100.0,350.0,100.0); Gllinewidth (1.0); Gllinestipple (1,0X1C47); Glbegin (Gl_line_strip); for (i = 0; i < 7; i++) glvertex2f (50.0+ ((glfloat) I * 50.0), 75.0); Glend (); for (i = 0; i < 6; i++) {Drawoneline (50.0 + ((glfloat) I * 50.0), 50.0,50.0+ ((glfloat) (i+1) *50.0), 50.0); } gllinestipple (5,0X1C47); Drawoneline (50.0,25.0,350.0,25.0); Gldisable (gl_line_stipple); Glflush ();} void reshape (int w,int h) {Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity (); gluortho2d (0.0, (gldouble) w,0.0, (gldouble) h);} int main (int argc, char** argv) {glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowsize (400,150); Glutinitwindowposition (100,100); Glutcreatewindow (Argv[0]); Init (); Glutdisplayfunc (display); Glutreshapefunc (reshape); Glutmainloop (); return 0;}
4. Polygon Draw Point
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include < cstdlib> #include <cstdio> #include <cmath>void display (void) {Glubyte fly[] = {0x00,0x00,0x00,0x0 0,0x00,0x00,0x00,0x00, 0x03,0x80,0x01,0xc0,0x06,0xc0,0x03,0x60, 0x04,0x60,0x06,0x20,0x04,0x30,0x0c,0x20, 0x04,0x18,0x18,0x20,0x04,0x0c,0x30,0x20, 0x04,0x06,0x60,0x20,0x04,0x03,0xc0,0x22, 0x44,0x01,0x80,0x22,0x 44,0x01,0x80,0x22, 0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22, 0x44,0x01,0x80,0x22,0x44,0x01,0x80,0x22, 0X66,0X01,0X80,0X66,0X33,0X01,0X80,0XCC, 0x19,0x81,0x81,0x98,0x0c,0xc1,0x83,0x30, 0x07,0xe1,0x87,0xe0,0x03,0 X3F,0XFC,0XC0, 0x03,0x31,0x8c,0xc0,0x03,0x33,0xcc,0xc0, 0x06,0x64,0x26,0x60,0x0c,0xcc,0x33,0x30, 0x18 , 0xcc,0x33,0x18,0x10,0xc4,0x23,0x08, 0x10,0x63,0xc6,0x08,0x10,0x30,0x0c,0x08, 0x10,0x18,0x18,0x08,0x10,0x00, 0x00,0x08,}; Glubyte HalfTone[] = {0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xAA,0xA A,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55 , 0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xAA,0xAA,0x Aa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x5 5, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xAA,0xAA,0xAA,0 Xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55, 0xaa,0xaa,0xaa,0xaa,0x55,0x55,0x55,0x55,}; Glclear (Gl_color_buffer_bit); glcolor3f (1.0,1.0,1.0); GLRECTF (25.0,25.0,125.0,125.0); Glenable (gl_polygon_stipple); Glpolygonstipple (fly); GLRECTF (125.0,25.0,225.0,125.0); Glpolygonstipple (halftone); GLRECTF (225.0,25.0,325.0,125.0); Gldisable (gl_polygon_stipple); Glflush ();} void init (void) {Glclearcolor (0.0,0.0,0.0,0.0); Glshademodel (Gl_flat);} void reshape (int w,int h) {Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity (); gluortho2d (0.0, (gldouble) w,0.0, (gldouble) h);} int main (int argc,char** argv) {glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowsize (350,150); Glutcreatewindow (Argv[0]); Init (); Glutdisplayfunc (display); Glutreshapefunc (reshape); Glutmainloop (); return 0;}
5. Feedback mode
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include < cstdlib> #include <cstdio> #include <cmath>void init (void) {glenable (gl_lighting);//Enable light glenable (gl_l IGHT0);//enable light source 0}void Drawgeometry (glenum mode) {Glbegin (Gl_line_strip); glnormal3f (0.0,0.0,1.0); glvertex3f (30.0,30.0,0.0); glvertex3f (50.0,60.0,0.0); glvertex3f (70.0,40.0,0.0); Glend (); if (mode = = Gl_feedback) Glpassthrough (1.0); Glbegin (gl_points); glvertex3f ( -100.0,-100.0,-100.0); Glend (); if (mode = = Gl_feedback) glpassthrough (2.0); Glbegin (gl_points); glnormal3f (0.0,0.0,1.0); glvertex3f (50.0,50.0,0.0); Glend (); Glflush ();} void Print3dcolorvertex (Glint size,glint *count,glfloat *buffer) {int i; printf (""); for (i = 0; i < 7; i++) {printf ("%4.2f", buffer[size-(*count)]); *count = *count-1; } printf ("\ n");} void Printbuffer (Glint sizE,glfloat *buffer) {glint count; Glfloat token; count = size; while (count) {token = Buffer[size-count]; count--; if (token = = Gl_pass_through_token) {printf ("Gl_pass_through_token \ n"); printf ("%4.2f \ n", Buffer[size-count]); count--; }else if (token = = Gl_point_token) {printf ("Gl_point_token \ n"); Print3dcolorvertex (Size,&count,buffer); }else if (token = = Gl_line_token) {printf ("Gl_line_token \ n"); Print3dcolorvertex (Size,&count,buffer); Print3dcolorvertex (Size,&count,buffer); }else if (token = = Gl_line_reset_token) {printf ("Gl_line_reset_token \ n"); Print3dcolorvertex (Size,&count,buffer); Print3dcolorvertex (Size,&count,buffer); }}}void display (void) {glfloat feedbuffer[1024]; Glint size; Glmatrixmode (gl_projection); Glloadidentity (); Glortho (0.0,100.0,0.0,100.0,0.0,1.0); Glclearcolor (0.0,0.0,0.0,0.0); Glclear (Gl_color_buffer_bit); Drawgeometry (Gl_render); Glfeedbackbuffer (1024,gl_3d_color,feedbuffer); (void) Glrendermode (gl_feedback); Drawgeometry (Gl_feedback); Size = Glrendermode (Gl_render); Printbuffer (Size,feedbuffer);} int main (int argc,char** argv) {glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowsize (100,100); Glutinitwindowsize (100,100); Glutcreatewindow (Argv[0]); Init (); Glutdisplayfunc (display); Glutmainloop (); return 0;}
6. Transform cubes
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include < cstdlib> #include <cstdio> #include <cmath>void init (void) {Glclearcolor (0.0,0.0,0.0,0.0);//Black background Glsha Demodel (Gl_flat);///Monotone coloring}void display (void) {glclear (gl_color_buffer_bit); glcolor3f (1.0,1.0,1.0);//white glloadidentity ();//restore initial coordinate system Glulookat (0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); Glscalef (1.0,2.0,1.0);//Zoom Glutwirecube (1.0);//Draw Cube Glflush ();} void reshape (int w,int h) {Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity (); Glfrustum ( -1.0,1.0,-1.0,1.0,1.5,20.0); Glmatrixmode (Gl_modelview);} int main (int argc,char** argv) {glutinit (&ARGC,ARGV); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowposition (100,100); Glutinitwindowsize (500,500); Glutcreatewindow (Argv[0]); Init (); Glutdisplayfunc (display); Glutreshapefunc (reshape); Glutmainloop (); return 0;}
7. A wireframe sphere clipped by two clipping planes
#include <windows.h> #include <GL/gl.h> #include <GL/glu.h> #include <GL/glut.h> #include < cstdlib> #include <cstdio> #include <cmath>void init (void) {Glclearcolor (0.0,0.0,0.0,0.0);//Black Glshade Model (Gl_flat);//Monotone coloring}void display (void) {gldouble eqn[4] = {0.0,1.0,0.0,0.0};//y axis clipping gldouble eqn2[4] = {1.0,0.0,0.0 , 0.0};//x shaft cutting glclear (gl_color_buffer_bit); glcolor3f (1.0,1.0,1.0); Glpushmatrix (); Gltranslatef (0.0,0.0,-5.0); Glclipplane (GL_CLIP_PLANE0,EQN);//enable clipping plane 0 glenable (GL_CLIP_PLANE0);//enable cropping 0 Glclipplane (GL_CLIP_PLANE1,EQN2);// Enable crop plane 1 glenable (GL_CLIP_PLANE1);//enable cropping 1 Glrotatef (90.0,1.0,0.0,0.0); Glutwiresphere (1.0,20,16);//Draw Sphere Glpopmatrix (); Glflush ();} void reshape (int w,int h) {Glviewport (0,0, (Glsizei) W, (Glsizei) h); Glmatrixmode (gl_projection); Glloadidentity (); Gluperspective (60.0, (glfloat) w/(glfloat) h,1.0,20.0); Glmatrixmode (Gl_modelview);} int main (int argc,char** argv) {glutinit (& argc,argv); Glutinitdisplaymode (glut_single| GLUT_RGB); Glutinitwindowsize (500,500); Glutinitwindowposition (100,100); Glutcreatewindow (Argv[0]); Init (); Glutdisplayfunc (display); Glutreshapefunc (reshape); Glutmainloop (); return 0;}
lane-Learning OpenGL (1)-Seven simple examples