private void RenderModel(int modelToRender, bool renderToTexture = false) { EditorCamera camera = ((renderToTexture) ? defaultCam : panelCam); camera.FOV = (float)(Math.PI / 4); camera.Aspect = splitContainer1.Panel2.Width / (float)splitContainer1.Panel2.Height; float cameraDistance = camera.DrawDistance; if (renderToTexture && (modelToRender <= 0)) // we're using our default camera, set it to a position that will show the entire model { camera.MoveToShowBounds(modelList[modelToRender].Value.Bounds); cameraDistance = Math.Max(camera.Distance, Vector3.Distance(camera.Position, camera.Position + (camera.Look * modelList[modelToRender].Value.Bounds.Radius * 2))); if (cameraDistance < camera.DrawDistance) { cameraDistance = camera.DrawDistance; } } d3dDevice.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH(camera.FOV, camera.Aspect, 1, cameraDistance)); d3dDevice.SetTransform(TransformState.View, camera.ToMatrix()); UpdateTitleBar(camera); d3dDevice.SetRenderState(RenderState.FillMode, EditorOptions.RenderFillMode); d3dDevice.SetRenderState(RenderState.CullMode, EditorOptions.RenderCullMode); d3dDevice.Material = new Material { Ambient = System.Drawing.Color.White.ToRawColor4() }; d3dDevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Gray.ToRawColorBGRA(), 1, 0); d3dDevice.SetRenderState(RenderState.ZEnable, true); if (renderToTexture) { if (screenRenderTexture == null) // we can't render onto a null surface { screenRenderTexture = new Texture(d3dDevice, 256, 256, 0, Usage.RenderTarget, Format.A8R8G8B8, Pool.Default); } d3dDevice.SetRenderTarget(0, screenRenderTexture.GetSurfaceLevel(0)); } else { d3dDevice.SetRenderTarget(0, defaultRenderTarget); } d3dDevice.BeginScene(); //all drawings after this line if (modelToRender >= 0) { MatrixStack transform = new MatrixStack(); njs_object.Attach = modelList[modelToRender].Value; RenderInfo.Draw(njs_object.DrawModel(d3dDevice.GetRenderState <FillMode>(RenderState.FillMode), transform, null, meshes[modelToRender], true), d3dDevice, camera); } else // invalid selection, show a message telling the user to select something { // see if we can hide the panel's picture and/or draw an actual label here //onscreenFont.DrawText(textSprite, "No model selected.", screenCenter, Color.Black); } d3dDevice.EndScene(); //all drawings before this line d3dDevice.Present(); if (renderToTexture) { int renderIndex = -1; renderIndex = attachListRenders.FindIndex(item => item.Key == modelList[modelToRender].Key); // convert our texture into a bitmap, add it to the rendertextures list Surface surface = screenRenderTexture.GetSurfaceLevel(0); Stream surfaceStream = Surface.ToStream(surface, ImageFileFormat.Bmp); attachListRenders[renderIndex] = new KeyValuePair <int, Bitmap>(modelList[modelToRender].Key, new Bitmap(surfaceStream)); } }
internal void DrawEntireModel() { if (!loaded) { return; } d3ddevice.SetTransform(TransformState.Projection, Matrix.PerspectiveFovRH(cam.FOV, panel1.Width / (float)panel1.Height, 1, cam.DrawDistance)); d3ddevice.SetTransform(TransformState.View, cam.ToMatrix()); UpdateStatusString(); d3ddevice.SetRenderState(RenderState.FillMode, EditorOptions.RenderFillMode); d3ddevice.SetRenderState(RenderState.CullMode, EditorOptions.RenderCullMode); d3ddevice.Material = new Material { Ambient = Color.White.ToRawColor4() }; d3ddevice.Clear(ClearFlags.Target | ClearFlags.ZBuffer, Color.Black.ToRawColorBGRA(), 1, 0); d3ddevice.SetRenderState(RenderState.ZEnable, true); d3ddevice.BeginScene(); //all drawings after this line EditorOptions.RenderStateCommonSetup(d3ddevice); MatrixStack transform = new MatrixStack(); List <RenderInfo> renderList = new List <RenderInfo>(); for (int i = 0; i < @event.Scenes[0].Entities.Count; i++) { NJS_OBJECT model = @event.Scenes[0].Entities[i].Model; if (model == null) { model = @event.Scenes[0].Entities[i].GCModel; } if (model != null) { if (model.HasWeight) { renderList.AddRange(model.DrawModelTreeWeighted(EditorOptions.RenderFillMode, transform.Top, Textures, meshes[0][i])); if (@event.Scenes[0].Entities[i] == selectedObject) { renderList.AddRange(model.DrawModelTreeWeightedInvert(transform.Top, meshes[0][i])); } } else { renderList.AddRange(model.DrawModelTree(EditorOptions.RenderFillMode, transform, Textures, meshes[0][i])); if (@event.Scenes[0].Entities[i] == selectedObject) { renderList.AddRange(model.DrawModelTreeInvert(transform, meshes[0][i])); } } } } if (scenenum > 0) { for (int i = 0; i < @event.Scenes[scenenum].Entities.Count; i++) { NJS_OBJECT model = @event.Scenes[scenenum].Entities[i].Model; if (model == null) { model = @event.Scenes[scenenum].Entities[i].GCModel; } if (model != null) { if (model.HasWeight) { if (animframe != -1 && @event.Scenes[scenenum].Entities[i].Motion != null) { transform.Push(); transform.NJTranslate(@event.Scenes[scenenum].Entities[i].Motion.Models[0].GetPosition(animframe)); } renderList.AddRange(model.DrawModelTreeWeighted(EditorOptions.RenderFillMode, transform.Top, Textures, meshes[scenenum][i])); if (@event.Scenes[scenenum].Entities[i] == selectedObject) { renderList.AddRange(model.DrawModelTreeWeightedInvert(transform.Top, meshes[scenenum][i])); } if (animframe != -1 && @event.Scenes[scenenum].Entities[i].Motion != null) { transform.Pop(); } } else if (animframe == -1 || @event.Scenes[scenenum].Entities[i].Motion == null) { renderList.AddRange(model.DrawModelTree(EditorOptions.RenderFillMode, transform, Textures, meshes[scenenum][i])); if (@event.Scenes[scenenum].Entities[i] == selectedObject) { renderList.AddRange(model.DrawModelTreeInvert(transform, meshes[scenenum][i])); } } else { renderList.AddRange(model.DrawModelTreeAnimated(EditorOptions.RenderFillMode, transform, Textures, meshes[scenenum][i], @event.Scenes[scenenum].Entities[i].Motion, animframe)); if (@event.Scenes[scenenum].Entities[i] == selectedObject) { renderList.AddRange(model.DrawModelTreeAnimatedInvert(transform, meshes[scenenum][i], @event.Scenes[scenenum].Entities[i].Motion, animframe)); } } } } if (@event.Scenes[scenenum].Big?.Model != null) { if (@event.Scenes[scenenum].Big.Model.HasWeight) { renderList.AddRange(@event.Scenes[scenenum].Big.Model.DrawModelTreeWeighted(EditorOptions.RenderFillMode, transform.Top, Textures, bigmeshes[scenenum])); } else if (animframe == -1) { renderList.AddRange(@event.Scenes[scenenum].Big.Model.DrawModelTree(EditorOptions.RenderFillMode, transform, Textures, bigmeshes[scenenum])); } else { int an = 0; int fr = animframe; while (an < @event.Scenes[scenenum].Big.Motions.Count && @event.Scenes[scenenum].Big.Motions[an].a.Frames < fr) { fr -= @event.Scenes[scenenum].Big.Motions[an].a.Frames; an++; } if (an < @event.Scenes[scenenum].Big.Motions.Count) { renderList.AddRange(@event.Scenes[scenenum].Big.Model.DrawModelTreeAnimated(EditorOptions.RenderFillMode, transform, Textures, bigmeshes[scenenum], @event.Scenes[scenenum].Big.Motions[an].a, fr)); } } } if (!eventcamera && animframe != -1 && showCameraToolStripMenuItem.Checked) { transform.Push(); transform.LoadMatrix(cammatrix); renderList.AddRange(cammodel.DrawModel(EditorOptions.RenderFillMode, transform, null, cammesh, true)); transform.Pop(); } } RenderInfo.Draw(renderList, d3ddevice, cam); d3ddevice.EndScene(); //all drawings before this line d3ddevice.Present(); }