OpenGL Red Book Learning notes (1)

Source: Internet
Author: User

The main actions that OpenGL performs when rendering images in a scene:

1. Create a shape from a geometry to establish a mathematical description of the object (OpenGL puts points, lines, polygons, and bitmaps as basic primitives)

2. Arrange the objects in three-dimensional space and choose to observe the perspective in the composite scene

3. Calculate the color of all objects. Colors can be explicitly specified by the application, can be determined according to specific lighting conditions, or can be obtained by pasting the texture onto the surface of the object, or a mixture of the above three operations, which may be performed using shaders, which explicitly control all color calculations, Or it may be possible to use OpenGL's pre-programmed algorithm to execute within it (I'm using the term fixed-line pipeline to represent the latter)

4. The process of translating the mathematical description of an object and the color information associated with an object into pixels on the screen is called Rasterization.

During these phases, OpenGL may also perform other actions, such as removing objects that are obscured by other objects (or part of the object). In addition, after the scene is rasterized but before the screen is drawn, you can still perform some actions on the pixel data as needed.

Here is a simple OpenGL program that displays a white square in a window with a black background.

#include <gl\glut.h>void display () {glcolor3f (1.0, 1.0, 1.0);//Set Clear color Glbegin (Gl_polygon);//Specify Drawing mode 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 () ;//enforce OpenGL code}void init () {glmatrixmode (gl_projection);//Set Projection mode glloadidentity (); Glortho (0.0, 1.0, 0.0, 1.0,-1.0, 1.0)///Set Display range}int main (int argc, char** argv) {glutinit (&ARGC, argv);//Initialize Glutglutinitdisplaymode (Glut_rgb | Glut_single)///Set display mode for the color model using RGB, cache mode using single cache glutinitwindowposition (100, 100);//Set the position of the display window Glutinitwindowsize (250, 250 )///Set the size of the display window Glutcreatewindow ("A Simple OpenGL Program");//Create window, specify window caption init (); Glutdisplayfunc (display);// Register Display callback function Glutmainloop ();//Start program, enter message loop return 0;}

Because OpenGL is designed to be a streamlined, hardware-independent interface that is implemented on many different hardware platforms, OpenGL also includes functions for performing window tasks or getting user input, in order to achieve this goal. So use the GLUT toolkit.

The second procedure is to display a rotated rectangle by double buffering

#include <gl\glut.h>StaticGlfloat spin =0.0;voidinit () {Glclearcolor (0.0,0.0,0.0,0.0); Glshademodel (Gl_flat);}voiddisplay () {glclear (gl_color_buffer_bit);    Glpushmatrix (); GLROTATEF (Spin,0.0,0.0,1.0);//RotateGLCOLOR3F (1.0,1.0,1.0); GLRECTF (-25.0, -25.0,25.0,25.0);    Glpopmatrix (); Glutswapbuffers ();//Cache Swapping}voidSpindisplay () {Spin+=2.0; if(Spin >360.0) Spin-=360.0; Glutpostredisplay ();}voidReshape (intWinth) {Glviewport (0,0, (Glsizei) W, (Glsizei) h);    Glmatrixmode (gl_projection);    Glloadidentity (); Glortho (-50.0,50.0, -50.0,50.0, -1.0,1.0);    Glmatrixmode (Gl_modelview); Glloadidentity ();}voidMouseintbuttonintStateintXinty) {    Switch(Button) { CaseGlut_left_button:if(state = =glut_down) Glutidlefunc (Spindisplay);  Break;  CaseGlut_middle_button:if(state = =glut_down) Glutidlefunc (nullptr);  Break; default:         Break; }}intMainintargcChar**argv) {Glutinit (&argc, argv); Glutinitdisplaymode (Glut_rgb|glut_double); Glutinitwindowposition ( -, -); Glutinitwindowsize ( -, -); Glutcreatewindow ("Rotate Rectangle");    Init ();    Glutdisplayfunc (display);    Glutreshapefunc (reshape);    Glutmousefunc (mouse);        Glutmainloop (); return 0;}

Among them :

void Glrotatef ( glfloat Span style= "Background:none repeat scroll 0% 0% white; Color:black; " > angle, glfloat X, glfloat Y, glfloat Z); set rotation direction

from coordinates (0,0,0) that is, the origin point, leads to a line to (x, Y, z) , hold the line with your right hand, and the other four fingers bend in the direction of the object's rotation.

OpenGL Red Book Learning notes (1)

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.