A small summary of OpenGL partial knowledge

Source: Internet
Author: User

A small summary of OpenGL partial knowledge



Illumination:

Glenable (GL_LIGHT0);
Glenable (gl_lighting);

To create a light source:
GLLIGHTF (Glenum light, glenum pname, glfloat param);
PName Parameters:
gl_ambient--Ambient Light
gl_diffuse--scattered light intensity, LIGHT0 default is white, others are black
gl_specular--specular strength, LIGHT0 default is white, others are black
gl_position--Light source Position
gl_spot_direction--Spotlight Direction
gl_spot_exponent--Spotlight Index
gl_spot_cutoff--Spotlight Cutting Angle
gl_constant_attenuation--constant Attenuation factor
gl_linear_attenuation--linear attenuation factor
gl_quadratic_attenuation--two-time attenuation factor

Illumination Model:
Gllightmodel* () describes the parameters of the lighting model.
PName Parameters:
Gl_light_model_ambient: Rgba parameters for ambient light throughout the scene
Gl_light_model_local_viewer: How the specular angle is calculated, gl_false the observation point to an infinite distance
Gl_light_model_two_side: Single or double sided illumination
Gl_light_model_color_control: Whether the calculation of specular color is separated from the environment and the scattering color

Material properties:
Glmaterialf (Glenum face, glenum pname, glfloat param);
PName Parameters:
Gl_ambient: Ambient color of the material
Gl_diffuse: Scattering color of the material
Gl_ambient_and_diffuse
Gl_specular: Specular color of the material
Gl_shininess: Mirror Index [0.0, 128.0], the greater the value, the smaller the bright spot, the brighter
Gl_emission: Material emission color, eg: Analogue table lamp
Gl_color_indexes: Ambient, scatter, and specular color index

To reduce the cost of material properties, you can use Glcolormaterial (glenum face, glenum mode);
Face:gl_front,gl_back,gl_front_and_back. Mode:gl_ambient, Gl_diffuse, Gl_specular
Then use glcolor3f () to specify the color

If you want to change the value of a single material parameter, you can use Glcolormaterial () and if you want to change the values of multiple material parameters, you can use glmaterial* ().


Mix:

Glenable (Gl_blend);
Glblendfunc (Glenum sfactor, Glenum dfactor);
Mixing factor:
Gl_zero
Gl_one
Gl_src_color
Gl_one_minus_src_color
Gl_dst_color
Gl_one_minus_dst_color
Gl_src_alpha
Gl_one_minus_src_alpha
Gl_dst_alpha
Gl_one_minus_dst_alpha
Gl_constant_color
Gl_one_minus_constant_color
Gl_constant_alpha
Gl_one_minus_constant_alpha
Gl_src_alpha_saturate
If you use gl_*constant* blending, you need to specify a constant color using Glblendcolor (r,g,b,a)

anti-aliasing:

Glhint (gl_perspective_correction_hint,gl_nicest); The Best perspective corrections
Anti-aliasing for points and lines:
Glenable (Gl_point_smooth);
Glenable (Gl_line_smooth);
Glhint (Gl_line_smooth_hint, Gl_smooth);
Anti-aliasing in RGB mode requires the blending function Glblendfunc (gl_src_alpha,gl_one_minus_src_alpha) enabled;

Antialiasing of geometric elements using multiple uses:
Multi-sampling is particularly suitable for antialiasing the edges of polygons, because sorting is not required at this time, and if the polygon is antialiasing with alpha values,
The drawing order of translucent objects will affect the final color.
Add multiple sampling capabilities to your application:
1), get a window that supports multiple sampling: Glutinitdisplaymode (glut_double | Glut_rgb | Glut_multisample);;
2), after opening the window, you need to verify that the multi-sampling function is available, if the query state variable gl_sample_buffers returns a value of 1, and Gl_smaples
The returned value is greater than 1 and can be used with multiple sampling functions:
Glint Bufs, Smaples;
Glgetintegerv (Gl_sample_buffers, &BUFS);
Glgetintegerv (Gl_samples, &samples);
3), enable the multi-sampling function:
Glenable (gl_multisample);


