Very early plan to learn OpenGL, but because the curriculum and learning algorithms occupy the time, has not been able to start, now this time began to make OpenGL, these days are watching "OpenGL into the 8th edition of the Guide," first read GLSL write shader. Here, try a simple small example with a fixed pipeline, and then start the programmable pipeline.
This small example is very simple, is to draw a square in the 2D plane, and then move around.
The idea is simple, mainly two functions, draw and Move,draw is responsible for drawing, move is responsible for the control of movement, and then set two callback functions, Glutdisplayfunc and Glutidlefunc, so that in the idle time will call the Move function, set the Mobile information , and then sends a re-display of the signal so that it can callback the draw function to redraw the new position after the move.
#include <gl/glew.h> #include <gl/glut.h> #include <iostream> #include <cstdlib>using namespace Std; Glfloat rtx = 0, Rty = 0, Rtz = 0;void init () {glloadidentity (); Glclearcolor (0.0, 0.0, 0.0, 0.0);} void Draw (void) {glclear (gl_color_buffer_bit); Glpushmatrix (); Gltranslatef (Rtx, Rty, Rtz); Glbegin (gl_quads); glcolor3f (1.0f, 0.0f, 0.0f), glvertex2f ( -0.5f, 0.5f), glcolor3f (0.0f, 1.0f, 0.0f), glvertex2f (0.5f, 0.5f), glcolor3f ( 0.0f, 0.0f, 1.0f), glvertex2f (0.5f, -0.5f), glcolor3f (0.5f, 0.5f, 0.5f), glvertex2f ( -0.5f, -0.5f); Glend (); Glpopmatrix () ; Glutswapbuffers ();} void Move () {static Glfloat step = 0.0002;if (rtx + step > 0.5 | | rtx + STEP < -0.5) Step =-step;rtx + = Step;glutpost Redisplay ();} int main (int argc, char *argv[]) {glutinit (&ARGC, argv); Glutinitdisplaymode (Glut_rgba | glut_double); glutinitwindowposition (+); glutinitwindowsize (+); Glutcreatewindow ("test"); Init (); Glutdisplayfunc (draw); Glutidlefunc (move); Glutmainloop (); return 0;}
Simple one OpenGL program, 2D Graphics mobile