MatsK

Members
  • Content count

    157
  • Joined

  • Last visited

Community Reputation

394 Neutral

About MatsK

  • Rank
    Member
  1. Doing this: m_World = Matrix.CreateScale(m_Scale) * Matrix.CreateRotationX(m_RotateX) * Matrix.CreateRotationY(m_RotateY) * Matrix.CreateRotationZ(m_RotateZ) /** Matrix.CreateScale(m_Scale)*/ * Matrix.CreateTranslation(m_Position); doesn't seem to have any effect. :( Does the fact that I'm sending my world matrix to the shader as such have anything to do with it? m_VitaboyShader.Parameters["World"].SetValue(WorldMatrix * Skel.Bones[Skel.FindBone("HEAD")].AbsoluteMatrix); Also, how would I create a single rotation matrix by using quats?
  2. I managed to make the model render right side up. I also added a very basic pixel shader to render the texture. I extended the shader to include hands and accessories; float4x4 World; float4x4 View; float4x4 Projection; float4 AmbientColor = float4(1, 1, 1, 1); float AmbientIntensity = 0.1; texture HeadTexture; sampler HeadTextureSampler = sampler_state { Texture = <HeadTexture>; }; texture AccessoryTexture; sampler AccessoryTextureSampler = sampler_state { Texture = <AccessoryTexture>; }; texture LeftHandTexture; sampler LeftHandTextureSampler = sampler_state { Texture = <LeftHandTexture>; }; texture RightHandTexture; sampler RightHandTextureSampler = sampler_state { Texture = <RightHandTexture>; }; struct VertexShaderHeadInput { float4 Position : POSITION0; float4 Normal : NORMAL0; float4 TexPosition : TEXCOORD0; }; struct VertexShaderHeadOutput { float4 Position : POSITION0; float4 Normal : NORMAL0; float4 TexPosition : TEXCOORD0; }; VertexShaderHeadOutput TransformHead(VertexShaderHeadInput Input) { VertexShaderHeadOutput Output; float4 WorldPosition = mul(Input.Position, World); float4 ViewPosition = mul(WorldPosition, View); Output.Position = mul(ViewPosition, Projection); Output.Normal = mul(ViewPosition, Projection); Output.TexPosition = Input.TexPosition; return Output; } VertexShaderHeadOutput TransformAccessory(VertexShaderHeadInput Input) { VertexShaderHeadOutput Output; float4 WorldPosition = mul(Input.Position, World); float4 ViewPosition = mul(WorldPosition, View); Output.Position = mul(ViewPosition, Projection); Output.Normal = mul(ViewPosition, Projection); Output.TexPosition = Input.TexPosition; return Output; } VertexShaderHeadOutput TransformLeftHand(VertexShaderHeadInput Input) { VertexShaderHeadOutput Output; float4 WorldPosition = mul(Input.Position, World); float4 ViewPosition = mul(WorldPosition, View); Output.Position = mul(ViewPosition, Projection); Output.Normal = mul(ViewPosition, Projection); Output.TexPosition = Input.TexPosition; return Output; } VertexShaderHeadOutput TransformRightHand(VertexShaderHeadInput Input) { VertexShaderHeadOutput Output; float4 WorldPosition = mul(Input.Position, World); float4 ViewPosition = mul(WorldPosition, View); Output.Position = mul(ViewPosition, Projection); Output.Normal = mul(ViewPosition, Projection); Output.TexPosition = Input.TexPosition; return Output; } float4 HeadPixelShaderFunction(VertexShaderHeadOutput Input) : COLOR0 { float4 Color = tex2D(HeadTextureSampler, Input.TexPosition); return Color; } float4 AccessoryPixelShaderFunction(VertexShaderHeadOutput Input) : COLOR0 { float4 Color = tex2D(AccessoryTextureSampler, Input.TexPosition); return Color; } float4 LeftHandPixelShaderFunction(VertexShaderHeadOutput Input) : COLOR0 { float4 Color = tex2D(LeftHandTextureSampler, Input.TexPosition); return Color; } float4 RightHandPixelShaderFunction(VertexShaderHeadOutput Input) : COLOR0 { float4 Color = tex2D(RightHandTextureSampler, Input.TexPosition); return Color; } technique TransformHeadTechnique { pass HeadPass { VertexShader = compile vs_3_0 TransformHead(); PixelShader = compile ps_3_0 HeadPixelShaderFunction(); } } technique TransformAccessoryTechnique { pass HeadPass { VertexShader = compile vs_3_0 TransformAccessory(); PixelShader = compile ps_3_0 AccessoryPixelShaderFunction(); } } technique TransformLeftHandTechnique { pass HeadPass { VertexShader = compile vs_3_0 TransformLeftHand(); PixelShader = compile ps_3_0 LeftHandPixelShaderFunction(); } } technique TransformRightHandTechnique { pass HeadPass { VertexShader = compile vs_3_0 TransformRightHand(); PixelShader = compile ps_3_0 RightHandPixelShaderFunction(); } } Now, the head, hands and accessory renders in place, but also seems to be bobbing up and down along the X-axis in addition to going around the Y-axis (which it is supposed to do). Here's how I create my rotation: /// <summary> /// This avatar's world matrix. Used for rendering. /// </summary> public Matrix WorldMatrix { get { if (m_WorldIsDirty) { m_World = Matrix.CreateRotationX(m_RotateX) * Matrix.CreateRotationY(m_RotateY) * Matrix.CreateRotationZ(m_RotateZ) * Matrix.CreateScale(m_Scale) * Matrix.CreateTranslation(m_Position); m_WorldIsDirty = false; } return m_World; } } /// <summary> /// Gets or sets the angle (in degrees) of the rotation axis for this Avatar. /// </summary> public float RotationY { get { return m_RotateY; } set { m_RotateY = value; m_WorldIsDirty = true; } } /// <summary> /// Converts an angle given in degrees to radians. /// </summary> /// <param name="Angle">The angle to convert.</param> /// <returns>The angle in radians.</returns> private double DegreesToRadians(double Angle) { return (Math.PI / 180) * Angle; } /// <summary> /// Updates this avatar's rotation. /// </summary> /// <param name="GTime">A GameTime instance.</param> public void Update(GameTime GTime) { if (ShouldRotate) { float Time = GTime.TotalGameTime.Ticks; float Phase = (Time % RotationSpeed) / RotationSpeed; double Multiplier = Math.Sin((Math.PI * 2) * Phase); double NewAngle = RotationStartAngle + (RotationRange * Multiplier); RotationY = (float)DegreesToRadians(NewAngle); } } Also, does anyone know how I can use the normals with my texture in the pixel shader??
  3. Auskennfuchs: No, it's plainly visible :) I found out that I could do:  m_HeadShader.Parameters["World"].SetValue(WorldMatrix * Skel.Bones[16].AbsoluteMatrix); in my program in order to render the model in place. That means I didn't have to do: Output.Position *= mul(Input.Position, BoneMatrix); Output.Normal *= mul(Input.Normal, BoneMatrix); Output.TexPosition *= Input.TexPosition; in my shader. Here's the result: So how do I make sure that the model is rendered right side up? :)
  4. Ah! Thanks! Changing to bright red gave me this: It seems like my shader is rendering exactly what it's supposed to be rendering, but in the wrong place. So what's wrong with my vertex shader calculations? :\ I don't understand your sample. What is gWorldInverseTranspose?
  5. I'm learning HLSL, and my VertexShader is compiling fine, but it isn't displaying anything. Why? Here's my shader: float4x4 BoneMatrix; float4x4 World; float4x4 View; float4x4 Projection; float4 AmbientColor = float4(1, 1, 1, 1); float AmbientIntensity = 0.1; struct VertexShaderHeadInput { float4 Position : POSITION0; float4 Normal : NORMAL0; float4 TexPosition : TEXCOORD0; }; struct VertexShaderHeadOutput { float4 Position : POSITION0; float4 Normal : NORMAL0; float4 TexPosition : TEXCOORD0; }; VertexShaderHeadOutput TransformHead(VertexShaderHeadInput Input) { VertexShaderHeadOutput Output; float4 WorldPosition = mul(Input.Position, World); float4 ViewPosition = mul(WorldPosition, View); Output.Position = mul(ViewPosition, Projection); Output.Normal = mul(ViewPosition, Projection); Output.TexPosition = mul(ViewPosition, Projection); Output.Position *= mul(Input.Position, BoneMatrix); Output.Normal *= mul(Input.Normal, BoneMatrix); Output.TexPosition *= Input.TexPosition; return Output; } float4 PixelShaderFunction(VertexShaderHeadOutput Input) : COLOR0 { return AmbientColor * AmbientIntensity; } technique TransformHeadTechnique { pass Pass1 { VertexShader = compile vs_3_0 TransformHead(); PixelShader = compile ps_3_0 PixelShaderFunction(); } } Here's my usage code: m_HeadShader.Parameters["World"].SetValue(WorldMatrix); m_HeadShader.Parameters["View"].SetValue(ViewMatrix); m_HeadShader.Parameters["Projection"].SetValue(ProjectionMatrix); m_HeadShader.Parameters["BoneMatrix"].SetValue(Skel.Bones[16].AbsoluteMatrix); foreach(EffectPass Pass in m_HeadShader.CurrentTechnique.Passes) { Pass.Apply(); foreach (Vector3 Fce in HeadMesh.Faces) { VertexPositionNormalTexture[] Vertex = new VertexPositionNormalTexture[3]; Vertex[0] = HeadMesh.RealVertices[(int)Fce.X]; Vertex[1] = HeadMesh.RealVertices[(int)Fce.Y]; Vertex[2] = HeadMesh.RealVertices[(int)Fce.Z]; Vertex[0].TextureCoordinate = HeadMesh.RealVertices[(int)Fce.X].TextureCoordinate; Vertex[1].TextureCoordinate = HeadMesh.RealVertices[(int)Fce.Y].TextureCoordinate; Vertex[2].TextureCoordinate = HeadMesh.RealVertices[(int)Fce.Z].TextureCoordinate; Vertex[0].Normal = HeadMesh.RealVertices[(int)Fce.X].Normal; Vertex[1].Normal = HeadMesh.RealVertices[(int)Fce.Y].Normal; Vertex[2].Normal = HeadMesh.RealVertices[(int)Fce.Z].Normal; m_Devc.DrawUserPrimitives(PrimitiveType.TriangleList, Vertex, 0, 1); } } My worldmatrix: /// <summary> /// This avatar's world matrix. Used for rendering. /// </summary> public Matrix WorldMatrix { get { if (m_WorldIsDirty) { m_World = Matrix.CreateRotationX(m_RotateX) * Matrix.CreateRotationY(m_RotateY) * Matrix.CreateRotationZ(m_RotateZ) * Matrix.CreateScale(m_Scale) * Matrix.CreateTranslation(m_Position); m_WorldIsDirty = false; } return m_World; } } This is the code I've been trying to convert to HLSL (High Level Shader Language): case MeshType.Head: for (int i = 0; i < Msh.TotalVertexCount; i++) { //Transform the head vertices' position by the absolute transform //for the headbone (which is always bone 17) to render the head in place. Msh.TransformedVertices[i].Position = Vector3.Transform(Msh.RealVertices[i].Position, Skel.Bones[16].AbsoluteMatrix); Msh.TransformedVertices[i].TextureCoordinate = Msh.RealVertices[i].TextureCoordinate; //Transform the head normals' position by the absolute transform //for the headbone (which is always bone 17) to render the head in place. Msh.TransformedVertices[i].Normal = Vector3.Transform(Msh.RealVertices[i].Normal, Skel.Bones[16].AbsoluteMatrix); } return; I might add that I tried to debug the shader using RenderDoc, but it actually crashed as it was loading its log. :|
  6. My favorite essay

    https://github.com/Droogans/unmaintainable-code
  7. Rendering 3D Over 2D

    Solved it! :D Turns out that the Depth parameter in GraphicsDevice.Clear works exactly like the one in SpriteBatch.Draw :)
  8. I enabled SpriteSortMode.FrontToBack in my drawing callback: /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { Resolution.BeginDraw(); spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend, null, null, RasterizerState.CullCounterClockwise, null, Resolution.getTransformationMatrix()); m_ScrManager.Draw(); spriteBatch.End(); //Reset device to defaults before rendering... GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; GraphicsDevice.Viewport = new Viewport(0, 0, GlobalSettings.Default.ScreenWidth, GlobalSettings.Default.ScreenHeight); GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 0.5f, 1); m_ScrManager.Draw3D(); HitVM.Step(); base.Draw(gameTime); } Then, in my UIScreen.Draw method, I did: public virtual void Draw() { foreach (KeyValuePair<string, UIElement> KVP in m_Elements) { try { if (KVP.Value is UIDialog || KVP.Value is WillWrightDiag) KVP.Value.Draw(m_SBatch, UIElement.GetLayerDepth(LayerDepth.DialogLayer)); else if (KVP.Value is UIButton) KVP.Value.Draw(m_SBatch, UIElement.GetLayerDepth(LayerDepth.ButtonLayer)); else if (KVP.Value is UIImage) KVP.Value.Draw(m_SBatch, UIElement.GetLayerDepth(LayerDepth.ImageLayer)); else KVP.Value.Draw(m_SBatch, UIElement.GetLayerDepth(LayerDepth.Default)); } catch(Exception) { continue; } } } Here is UIElement.GetLayerDepth: /// <summary> /// Gets the value of the corresponding layer depth. /// </summary> /// <param name="Depth">The depth for which to retrieve a value.</param> /// <returns>The value of the specified depth.</returns> public static float GetLayerDepth(LayerDepth Depth) { switch(Depth) { case LayerDepth.Default: return 0.0f; case LayerDepth.ImageLayer: return 0.8f; case LayerDepth.ButtonLayer: return 0.9f; case LayerDepth.DialogLayer: return 0.10f; default: return 0.0f; } } Then in Credits.cs, I added the WillWrightDiag to my list of elements to be drawn by UIScreen.Elements, as such: m_WillWrightDiag = new WillWrightDiag(WillImage, this, new Vector2(100, 100)); m_WillWrightDiag.IsDrawn = false; m_Elements.Add("WillWrightDiag", m_WillWrightDiag); Here's how I draw a UIDialog: public override void Draw(SpriteBatch SBatch, float? LayerDepth) { float Depth; if (LayerDepth != null) Depth = (float)LayerDepth; else Depth = 0.10f; if (IsDrawn) { Image.DrawTextureTo(SBatch, null, Image.Slicer.TLeft, Image.Position + Vector2.Zero, Depth); Image.DrawTextureTo(SBatch, Image.Slicer.TCenter_Scale, Image.Slicer.TCenter, Image.Position + new Vector2(Image.Slicer.LeftPadding, 0), Depth); Image.DrawTextureTo(SBatch, null, Image.Slicer.TRight, Image.Position + new Vector2(Image.Slicer.Width - Image.Slicer.RightPadding, 0), Depth); Image.DrawTextureTo(SBatch, Image.Slicer.CLeft_Scale, Image.Slicer.CLeft, Image.Position + new Vector2(0, Image.Slicer.TopPadding), null); Image.DrawTextureTo(SBatch, Image.Slicer.CCenter_Scale, Image.Slicer.CCenter, Image.Position + new Vector2(Image.Slicer.LeftPadding, Image.Slicer.TopPadding), Depth); Image.DrawTextureTo(SBatch, Image.Slicer.CRight_Scale, Image.Slicer.CRight, Image.Position + new Vector2(Image.Slicer.Width - Image.Slicer.RightPadding, Image.Slicer.TopPadding), Depth); int BottomY = Image.Slicer.Height - Image.Slicer.BottomPadding; Image.DrawTextureTo(SBatch, null, Image.Slicer.BLeft, Image.Position + new Vector2(0, BottomY), null); Image.DrawTextureTo(SBatch, Image.Slicer.BCenter_Scale, Image.Slicer.BCenter, Image.Position + new Vector2(Image.Slicer.LeftPadding, BottomY), Depth); Image.DrawTextureTo(SBatch, null, Image.Slicer.BRight, Image.Position + new Vector2(Image.Slicer.Width - Image.Slicer.RightPadding, BottomY), Depth); if (m_HasExitBtn) { m_CloseBtnBack.Draw(SBatch, null, Depth); m_CloseButton.Draw(SBatch, Depth); } } } And here's how I draw a WillWrightDiag, which inherits from UIDialog: public override void Draw(SpriteBatch SBatch, float? LayerDepth) { float Depth; if (LayerDepth != null) Depth = (float)LayerDepth; else Depth = 0.10f; if (IsDrawn) SBatch.Draw(m_WillWrightImg.Texture, m_WillWrightImg.Position, null, null, new Vector2(0.0f, 0.0f), 0.0f, null, Color.White, SpriteEffects.None, Depth); base.Draw(SBatch, LayerDepth); } Despite this, I still get this weird case where the Maxis-button is drawn on top of the dialog and the exit button is occasionally not drawn. Why? :\   [attachment=32955:tsocredits.png]   Is there a better, less error-prone way to achieve depth sorting? Here is the entire code for Credits.cs if it helps: using System; using System.Timers; using System.Collections.Generic; using System.Text; using Gonzo; using Gonzo.Elements; using Files.Manager; using Files.IFF; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace GonzoTest { public class CreditsScreen : UIScreen { private UIImage BackgroundImg, TSOLogoImage, BackButtonIndentImage, WillImage; private UIButton MaxisButton; private Iff m_Credits; private List<string> m_CreditsStrings = new List<string>(); private UIControl m_CreditsArea; private float m_CreditsY = 0; //Upwards position of credits text. private float m_CreditsCenterX = 0; //Center of credits area. private Timer m_CreditsTimer; //Timer for controlling text scroll. private WillWrightDiag m_WillWrightDiag; public CreditsScreen(ScreenManager Manager, SpriteBatch SBatch) : base(Manager, "Credits", SBatch, new Vector2(0, 0), new Vector2(GlobalSettings.Default.ScreenWidth, GlobalSettings.Default.ScreenHeight), GlobalSettings.Default.StartupPath + "\\" + "gamedata\\uiscripts\\credits.uis") { BackgroundImg = (UIImage)m_Elements["\"BackgroundImage\""]; TSOLogoImage = m_Controls["\"TSOLogoImage\""].Image; BackButtonIndentImage = m_Controls["\"BackButtonIndentImage\""].Image; WillImage = (UIImage)m_Elements["\"WillImage\""]; MaxisButton = (UIButton)m_Elements["\"MaxisButton\""]; MaxisButton.OnButtonClicked += MaxisButton_OnButtonClicked; m_WillWrightDiag = new WillWrightDiag(WillImage, this, new Vector2(100, 100)); m_WillWrightDiag.IsDrawn = false; m_Elements.Add("WillWrightDiag", m_WillWrightDiag); m_Credits = FileManager.GetIFF("credits.iff"); m_CreditsArea = (UIControl)m_Controls["\"CreditsArea\""]; m_CreditsY = m_CreditsArea.Size.Y; foreach(TranslatedString TStr in m_Credits.GetSTR(163).GetStringList(LanguageCodes.EngUS)) { foreach (string Str in TStr.TranslatedStr.Split('\n')) m_CreditsStrings.Add(Str); } m_CreditsTimer = new Timer(300); m_CreditsTimer.Elapsed += M_CreditsTimer_Elapsed; m_CreditsTimer.Start(); } private void M_CreditsTimer_Elapsed(object sender, ElapsedEventArgs e) { m_CreditsY -= 1.5f; } private void MaxisButton_OnButtonClicked(UIButton ClickedButton) { m_WillWrightDiag.IsDrawn = true; } public override void Update(InputHelper Input) { m_WillWrightDiag.Update(Input); base.Update(Input); } public override void Draw() { BackgroundImg.Draw(m_SBatch, null, 0.0f); TSOLogoImage.Draw(m_SBatch, null, 0.0f); BackButtonIndentImage.Draw(m_SBatch, null, 0.0f); float Separation = 1.0f; foreach (string Str in m_CreditsStrings) { m_CreditsCenterX = (m_CreditsArea.Size.X / 2) - (Manager.Font12px.MeasureString(Str).X / 2); if ((m_CreditsY + Separation) > m_CreditsArea.Position.Y && (m_CreditsY + Separation) < m_CreditsArea.Size.Y) { m_SBatch.DrawString(Manager.Font12px, Str, new Vector2(m_CreditsArea.Position.X + m_CreditsCenterX, m_CreditsY + Separation), Color.Wheat); } Separation += 15.0f; } base.Draw(); } } } Also, why is the chat down? :\ There's a much higher signal to noise ratio in the chat.
  9. Rendering 3D Over 2D

    That didn't work either :( But thanks for pointing out that SpriteSortMode.Deferred actually ignores depth! :D
  10. Rendering 3D Over 2D

    Ok, I changed my drawing callback to: /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { Resolution.BeginDraw(); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, Resolution.getTransformationMatrix()); m_ScrManager.Draw(); spriteBatch.End(); //Reset device to defaults before rendering... GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; GraphicsDevice.Viewport = new Viewport(0, 0, GlobalSettings.Default.ScreenWidth, GlobalSettings.Default.ScreenHeight); GraphicsDevice.Clear(ClearOptions.DepthBuffer, Color.Black, 0.5f, 1); m_ScrManager.Draw3D(); HitVM.Step(); base.Draw(gameTime); } Still doesn't work. :( Are the parameters correct? They're not exactly very well explained on MSDN, so I just put something...
  11. Rendering 3D Over 2D

    Yes. As is I only see the 2D. How do I clear the depth buffer?
  12. Rendering 3D Over 2D

    Yeah, I believe it does. Here's how I'm drawing a button to the UI, for instance: public override void Draw(SpriteBatch SBatch, float? LayerDepth) { if (Visible) { float Depth; if (LayerDepth != null) Depth = (float)LayerDepth; else Depth = 0.0f; if (Image != null && Image.Loaded) { Image.Draw(SBatch, new Rectangle((int)m_SourcePosition.X, (int)m_SourcePosition.Y, (int)m_Size.X, (int)m_Size.Y), Depth); } if (m_IsTextButton) SBatch.DrawString(m_Font, m_Text, m_TextPosition, TextColor); } } Notice how I'm passing Depth to Image.Draw: public override void Draw(SpriteBatch SBatch, Rectangle? SourceRect, float? LayerDepth) { float Depth; if (LayerDepth != null) Depth = (float)LayerDepth; else Depth = 0.0f; if (Visible) { if (SourceRect != null) { SBatch.Draw(Texture, Position, null, SourceRect, new Vector2(0.0f, 0.0f), 0.0f, null, Color.White, SpriteEffects.None, Depth); } else { SBatch.Draw(Texture, Position, null, null, new Vector2(0.0f, 0.0f), 0.0f, null, Color.White, SpriteEffects.None, Depth); } } } But in my drawing callback in the OP, I'm resetting the DepthStencilState before rendering 3D... I thought that should do the trick! 
  13. I'm trying to render 3D over 2D in Monogame, but can't figure out why it won't work!   Here's the drawing callback: /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { Resolution.BeginDraw(); spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, DepthStencilState.Default, RasterizerState.CullCounterClockwise, null, Resolution.getTransformationMatrix()); m_ScrManager.Draw(); spriteBatch.End(); //Reset device to defaults before rendering... GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.RasterizerState = RasterizerState.CullCounterClockwise; GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap; GraphicsDevice.Viewport = new Viewport(0, 0, GlobalSettings.Default.ScreenWidth, GlobalSettings.Default.ScreenHeight); m_ScrManager.Draw3D(); HitVM.Step(); base.Draw(gameTime); } } Here is Resolution.cs: ////////////////////////////////////////////////////////////////////////// ////License: The MIT License (MIT) ////Copyright (c) 2010 David Amador (http://www.david-amador.com) //// ////Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: //// ////The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. //// ////THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace Gonzo { public static class Resolution { static private GraphicsDeviceManager _Device = null; //From: http://www.discussiongenerator.com/2012/09/15/resolution-independent-2d-rendering-in-xna-4/ static private int virtualViewportX; static private int virtualViewportY; static private int _Width = 800; static private int _Height = 600; static private int _VWidth = 1024; static private int _VHeight = 768; static private Matrix _ScaleMatrix; static private bool _FullScreen = false; static private bool _dirtyMatrix = true; public static int VirtualViewportX { get { return virtualViewportX; } } public static int VirtualViewportY { get { return virtualViewportY; } } static public void Init(ref GraphicsDeviceManager device) { _Width = device.PreferredBackBufferWidth; _Height = device.PreferredBackBufferHeight; _Device = device; _dirtyMatrix = true; ApplyResolutionSettings(); } static public Matrix getTransformationMatrix() { if (_dirtyMatrix) RecreateScaleMatrix(); return _ScaleMatrix; } static public void SetResolution(int Width, int Height, bool FullScreen) { _Width = Width; _Height = Height; _FullScreen = FullScreen; ApplyResolutionSettings(); } static public void SetVirtualResolution(int Width, int Height) { _VWidth = Width; _VHeight = Height; _dirtyMatrix = true; } static private void ApplyResolutionSettings() { #if XBOX360 _FullScreen = true; #endif // If we aren't using a full screen mode, the height and width of the window can // be set to anything equal to or smaller than the actual screen size. if (_FullScreen == false) { if ((_Width <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width) && (_Height <= GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height)) { _Device.PreferredBackBufferWidth = _Width; _Device.PreferredBackBufferHeight = _Height; _Device.IsFullScreen = _FullScreen; _Device.ApplyChanges(); } } else { // If we are using full screen mode, we should check to make sure that the display // adapter can handle the video mode we are trying to set. To do this, we will // iterate through the display modes supported by the adapter and check them against // the mode we want to set. foreach (DisplayMode dm in GraphicsAdapter.DefaultAdapter.SupportedDisplayModes) { // Check the width and height of each mode against the passed values if ((dm.Width == _Width) && (dm.Height == _Height)) { // The mode is supported, so set the buffer formats, apply changes and return _Device.PreferredBackBufferWidth = _Width; _Device.PreferredBackBufferHeight = _Height; _Device.IsFullScreen = _FullScreen; _Device.ApplyChanges(); } } } _dirtyMatrix = true; _Width = _Device.PreferredBackBufferWidth; _Height = _Device.PreferredBackBufferHeight; } /// <summary> /// Sets the device to use the draw pump /// Sets correct aspect ratio /// </summary> static public void BeginDraw() { // Start by reseting viewport to (0,0,1,1) FullViewport(); // Clear to Black _Device.GraphicsDevice.Clear(Color.Black); // Calculate Proper Viewport according to Aspect Ratio ResetViewport(); // and clear that // This way we are gonna have black bars if aspect ratio requires it and // the clear color on the rest _Device.GraphicsDevice.Clear(Color.CornflowerBlue); } static private void RecreateScaleMatrix() { _dirtyMatrix = false; _ScaleMatrix = Matrix.CreateScale( (float)_Device.GraphicsDevice.Viewport.Width / _VWidth, (float)_Device.GraphicsDevice.Viewport.Width / _VWidth, 1f); } static public void FullViewport() { Viewport vp = new Viewport(); vp.X = vp.Y = 0; vp.Width = _Width; vp.Height = _Height; _Device.GraphicsDevice.Viewport = vp; } /// <summary> /// Get virtual Mode Aspect Ratio /// </summary> /// <returns>aspect ratio</returns> static public float getVirtualAspectRatio() { return (float)_VWidth / (float)_VHeight; } static public void ResetViewport() { float targetAspectRatio = getVirtualAspectRatio(); // figure out the largest area that fits in this resolution at the desired aspect ratio int width = _Device.PreferredBackBufferWidth; int height = (int)(width / targetAspectRatio + .5f); bool changed = false; if (height > _Device.PreferredBackBufferHeight) { height = _Device.PreferredBackBufferHeight; // PillarBox width = (int)(height * targetAspectRatio + .5f); changed = true; } // set up the new viewport centered in the backbuffer Viewport viewport = new Viewport(); viewport.X = (_Device.PreferredBackBufferWidth / 2) - (width / 2); viewport.Y = (_Device.PreferredBackBufferHeight / 2) - (height / 2); viewport.Width = width; viewport.Height = height; viewport.MinDepth = 0; viewport.MaxDepth = 1; virtualViewportX = viewport.X; virtualViewportY = viewport.Y; if (changed) { _dirtyMatrix = true; } _Device.GraphicsDevice.Viewport = viewport; } } } Here is the 3D rendering: public void Render(Matrix ViewMatrix, Matrix WorldMatrix, Matrix ProjectionMatrix) { //This sets DepthBufferEnable and DepthBufferWriteEnable. m_Devc.DepthStencilState = DepthStencilState.Default; m_Devc.BlendState = BlendState.AlphaBlend; m_Devc.RasterizerState = RasterizerState.CullNone; // Configure effects m_HeadEffect.World = WorldMatrix; m_HeadEffect.View = ViewMatrix; m_HeadEffect.Projection = ProjectionMatrix; m_HeadEffect.EnableDefaultLighting(); if (HeadTexture != null) { m_HeadEffect.Texture = HeadTexture; m_HeadEffect.TextureEnabled = true; } m_BodyEffect.World = WorldMatrix; m_BodyEffect.View = ViewMatrix; m_BodyEffect.Projection = ProjectionMatrix; m_BodyEffect.EnableDefaultLighting(); if (m_BodyEffect != null) { m_BodyEffect.Texture = BodyTexture; m_BodyEffect.TextureEnabled = true; } // Configure effects m_LeftHandEffect.World = WorldMatrix; m_LeftHandEffect.View = ViewMatrix; m_LeftHandEffect.Projection = ProjectionMatrix; m_LeftHandEffect.EnableDefaultLighting(); if (LeftHandTexture != null) { m_LeftHandEffect.Texture = LeftHandTexture; m_LeftHandEffect.TextureEnabled = true; } if (HeadMesh != null) { foreach (EffectPass Pass in m_HeadEffect.CurrentTechnique.Passes) { Pass.Apply(); foreach (Vector3 Fce in HeadMesh.Faces) { // Draw VertexPositionNormalTexture[] Vertex = new VertexPositionNormalTexture[3]; Vertex[0] = HeadMesh.TransformedVertices[(int)Fce.X]; Vertex[1] = HeadMesh.TransformedVertices[(int)Fce.Y]; Vertex[2] = HeadMesh.TransformedVertices[(int)Fce.Z]; Vertex[0].TextureCoordinate = HeadMesh.TransformedVertices[(int)Fce.X].TextureCoordinate; Vertex[1].TextureCoordinate = HeadMesh.TransformedVertices[(int)Fce.Y].TextureCoordinate; Vertex[2].TextureCoordinate = HeadMesh.TransformedVertices[(int)Fce.Z].TextureCoordinate; Vertex[0].Normal = HeadMesh.TransformedVertices[(int)Fce.X].Normal; Vertex[1].Normal = HeadMesh.TransformedVertices[(int)Fce.Y].Normal; Vertex[2].Normal = HeadMesh.TransformedVertices[(int)Fce.Z].Normal; m_Devc.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, Vertex, 0, 1); } TransformVertices(HeadMesh, null, MeshType.Head); } } if (BodyMesh != null) { foreach (EffectPass Pass in m_BodyEffect.CurrentTechnique.Passes) { Pass.Apply(); foreach (Vector3 Fce in BodyMesh.Faces) { // Draw VertexPositionNormalTexture[] Vertex = new VertexPositionNormalTexture[3]; Vertex[0] = BodyMesh.TransformedVertices[(int)Fce.X]; Vertex[1] = BodyMesh.TransformedVertices[(int)Fce.Y]; Vertex[2] = BodyMesh.TransformedVertices[(int)Fce.Z]; Vertex[0].TextureCoordinate = BodyMesh.TransformedVertices[(int)Fce.X].TextureCoordinate; Vertex[1].TextureCoordinate = BodyMesh.TransformedVertices[(int)Fce.Y].TextureCoordinate; Vertex[2].TextureCoordinate = BodyMesh.TransformedVertices[(int)Fce.Z].TextureCoordinate; Vertex[0].Normal = BodyMesh.TransformedVertices[(int)Fce.X].Normal; Vertex[1].Normal = BodyMesh.TransformedVertices[(int)Fce.Y].Normal; Vertex[2].Normal = BodyMesh.TransformedVertices[(int)Fce.Z].Normal; m_Devc.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, Vertex, 0, 1); } TransformVertices(BodyMesh, Skel.Bones[0], MeshType.Body); } } if (LeftHandMesh != null) { foreach (EffectPass Pass in m_LeftHandEffect.CurrentTechnique.Passes) { Pass.Apply(); foreach (Vector3 Fce in LeftHandMesh.Faces) { // Draw VertexPositionNormalTexture[] Vertex = new VertexPositionNormalTexture[3]; Vertex[0] = LeftHandMesh.TransformedVertices[(int)Fce.X]; Vertex[1] = LeftHandMesh.TransformedVertices[(int)Fce.Y]; Vertex[2] = LeftHandMesh.TransformedVertices[(int)Fce.Z]; m_Devc.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, Vertex, 0, 1); } TransformVertices(LeftHandMesh, null, MeshType.LHand); } } if (RightHandMesh != null) { foreach (EffectPass Pass in m_LeftHandEffect.CurrentTechnique.Passes) { Pass.Apply(); foreach (Vector3 Fce in RightHandMesh.Faces) { // Draw VertexPositionNormalTexture[] Vertex = new VertexPositionNormalTexture[3]; Vertex[0] = RightHandMesh.TransformedVertices[(int)Fce.X]; Vertex[1] = RightHandMesh.TransformedVertices[(int)Fce.Y]; Vertex[2] = RightHandMesh.TransformedVertices[(int)Fce.Z]; m_Devc.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, Vertex, 0, 1); } TransformVertices(RightHandMesh, null, MeshType.RHand); } } } /// <summary> /// Transforms all vertices in a given mesh to their correct positions. /// </summary> /// <param name="Msh">The mesh to transform.</param> /// <param name="bone">The bone to transform to.</param> private void TransformVertices(Mesh Msh, Bone bone, MeshType MshType) { switch (MshType) { case MeshType.Head: for (int i = 0; i < Msh.TotalVertexCount; i++) { //Transform the head vertices' position by the absolute transform //for the headbone (which is always bone 17) to render the head in place. Msh.TransformedVertices[i].Position = Vector3.Transform(Msh.RealVertices[i].Position, Skel.Bones[16].AbsoluteMatrix); Msh.TransformedVertices[i].TextureCoordinate = Msh.RealVertices[i].TextureCoordinate; //Transform the head normals' position by the absolute transform //for the headbone (which is always bone 17) to render the head in place. Msh.TransformedVertices[i].Normal = Vector3.Transform(Msh.RealVertices[i].Normal, Skel.Bones[16].AbsoluteMatrix); } return; case MeshType.Body: BoneBinding boneBinding = Msh.BoneBindings.FirstOrDefault(x => Msh.Bones[(int)x.BoneIndex] == bone.Name); if (boneBinding != null) { for (int i = 0; i < boneBinding.RealVertexCount; i++) { int vertexIndex = (int)boneBinding.FirstRealVertexIndex + i; VertexPositionNormalTexture relativeVertex = Msh.RealVertices[vertexIndex]; Matrix translatedMatrix = Matrix.CreateTranslation(new Vector3(relativeVertex.Position.X, relativeVertex.Position.Y, relativeVertex.Position.Z)) * bone.AbsoluteMatrix; Msh.TransformedVertices[vertexIndex].Position = Vector3.Transform(Vector3.Zero, translatedMatrix); Msh.TransformedVertices[vertexIndex].TextureCoordinate = relativeVertex.TextureCoordinate; //Normals... translatedMatrix = Matrix.CreateTranslation(new Vector3(relativeVertex.Normal.X, relativeVertex.Normal.Y, relativeVertex.Normal.Z)) * bone.AbsoluteMatrix; Msh.TransformedVertices[vertexIndex].Normal = Vector3.Transform(Vector3.Zero, translatedMatrix); } } foreach (var child in bone.Children) TransformVertices(Msh, child, MshType); break; case MeshType.LHand: for (int i = 0; i < Msh.TotalVertexCount; i++) { //Transform the left hand vertices' position by the absolute transform //for the left handbone (which is always bone 10) to render the left hand in place. Msh.TransformedVertices[i].Position = Vector3.Transform(Msh.RealVertices[i].Position, Skel.Bones[9].AbsoluteMatrix); //Transform the left hand normals' position by the absolute transform //for the left handbone (which is always bone 10) to render the left hand in place. Msh.TransformedVertices[i].Normal = Vector3.Transform(Msh.RealVertices[i].Normal, Skel.Bones[9].AbsoluteMatrix); } return; case MeshType.RHand: for (int i = 0; i < Msh.TotalVertexCount; i++) { //Transform the right hand vertices' position by the absolute transform //for the right handbone (which is always bone 15) to render the right hand in place. Msh.TransformedVertices[i].Position = Vector3.Transform(Msh.RealVertices[i].Position, Skel.Bones[14].AbsoluteMatrix); //Transform the right hand normals' position by the absolute transform //for the right handbone (which is always bone 15) to render the right hand in place. Msh.TransformedVertices[i].Normal = Vector3.Transform(Msh.RealVertices[i].Normal, Skel.Bones[14].AbsoluteMatrix); } return; } } The 3D rendering works fine when I comment out the 2D rendering from the drawing callback. Why? :\
  14. Hi! I'm trying to create a parser for a custom UI language. I thought I was done with it, as it has successfully parsed other scripts, but all of a sudden I found a script that caused GrammarExplorer to say "expected alignments". I searched for that syntax error on these boards, and I couldn't find it. Is this possibly a bug with Irony? <a href="http://pastebin.com/Qpz8ANgj">Here's my grammar</a> <a href="http://pastebin.com/6M9SFrcz">Here's the script I'm trying to parse</a> <a href="http://pastebin.com/tuLPkaUw">Here's my parser conflicts.</a> Grammar Explorer is complaining about line 204, character 17. Please tell me what I can do to correct this, thanks!