Fog:
Glfogi (Gl_fog_mode, GL_EXP2); Setting the mode of fog
GLFOGFV (Gl_fog_color, Fogcolor); Set the color of the fog
GLFOGF (gl_fog_density, 0.35); Set the density of the fog
Glhint (Gl_fog_hint,gl_dont_care); Set how fog is rendered
GLFOGF (Gl_fog_start, 1.0); Set the start position of the fog
GLFOGF (Gl_fog_end, 5.0); Set the end position of the fog

Point parameter:
Sometimes we render relative to objects that look like circles or spheres, but do not want to use less efficient polygonal approximation simulations. For example, an aircraft approaching the runway
Street lights, the street lights become larger and brighter, or to simulate droplets. These phenomena can be simulated using particle systems.
The point parameter attenuates the size and brightness of the point, depending on the distance between the point and the viewer. Use glpointparameterf* () to specify the parameters and alpha of the attenuation equation.
Glpointparameterf (glenum pname, glfloat param);
Glpointparameterf (glenum pname, const type* param);
Pname:gl_point_distance_attenuation, param: (a,b,c) array-constant, linear, two-time attenuation coefficients
Gl_point_size_min or Gl_point_size_max, param represents the minimum or maximum value
...
In order to use point parameters to get dots instead of block points, you need to enable the point antialiasing feature:
Glenable (Gl_point_smooth);
Glenable (Gl_blend);
Glblendfunc (Gl_src_alpha, Gl_one_minus_src_alpha);


Polygon Offset:
If you want to highlight the edges of a solid object, you need to use a polygon offset
Enable polygon Offset:
Glenable (Gl_polygon_offset_fill); Gl_polygon_offset_line, Gl_polygon_offset_point
You also need to use Glpolygonmode () to set the current polygon Rasterization method
eg

