DirectX 9.0c Game Development Notes: longshu version 2 Study Notes 8: Chap10: Lighting, longshu chap10

Source: Internet
Author: User

DirectX 9.0c Game Development Notes: longshu version 2 Study Notes 8: Chap10: Lighting, longshu chap10

This chapter is about illumination. Lighting is a very important concept in Direct3D, And it corresponds to the concept of material. If there is no material, the effect of illumination cannot be reflected.

In earlier programming tutorials on DirectX 9, The D3DLIGHT9 struct is generally used to create a light source, and the D3DMATERIAL9 struct is used to define the material of an object. What we need to do is to do some trivial housework. Basically, we need to create these struct objects, set parameters, enable illumination, and so on, as for the specific implementation details, we don't have to worry about it.

However, in our second version of longshu, we use the shader Method for illumination processing. This means two points:

1. The D3DLIGHT9 and D3DMATERIAL9 struct are not displayed. Instead, we will use our own variables or struct to implement D3DLIGHT9 and D3DMATERIAL9 functions.

2. We must consider the details of implementing illumination. This means that we must have a good understanding of the theoretical knowledge in the book and then represent the formula in code. However, from the actual point of view, we only need to save the commonly used. fx files for illumination processing for future reference.

 

In addition, this allows us to make full use of our creativity: for example, we will find at least two different ways to implement mirror reflection. In addition, some special rendering effects can be achieved through the shader method, such as the cartoon rendering style.

Next, let's go to the exercise description section!

 

The exercises in this chapter are very interesting and I feel very satisfied with myself.

 

Exercise Part 1:

========================================================== ========================================================

This question introduces another method for implementing mirror reflection. I call it the halfway method because it uses a halfway vector. Personally, this method is quite good. When all the parameters in the external environment (model) remain unchanged, it seems that this method makes the model smoother!

Let's take a look at the effect! This is the result of the classic method described in the book:


This is the result of the halfway method:



We can see that the halfway method is more delicate and the light is no longer dazzling! For convenience, I set a switching function. As long as you press the number key 1 or 2, you can switch between the two mirror reflection methods to visually observe the effect.

Of course, I won't forget to give you the code:

Exercise 1 code

 

========================================================== ========================================================

 


Exercise Part 2:

========================================================== ========================================================

This topic introduces another exciting topic: cartoon style rendering! Of course, there are few things that can be done here. Real cartoon style rendering is not that simple. There are still many things we have to do to implement the cartoon style rendering with clear outlines, fast variation in light and shade, and solid colors (and I have not learned it yet ). However, the function here seems to be a problem, and the entity book I ordered has never been available, so I have to do it.

In addition, I'm curious about how to use this function. I understand it as a scale-down factor of the final pixel. I don't know what you mean?

No matter what you do, check the effect!


 

The following code:

Exercise 2

 

========================================================== ========================================================

 


Exercise Part 3:

========================================================== ========================================================

This question is very simple. Let's make a slight change to a program in the book to make the light source move. There is nothing to say about this question.

The following is the code:

Exercise 3

========================================================== ========================================================

 



Exercise Part 4:

========================================================== ========================================================

This question is a bit interesting. Let's set two light sources in the scene at the same time. I thought it was very simple, but I had a day to get it done. I finally found that the main error was that I forgot to set it after updating technique!

However, I think the current code is still a bit flawed, that is, for fun, I allow users to switch under three modes: press the 0 key is to enable the first light source, press 1 to enable the second light source, while press 2 to enable both light sources at the same time. Then I found that to achieve this effect, it seems that three techniques must be written, which correspond to the three situations respectively! I think this is a waste, but I didn't think of a better solution. I don't know if anyone can propose a better solution?

The following is the program running:


 

Still the Code:

Exercise 4

 

========================================================== ========================================================

 

 

Exercise Part 5:

========================================================== ========================================================

This question is very simple. In fact, the last section of the book has already told you the method. I will not map it here. Send the address below:

Exercise 5

 

Supplement: although the halfway method in question 1st makes the highlighted part softer, the difference between the two is quite big.

 


========================================================== ========================================================

 


Exercise Part 6:

========================================================== ========================================================

This question is hard to explain and simple. The key is whether you have learned calculus. Everything you have learned is easy to say. In any case, I have implemented this program easily, and the effect is good!

In addition, in order to make this program more fun, and to make it easier to see the interaction between light and material, I added the "adjust light color" function: when running the program, press the number key 1 (2, 3) and press the Space key/left Control key to increase/decrease the value of the red (green, blue) component! The effect is amazing! Below are a few images:

 





Finally, the Code is:

Exercise answer 6

 

========================================================== ========================================================

 

Well, this issue is now over!


What language does the dragon book series use for DirectX 90 3D Game Development Programming basics?

Isn't it c ++?
 

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.