With a little understanding of OpenGL, we are now starting to actually write code.
Today's content:
Using Freeglut to create a context environment for OpenGL
Initialize Glew
Create an OpenGL template example
The first step:
An OpenGL context would allow us to pass the command to the layer of hardware, so we need a context. You first need to configure a feature form, create a file named chapter1.c, open it in your editor or an integrated development environment, insert code such as the following:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <GL/glew.h> #include <gl /freeglut.h> #define WINDOW_TITLE_PREFIX "Chapter 1" int currentwidth = N, currentheight = +, WindowHandle = 0; void Initialize (int, char*[]), void Initwindow (int, char*[]), void resizefunction (int, int), void renderfunction (void); int main (int argc, char* argv[]) {Initialize (argc, argv); Glutmainloop (); Exit (exit_success);} void Initialize (int argc, char* argv[]) {Initwindow (argc, argv); fprintf (stdout, "Info:opengl Version:%s\n", glgetstring (gl_version)); Glclearcolor (0.0f, 0.0f, 0.0f, 0.0f);} void Initwindow (int argc, char* argv[]) {glutinit (&ARGC, argv); Glutinitcontextversion (4, 0); Glutinitcontextflags (glut_forward_compatible); Glutinitcontextprofile (Glut_core_profile); Glutsetoption (Glut_action_on_window_close, glut_action_glutmainloop_returns); Glutinitwindowsize (Currentwidth, currentheight); GlutinitdisplayMode (glut_depth | glut_double | GLUT_RGBA); WindowHandle = Glutcreatewindow (Window_title_prefix); if (WindowHandle < 1) {fprintf (stderr, "error:could not create a new rendering window.\n"); Exit (Exit_failure); } glutreshapefunc (Resizefunction); Glutdisplayfunc (renderfunction);} void resizefunction (int Width, int Height) {currentwidth = width; Currentheight = Height; Glviewport (0, 0, currentwidth, currentheight);} void Renderfunction (void) {Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit); Glutswapbuffers (); Glutpostredisplay ();}
The compiled execution will be given for example the following form:
Explain:
Window_title_prefix Define form Name
Dimensions of the currentwidth, currentheight form
WindowHandle freeglut the form processing handle created
Initialization
Declare the prototype of the function and define the main application entry function, and call the Initwindow function in the Initialize function. Initwindow calls several Freeglut functions to create a form, starting with the Glutinit function, which is to initialize the Freeglut class library. The number of references is two of the parameters that are passed into the main, and you can not use any of the parameters.
Context type and form options:
Glutinitcontextversion
Glutinitcontextflags
Glutinitcontextfrofile
Glutsetoption its glut_action_on_window_close is to ensure that the Glutmainloop function clears it at the end of Main.
Glutinitwindowsize the size of the form to initialize
Display mode:
Glutinitdisplaymode defines the OpenGL context type, how the device should render the scene, and optionally:
Glut_depath starts depth buffer, also known as Z-buffer, which determines the rendering order on the pixel screen.
Glut_double start Double-buffer, that is, the front and rear end of the display cache, mainly to reduce the flicker of the picture
Glut_rgba defines the blending mode of the color, which is the RGBA blending mode
To create a form:
Glutcreatewindow creates our context and returns the newly created form processing handle. Its parameters window_title_prefix the name of the form
Function callback:
Glutreshapefunction Processing Form Dimensions
Glutdisplayfunction Rendering Scenes
Debug output:
Glgetstring get the version number of OpenGL
fprintf c function, printing characters
For example, the following:
Screen Refresh color:
Glclearcolor its parameters corresponding to the Freeglut Glut_rgba setting mode, the value range 0~1.0f corresponding 1~100% color intensity.
The following is an intuitive mix of Rgba:
Examples:
glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
For red
glClearColor(0.0f, 1.0f, 0.0f, 0.0f);
For Green
glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
For Blue
glClearColor(1.0f, 0.0f, 1.0f, 0.0f);
For purple
glClearColor(0.0f, 0.0f, 0.5f, 0.0f);
For dark blue
the final number of references is transparency.
main loop:Glutmainloop just want the form to be active, it will run this function continuously, knowing that the form exits. Reset Render Size:resizefunction its currentwith and currentheight form dimensions Glviewpoirt the size of the x-coordinate,y-coordinate,width,height that is rendered
Rendering:Renderfunction drawing ObjectsGlclear is the color that is populated in the first back-end cache, after which other objects are drawnglutswapbuffer Exchange front-end display cache data
after calling play Glutswapbuffers:
Join Glew:
1 of OpenGL micro-development-start from scratch