public void Render() { ////测试画线函数------>>>>>> //VertexOut pA = new VertexOut(); //pA.posProjective = new Vector4D(100,0,0,1); //VertexOut pB = new VertexOut(); //pB.posProjective = new Vector4D(100, 100, 0, 1); //BresenhamLineRasterization(pA, pB); //return; ////测试画线函数<<<<<<<-------- /// Vector3D tmp = scene.camera.transform.GetPosition(); cameraPosition = new Vector3D(tmp.x, tmp.y, tmp.z); //cameraPosition.x = -cameraPosition.x; //cameraPosition.y = -cameraPosition.y; //cameraPosition.z = -cameraPosition.z; Vector3D up = new Vector3D(0, 1, 0); //up.x = -up.x; //up.y = -up.y; //up.z = -up.z; viewMatrix.SetLookAt(cameraPosition, new Vector3D(0, 0, 0), up); backBuffer.Clear(); //scene.camera for (int i = 0; i < scene.gameObjects.Count; i++) { GameObject3D gameObject = scene.gameObjects[i]; BaseShader shader = gameObject.meshRenderer.material.shader; shader.SetModelMatrix(gameObject.transform.ToMatrix()); shader.SetViewMatrix(viewMatrix); shader.SetEyePos(cameraPosition); shader.SetProjectMatrix(projectMatrix); shader.SetLight(scene.light); //图元装配 Mesh mesh = gameObject.meshRenderer.mesh; //Debug.LogError("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%"); for (int j = 0; j < mesh.indices.Length; j += 3) { Vertex p1, p2, p3; p1 = mesh.vertices[mesh.indices[j]]; p2 = mesh.vertices[mesh.indices[j + 1]]; p3 = mesh.vertices[mesh.indices[j + 2]]; //顶点着色(遍历三角形) VertexOut vertexOut1, vertexOut2, vertexOut3; vertexOut1 = shader.VertexShader(p1); vertexOut2 = shader.VertexShader(p2); vertexOut3 = shader.VertexShader(p3); shader.ProjectedMap(vertexOut1); shader.ProjectedMap(vertexOut2); shader.ProjectedMap(vertexOut3); //背面裁剪 if (shader.backFaceCulling) { if (polygonMode != PolygonMode.Wire && !BackFaceCulling(vertexOut1.posWorld, vertexOut2.posWorld, vertexOut3.posWorld)) { continue; } } //透视处理 PerspectiveDivision(vertexOut1); PerspectiveDivision(vertexOut2); PerspectiveDivision(vertexOut3); //几何裁剪 //屏幕空间转换 vertexOut1.posProjective = Matrix4x4.MultiplyVector4D(viewPortMatrix, vertexOut1.posProjective); vertexOut2.posProjective = Matrix4x4.MultiplyVector4D(viewPortMatrix, vertexOut2.posProjective); vertexOut3.posProjective = Matrix4x4.MultiplyVector4D(viewPortMatrix, vertexOut3.posProjective); //Debug.LogError("vertexOut1.posProjective " + vertexOut1.posProjective.x // + " " + vertexOut1.posProjective.y + " " + vertexOut1.posProjective.z ); //Debug.LogError("vertexOut2.posProjective " + vertexOut2.posProjective.x // + " " + vertexOut2.posProjective.y + " " + vertexOut2.posProjective.z ); //Debug.LogError("vertexOut3.posProjective " + vertexOut3.posProjective.x // + " " + vertexOut3.posProjective.y + " " + vertexOut3.posProjective.z ); //Debug.LogError("-----------------"); //片段着色 if (polygonMode == PolygonMode.Wire) { //线框模式 BresenhamLineRasterization(vertexOut1, vertexOut2); BresenhamLineRasterization(vertexOut1, vertexOut3); BresenhamLineRasterization(vertexOut2, vertexOut3); } else { //三角形填充模式 EdgeWalkingFillRasterization(vertexOut1, vertexOut2, vertexOut3, shader); } } } }