C ++ STRUCTURE REVIEW AND STRUCTURE REVIEW
Structure: struct, used to store a certain type of variables, in the cpp file.
Struct DirLight {
Vec3 direction;
Vec3 ambient;
Vec3 diffuse;
Vec3 specular;
};
Uniform DirLight dirLight; // you can use dirlignt. ambient to access a property.
The structure is suitable for classifying a class of attributes without creating a class. For example, you do not need to create a lamp class when creating all the properties of a lamp. We can call it through the lamp. Property.
Halt: exit the program.
# The exit (0) method in include can exit the program immediately.
Switch: (do not forget for a long time)
Switch (color)
{
CaseCOLOR_BLACK:
Std: cout <"Black ";
Break;
CaseCOLOR_WHITE:
Std: cout <"White ";
Break;
Default:
Std: cout <"Unknown ";
Break;
}
Static_cast (x) is incorrect. static_cast (y) can only implement forced conversion of basic types.
Same as Java, break jumps out of the entire loop, while continue jumps out of this loop.
Generate random number:
# Include // for rand () and srand ()
Intmain ()
{
Srand (5323); // sets an initial value and is called only once (0--32767)
Std: cout <rand () <"\ t"; // call repeatedly to return a new random number
// Multiple runs will return the same sequence (if the initial values in srand () are the same ). If you want to return different sequences, You can input a time value because the time is always different.
Srand (static_cast (time (0 )));
Return0;
}
Generate a random number within the given range: (call getRandomNumber () to generate a random number between 1 and 6)
IntgetRandomNumber (intmin, intmax)
{
Staticconstdoublefraction = 1.0/(static_cast (RAND_MAX) + 1.0); // static used for efficiency, so we only calculate this value once
// Evenly distribute the random number within SS our range
Returnmin + static_cast (max-min + 1) * (rand () * fraction ));
}
Consider how users abuse programs, especially in text input. For each text input point, consider: Has the extraction failed? Can Users enter more input than expected? Can Users enter meaningless input? Can the user overflow input?
Therefore, you need to test and repair the user input:
If (std: cin. fail () // if user input fails
{
Std: cin. clear (); // clears the input buffer.
Std: cin. ignore (32767, '\ n'); // failed to remove the input.
}