3D Programming: Multiple Lights

Source: Internet
Author: User
Tags define data structures

Multiple Lights

So far, only one light source (not ambient light) has been used in all effect. There is no reason why directinal,point and spotlights, or multiple sources of the same type, cannot be combined in the same effect. The main bottleneck for using multiple light sources at the same time is performance issues and the number of instructions available in a shader model. Listings 7.4 and 7.5 list The effect code that supports multiple sources of light. This code has no new constructs, so we first discuss the updated Common.fxh file, which adds some new structures and common functions. Then go into the effect part of the code.

List 7.4 common.fxh for common functions in multiple point lights

#ifndef _common_fxh #define _COMMON_FXH/************* Constants *************/#define FLIP_TEXTURE_Y 1/************
    * Data Structures *************/struct Point_light {float3 Position;
    float Lightradius;
FLOAT4 Color;

};
    struct Light_contribution_data {float4 Color;
    FLOAT3 Normal;
    FLOAT3 viewdirection;
    FLOAT4 Lightcolor;	
    FLOAT4 lightdirection;
    FLOAT4 Specularcolor;	
float Specularpower;

}; /************* Utility functions *************/float2 get_corrected_texture_coordinate (Float2 texturecoordinate) {# 
    If Flip_texture_y return Float2 (texturecoordinate.x, 1.0-texturecoordinate.y); 
    #else return texturecoordinate; #endif} float3 Get_vector_color_contribution (float4 light, float3 color) {//color (. RGB) * intensity (. a) Retu
RN Light.rgb * LIGHT.A * color; FLOAT3 Get_scalar_color_contribution (float4 light, float color) {//color (. RGB) * intensity (. A) return light . RGB * LIGHT.A *Color
    } float4 Get_light_data (float3 lightposition, float3 worldposition, float lightradius) {float4 lightdata;

    FLOAT3 lightdirection = lightposition-worldposition;
    LIGHTDATA.XYZ = normalize (lightdirection); LIGHTDATA.W = Saturate (1.0f-length (lightdirection)/Lightradius);
Attenuation return lightdata; } FLOAT3 Get_light_contribution (Light_contribution_data in) {FLOAT3 lightdirection = in.	
    LIGHTDIRECTION.XYZ; float n_dot_l = dot (in.	
    Normal, lightdirection); FLOAT3 Halfvector = normalize (Lightdirection + in.
    Viewdirection); float N_dot_h = dot (in.
    
    Normal, Halfvector); Float4 lightcoefficients = Lit (n_dot_l, n_dot_h, in.
    Specularpower); FLOAT3 diffuse = get_vector_color_contribution (in. Lightcolor, Lightcoefficients.y * in. COLOR.RGB) * in.
    LIGHTDIRECTION.W; FLOAT3 specular = Get_scalar_color_contribution (in. Specularcolor, Min (Lightcoefficients.z, in. COLOR.W)) * in. LIGHTDIRECTION.W * in.
    
    LIGHTCOLOR.W; Return (diffuse + specular);
 } #endif/* _common_fxh * *

Support Structures and Utility functions

Each point light needs a position, a color, and a radius. Therefore, if you want to use 4 point lights at the same time, you need 4 groups of position,color and radius. By point_light the three members together, you can use a simple declaration to create an array of point LIGHTS, such as Point_light pointlights[num_lights. Note that the Point_light data members do not specify annotations (semantics). When you use an array of structs, you cannot set the annotations of the members of the struct, and annotations cannot be used for the members of the struct body.

Another new addition to the structure is light_contribution_data. The members of the structure include the surface color and normal, as well as view direction,light color,light direction,specular color, and specular power, These members are used to implement the diffuse and specular operations of a single light source. The parameter of a common function get_light_contribution () is the LIGHT_CONTRIBUTEION_DTA data type, which uses the same steps as before to compute diffuse and specular, and returns a color value that combines the diffuse and specular float3 types. Another common function, Get_light_data (), is used to compute light directiont and attenuation members in the LIGHT_CONTRIBUTION_DATA structure. Listing 7.5 Lists the code for multiple point light effect, which uses the structure and common functions in common.fxh.

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.