Skip to content

Commit

Permalink
Changed many casts with 'as' to use C-style casts because "InvalidCas…
Browse files Browse the repository at this point in the history
…tException: Could not cast type A to type B" is much informative than "NullReferenceException."
  • Loading branch information
pjcozzi committed Oct 30, 2010
1 parent 44682bc commit 5498d02
Show file tree
Hide file tree
Showing 55 changed files with 179 additions and 177 deletions.
2 changes: 1 addition & 1 deletion Source/Examples/Chapter03/Triangle/Triangle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void main()
fragmentColor = u_color;
}";
ShaderProgram sp = Device.CreateShaderProgram(vs, fs);
(sp.Uniforms["u_color"] as Uniform<Vector3S>).Value = new Vector3S(1, 0, 0);
((Uniform<Vector3S>)sp.Uniforms["u_color"]).Value = new Vector3S(1, 0, 0);

///////////////////////////////////////////////////////////////////

Expand Down
4 changes: 2 additions & 2 deletions Source/Examples/Chapter3/NightLights/NightLights.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ void main()
ShaderProgram sp = Device.CreateShaderProgram(vs, fs);

float blendDurationScale = 0.1f;
(sp.Uniforms["u_blendDuration"] as Uniform<float>).Value = blendDurationScale;
(sp.Uniforms["u_blendDurationScale"] as Uniform<float>).Value = 1 / (2 * blendDurationScale);
((Uniform<float>)sp.Uniforms["u_blendDuration"]).Value = blendDurationScale;
((Uniform<float>)sp.Uniforms["u_blendDurationScale"]).Value = 1 / (2 * blendDurationScale);