#include <GL/glut.h> #include <stdio.h> #include <stdlib.h> #ifdef gl_version_1_1gluint list; Glint Spinx = 0; Glint spiny = 0; Glfloat tdist = 0.0; Glfloat Polyfactor = 1.0;    Glfloat polyunits = 1.0;void display (void) {glfloat gray[] = {0.8, 0.8, 0.8, 1.0};    Glfloat black[] = {0.0, 0.0, 0.0, 1.0}; Glclear (Gl_color_buffer_bit |    Gl_depth_buffer_bit);    Glpushmatrix ();    Gltranslatef (0.0, 0.0, tdist);    Glrotatef ((glfloat) Spinx, 1.0, 0.0, 0.0);    Glrotatef ((glfloat) Spiny, 0.0, 1.0, 0.0);    GLMATERIALFV (Gl_front, Gl_ambient_and_diffuse, Gray);    GLMATERIALFV (Gl_front, Gl_specular, Black);    Glmaterialf (Gl_front, gl_shininess, 0.0);    Glenable (gl_lighting);    Glenable (GL_LIGHT0);    Glenable (Gl_polygon_offset_fill);    (Polyfactor, polyunits);    Glcalllist (list);    Gldisable (Gl_polygon_offset_fill);    Gldisable (gl_lighting);    Gldisable (GL_LIGHT0);    GLCOLOR3F (1.0, 1.0, 1.0);    Glpolygonmode (Gl_front_and_back, gl_line);   Glcalllist (list); Glpolygonmode (Gl_front_and_back, Gl_fill);    Glpopmatrix (); Glflush ();}    void Gfxinit (void) {glfloat light_ambient[] = {0.0, 0.0, 0.0, 1.0};    Glfloat light_diffuse[] = {1.0, 1.0, 1.0, 1.0};    Glfloat light_specular[] = {1.0, 1.0, 1.0, 1.0};    Glfloat light_position[] = {1.0, 1.0, 1.0, 0.0};    Glfloat global_ambient[] = {0.2, 0.2, 0.2, 1.0};    Glclearcolor (0.0, 0.0, 0.0, 1.0);    List = glgenlists (1);       Glnewlist (list, gl_compile);    Glutsolidsphere (1.0, 20, 12);    Glendlist ();    Glenable (gl_depth_test);    GLLIGHTFV (Gl_light0, gl_ambient, light_ambient);    GLLIGHTFV (Gl_light0, Gl_diffuse, Light_diffuse);    GLLIGHTFV (Gl_light0, Gl_specular, light_specular);    GLLIGHTFV (Gl_light0, gl_position, light_position); GLLIGHTMODELFV (Gl_light_model_ambient, global_ambient);}    void reshape (int width, int height) {glviewport (0, 0, width, height);    Glmatrixmode (gl_projection);    Glloadidentity (); Gluperspective (45.0, (gldouble) width/(gldouble) HEight, 1.0, 10.0);    Glmatrixmode (Gl_modelview);    Glloadidentity (); Glulookat (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);} void Mouse (int button, int state, int x, int. y) {switch (button) {case Glut_left_button:switch (state) {case Glut_                     Down:spinx = (Spinx + 5)% 360;    Glutpostredisplay ();            Break;default:break;                     } break;case Glut_middle_button:switch (state) {Case Glut_down:spiny = (spiny + 5)% 360;    Glutpostredisplay ();            Break;default:break;    } break;case Glut_right_button:switch (state) {case Glut_up:exit (0);            Break;default:break;        } break;    Default:break;            }}void keyboard (unsigned char key, int x, int y) {switch (key) {case ' t ': if (Tdist < 4.0) {            Tdist = (tdist + 0.5);         Glutpostredisplay ();      } break;    Case ' T ': if (Tdist >-5.0) {        Tdist = (tdist-0.5);         Glutpostredisplay ();      } break; Case ' F ': polyfactor = Polyfactor + 0.1;         printf ("Polyfactor is%f\n", polyfactor);         Glutpostredisplay ();      Break Case ' f ': Polyfactor = polyfactor-0.1;         printf ("Polyfactor is%f\n", polyfactor);         Glutpostredisplay ();      Break Case ' U ': polyunits = polyunits + 1.0;         printf ("Polyunits is%f\n", polyunits);         Glutpostredisplay ();      Break Case ' u ': polyunits = polyunits-1.0;         printf ("Polyunits is%f\n", polyunits);         Glutpostredisplay ();      Break   Default:break;    }}int Main (int argc, char** argv) {glutinit (&AMP;ARGC, argv); Glutinitdisplaymode (Glut_single | Glut_rgb |    Glut_depth);    Glutcreatewindow (Argv[0]);    Glutreshapefunc (reshape);    Glutdisplayfunc (display);    Glutmousefunc (mouse);    Glutkeyboardfunc (keyboard);    Gfxinit ();    Glutmainloop (); return 0;} #elseintMain (int argc, char** argv) {fprintf (stderr, "This program demonstrates a feature which are not in OpenGL Version 1.0.\    n ");    fprintf (stderr, "If your implementation of OpenGL Version 1.0 have the right extensions,\n");    fprintf (stderr, "your may is able to modify this program to make it run.\n"); return 0;} #endif


Display list:
Glgenlists (Glsizei range); Produces several unused index values that are used as the first parameter of a glnewlist
Glnewlist (gluint list, glenum mode); Mode is generally used gl_compile, only compiled, not executed, opposite Gl_compile_and_execute
Glendlist ();
Glcalllist (gluint list); Call the Display list
When you create a display list, only the values of the expressions are stored in the display list, and the values in the display list do not change if the values in the array change.
Not all OpenGL functions can be stored in the display list and executed, such as functions that set state and fetch state values, cannot be stored in the display list when creating
When these functions are called when the list is displayed, they are executed immediately.
The display list can be nested.
To perform multiple display lists:
Multiple display lists can be executed consecutively, but the Display list index must be placed in an array and called glcalllists (), gllistbase (gluint base) to specify the initial offset,
It will be added to the Display list index in glcalllists (), and the display list base address has no effect on glcalllist () and Glnewlist ().
Glcalllists (glsizei N, glenum type, const glvoid *lists);



A small summary of OpenGL partial knowledge

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.