private void simpleOpenGlControl1_Paint(object sender, PaintEventArgs e) { _window.Renderer.Clear(); if (_initialDataLoaded) { _window.Renderer.BeginScene(); if (_renderHemicube) { Gk3Main.Math.Vector3 forward = Gk3Main.Math.Vector3.Forward; Gk3Main.Math.Vector3 up = _camera.Orientation * Gk3Main.Math.Vector3.Up; forward = _camera.Orientation * forward; Gk3Main.Game.Radiosity.RenderHemicube(_camera.Position, forward, up); } else { Gk3Main.SceneManager.Render(_camera); } _window.Renderer.EndScene(); } _window.Present(); }
public Camera(Math.Matrix projection) { _orientation = new Gk3Main.Math.Quaternion(); _position = new Gk3Main.Math.Vector3(); _projection = projection; }
private static List <Gk3Main.Game.NounVerbCase> getNounVerbCasesUnderCursor(Gk3Main.Graphics.Camera camera, int mx, int my) { if (camera == null) { throw new ArgumentNullException("camera"); } Gk3Main.Math.Vector3 unprojected = camera.Unproject(new Gk3Main.Math.Vector3(mx, my, 0)); string model = Gk3Main.SceneManager.GetCollisionModel(camera.Position, (unprojected - camera.Position).Normalize(), 1000.0f); if (model != null) { Gk3Main.Game.Nouns noun = Gk3Main.SceneManager.GetModelNoun(model); if (noun != Gk3Main.Game.Nouns.N_NONE) { List <Gk3Main.Game.NounVerbCase> nvcs = Gk3Main.Game.NvcManager.GetNounVerbCases(noun, true); if (nvcs.Count > 0) { return(nvcs); } } } return(null); }
bool look(string[] args, Gk3Main.Console console) { Gk3Main.Math.Vector3 forward = Gk3Main.Math.Vector3.Forward; forward = _camera.Orientation * forward; console.WriteLine("Looking at {0}", forward); return(true); }
public Camera(float fov, float aspect, float near, float far, bool zNegOne) { _orientation = new Gk3Main.Math.Quaternion(); _position = new Gk3Main.Math.Vector3(); _projection = Math.Matrix.PerspectiveLH(fov, aspect, near, far, zNegOne); _near = near; _far = far; }
private void simpleOpenGlControl1_MouseMove(object sender, MouseEventArgs e) { int relx = e.X - _oldMouseX; int rely = e.Y - _oldMouseY; if (_leftMouseButton) { if (_rightMouseButton) { _camera.AddRelativePositionOffset(new Gk3Main.Math.Vector3(relx, 0, 0)); _camera.AddPositionOffset(0, -rely, 0); } else { if (_keys[(int)Keys.ShiftKey]) { _camera.AdjustYaw(relx * 0.01f); _camera.AdjustPitch(rely * 0.01f); } else { _camera.AdjustYaw(relx * 0.01f); _camera.AddRelativePositionOffset(new Gk3Main.Math.Vector3(0, 0, -rely)); } } pbRenderWindow.Refresh(); } else { if (Gk3Main.SceneManager.CurrentRoom != null) { Gk3Main.Graphics.BspSurface surface; Gk3Main.Math.Vector3 unprojected = _camera.Unproject(new Gk3Main.Math.Vector3(e.X, e.Y, 0)); Gk3Main.SceneManager.CurrentRoom.CollideRayWithSurfaces(_camera.Position, (unprojected - _camera.Position).Normalize(), 10000.0f, out surface); if (surface != null) { lblStatusSurface.Visible = true; lblSurfaceIndexValue.Visible = true; lblSurfaceIndexValue.Text = surface.index.ToString(); } else { lblStatusSurface.Visible = false; lblSurfaceIndexValue.Visible = false; } } } _oldMouseX = e.X; _oldMouseY = e.Y; }
private static int getNounVerbCaseCountUnderCursor(Gk3Main.Graphics.Camera camera, int mx, int my) { if (camera == null) { throw new ArgumentNullException("camera"); } Gk3Main.Math.Vector3 unprojected = camera.Unproject(new Gk3Main.Math.Vector3(mx, my, 0)); string model = Gk3Main.SceneManager.GetCollisionModel(camera.Position, (unprojected - camera.Position).Normalize(), 1000.0f); Gk3Main.Game.Nouns noun; if (model != null) { noun = Gk3Main.SceneManager.GetModelNoun(model); if (_lastNoun != noun) { _lastNoun = noun; if (noun != Gk3Main.Game.Nouns.N_NONE) { int count = Gk3Main.Game.NvcManager.GetNounVerbCases(noun, true).Count; _lastNounVerbCount = count; } else { _lastNounVerbCount = 0; } } } else { _lastNoun = Gk3Main.Game.Nouns.N_NONE; _lastNounVerbCount = 0; } return(_lastNounVerbCount); }
public ModelResource(string name, System.IO.Stream stream, Resource.ResourceManager content) : base(name, true) { _content = content; int currentStreamPosition = (int)stream.Position; System.IO.BinaryReader reader = new System.IO.BinaryReader(stream, Encoding.ASCII); // read the header ModHeader header; header.heading = reader.ReadUInt32(); header.minorVersion = reader.ReadByte(); header.majorVersion = reader.ReadByte(); header.unknown1 = reader.ReadUInt16(); header.numMeshes = reader.ReadUInt32(); header.size = reader.ReadUInt32(); header.lodDistance = reader.ReadSingle(); header.unknown3 = reader.ReadUInt32(); if (header.minorVersion == 9 && header.majorVersion == 1) { ModHeaderExtension headerExtension; headerExtension.isBillboard = reader.ReadUInt32() != 0; headerExtension.useCenterForBillboard = reader.ReadUInt32() != 0; headerExtension.centerX = reader.ReadSingle(); headerExtension.centerY = reader.ReadSingle(); headerExtension.centerZ = reader.ReadSingle(); headerExtension.smooth = reader.ReadUInt32() != 0; _isBillboard = headerExtension.isBillboard; _useBillboardCenter = headerExtension.useCenterForBillboard; _billboardCenter.X = headerExtension.centerX; _billboardCenter.Y = headerExtension.centerY; _billboardCenter.Z = headerExtension.centerZ; } // read the meshes _meshes = new ModMesh[header.numMeshes]; for (uint i = 0; i < header.numMeshes; i++) { ModMesh mesh = new ModMesh(); mesh.heading = reader.ReadUInt32(); // if we didn't find it then we're screwed if (mesh.heading != 0x4D455348) { throw new Resource.InvalidResourceFileFormat("Not a valid model file! Unable to find MESH section!"); } float[] transform = new float[16]; transform[0] = reader.ReadSingle(); transform[1] = reader.ReadSingle(); transform[2] = reader.ReadSingle(); transform[3] = 0; transform[4] = reader.ReadSingle(); transform[5] = reader.ReadSingle(); transform[6] = reader.ReadSingle(); transform[7] = 0; transform[8] = reader.ReadSingle(); transform[9] = reader.ReadSingle(); transform[10] = reader.ReadSingle(); transform[11] = 0; transform[12] = reader.ReadSingle(); transform[13] = reader.ReadSingle(); transform[14] = reader.ReadSingle(); transform[15] = 1.0f; mesh.TransformMatrix = new Gk3Main.Math.Matrix(transform); mesh.numSections = reader.ReadUInt32(); Math.Vector3 bbMin, bbMax; bbMin.X = reader.ReadSingle(); bbMin.Y = reader.ReadSingle(); bbMin.Z = reader.ReadSingle(); bbMax.X = reader.ReadSingle(); bbMax.Y = reader.ReadSingle(); bbMax.Z = reader.ReadSingle(); mesh.OriginalBoundingBox = new AxisAlignedBoundingBox(bbMin, bbMax); mesh.UpdatedBoundingBox = mesh.OriginalBoundingBox.Transform(mesh.TransformMatrix); mesh.sections = new ModMeshSection[mesh.numSections]; for (int j = 0; j < mesh.numSections; j++) { ModMeshSection meshSection = new ModMeshSection(); meshSection.heading = reader.ReadUInt32(); // if we didn't find it then we're screwed if (meshSection.heading != 0x4D475250) { throw new Resource.InvalidResourceFileFormat("Not a valid model file! Unable to find valid mesh section header!"); } meshSection.texture = Gk3Main.Utils.ConvertAsciiToString(reader.ReadBytes(32)); meshSection.textureResource = _content.Load <TextureResource>(meshSection.texture); meshSection.color = Utils.ConvertFromWin32COLORREF(reader.ReadUInt32()); meshSection.smooth = reader.ReadUInt32() != 0; meshSection.numVerts = reader.ReadUInt32(); meshSection.numTriangles = reader.ReadUInt32(); meshSection.numLODs = reader.ReadUInt32(); meshSection.axes = reader.ReadUInt32() != 0; // read the vertices const int vertexStride = 3 + 3 + 2; meshSection.vertices = new float[meshSection.numVerts * vertexStride]; Math.Vector3 dummy = new Gk3Main.Math.Vector3(); for (uint k = 0; k < meshSection.numVerts; k++) { dummy.X = reader.ReadSingle(); dummy.Y = reader.ReadSingle(); dummy.Z = reader.ReadSingle(); meshSection.vertices[k * vertexStride + 0] = dummy.X; meshSection.vertices[k * vertexStride + 1] = dummy.Y; meshSection.vertices[k * vertexStride + 2] = dummy.Z; } // read the normals meshSection.normals = new float[meshSection.numVerts * 3]; for (uint k = 0; k < meshSection.numVerts; k++) { meshSection.vertices[k * vertexStride + 5] = reader.ReadSingle(); meshSection.vertices[k * vertexStride + 6] = reader.ReadSingle(); meshSection.vertices[k * vertexStride + 7] = reader.ReadSingle(); } // read the tex coords meshSection.texCoords = new float[meshSection.numVerts * 2]; for (uint k = 0; k < meshSection.numVerts; k++) { meshSection.vertices[k * vertexStride + 3] = reader.ReadSingle(); meshSection.vertices[k * vertexStride + 4] = reader.ReadSingle(); } // read the indices meshSection.indices = new uint[meshSection.numTriangles * 3]; for (uint k = 0; k < meshSection.numTriangles; k++) { meshSection.indices[k * 3 + 0] = reader.ReadUInt16(); meshSection.indices[k * 3 + 1] = reader.ReadUInt16(); meshSection.indices[k * 3 + 2] = reader.ReadUInt16(); reader.ReadUInt16(); } meshSection.vertexBuffer = RendererManager.CurrentRenderer.CreateVertexBuffer(VertexBufferUsage.Static, meshSection.vertices, (int)meshSection.numVerts, _elements); meshSection.AnimatedVertexBuffer = RendererManager.CurrentRenderer.CreateVertexBuffer(VertexBufferUsage.Dynamic, meshSection.vertices, (int)meshSection.numVerts, _elements); meshSection.indexBuffer = RendererManager.CurrentRenderer.CreateIndexBuffer(meshSection.indices); mesh.sections[j] = meshSection; // read the LODK sections for (uint k = 0; k < meshSection.numLODs; k++) { ModMeshLod lod = new ModMeshLod(); lod.Magic = reader.ReadUInt32(); lod.Heading = reader.ReadUInt32(); lod.Heading2 = reader.ReadUInt32(); lod.Heading3 = reader.ReadUInt32(); if (lod.Magic != 0x4C4F444B) { throw new Resource.InvalidResourceFileFormat("Unable to find LODK section"); } // read each section lod.Section1 = new ushort[lod.Heading * 4]; lod.Section2 = new ushort[lod.Heading2 * 2]; lod.Section3 = new ushort[lod.Heading3]; for (uint l = 0; l < lod.Heading; l++) { lod.Section1[l * 4 + 0] = reader.ReadUInt16(); lod.Section1[l * 4 + 1] = reader.ReadUInt16(); lod.Section1[l * 4 + 2] = reader.ReadUInt16(); lod.Section1[l * 4 + 3] = reader.ReadUInt16(); } for (uint l = 0; l < lod.Heading2; l++) { lod.Section2[l * 2 + 0] = reader.ReadUInt16(); lod.Section2[l * 2 + 1] = reader.ReadUInt16(); } for (uint l = 0; l < lod.Heading3; l++) { lod.Section3[l] = reader.ReadUInt16(); } } } _meshes[i] = mesh; } // we might be at the end of the file now if (reader.BaseStream.Position + 4 >= reader.BaseStream.Length) { return; } // read the MODX stuff uint modxMagic = reader.ReadUInt32(); if (modxMagic == 0x4d4f4458) { for (int i = 0; i < _meshes.Length; i++) { for (int j = 0; j < _meshes[i].numSections; j++) { uint grpxMagic = reader.ReadUInt32(); uint numVertices = reader.ReadUInt32(); if (grpxMagic == 0x47525058) { for (int k = 0; k < numVertices; k++) { byte b = reader.ReadByte(); for (int l = 0; l < b; l++) { byte meshIndex = reader.ReadByte(); byte groupIndex = reader.ReadByte(); ushort polyIndex = reader.ReadUInt16(); } } } } } } }