Using OpenGL to realize three-dimensional drawing under VC 2005

Source: Internet
Author: User
Tags definition final range reflection requires
In the process of the development of three-dimensional drawing, computer company has launched a large number of three-dimensional drawing software packages. One of the SGI companies launched OpenGL, as a superior performance of the graphical Application Design Interface (API), has made great achievements. It is a high-performance interactive three-dimensional graphics modeling capabilities and easy programming development, has been Microsoft, IBM, DEC, Sun, HP and other large companies recognized. Therefore, OpenGL has become a three-dimensional graphic development standards, is engaged in three-dimensional graphic development work of the necessary tools.

1. Initialize OpenGL drawing Environment

1.1 Defining color formats and buffering modes

OpenGL offers two color modes: RGB (RGBA) mode and color indexing mode (palette). The definition of all colors in RGBA mode is represented by RGB three values, sometimes with alpha values (for transparency). The range of RGB three component values is between 0 and 1, and their proportions in the final color are proportional to their values. For example: (1, 1, 0) indicates yellow, (0, 0, 1) indicates blue. The color of each pixel in the color index mode is represented by a color index value in the Color Index table (similar to selecting a color from the palette). Because the three-dimensional graphics processing requires a flexible color, and in the shadow, lighting, atomization, fusion and other effects of processing rgba better than the color index mode, so, in programming most of the RGBA mode.

OpenGL provides a dual cache to draw images. That is, when the image in the foreground cache is displayed, the background cache draws the second image. When the background is finished, the image in the background cache is displayed, and the original foreground cache begins to draw a third image, so it repeats itself to increase the output speed of the image.

To set the window display mode function:

void Auxinitdisplaymode (
aux_double | Double cache mode
Aux_rgba//RGBA color mode
);

1.2 Setting the light source

OpenGL's light source is generally divided into three kinds: ambient light (ambient light), that is, from the surrounding environment has no fixed direction of light. The diffuse light (diffuse light) comes from the same direction and radiates uniformly across the surface of the object in all directions. The specular light (specular light) comes from the same direction and is reflected in the same direction. The global environment is a special ambient light, which does not come from a certain light source, usually as a scene of natural daylight.

Specify the Light function:

void Gllightfv (
Glenum light,//light source number
Glenum pname,//indicates the type of light source:
Gl_diffuse light source for diffuse light source
Gl_ambient light source for ambient light
Gl_specular light source for specular light
Const glfloat* Params//pointer to color vector
);

To set the global ambient light function:

void Gllightmodelfv (
Gl_light_model_ Ambient,
Const glfloat* param//param: pointers to color vectors
);

To have a light function:

void Glenable (gl_lighting);
void glenable (gl_enum cap); Cap: Indicates the source number

1.3 Set Material

In OpenGL, the color of the material is defined by the reflectivity of the three primary colors of light (red, green, and blue). Corresponding to the light source, the color of the material, also divided into ambient color, diffuse reflection and specular reflection color, which determines that the material corresponds to different light showing different reflectivity. The color of the object is the color of the light emitted by the light that is reflected into the eye by the object. Therefore, the color of the object is the ambient light of the light source, diffuse reflection light and specular reflection light and material environment color, diffuse reflection color and specular reflection color synthesis. For example: OpenGL light source Color (LR, LG, LB), Material color (MR, MG, MB), then, in the case of ignoring other reflective effects, the final color of the eye is (LR*MR, LG*MG, LB*MB).

Material definition function:

void Glmaterialfv (
Glenum face,//indicates the color of the surface on which the material is set.
It could be Gl_front, Gl_back, Gl_front_and_back.
Glenum pname,//similar to the pname parameters of the light source
Const float* params//color vector pointing to material
);

1.4 Defining Projection Mode

It is also the choice to observe the angle and range of the object. Because we are three-dimensional drawing, so using different viewpoints and observation range, it will produce different observation results. Because the computer can only display two-dimensional graphics, so in the real world to represent three-dimensional graphics, it is necessary to transform three-dimensional visual scene into two-dimensional view. This is the key to producing three-dimensional effects. OpenGL provides two ways to convert a 3D graphic into a 2D graphic. Positive projections (Orthographic projection) and perspective projections (perspective projection). Among them, the projection refers to the size of the object and the point of view is independent of the distance, is usually used in CAD design, and perspective projection is in line with human psychological habits, near the object is large, far from the point of view objects small. In addition, the projection range is defined in OpenGL, and only objects within that range are projected onto the computer screen, and objects outside the projection range are cut off.

Define the projection range (different projection modes correspond to different functions):

