CN102542612A - Method for setting light source parameters and calculating light color based on OpenGL core mode - Google Patents
Method for setting light source parameters and calculating light color based on OpenGL core mode Download PDFInfo
- Publication number
- CN102542612A CN102542612A CN2010106076449A CN201010607644A CN102542612A CN 102542612 A CN102542612 A CN 102542612A CN 2010106076449 A CN2010106076449 A CN 2010106076449A CN 201010607644 A CN201010607644 A CN 201010607644A CN 102542612 A CN102542612 A CN 102542612A
- Authority
- CN
- China
- Prior art keywords
- light
- light source
- vector
- angle
- attenuation coefficient
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
Images
Landscapes
- Image Generation (AREA)
Abstract
The invention relates to a video image processing technology, in particular to a method for setting light source parameters and calculating light color based on an OpenGL core mode. In the method, relevant parameters of three common light sources including a point light source, a parallel light and a spotlight in the OpenGL core mode are set by aiming at the three common light sources, so that no conditional branch instruction execution condition is caused when the light color of a pixel is dynamically rendered according to the type of each light, and furthermore, the rendering speed in a complex three-dimensional scene with a plurality of lights is greatly increased.
Description
Technical field
The present invention relates to video image processing technology, be specifically related to a kind of method of setting and calculate the illumination color based on the light source parameters of OpenGL core schema.
Background technology
In video later stage special effect processing and composite software,, often all a kind of means that image is synthesized are provided in three dimensions, generally are referred to as three-dimensional compositor to the user in order to reach image synthetic effect true to nature.The user can add the three-dimensional body of any amount in three-dimensional compositor; Such as the plane; Cube, spheroid, three-dimensional body that imports from the three-dimensional model file in addition or the like; Add material properties for these three-dimensional bodies, illumination and can synthesize three-dimensional scene true to nature by the texture of the image construction among the special efficacy figure.Wherein three dimensions is provided with light so that making object lighting effect occur is user's a kind of mode of playing up the most commonly used, in order to reach effect very true to nature, the user tends in three-dimensional scenic, place tens light or even dozens of light.
OpenGL (Open Graphics Library) is a professional graphic package interface at present commonly used, be one powerful, call bottom shape library easily.Directly light is supported among traditional API of OpenGL that can specify the quantity of light and the type and the concrete parameter of each light through API, finally these light parameters pass to renderer shader with interior form of building variable.But this traditional API can only specify 8 light at most, and this can't satisfy the demand of high-quality composite software to light quantity far away; Core profile from OpenGL 3.2 beginnings has begun not support a lot of traditional OpenGL technology in addition; This wherein just comprises built-in lighting mechanism, if therefore use OpenGL 3.2 coreprofile just must set the light parameter to shader through the mode of uniform.But this just a problem occurred: because the quantity of light is dynamic; And the kind of each light all possibly be different; Need among the uniform of light parameter to give each light specified type so in general set, renderer shader will come dynamically to select the different rendering method according to the type of each light, yet for the hardware of video card; Execution with instruction sequence of conditional branching is slow; Therefore this light parameter transfer mode can reduce processing speed, and in order to realize rendering effect more true to nature, and video later stage special effect processing and composite software generally can use and carry out illumination by the mode of pixel and play up; The processing that is to say each pixel all can face this problem, has therefore just aggravated the decline of speed.
Summary of the invention
The objective of the invention is to the problems referred to above, provide a kind of and set and calculate the method for illumination color, carrying out the efficient of light when playing up to improve renderer based on the light source parameters of OpenGL core schema.
Technical scheme of the present invention is following: a kind of light source parameters establishing method based on the OpenGL core schema comprises the steps:
(1) surround lighting of input light source, diffuse and the color of specular light;
(2) judge whether light source is directional light, if the direction vector of light source is set; If the location parameter of light source, then is not set;
(3) for directional light, it is 0 that the light source direction mixing constant is set; For non-parallel light, it is 1 that the light source direction mixing constant is set;
(4), be provided with according to adjust the distance constant range attenuation coefficient, linear range attenuation coefficient, the secondary range attenuation coefficient of decay factor of the value of user's appointment for non-parallel light; For directional light, above-mentioned three coefficients are made as 1,0,0 respectively;
(5) judge whether light source is spotlight, if calculate the cosine function value of the focus angle of angle decay factor, the cosine function value of decay angle; If, then above-mentioned two values are not made as-2 ,-3 respectively.
Further, aforesaid light source parameters establishing method based on the OpenGL core schema, in the step (2), the direction vector of described light source just is meant the contrary direction of directional light; The location parameter of described light source is exactly the position coordinates of giving directions light source or spotlight light source.
A kind of method based on OpenGL core schema calculating illumination color comprises the steps:
(a) locus according to current pixel calculates the vector that is constituted with light source position, is designated as the light vector; The mould that calculates this vector then is the represented point of current pixel and the distance of light source;
(b) color and the light vector according to light source utilizes normalized form to calculate the diffuse reflection color and the direct reflection color of this pixel, and it is designated as the illumination color with value;
(c) according to following formula computed range decay factor distance_attenuation,
distance_attenuation=1/(constantAttenuation+linearAttenuation×lightDistance+quadraticAttenuation×lightDistance×lightDistance)
Wherein, ConstantAttenuation is a constant range attenuation coefficient, and linearAttenuation is the linear range attenuation coefficient, and quadraticAttenuation is the secondary range attenuation coefficient; LightDistance is the represented point of current pixel and the distance of light source
For directional light, constant range attenuation coefficient, linear range attenuation coefficient, secondary range attenuation coefficient are made as 1,0,0 respectively;
(d) calculate angle decay factor angle_attenuation according to following formula,
angle_attenuation=smoothstep(cosPhi,cosTheta,dot(lightVector,lightDir))
Wherein, cosPhi is the cosine function value of decay angle, and cosTheta is the cosine function value of focus angle; LightVector is the light vector; LightDir is the direction vector of directional light, and function smoothstep, dot are the built-in functions among the GLSL (OpenGL Shader Language)
For pointolite and directional light, respectively the cosine function value of focus angle and the cosine function value of decay angle are made as-2 ,-3;
(e) the illumination color that obtains in the step (b) multiply by two decay factors that obtain in step (c) and the step (d) and can draw final illumination color.
Further, aforesaid method based on OpenGL core schema calculating illumination color in step (a), also comprises the operation of the light vector being revised through following formula,
lightVector=mix(lightDir,Normalize(lightVector),lightDirMixFactor)
Wherein, lightVector is the light vector, and lightDir is the direction vector of directional light, and lightDirMixFactor light source direction mixing constant, function Normalize are meant pairing vector of unit length of vector of calculating,
For directional light, the light source direction mixing constant is 0, and the light source direction mixing constant of other light is 1.
Beneficial effect of the present invention is following: the present invention is directed to the OpenGL core schema and proposed a kind of parameter setting of light efficiently mechanism and used the rendering intent of the machine-processed pixel light of being carried out of this light parameter setting according to color; This mechanism can so that fragment shader in the situation of carrying out avoiding when light is played up the Conditions branch instruction to carry out, thereby significantly improved the speed of playing up in the complex three-dimensional scene with a lot of light.
Description of drawings
Fig. 1 is the process flow diagram that is provided with of light source parameters of the present invention;
Fig. 2 calculates the method flow diagram of illumination color for the present invention.
Embodiment
Describe the present invention below in conjunction with embodiment and accompanying drawing.
To present special video effect process software, like OpenGL 3.2 Core Profile, in general, light is divided into three types: pointolite (point light), directional light (directional light) and spotlight (spot light).
Pointolite: the parameter that need set has the position of light source, the color of range attenuation coefficient of light intensity (attenuation coefficient is divided into three, is respectively secondary, linearity and constant attenuation coefficient) and light source.
Directional light: the parameter that need set has the color of the direction vector and the light source of light source.
Spotlight: the parameter that need set has the position of light source, the direction of light source, focus angle (hot spotangle); Decay angle (cutoff angle); (attenuation coefficient is divided into three to the range attenuation coefficient of light intensity, is respectively second order, single order and constant attenuation coefficient; Second order, single order attenuation coefficient can corresponding secondaries, linear attenuation coefficient) and the color of light source.
This shows that the parameter of different light sources differs greatly.
As shown in Figure 1, the invention provides a kind of light source parameters establishing method based on the OpenGL core schema, comprise the steps:
(1) surround lighting of input light source, diffuse and the color of specular light;
(2) judge whether light source is directional light, if the direction vector of light source is set; If the location parameter of light source, then is not set; The direction vector of described light source just is meant the contrary direction of directional light; The location parameter of described light source is exactly the position coordinates of giving directions light source or spotlight light source;
(3) for directional light, it is 0 that the light source direction mixing constant is set; For non-parallel light, it is 1 that the light source direction mixing constant is set;
(4), be provided with according to adjust the distance constant range attenuation coefficient, linear range attenuation coefficient, the secondary range attenuation coefficient of decay factor of the value of user's appointment for non-parallel light; For directional light, above-mentioned three coefficients are made as 1,0,0 respectively;
(5) judge whether light source is spotlight, if calculate the cosine function value of the focus angle of angle decay factor, the cosine function value of decay angle; If, then above-mentioned two values are not made as-2 ,-3 respectively.
The concrete software configuration of said method is as follows:
struct?SLightSource{
vec4?ambient;
vec4?diffuse;
vec4?specular;
vec3?position;//light?source?position?in?eye?coordinates
vec3?lightDir;//unit?vector?for?light?direction(toward?light),in?eye?coordinates
float?lightDirMixFactor;//0?for?directional?light;1?for?point?or?spot?light
float?cosTheta;//cosTheta?must?be?greater?than?cosPhi
float?cosPhi;
float?constantAttenuation;
float?linearAttenuation;
float?quadraticAttenuation;
};
Top structure the inside ambient, three members of diffuse and specular are surround lighting, the color of diffuse reflection and specular light, they have constituted the color of light source jointly.
Position is the position of light source, is not need this for directional light.
LightDir is the direction vector (towards the vector of light) of light, has only directional light to need this parameter.
LightDirMixFactor is a light direction mixing constant, is 0 for this coefficient of directional light, and other light is 1.
CosTheta just is set at the cosine function value of focus angle for spotlight, is set at-2 for other light.
CosPhi for spotlight just be set at the decay angle the cosine function value, be set at-3 for other light.
ConstantAttenuation is exactly a constant range attenuation coefficient, is set at 1 for directional light, and remaining is set at the specified as required value of user.
LinearAttenuation is exactly the linear range attenuation coefficient, is set at 0 for directional light, and remaining is set at the specified as required value of user.
QuadraticAttenuation is exactly the secondary range attenuation coefficient, is set at 0 for directional light, and remaining is set at the specified as required value of user.
Giving in the uniform of fragment shade renderer r according to said method after each light set parameter, just can utilize method as shown in Figure 2 to calculate the color of each light uniformly in certain pixel reflects, concrete calculating comprises the steps:
(a) locus according to current pixel calculates the vector that is constituted with light source position, is designated as the light vector; The mould that calculates this vector then is the represented point of current pixel and the distance of light source;
The operation of the light vector being revised through following formula,
lightVector=mix(lightDir,Normalize(lightVector),lightDirMixFactor)
Wherein, LightVector is the light vector; LightDir is the direction vector of directional light; LightDirMixFactor light source direction mixing constant, function Normalize are the built-in functions among the GLSL (OpenGL ShaderLanguage), are meant to calculate a pairing vector of unit length of vector (can with reference to the OpenGL handbook).
For directional light, the light source direction mixing constant is 0, and the light source direction mixing constant of other light is 1.
(b) color and the light vector according to light source utilizes normalized form to calculate the diffuse reflection color and the direct reflection color of this pixel, and it is designated as the illumination color with value.
(c) according to following formula computed range decay factor distance_attenuation,
distance_attenuation=1/(constantAttenuation+linearAttenuation×lightDistance+quadraticAttenuation×lightDistance×lightDistance)
Wherein, ConstantAttenuation is a constant range attenuation coefficient, and linearAttenuation is the linear range attenuation coefficient, and quadraticAttenuation is the secondary range attenuation coefficient; LightDistance is the represented point of current pixel and the distance of light source
For directional light, constant range attenuation coefficient, linear range attenuation coefficient, secondary range attenuation coefficient are made as 1,0,0 respectively.
(d) calculate angle decay factor angle_attenuation according to following formula,
angle_attenuation=smoothstep(cosPhi,cosTheta,dot(lightVector,lightDir))
Wherein, cosPhi is the cosine function value of decay angle, and cosTheta is the cosine function value of focus angle, and lightVector is the light vector, and lightDir is the direction vector of directional light.
Function smoothstep, dot are the built-in functions among the GLSL (OpenGL Shader Language), can be with reference to the OpenGL handbook.
(
http://www.opengl.org/registry/doc/GLSLangSpec.1.50.11.pdf)
For pointolite and directional light, respectively the cosine function value of focus angle and the cosine function value of decay angle are made as-2 ,-3.
(e) the illumination color lightColor that obtains in the step (b) multiply by two the decay factor distance_attenuation, the angle_attenuation that obtain in step (c) and the step (d) and can draw final illumination color lightColor_end, formula is following:
lightColor_end=lightColor*distance_attenuation*angle_attenuation。
Can find out through above computation process; In the top illumination calculation step is to have no conditional branching; That is to say and to calculate the illumination color of variety classes light source with unified method, thereby can significantly improve the speed of playing up in the playing up of complex three-dimensional scene with a lot of light.
Obviously, those skilled in the art can carry out various changes and modification to the present invention and not break away from the spirit and scope of the present invention.Like this, belong within the scope of claim of the present invention and equivalent technology thereof if of the present invention these are revised with modification, then the present invention also is intended to comprise these changes and modification interior.
Claims (4)
1. the light source parameters establishing method based on the OpenGL core schema comprises the steps:
(1) surround lighting of input light source, diffuse and the color of specular light;
(2) judge whether light source is directional light, if the direction vector of light source is set; If the location parameter of light source, then is not set;
(3) for directional light, it is 0 that the light source direction mixing constant is set; For non-parallel light, it is 1 that the light source direction mixing constant is set;
(4), be provided with according to adjust the distance constant range attenuation coefficient, linear range attenuation coefficient, the secondary range attenuation coefficient of decay factor of the value of user's appointment for non-parallel light; For directional light, above-mentioned three coefficients are made as 1,0,0 respectively;
(5) judge whether light source is spotlight, if calculate the cosine function value of the focus angle of angle decay factor, the cosine function value of decay angle; If, then above-mentioned two values are not made as-2 ,-3 respectively.
2. the light source parameters establishing method based on the OpenGL core schema as claimed in claim 1 is characterized in that: in the step (2), the direction vector of described light source just is meant the contrary direction of directional light; The location parameter of described light source is exactly the position coordinates of giving directions light source or spotlight light source.
3. the method based on OpenGL core schema calculating illumination color comprises the steps:
(a) locus according to current pixel calculates the vector that is constituted with light source position, is designated as the light vector; The mould that calculates this vector then is the represented point of current pixel and the distance of light source;
(b) color and the light vector according to light source utilizes normalized form to calculate the diffuse reflection color and the direct reflection color of this pixel, and it is designated as the illumination color with value;
(c) according to following formula computed range decay factor distance_attenuation,
distance_attenuation=1/(constantAttenuation+linearAttenuation×lightDistance+quadraticAttenuation×lightDistance×lightDistance)
Wherein, ConstantAttenuation is a constant range attenuation coefficient, and linearAttenuation is the linear range attenuation coefficient, and quadraticAttenuation is the secondary range attenuation coefficient; LightDistance is the represented point of current pixel and the distance of light source
For directional light, constant range attenuation coefficient, linear range attenuation coefficient, secondary range attenuation coefficient are made as 1,0,0 respectively;
(d) calculate angle decay factor angle_attenuation according to following formula,
angle_attenuation=smoothstep(cosPhi,cosTheta,dot(lightVector,lightDir))
Wherein, cosPhi is the cosine function value of decay angle, and cosTheta is the cosine function value of focus angle; LightVector is the light vector; LightDir is the direction vector of directional light, and function smoothstep, dot are the built-in functions among the OpenGL Shader Language
For pointolite and directional light, respectively the cosine function value of focus angle and the cosine function value of decay angle are made as-2 ,-3;
(e) the illumination color that obtains in the step (b) multiply by two decay factors that obtain in step (c) and the step (d) and can draw final illumination color.
4. the method based on OpenGL core schema calculating illumination color as claimed in claim 3 is characterized in that: in step (a), also comprise the operation of the light vector being revised through following formula,
lightVector=mix(lightDir,Normalize(lightVector),lightDirMixFactor)
Wherein, lightVector is the light vector, and lightDir is the direction vector of directional light, and lightDirMixFactor light source direction mixing constant, function Normalize are meant pairing vector of unit length of vector of calculating,
For directional light, the light source direction mixing constant is 0, and the light source direction mixing constant of other light is 1.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN2010106076449A CN102542612A (en) | 2010-12-27 | 2010-12-27 | Method for setting light source parameters and calculating light color based on OpenGL core mode |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN2010106076449A CN102542612A (en) | 2010-12-27 | 2010-12-27 | Method for setting light source parameters and calculating light color based on OpenGL core mode |
Publications (1)
Publication Number | Publication Date |
---|---|
CN102542612A true CN102542612A (en) | 2012-07-04 |
Family
ID=46349422
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN2010106076449A Pending CN102542612A (en) | 2010-12-27 | 2010-12-27 | Method for setting light source parameters and calculating light color based on OpenGL core mode |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN102542612A (en) |
Cited By (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106840389A (en) * | 2016-12-30 | 2017-06-13 | 歌尔科技有限公司 | Light source estimating and measuring method and device, intelligent electronic device based on multiple balls |
CN107197171A (en) * | 2017-06-22 | 2017-09-22 | 西南大学 | A kind of digital photographing processing method for adding intelligence software light source |
CN110648372A (en) * | 2019-03-29 | 2020-01-03 | 完美世界(北京)软件科技发展有限公司 | Method and system for determining color of pixel |
CN114581591A (en) * | 2021-12-13 | 2022-06-03 | 北京市建筑设计研究院有限公司 | Three-dimensional scene light configuration method and device, electronic equipment and storage medium |
-
2010
- 2010-12-27 CN CN2010106076449A patent/CN102542612A/en active Pending
Cited By (5)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
CN106840389A (en) * | 2016-12-30 | 2017-06-13 | 歌尔科技有限公司 | Light source estimating and measuring method and device, intelligent electronic device based on multiple balls |
CN107197171A (en) * | 2017-06-22 | 2017-09-22 | 西南大学 | A kind of digital photographing processing method for adding intelligence software light source |
CN110648372A (en) * | 2019-03-29 | 2020-01-03 | 完美世界(北京)软件科技发展有限公司 | Method and system for determining color of pixel |
CN110648372B (en) * | 2019-03-29 | 2022-04-22 | 完美世界(北京)软件科技发展有限公司 | Method and system for determining color of pixel |
CN114581591A (en) * | 2021-12-13 | 2022-06-03 | 北京市建筑设计研究院有限公司 | Three-dimensional scene light configuration method and device, electronic equipment and storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
KR102132040B1 (en) | Patched shading in graphics processing | |
KR101653157B1 (en) | Mapping multi-rate shading to monolithic programs | |
US9508185B2 (en) | Texturing in graphics hardware | |
JP5166535B2 (en) | Fragment shader bypass in graphics processing unit, apparatus and method thereof | |
US8854384B2 (en) | Method and system for processing pixels utilizing scoreboarding | |
US9202308B2 (en) | Methods of and apparatus for assigning vertex and fragment shading operations to a multi-threaded multi-format blending device | |
CN104077802B (en) | The method that enhancing virtual scene real-time simulation image is shown | |
KR20160001710A (en) | Method and apparatus for computing precision | |
CN109364481B (en) | Method, device, medium and electronic equipment for real-time global illumination in game | |
TWI725024B (en) | Apparatus, method, and non-transistory machine-readable medium for facilitating efficient graphics command generation and execution | |
CN102542612A (en) | Method for setting light source parameters and calculating light color based on OpenGL core mode | |
CN111406277B (en) | Low resolution depth storage based on image block | |
CN104392479A (en) | Method of carrying out illumination coloring on pixel by using light index number | |
CN102074041B (en) | Method for drawing planar caustic effect of 3D virtual scene produced by specular reflection | |
CN102819855A (en) | Method and device for generating two-dimensional images | |
WO2022166656A1 (en) | Method and apparatus for generating lighting image, device, and medium | |
CN103310488A (en) | mental ray rendering-based virtual reality rendering method | |
JP2007272847A (en) | Lighting simulation method and image composition method | |
US11880924B2 (en) | Synchronization free cross pass binning through subpass interleaving | |
Iwasaki et al. | Precomputed radiance transfer for dynamic scenes with diffuse interreflection | |
TW202324290A (en) | Constant data storage | |
Persson | Making It Large, Beautiful, Fast and Consistent: Lessons Learned Developing Just Cause 2 | |
Riguer | The Radeon X1000 Series Programming Guide | |
Rege | Shader Model 3.0 | |
CN117078819A (en) | Heuristic-enabled tile rendering |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
C06 | Publication | ||
PB01 | Publication | ||
C10 | Entry into substantive examination | ||
SE01 | Entry into force of request for substantive examination | ||
C12 | Rejection of a patent application after its publication | ||
RJ01 | Rejection of invention patent application after publication |
Application publication date: 20120704 |