Simple one OpenGL program, 2D Graphics mobile

Source: Internet
Author: User

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 (&AMP;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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.