Mesh mesh = SubdivisionEllipsoidTessellator.Compute(Ellipsoid.ScaledWgs84, 5, SubdivisionEllipsoidVertexAttributes.Position);
VertexArray va = _window.Context.CreateVertexArray(mesh, sp.VertexAttributes, BufferHint.StaticDraw);
Expand Down
2 changes: 1 addition & 1 deletion Source/Examples/Chapter9/Multithreading/Multithreading.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public void Dispose()
{
foreach (IRenderable shapefile in _shapefiles)
{
(shapefile as IDisposable).Dispose();
((IDisposable)shapefile).Dispose();
}

_doneQueue.Dispose();
Expand Down
34 changes: 17 additions & 17 deletions Source/Renderer/Device.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
{
if (mesh.Indices.Datatype == IndicesType.Byte)
{
IList<byte> meshIndices = (mesh.Indices as IndicesByte).Values;
IList<byte> meshIndices = ((IndicesByte)mesh.Indices).Values;

byte[] indices = new byte[meshIndices.Count];
meshIndices.CopyTo(indices, 0);
Expand All @@ -143,7 +143,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
}
else if (mesh.Indices.Datatype == IndicesType.UnsignedShort)
{
IList<ushort> meshIndices = (mesh.Indices as IndicesUnsignedShort).Values;
IList<ushort> meshIndices = ((IndicesUnsignedShort)mesh.Indices).Values;

ushort[] indices = new ushort[meshIndices.Count];
for (int j = 0; j < meshIndices.Count; ++j)
Expand All @@ -159,7 +159,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
{
Debug.Assert(mesh.Indices.Datatype == IndicesType.UnsignedInt);

IList<uint> meshIndices = (mesh.Indices as IndicesUnsignedInt).Values;
IList<uint> meshIndices = ((IndicesUnsignedInt)mesh.Indices).Values;

uint[] indices = new uint[meshIndices.Count];
for (int j = 0; j < meshIndices.Count; ++j)
Expand All @@ -185,7 +185,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl

if (attribute.Datatype == VertexAttributeType.Double)
{
IList<double> values = (attribute as VertexAttribute<double>).Values;
IList<double> values = ((VertexAttribute<double>)attribute).Values;

float[] valuesArray = new float[values.Count];
for (int i = 0; i < values.Count; ++i)
Expand All @@ -200,7 +200,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
}
else if (attribute.Datatype == VertexAttributeType.DoubleVector2)
{
IList<Vector2D> values = (attribute as VertexAttribute<Vector2D>).Values;
IList<Vector2D> values = ((VertexAttribute<Vector2D>)attribute).Values;

Vector2S[] valuesArray = new Vector2S[values.Count];
for (int i = 0; i < values.Count; ++i)
Expand All @@ -215,7 +215,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
}
else if (attribute.Datatype == VertexAttributeType.DoubleVector3)
{
IList<Vector3D> values = (attribute as VertexAttribute<Vector3D>).Values;
IList<Vector3D> values = ((VertexAttribute<Vector3D>)attribute).Values;

Vector3S[] valuesArray = new Vector3S[values.Count];
for (int i = 0; i < values.Count; ++i)
Expand All @@ -230,7 +230,7 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
}
else if (attribute.Datatype == VertexAttributeType.DoubleVector4)
{
IList<Vector4D> values = (attribute as VertexAttribute<Vector4D>).Values;
IList<Vector4D> values = ((VertexAttribute<Vector4D>)attribute).Values;

Vector4S[] valuesArray = new Vector4S[values.Count];
for (int i = 0; i < values.Count; ++i)
Expand All @@ -245,56 +245,56 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
}
else if (attribute.Datatype == VertexAttributeType.HalfFloat)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Half>).Values, SizeInBytes<Half>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Half>)attribute).Values, SizeInBytes<Half>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 1);
}
else if (attribute.Datatype == VertexAttributeType.HalfFloatVector2)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Vector2H>).Values, SizeInBytes<Vector2H>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Vector2H>)attribute).Values, SizeInBytes<Vector2H>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 2);
}
else if (attribute.Datatype == VertexAttributeType.HalfFloatVector3)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Vector3H>).Values, SizeInBytes<Vector3H>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Vector3H>)attribute).Values, SizeInBytes<Vector3H>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 3);
}
else if (attribute.Datatype == VertexAttributeType.HalfFloatVector4)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Vector4H>).Values, SizeInBytes<Vector4H>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Vector4H>)attribute).Values, SizeInBytes<Vector4H>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.HalfFloat, 4);
}
else if (attribute.Datatype == VertexAttributeType.Float)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<float>).Values, sizeof(float), usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<float>)attribute).Values, sizeof(float), usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 1);
}
else if (attribute.Datatype == VertexAttributeType.FloatVector2)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Vector2S>).Values, SizeInBytes<Vector2S>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Vector2S>)attribute).Values, SizeInBytes<Vector2S>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 2);
}
else if (attribute.Datatype == VertexAttributeType.FloatVector3)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Vector3S>).Values, SizeInBytes<Vector3S>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Vector3S>)attribute).Values, SizeInBytes<Vector3S>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 3);
}
else if (attribute.Datatype == VertexAttributeType.FloatVector4)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<Vector4S>).Values, SizeInBytes<Vector4S>.Value, usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<Vector4S>)attribute).Values, SizeInBytes<Vector4S>.Value, usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.Float, 4);
Expand All @@ -303,14 +303,14 @@ public static MeshBuffers CreateMeshBuffers(Mesh mesh, ShaderVertexAttributeColl
{
if (attribute is VertexAttributeRGBA)
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<byte>).Values, sizeof(byte), usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<byte>)attribute).Values, sizeof(byte), usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 4, true, 0, 0);
}
else
{
VertexBuffer vertexBuffer = CreateVertexBuffer((attribute as VertexAttribute<byte>).Values, sizeof(byte), usageHint);
VertexBuffer vertexBuffer = CreateVertexBuffer(((VertexAttribute<byte>)attribute).Values, sizeof(byte), usageHint);

meshBuffers.Attributes[shaderAttribute.Location] =
new VertexBufferAttribute(vertexBuffer, ComponentDatatype.UnsignedByte, 1);
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/GL3x/Buffers/PixelBufferGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public void CopyFromBitmap(Bitmap bitmap)
//
// OpenGL wants rows bottom to top.
//
Bitmap flippedBitmap = bitmap.Clone() as Bitmap;
Bitmap flippedBitmap = (Bitmap)bitmap.Clone();
flippedBitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
lockedBitmap = flippedBitmap;
}
Expand Down
10 changes: 5 additions & 5 deletions Source/Renderer/GL3x/ContextGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ public override Rectangle Viewport
public override FrameBuffer FrameBuffer
{
get { return _setFrameBuffer; }
set { _setFrameBuffer = value as FrameBufferGL3x; }
set { _setFrameBuffer = (FrameBufferGL3x)value; }
}