void Glortho (
Gldouble left, gldouble right,///(Left,bottom,near) and (Right,top,far) give forward injection respectively
Gldouble Bottom, gldouble top,//shadow projection range of the lower left and upper right corner coordinates.
Gldouble near,gldouble FAR);

2. Define system functions with Windows interface

2.1 Define the position of the drawing window

(x,y) gives the upper-left corner coordinate of the window
Width and heigh give the height of the window
void Auxinitposition (Glint x,glint y,glsizei width, Glsizei heigh);

2.2 Defining the title of the drawing window

str denotes window caption string
void Auxinitwindow (glbyte* STR);

2.3 Defining the Window Refresh function when the drawing window changes

When the window changes shape Si cho The specified callback function
Name indicates callback function names
void Auxreshapefunc (NAME);

2.4 To define idle state functions of the idle state to achieve animation

Call the specified callback function when the system is idle
Name indicates callback function names
void Auxidlefunc (NAME);

2.5 Defining scene drawing functions (called when window updates or scene changes)

Called when the window needs to be updated or the scene changes
Name indicates callback function names
void Auxmainloop (NAME);

3, under the VC implementation of the program compiled

After you type the following code under the VC editor, Save as a C + + file with a. cpp suffix. Start compiling, in "The build command requires an active project Workspace". "Would to create a default project Workspace"? When prompted, select Yes (Y). Go to the Project menu, select "Setting", pop the "Project Setting" dialog box, select "Link", and add the function library provided by OpenGL in the "Libaray" column: "Opengl32.lib glu32.lib Glaux.lib ". (Note: When executing a program, the Windows system directory contains Opengl32.dll and glu32.dll two dynamic connection libraries). Attached source code:

#include "Windows.h"
#include "gl/gl.h"
#include "gl/glaux.h"
#include "Gl/glu.h"
#include "math.h"
void Myinit ()
{
Glclearcolor (1,1,0,0);
Glfloat ambient[]={.5,.5,.5,0};
GLLIGHTMODELFV (gl_light_model_ambient, ambient);
Glfloat mat_ambient[]={.8,.8,.8,1.0};
Glfloat mat_diffuse[]={.8,.0,.8,1.0};
Glfloat mat_specular[]={1.0,.0,1.0,1.0};
Glfloat mat_shininess[]={50.0};
Glfloat light_diffuse[]={0,0,.5,1};
Glfloat light_position[]={0,0,1.0,0};
GLMATERIALFV (gl_front_and_back,gl_ambient,mat_ambient);
GLMATERIALFV (Gl_front_and_back,gl_diffuse,mat_diffuse);
GLMATERIALFV (Gl_front_and_back,gl_specular,mat_specular);
GLMATERIALFV (gl_front_and_back,gl_shininess,mat_shininess);
GLLIGHTFV (Gl_light0, Gl_diffuse, Light_diffuse);
GLLIGHTFV (Gl_light0,gl_position, light_position);
Glenable (gl_lighting);
Glenable (GL_LIGHT0);
Gldepthfunc (gl_less);
Glenable (gl_depth_test);
}

void CALLBACK display ()
{
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
Auxsolidsphere (1.0); Draw a solid ball with a radius of 1.0
Glflush (); Force output image
Auxswapbuffers (); Swap drawing Cache
Sleep (100);
}

void CALLBACK Idledisplay ()
{
X,y meet x2+y2=0.01. This allows the object to move along the circular trajectory.
static float x=-.1,y=0.0;
Static BOOL mark=true;
static float step=.01;
X+=step;
if (x<=.1&&x>=-.1)
{
if (step>0)
Y=sqrt (. 01-x*x);
Else
Y=-sqrt (. 01-x*x);
Gltranslatef (x,y,0);
}
Else
{
Step=0-step;
}
Display ();
}

void CALLBACK Myreshape (glsizei w,glsizei h)
{
Glviewport (0,0,W,H);
Glmatrixmode (gl_projection);
Glloadidentity ();
if (w<=h)
Glortho ( -3.5,3.5,-3.5* (glfloat) w/(Glfloat) H, 3.5* (Glfloat) w/(glfloat) h,-10,10);
Else
Glortho ( -3.5* (glfloat) w/(glfloat) h,3.5* glfloat (w/) glfloat);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
}

void Main ()
{
Auxinitdisplaymode (aux_double| AUX_RGBA);
Auxinitposition (0,0,400,400);
Auxinitwindow ("Circle");
Myinit ();
Auxreshapefunc (Myreshape);
Auxidlefunc (Idledisplay);
Auxmainloop (display);
}

Compiling under Visual C + + 2005

CL A.cpp/linkopengl32.lib glu32.lib glaux.lib user32.lib gdi32.lib kernel32.lib

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.