this article by Zhangbaochong Original, reprint please indicate the source http://www.cnblogs.com/zhangbaochong/p/5751111.html
Because of the beginning of the senior senior face looking for work, in order to put a few items on the resume, recently to the school period to practice handwriting of a C + + soft grating rendering demo to take out a change, slightly optimized, the way to increase the light. Although the writing is relatively simple, but still take it out to share, hoping to play a role in it, also welcome to criticize O (∩_∩) o~
(PS: Project code is only about 3000 lines, performance is also relatively slag, high-performance grating rendering can see the empty Ming big salviahttp://www.cnblogs.com/lingjingqiu/)
1. Main Module function 1.1 math Library
vector, matrix implementation and transformation (matrix translation, rotation, transpose, determinant, inverse matrix);
Perspective matrix, projection matrix and screen coordinate transformation matrix;
Some commonly used functions (interpolation, RGB color to uint, angle radians conversion, reflection vectors).
1.2 Back buffer
Setting the pixels, I started with the SetPixel () function, and found that the speed was slow to look straight. Later changed to use Createdibsections to create a bitmap, directly into memory to write pixel color values, after the end of a piece of bitblt to the window on the line.
1.3 Model Standard
Since I have been writing directx11 code, we also use the standard D3D coordinate model, the left-hand system +world view proj three matrices
1.4 Geometrical Stages
Vertices from model space to world space to camera space to the homogeneous clipping space, corresponding to the main three matrix of Worlds view Proj, the corresponding method is provided in the math library.
Element assembly, that is, the vertices are assembled into triangles in a certain order, the vertex index is used to organize the vertices in this project, and the corresponding normals of the triangular clockwise order are facing out. The reason for the order is that the normal direction of consistency, only in order to carry out behind the back of the blanking.
The projected coordinates are then pivoted to divide, the vertex coordinates are in CVV (x[-1,1],y[-1,1],z[0,1]), and the CVV coordinates are converted to the corresponding coordinates in the screen after cropping.
1.4 Triangular rasterization
Bresenham line drawing algorithm used by Wireframe model.
Triangular grating algorithm: http://blog.csdn.net/cppyin/article/details/6232453
The sweep line used by the fill triangle.
1.5 Reverse blanking
Refer to: http://blog.csdn.net/cppyin/article/details/6207206
1.6 Simple CVV clipping 1.7 support textures
Texture UV coordinates are the same as D3D, texture coordinates address the same D3D wrap style, and the map repeats on the surface of the object, similar to how we set the desktop to be the tile of the picture. When the coordinate is greater than 1 o'clock, the texture value is obtained by removing the integer part, and the coordinate is less than 0, then a minimum positive number is added, and the coordinates are greater than 0.
1.8 Simple Illumination
Phong illumination model, realize three kinds of parallel light, point light source and spotlight.
1.9 Simple Shaders
Write a shader class with VS and PS two functions corresponding to the vertex shader and pixel shader, some of the code is as follows:
1Vertexout Boxshader::vs (Constvertexin&vin)2 {3Vertexout out;4 out. PosH = Vin.pos *m_worldviewproj;5 6 out. Postrans = Vin.pos *M_world;7 out. normal = out. Normal *M_worldinvtranspose;8 9 out. color =Vin.color;Ten out. Tex =Vin.tex; One A return out; - } - theZcvector Boxshader::P s (vertexout&pin) - { - //The unit of normal - pin.normal.Normalize (); + - //Texture Sampling +Zcvector Texcolor =M_tex. Sample (Pin.tex); A at //vertex to Observer point Vector -Zcvector Toeye = (M_eyepos-Pin.postrans). Normalize (); - - //Initialize the color value to all 0 -Zcvector Ambient (0. F,0. F,0. F,0. f); -Zcvector Diffuse (0. F,0. F,0. F,0. f); inZcvector Specular (0. F,0. F,0. F,0. f); - to //ambient light, diffuse reflection, high-light, resulting from the calculation of the light source + zcvector A, D, S; - the lights::computedirectionallight (m_material, M_dirlight, Pin.normal, Toeye, A, D, S); * $Ambient = ambient +A;Panax NotoginsengDiffuse = diffuse +D; -Specular = specular +S; the + A //Texture + Lighting Calculation formula: Texture * (ambient light + diffuse light) + highlight theZcvector Litcolor = Texcolor * (ambient + diffuse) +specular; + - //Final color transparency uses diffuse light transparency $LITCOLOR.W = M_MATERIAL.DIFFUSE.W *TEXCOLOR.W; $ - returnLitcolor; -}View Code
2. Effect 2.1 Wireframe Model
2.2 Vertex color
2.3 Textures + Illumination
The picture itself is a wood-textured material that looks rougher/(ㄒoㄒ)/~~
3. Source code
Project hosted on GitHub: Https://github.com/zhangbaochong/Tiny3D
I will optimize it when I have time, such as using SIMD acceleration and so on. Recently there should be no time to engage in this, D3D recently did not see much, in order to find work for the beginning of the algorithm data structure review!
Open source A simple C + + soft-grating renderer