public override void Clear(ClearState clearState)
Expand Down Expand Up @@ -176,7 +176,7 @@ public override void Draw(PrimitiveType primitiveType, int offset, int count, Dr
VerifyDraw(drawState, sceneState);
ApplyBeforeDraw(drawState, sceneState);

VertexArrayGL3x vertexArray = drawState.VertexArray as VertexArrayGL3x;
VertexArrayGL3x vertexArray = (VertexArrayGL3x)drawState.VertexArray;
IndexBufferGL3x indexBuffer = vertexArray.IndexBuffer as IndexBufferGL3x;

if (indexBuffer != null)
Expand All @@ -197,7 +197,7 @@ public override void Draw(PrimitiveType primitiveType, DrawState drawState, Scen
VerifyDraw(drawState, sceneState);
ApplyBeforeDraw(drawState, sceneState);

VertexArrayGL3x vertexArray = drawState.VertexArray as VertexArrayGL3x;
VertexArrayGL3x vertexArray = (VertexArrayGL3x)drawState.VertexArray;
IndexBufferGL3x indexBuffer = vertexArray.IndexBuffer as IndexBufferGL3x;

if (indexBuffer != null)
Expand Down Expand Up @@ -517,14 +517,14 @@ private void ApplyRenderState(RenderState renderState)

private void ApplyVertexArray(VertexArray vertexArray)
{
VertexArrayGL3x vertexArrayGL3x = vertexArray as VertexArrayGL3x;
VertexArrayGL3x vertexArrayGL3x = (VertexArrayGL3x)vertexArray;
vertexArrayGL3x.Bind();
vertexArrayGL3x.Clean();
}

private void ApplyShaderProgram(DrawState drawState, SceneState sceneState)
{
ShaderProgramGL3x shaderProgramGL3x = drawState.ShaderProgram as ShaderProgramGL3x;
ShaderProgramGL3x shaderProgramGL3x = (ShaderProgramGL3x)drawState.ShaderProgram;

if (_boundShaderProgram != shaderProgramGL3x)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/GL3x/FrameBuffer/FrameBufferGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ internal static void Attach(FramebufferAttachment attachPoint, Texture2D texture
if (texture != null)
{
// TODO: Mipmap level
Texture2DGL3x textureGL = texture as Texture2DGL3x;
Texture2DGL3x textureGL = (Texture2DGL3x)texture;
GL.FramebufferTexture(FramebufferTarget.Framebuffer, attachPoint, textureGL.Handle.Value, 0);
}
else
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/GL3x/Shaders/UniformBlockGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal UniformBlockGL3x(string name, int sizeInBytes, int bindHandle)
public override void Bind(UniformBuffer uniformBuffer)
{
// TODO: avoid duplicate binds?
BufferHandleGL3x bufferHandle = (uniformBuffer as UniformBufferGL3x).Handle;
BufferHandleGL3x bufferHandle = ((UniformBufferGL3x)uniformBuffer).Handle;
GL.BindBufferBase(BufferTarget.UniformBuffer, _bindHandle, bufferHandle.Value);
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/GL3x/Textures/Texture2DGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public override void CopyFromBuffer(
Debug.Assert(pixelBuffer.SizeInBytes >= TextureUtility.RequiredSizeInBytes(width, height, format, dataType, rowAlignment));
Debug.Assert((rowAlignment == 1) || (rowAlignment == 2) || (rowAlignment == 4) || (rowAlignment == 8));

WritePixelBufferGL3x bufferObjectGL = pixelBuffer as WritePixelBufferGL3x;
WritePixelBufferGL3x bufferObjectGL = (WritePixelBufferGL3x)pixelBuffer;

bufferObjectGL.Bind();
BindToLastTextureUnit();
Expand Down
4 changes: 2 additions & 2 deletions Source/Renderer/GL3x/Textures/TextureUnitGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override Texture2D Texture

set
{
Texture2DGL3x texture = value as Texture2DGL3x;
Texture2DGL3x texture = (Texture2DGL3x)value;

if (_texture != texture)
{
Expand All @@ -52,7 +52,7 @@ public override TextureSampler TextureSampler

set
{
TextureSamplerGL3x sampler = value as TextureSamplerGL3x;
TextureSamplerGL3x sampler = (TextureSamplerGL3x)value;

if (_textureSampler != sampler)
{
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/GL3x/Textures/TextureUnitsGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public TextureUnitsGL3x()
_textureUnits[i] = textureUnit;
}
_dirtyTextureUnits = new List<ICleanable>();
_lastTextureUnit = _textureUnits[numberOfTextureUnits - 1] as TextureUnitGL3x;
_lastTextureUnit = (TextureUnitGL3x)_textureUnits[numberOfTextureUnits - 1];
}

#region TextureUnits Members
Expand Down
2 changes: 1 addition & 1 deletion Source/Renderer/GL3x/VertexArray/VertexArrayGL3x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ internal void Clean()
{
if (_indexBuffer != null)
{
IndexBufferGL3x bufferObjectGL = _indexBuffer as IndexBufferGL3x;
IndexBufferGL3x bufferObjectGL = (IndexBufferGL3x)_indexBuffer;
bufferObjectGL.Bind();
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void Attach(int index)

GL.EnableVertexAttribArray(index);

VertexBufferGL3x bufferObjectGL = vb.VertexBuffer as VertexBufferGL3x;
VertexBufferGL3x bufferObjectGL = (VertexBufferGL3x)vb.VertexBuffer;

bufferObjectGL.Bind();
GL.VertexAttribPointer(index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class CameraEyeUniform : DrawAutomaticUniform
{
public CameraEyeUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Vector3S>;
_uniform = (Uniform<Vector3S>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class CameraLightPositionUniform : DrawAutomaticUniform
{
public CameraLightPositionUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Vector3S>;
_uniform = (Uniform<Vector3S>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class HighResolutionSnapScaleUniform : DrawAutomaticUniform
{
public HighResolutionSnapScaleUniform(Uniform uniform)
{
_uniform = uniform as Uniform<float>;
_uniform = (Uniform<float>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class InverseViewportDimensionsUniform : DrawAutomaticUniform
{
public InverseViewportDimensionsUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Vector2S>;
_uniform = (Uniform<Vector2S>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class LightPropertiesUniform : DrawAutomaticUniform
{
public LightPropertiesUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Vector4S>;
_uniform = (Uniform<Vector4S>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class ModelViewMatrixUniform : DrawAutomaticUniform
{
public ModelViewMatrixUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Matrix4>;
_uniform = (Uniform<Matrix4>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class ModelViewOrthographicMatrixUniform : DrawAutomaticUniform
{
public ModelViewOrthographicMatrixUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Matrix4>;
_uniform = (Uniform<Matrix4>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class ModelViewPerspectiveMatrixUniform : DrawAutomaticUniform
{
public ModelViewPerspectiveMatrixUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Matrix4>;
_uniform = (Uniform<Matrix4>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ internal class ModelZToClipCoordinatesUniform : DrawAutomaticUniform
{
public ModelZToClipCoordinatesUniform(Uniform uniform)
{
_uniform = uniform as Uniform<Matrix42>;
_uniform = (Uniform<Matrix42>)uniform;
}

#region DrawAutomaticUniform Members
Expand Down
Loading

0 comments on commit 5498d02

Please sign in to comment.