Getting Started with OpenGL "1 QuickStart"

Source: Internet
Author: User

OpenGL.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream> #include <GL/glut.h> #include <GL/glaux.h> using namespace Std;void background (void) {Glclearcolor (0.0,0.0,0.0,0.0);//Set background color to black}void mydisplay (void) {Glclear (gl_color_buffer _bit);//buffer is set to color writable Glbegin (gl_triangles);//Start Drawing triangle Glshademodel (Gl_smooth);//set to smooth light mode glcolor3f (1.0,0.0,0.0); /Set the first vertex to be Red glvertex2f ( -1.0,-1.0);//Set the coordinates of the first vertex to ( -1.0,-1.0) glcolor3f (0.0,1.0,0.0);//Set the second vertex to be green glvertex2f (0.0,- 1.0)///set the coordinates of the second vertex to (0.0,-1.0) glcolor3f (0.0,0.0,1.0);//Set the third vertex to Blue glvertex2f ( -0.5,1.0);// Set the coordinates of the third vertex to ( -0.5,1.0) glend ();//triangle End glflush ();//force OpenGL function to execute}void myreshape (glsizei W,glsizei h) in a limited time {Glviewport (0,0,w,h);//Set viewport Glmatrixmode (gl_projection);//indicate the current matrix is gl_projectionglloadidentity ();//replace the current matrix with a unit array if (w <= h) Gluortho2d ( -1.0,1.5,-1.5,1.5* (glfloat) h/(glfloat) w);//define two-dimensional face projection matrix elsegluortho2d ( -1.0,1.5* (glfloat) w/(GLfloat) H, -1.5,1.5)///is probably the coordinate glmatrixmode (Gl_modelview) that defines the three vertices,//indicates that the current matrix is gl_modelview}int main (iNT Argc,char * * argv) {/* Initialize */glutinit (&AMP;ARGC,ARGV);//used to initialize the GLUT library and negotiate with the form system dialog Glutinitdisplaymode (glut_single| GLUT_RGB);//used to determine the display mode of the created form. The Glut_single in this example refers to the order cache form, which is the default mode, and the corresponding mode is the glut_double double-cached form. The parameter Glut_rgb specifies the color RGBA mode, which is also the default mode, and the corresponding mode is the Glut_index color index mode form. Glutinitwindowsize (800,400);//Initializes the size of the form, the first parameter is the width of the form, and the second parameter is the height of the form, in pixels. Glutinitwindowposition (200,200);//sets the position of the initial form, the first parameter is the coordinate of the upper-left corner of the form, and the second parameter is the coordinate of the upper-left corner of the form, in pixels. The upper-left corner of the screen has coordinates (0,0), the horizontal axis is gradually added to the right, and the ordinate is gradually added. /* Create a form */glutcreatewindow ("Triangle");//Create a top-level form whose name is the number of references in the extension. /* Draw and display */background ();//Draw Background glutreshapefunc (myreshape);//Register the shape change callback function of the current form. When you change the size of the form, the shape of the form changes the callback function to be called. In this case, Myreshape specifies the shape change function. Glutdisplayfunc (Mydisplay);//Register the display callback function for the current form. When the image layer of a form needs to be drawn again, glut invokes the display callback function for that form. The Mydisplay in this example is to display a callback function that shows that the callback function does not take any parameters, it is responsible for drawing the entire image layer. Most of our work will be focused on this function. Glutmainloop ();//?glutmainloop enters the Glut event processing loop. The Glutmainloop function can only be called once in the glut program, and once it is called, it is no longer returned, and calls the registered callback function. So this function must be placed at the back of the register callback function, in this case Glutreshapefunc, Glutdisplayfunc. return (0);}

Now we have an understanding of the typical program structure of OpenGL programs. The first initialization includes the initialization of the glut library and the setting of the form and the display mode. Second, create the form. Third, the core part of his creation. IV, Glutmainloop enters the Glut event processing loop.

Glflush forces OpenGL functions to execute for a limited time. Glflush clears all buffer so that all commands can be executed within the specified time. must not forget this statement. Only by adding this sentence, the preceding command has the ability to execute.


Glbegin and Glend are one by one corresponding . These two functions limit the definition of a vertex for a group or multiple sets of elements. In the middle of these two functions is the entity you draw, composed of multiple vertices. The parameters of the function indicate the type of entity being drawn. The Gl_triangles in this example indicates that the drawing is a triangle.


Getting Started with OpenGL "1 QuickStart"

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.