Finally got the GL-Rendering to work, now investigating the

wrong texture
This commit is contained in:
SND\AstrorEnales_cp 2011-11-21 20:01:03 +00:00
parent 190f334122
commit c1f4b8a9be
10 changed files with 157 additions and 53 deletions

View File

@ -193,12 +193,14 @@ namespace ANX.Framework.Windows.GL3
GL.BlendEquationSeparate( GL.BlendEquationSeparate(
TranslateBlendFunction(ColorBlendFunction), TranslateBlendFunction(ColorBlendFunction),
TranslateBlendFunction(AlphaBlendFunction)); TranslateBlendFunction(AlphaBlendFunction));
ErrorHelper.Check("BlendEquationSeparate");
GL.BlendFuncSeparate( GL.BlendFuncSeparate(
TranslateBlendSrc(ColorSourceBlend), TranslateBlendSrc(ColorSourceBlend),
TranslateBlendDest(ColorDestinationBlend), TranslateBlendDest(ColorDestinationBlend),
TranslateBlendSrc(AlphaSourceBlend), TranslateBlendSrc(AlphaSourceBlend),
TranslateBlendDest(AlphaDestinationBlend)); TranslateBlendDest(AlphaDestinationBlend));
ErrorHelper.Check("BlendFuncSeparate");
SetColorWriteChannel(0, ColorWriteChannels); SetColorWriteChannel(0, ColorWriteChannels);
SetColorWriteChannel(1, ColorWriteChannels1); SetColorWriteChannel(1, ColorWriteChannels1);
@ -209,6 +211,7 @@ namespace ANX.Framework.Windows.GL3
BlendFactor.G * DatatypesMapping.ColorMultiplier, BlendFactor.G * DatatypesMapping.ColorMultiplier,
BlendFactor.B * DatatypesMapping.ColorMultiplier, BlendFactor.B * DatatypesMapping.ColorMultiplier,
BlendFactor.A * DatatypesMapping.ColorMultiplier); BlendFactor.A * DatatypesMapping.ColorMultiplier);
ErrorHelper.Check("BlendColor");
// TODO: multi sample mask // TODO: multi sample mask
} }
@ -252,6 +255,7 @@ namespace ANX.Framework.Windows.GL3
channels == Graphics.ColorWriteChannels.Alpha); channels == Graphics.ColorWriteChannels.Alpha);
GL.ColorMask(index, r, g, b, a); GL.ColorMask(index, r, g, b, a);
ErrorHelper.Check("ColorMask");
} }
#endregion #endregion

View File

@ -240,21 +240,26 @@ namespace ANX.Framework.Windows.GL3
/// </summary> /// </summary>
/// <param name="type">XNA PrimitiveType.</param> /// <param name="type">XNA PrimitiveType.</param>
/// <returns>Translated BeginMode for OpenGL.</returns> /// <returns>Translated BeginMode for OpenGL.</returns>
public static BeginMode PrimitiveTypeToBeginMode(PrimitiveType type) public static BeginMode PrimitiveTypeToBeginMode(PrimitiveType type,
int primitiveCount, out int count)
{ {
switch (type) switch (type)
{ {
case PrimitiveType.LineList: case PrimitiveType.LineList:
count = primitiveCount * 2;
return BeginMode.Lines; return BeginMode.Lines;
case PrimitiveType.LineStrip: case PrimitiveType.LineStrip:
count = primitiveCount + 1;
return BeginMode.LineStrip; return BeginMode.LineStrip;
default: default:
case PrimitiveType.TriangleList: case PrimitiveType.TriangleList:
count = primitiveCount * 3;
return BeginMode.Triangles; return BeginMode.Triangles;
case PrimitiveType.TriangleStrip: case PrimitiveType.TriangleStrip:
count = primitiveCount + 2;
return BeginMode.TriangleStrip; return BeginMode.TriangleStrip;
} }
} }

View File

@ -229,10 +229,13 @@ namespace ANX.Framework.Windows.GL3
{ {
GL.Disable(EnableCap.DepthTest); GL.Disable(EnableCap.DepthTest);
} }
ErrorHelper.Check("DepthTest");
GL.DepthFunc(TranslateDepthFunction(DepthBufferFunction)); GL.DepthFunc(TranslateDepthFunction(DepthBufferFunction));
ErrorHelper.Check("DepthFunc");
GL.DepthMask(DepthBufferWriteEnable); GL.DepthMask(DepthBufferWriteEnable);
ErrorHelper.Check("DepthMask");
#endregion #endregion
#region Stencil #region Stencil
@ -244,8 +247,10 @@ namespace ANX.Framework.Windows.GL3
{ {
GL.Disable(EnableCap.StencilTest); GL.Disable(EnableCap.StencilTest);
} }
ErrorHelper.Check("StencilTest");
GL.StencilMask(StencilWriteMask); GL.StencilMask(StencilWriteMask);
ErrorHelper.Check("StencilMask");
if (TwoSidedStencilMode) if (TwoSidedStencilMode)
{ {
@ -253,19 +258,23 @@ namespace ANX.Framework.Windows.GL3
TranslateStencilOp(StencilFail), TranslateStencilOp(StencilFail),
TranslateStencilOp(StencilDepthBufferFail), TranslateStencilOp(StencilDepthBufferFail),
TranslateStencilOp(StencilPass)); TranslateStencilOp(StencilPass));
ErrorHelper.Check("StencilOpSeparate Front");
GL.StencilOpSeparate(StencilFace.Back, GL.StencilOpSeparate(StencilFace.Back,
TranslateStencilOp(CounterClockwiseStencilFail), TranslateStencilOp(CounterClockwiseStencilFail),
TranslateStencilOp(CounterClockwiseStencilDepthBufferFail), TranslateStencilOp(CounterClockwiseStencilDepthBufferFail),
TranslateStencilOp(CounterClockwiseStencilPass)); TranslateStencilOp(CounterClockwiseStencilPass));
ErrorHelper.Check("StencilOpSeparate Back");
GL.StencilFuncSeparate(StencilFace.Front, GL.StencilFuncSeparate(StencilFace.Front,
TranslateStencilFunction(StencilFunction), TranslateStencilFunction(StencilFunction),
ReferenceStencil, StencilMask); ReferenceStencil, StencilMask);
ErrorHelper.Check("StencilFuncSeparate Front");
GL.StencilFuncSeparate(StencilFace.Back, GL.StencilFuncSeparate(StencilFace.Back,
TranslateStencilFunction(CounterClockwiseStencilFunction), TranslateStencilFunction(CounterClockwiseStencilFunction),
ReferenceStencil, StencilMask); ReferenceStencil, StencilMask);
ErrorHelper.Check("StencilFuncSeparate Back");
} }
else else
{ {
@ -273,9 +282,11 @@ namespace ANX.Framework.Windows.GL3
TranslateStencilOp(StencilFail), TranslateStencilOp(StencilFail),
TranslateStencilOp(StencilDepthBufferFail), TranslateStencilOp(StencilDepthBufferFail),
TranslateStencilOp(StencilPass)); TranslateStencilOp(StencilPass));
ErrorHelper.Check("StencilOp");
GL.StencilFunc(TranslateStencilFunction(StencilFunction), GL.StencilFunc(TranslateStencilFunction(StencilFunction),
ReferenceStencil, StencilMask); ReferenceStencil, StencilMask);
ErrorHelper.Check("StencilFunc");
} }
#endregion #endregion
} }

View File

@ -103,16 +103,20 @@ namespace ANX.Framework.Windows.GL3
int uniformCount; int uniformCount;
GL.GetProgram(programHandle, ProgramParameter.ActiveUniforms, GL.GetProgram(programHandle, ProgramParameter.ActiveUniforms,
out uniformCount); out uniformCount);
ErrorHelper.Check("GetProgram ActiveUniforms");
List<string> names = new List<string>(); List<string> names = new List<string>();
for (int index = 0; index < uniformCount; index++) for (int index = 0; index < uniformCount; index++)
{ {
string name = GL.GetActiveUniformName(programHandle, index); string name = GL.GetActiveUniformName(programHandle, index);
ErrorHelper.Check("GetActiveUniformName name=" + name);
if (names.Contains(name) == false) if (names.Contains(name) == false)
{ {
names.Add(name); names.Add(name);
int uniformIndex = GL.GetUniformLocation(programHandle, name); int uniformIndex = GL.GetUniformLocation(programHandle, name);
ErrorHelper.Check("GetUniformLocation name=" + name +
" uniformIndex=" + uniformIndex);
parameters.Add(new EffectParameter() parameters.Add(new EffectParameter()
{ {
NativeParameter = NativeParameter =
@ -174,8 +178,11 @@ namespace ANX.Framework.Windows.GL3
} }
programHandle = GL.CreateProgram(); programHandle = GL.CreateProgram();
ErrorHelper.Check("CreateProgram");
GL.AttachShader(programHandle, vertexShader); GL.AttachShader(programHandle, vertexShader);
ErrorHelper.Check("AttachShader vertexShader");
GL.AttachShader(programHandle, fragmentShader); GL.AttachShader(programHandle, fragmentShader);
ErrorHelper.Check("AttachShader fragmentShader");
GL.LinkProgram(programHandle); GL.LinkProgram(programHandle);
int result; int result;
@ -307,7 +314,6 @@ namespace ANX.Framework.Windows.GL3
{ {
if (GraphicsDeviceWindowsGL3.activeEffect != this) if (GraphicsDeviceWindowsGL3.activeEffect != this)
{ {
System.Diagnostics.Debug.WriteLine("GL: Shader.Apply");
GL.UseProgram(programHandle); GL.UseProgram(programHandle);
GraphicsDeviceWindowsGL3.activeEffect = this; GraphicsDeviceWindowsGL3.activeEffect = this;
ErrorHelper.Check("UseProgram"); ErrorHelper.Check("UseProgram");
@ -322,6 +328,7 @@ namespace ANX.Framework.Windows.GL3
public void Dispose() public void Dispose()
{ {
GL.DeleteProgram(programHandle); GL.DeleteProgram(programHandle);
ErrorHelper.Check("DeleteProgram");
int result; int result;
GL.GetProgram(programHandle, ProgramParameter.DeleteStatus, out result); GL.GetProgram(programHandle, ProgramParameter.DeleteStatus, out result);

View File

@ -94,7 +94,7 @@ namespace ANX.Framework.Windows.GL3
} }
#endregion #endregion
#region SetValue #region SetValue (Matrix)
/// <summary> /// <summary>
/// Set a matrix value to the effect parameter. /// Set a matrix value to the effect parameter.
/// </summary> /// </summary>
@ -102,7 +102,8 @@ namespace ANX.Framework.Windows.GL3
public void SetValue(Matrix value) public void SetValue(Matrix value)
{ {
GL.UseProgram(parentEffect.programHandle); GL.UseProgram(parentEffect.programHandle);
System.Diagnostics.Debug.WriteLine("GL: Setting Matrix uniform " + value); ErrorHelper.Check("UseProgram");
OpenTK.Matrix4 matrix = new OpenTK.Matrix4( OpenTK.Matrix4 matrix = new OpenTK.Matrix4(
value.M11, value.M12, value.M13, value.M14, value.M11, value.M12, value.M13, value.M14,
value.M21, value.M22, value.M23, value.M24, value.M21, value.M22, value.M23, value.M24,
@ -112,7 +113,9 @@ namespace ANX.Framework.Windows.GL3
GL.UniformMatrix4(UniformIndex, false, ref matrix); GL.UniformMatrix4(UniformIndex, false, ref matrix);
ErrorHelper.Check("UniformMatrix4"); ErrorHelper.Check("UniformMatrix4");
} }
#endregion
#region SetValue (Texture)
private Texture textureCache = null; private Texture textureCache = null;
/// <summary> /// <summary>
/// Set a texture value to the effect parameter. /// Set a texture value to the effect parameter.
@ -121,16 +124,19 @@ namespace ANX.Framework.Windows.GL3
public void SetValue(Texture value) public void SetValue(Texture value)
{ {
GL.UseProgram(parentEffect.programHandle); GL.UseProgram(parentEffect.programHandle);
ErrorHelper.Check("UseProgram");
if (textureCache == null || if (textureCache == null ||
textureCache != value) textureCache != value)
{ {
// TODO: multiple texture units // TODO: multiple texture units
TextureUnit textureUnit = TextureUnit.Texture0; TextureUnit textureUnit = TextureUnit.Texture0;
GL.Enable(EnableCap.Texture2D); GL.Enable(EnableCap.Texture2D);
ErrorHelper.Check("Enable");
GL.ActiveTexture(textureUnit); GL.ActiveTexture(textureUnit);
ErrorHelper.Check("ActiveTexture");
int handle = (value.NativeTexture as Texture2DGL3).NativeHandle; int handle = (value.NativeTexture as Texture2DGL3).NativeHandle;
System.Diagnostics.Debug.WriteLine("GL: Setting Texture uniform " + handle);
GL.BindTexture(TextureTarget.Texture2D, handle); GL.BindTexture(TextureTarget.Texture2D, handle);
ErrorHelper.Check("BindTexture");
int unitIndex = (int)(textureUnit - TextureUnit.Texture0); int unitIndex = (int)(textureUnit - TextureUnit.Texture0);
GL.Uniform1(UniformIndex, 1, ref unitIndex); GL.Uniform1(UniformIndex, 1, ref unitIndex);
ErrorHelper.Check("Uniform1"); ErrorHelper.Check("Uniform1");

View File

@ -81,6 +81,22 @@ namespace ANX.Framework.Windows.GL3
internal static EffectGL3 activeEffect; internal static EffectGL3 activeEffect;
#endregion #endregion
#region Public
#region VSync
public bool VSync
{
get
{
return nativeContext.VSync;
}
set
{
nativeContext.VSync = value;
}
}
#endregion
#endregion
#region Constructor #region Constructor
/// <summary> /// <summary>
/// Create a new OpenGL graphics context. /// Create a new OpenGL graphics context.
@ -144,7 +160,7 @@ namespace ANX.Framework.Windows.GL3
GraphicsMode graphicsMode = new GraphicsMode( GraphicsMode graphicsMode = new GraphicsMode(
DatatypesMapping.SurfaceToColorFormat( DatatypesMapping.SurfaceToColorFormat(
presentationParameters.BackBufferFormat), presentationParameters.BackBufferFormat),
depth, stencil, depth, stencil,
// AntiAlias Samples: 2/4/8/16/32 // AntiAlias Samples: 2/4/8/16/32
presentationParameters.MultiSampleCount); presentationParameters.MultiSampleCount);
@ -152,6 +168,15 @@ namespace ANX.Framework.Windows.GL3
nativeContext = new GraphicsContext(graphicsMode, nativeWindowInfo); nativeContext = new GraphicsContext(graphicsMode, nativeWindowInfo);
nativeContext.MakeCurrent(nativeWindowInfo); nativeContext.MakeCurrent(nativeWindowInfo);
nativeContext.LoadAll(); nativeContext.LoadAll();
//string version = GL.GetString(StringName.Version);
//nativeContext.Dispose();
//nativeContext = null;
//string[] parts = version.Split('.');
//nativeContext = new GraphicsContext(graphicsMode, nativeWindowInfo,
// int.Parse(parts[0]), int.Parse(parts[1]), GraphicsContextFlags.Default);
//nativeContext.MakeCurrent(nativeWindowInfo);
//nativeContext.LoadAll();
} }
#endregion #endregion
@ -181,6 +206,7 @@ namespace ANX.Framework.Windows.GL3
lastClearColor = newClearColor; lastClearColor = newClearColor;
GL.ClearColor(color.R * ColorMultiplier, color.G * ColorMultiplier, GL.ClearColor(color.R * ColorMultiplier, color.G * ColorMultiplier,
color.B * ColorMultiplier, color.A * ColorMultiplier); color.B * ColorMultiplier, color.A * ColorMultiplier);
ErrorHelper.Check("ClearColor");
} }
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit); GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
ErrorHelper.Check("Clear"); ErrorHelper.Check("Clear");
@ -205,6 +231,7 @@ namespace ANX.Framework.Windows.GL3
lastClearColor = newClearColor; lastClearColor = newClearColor;
GL.ClearColor(anxColor.R * ColorMultiplier, anxColor.G * ColorMultiplier, GL.ClearColor(anxColor.R * ColorMultiplier, anxColor.G * ColorMultiplier,
anxColor.B * ColorMultiplier, anxColor.A * ColorMultiplier); anxColor.B * ColorMultiplier, anxColor.A * ColorMultiplier);
ErrorHelper.Check("ClearColor");
} }
ClearBufferMask mask = (ClearBufferMask)0; ClearBufferMask mask = (ClearBufferMask)0;
@ -222,7 +249,9 @@ namespace ANX.Framework.Windows.GL3
} }
GL.ClearDepth(depth); GL.ClearDepth(depth);
ErrorHelper.Check("ClearDepth");
GL.ClearStencil(stencil); GL.ClearStencil(stencil);
ErrorHelper.Check("ClearStencil");
GL.Clear(mask); GL.Clear(mask);
ErrorHelper.Check("Clear"); ErrorHelper.Check("Clear");
} }
@ -247,16 +276,17 @@ namespace ANX.Framework.Windows.GL3
int baseVertex, int minVertexIndex, int numVertices, int startIndex, int baseVertex, int minVertexIndex, int numVertices, int startIndex,
int primitiveCount) int primitiveCount)
{ {
System.Diagnostics.Debug.WriteLine("GL: DrawIndexedPrimitives");
// TODO: baseVertex, minVertexIndex, numVertices, startIndex, primitiveCount // TODO: baseVertex, minVertexIndex, numVertices, startIndex, primitiveCount
DrawElementsType elementsType = DrawElementsType elementsType =
boundIndexBuffer.elementSize == IndexElementSize.SixteenBits ? boundIndexBuffer.elementSize == IndexElementSize.SixteenBits ?
DrawElementsType.UnsignedShort : DrawElementsType.UnsignedShort :
DrawElementsType.UnsignedInt; DrawElementsType.UnsignedInt;
GL.DrawElements( int count;
DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType), BeginMode mode = DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType,
numVertices, elementsType, 0); primitiveCount, out count);
GL.DrawElements(mode, count, elementsType, 0);
ErrorHelper.Check("DrawElements"); ErrorHelper.Check("DrawElements");
} }
#endregion #endregion
@ -313,9 +343,11 @@ namespace ANX.Framework.Windows.GL3
public void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset, public void DrawPrimitives(PrimitiveType primitiveType, int vertexOffset,
int primitiveCount) int primitiveCount)
{ {
GL.DrawArrays( int count;
DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType), BeginMode mode = DatatypesMapping.PrimitiveTypeToBeginMode(primitiveType,
vertexOffset, primitiveCount); primitiveCount, out count);
GL.DrawArrays(mode, vertexOffset, count);
ErrorHelper.Check("DrawArrays");
} }
#endregion #endregion
@ -325,7 +357,6 @@ namespace ANX.Framework.Windows.GL3
boundVertexBuffers = new VertexBufferGL3[vertexBuffers.Length]; boundVertexBuffers = new VertexBufferGL3[vertexBuffers.Length];
for (int index = 0; index < vertexBuffers.Length; index++) for (int index = 0; index < vertexBuffers.Length; index++)
{ {
System.Diagnostics.Debug.WriteLine("GL: SetVertexBuffer " + index);
boundVertexBuffers[index] = boundVertexBuffers[index] =
(VertexBufferGL3)vertexBuffers[index].VertexBuffer.NativeVertexBuffer; (VertexBufferGL3)vertexBuffers[index].VertexBuffer.NativeVertexBuffer;
GL.BindBuffer(BufferTarget.ArrayBuffer, GL.BindBuffer(BufferTarget.ArrayBuffer,
@ -343,14 +374,10 @@ namespace ANX.Framework.Windows.GL3
IndexBufferGL3 nativeBuffer = IndexBufferGL3 nativeBuffer =
(IndexBufferGL3)indexBuffer.NativeIndexBuffer; (IndexBufferGL3)indexBuffer.NativeIndexBuffer;
if (boundIndexBuffer != nativeBuffer) boundIndexBuffer = nativeBuffer;
{ GL.BindBuffer(BufferTarget.ElementArrayBuffer,
System.Diagnostics.Debug.WriteLine("GL: SetIndexBuffer"); nativeBuffer.BufferHandle);
boundIndexBuffer = nativeBuffer; ErrorHelper.Check("BindBuffer");
GL.BindBuffer(BufferTarget.ElementArrayBuffer,
nativeBuffer.BufferHandle);
ErrorHelper.Check("BindBuffer");
}
} }
#endregion #endregion
@ -379,17 +406,5 @@ namespace ANX.Framework.Windows.GL3
{ {
throw new NotImplementedException(); throw new NotImplementedException();
} }
public bool VSync
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
} }
} }

View File

@ -148,23 +148,33 @@ namespace ANX.Framework.Windows.GL3
#region BufferData (private helper) #region BufferData (private helper)
private void BufferData<T>(T[] data, int offset) where T : struct private void BufferData<T>(T[] data, int offset) where T : struct
{ {
IntPtr size = (IntPtr)((elementSize == IndexElementSize.SixteenBits ? int size = (elementSize == IndexElementSize.SixteenBits ?
2 : 4) * data.Length); 2 : 4) * data.Length;
GL.BindBuffer(BufferTarget.ElementArrayBuffer, bufferHandle); GL.BindBuffer(BufferTarget.ElementArrayBuffer, bufferHandle);
ErrorHelper.Check("BindBuffer"); ErrorHelper.Check("BindBuffer");
if (offset == 0) if (offset == 0)
{ {
GL.BufferData(BufferTarget.ElementArrayBuffer, size, data, usageHint); GL.BufferData(BufferTarget.ElementArrayBuffer, (IntPtr)size, data,
usageHint);
ErrorHelper.Check("BufferData size=" + size); ErrorHelper.Check("BufferData size=" + size);
} }
else else
{ {
GL.BufferSubData(BufferTarget.ElementArrayBuffer, (IntPtr)offset, GL.BufferSubData(BufferTarget.ElementArrayBuffer, (IntPtr)offset,
size, data); (IntPtr)size, data);
ErrorHelper.Check("BufferSubData offset=" + offset + " size=" + size); ErrorHelper.Check("BufferSubData offset=" + offset + " size=" + size);
} }
int setSize;
GL.GetBufferParameter(BufferTarget.ElementArrayBuffer,
BufferParameterName.BufferSize, out setSize);
if (setSize != size)
{
throw new Exception("Failed to set the indexBuffer data. DataSize=" +
size + " SetSize=" + setSize);
}
} }
#endregion #endregion

View File

@ -160,6 +160,7 @@ namespace ANX.Framework.Windows.GL3
#region Cull Mode #region Cull Mode
GL.FrontFace(FrontFaceDirection.Cw); GL.FrontFace(FrontFaceDirection.Cw);
ErrorHelper.Check("FrontFace");
if (CullMode == CullMode.None) if (CullMode == CullMode.None)
{ {
GL.Disable(EnableCap.CullFace); GL.Disable(EnableCap.CullFace);
@ -179,6 +180,7 @@ namespace ANX.Framework.Windows.GL3
GL.PolygonMode(MaterialFace.FrontAndBack, GL.PolygonMode(MaterialFace.FrontAndBack,
FillMode == FillMode.WireFrame ? PolygonMode.Line : PolygonMode.Fill); FillMode == FillMode.WireFrame ? PolygonMode.Line : PolygonMode.Fill);
ErrorHelper.Check("PolygonMode");
#region ScissorTestEnable #region ScissorTestEnable
if (ScissorTestEnable) if (ScissorTestEnable)

View File

@ -103,9 +103,19 @@ namespace ANX.Framework.Windows.GL3
ErrorHelper.Check("GenBuffers"); ErrorHelper.Check("GenBuffers");
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle); GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);
ErrorHelper.Check("BindBuffer"); ErrorHelper.Check("BindBuffer");
IntPtr size = (IntPtr)(vertexDeclaration.VertexStride * setVertexCount); int size = vertexDeclaration.VertexStride * setVertexCount;
GL.BufferData(BufferTarget.ArrayBuffer, size, IntPtr.Zero, usageHint); GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)size, IntPtr.Zero,
usageHint);
ErrorHelper.Check("BufferData"); ErrorHelper.Check("BufferData");
int setSize;
GL.GetBufferParameter(BufferTarget.ArrayBuffer,
BufferParameterName.BufferSize, out setSize);
if (setSize != size)
{
throw new Exception("Failed to set the vertexBuffer data. DataSize=" +
size + " SetSize=" + setSize);
}
} }
#endregion #endregion
@ -153,15 +163,16 @@ namespace ANX.Framework.Windows.GL3
} }
#endregion #endregion
#region BufferData (private helper) (TODO) #region BufferData (private helper)
private void BufferData<T>(T[] data, int offset) where T : struct private void BufferData<T>(T[] data, int offset) where T : struct
{ {
IntPtr size = (IntPtr)(vertexDeclaration.VertexStride * data.Length); int size = vertexDeclaration.VertexStride * data.Length;
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle); GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);
ErrorHelper.Check("BindBuffer"); ErrorHelper.Check("BindBuffer");
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)offset, size, data); GL.BufferSubData<T>(BufferTarget.ArrayBuffer, (IntPtr)offset,
(IntPtr)size, data);
ErrorHelper.Check("BufferSubData"); ErrorHelper.Check("BufferSubData");
} }
#endregion #endregion
@ -169,37 +180,53 @@ namespace ANX.Framework.Windows.GL3
#region MapVertexDeclaration #region MapVertexDeclaration
internal void MapVertexDeclaration(int programHandle) internal void MapVertexDeclaration(int programHandle)
{ {
foreach (VertexElement element in vertexDeclaration.GetVertexElements()) int attributes;
GL.GetProgram(programHandle, ProgramParameter.ActiveAttributes,
out attributes);
VertexElement[] elements = vertexDeclaration.GetVertexElements();
if (elements.Length != attributes)
{
throw new InvalidOperationException("Mapping the VertexDeclaration " +
"onto the glsl attributes failed because we have " +
attributes + " Shader Attributes and " + elements.Length +
" elements in the vertex declaration which doesn't fit!");
}
foreach (VertexElement element in elements)
{ {
// TODO: element.UsageIndex? // TODO: element.UsageIndex?
switch (element.VertexElementUsage) switch (element.VertexElementUsage)
{ {
case VertexElementUsage.Position: case VertexElementUsage.Position:
GL.EnableClientState(ArrayCap.VertexArray);
int loc = GL.GetAttribLocation(programHandle, "pos"); int loc = GL.GetAttribLocation(programHandle, "pos");
ErrorHelper.Check("GetAttribLocation pos");
GL.EnableVertexAttribArray(loc); GL.EnableVertexAttribArray(loc);
ErrorHelper.Check("EnableVertexAttribArray pos");
GL.VertexAttribPointer(loc, 3, VertexAttribPointerType.Float, GL.VertexAttribPointer(loc, 3, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset); false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check(); ErrorHelper.Check("VertexAttribPointer pos");
break; break;
case VertexElementUsage.Color: case VertexElementUsage.Color:
GL.EnableClientState(ArrayCap.ColorArray);
int col = GL.GetAttribLocation(programHandle, "col"); int col = GL.GetAttribLocation(programHandle, "col");
ErrorHelper.Check("GetAttribLocation col");
GL.EnableVertexAttribArray(col); GL.EnableVertexAttribArray(col);
GL.VertexAttribPointer(col, 4, VertexAttribPointerType.Float, ErrorHelper.Check("EnableVertexAttribArray col");
false, vertexDeclaration.VertexStride, element.Offset); GL.VertexAttribPointer(col, 1, VertexAttribPointerType.UnsignedInt,
ErrorHelper.Check(); false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check("VertexAttribPointer col");
break; break;
case VertexElementUsage.TextureCoordinate: case VertexElementUsage.TextureCoordinate:
GL.EnableClientState(ArrayCap.TextureCoordArray);
int tex = GL.GetAttribLocation(programHandle, "tex"); int tex = GL.GetAttribLocation(programHandle, "tex");
ErrorHelper.Check("GetAttribLocation tex");
GL.EnableVertexAttribArray(tex); GL.EnableVertexAttribArray(tex);
ErrorHelper.Check("EnableVertexAttribArray tex");
GL.VertexAttribPointer(tex, 2, VertexAttribPointerType.Float, GL.VertexAttribPointer(tex, 2, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset); false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check(); ErrorHelper.Check("VertexAttribPointer tex");
break; break;
// TODO // TODO
@ -226,6 +253,7 @@ namespace ANX.Framework.Windows.GL3
public void Dispose() public void Dispose()
{ {
GL.DeleteBuffers(1, ref bufferHandle); GL.DeleteBuffers(1, ref bufferHandle);
ErrorHelper.Check("DeleteBuffers");
} }
#endregion #endregion
} }

View File

@ -71,6 +71,11 @@ namespace WindowsGame1
float[] y = new float[] { 10f, 10f, 10f, 10f, 10f, 10f }; float[] y = new float[] { 10f, 10f, 10f, 10f, 10f, 10f };
Random r = new Random(); Random r = new Random();
private float elapsedLastSecond = 0f;
private int fpsCount = 0;
private int lastFps = 60;
public Game1() public Game1()
: base("OpenGL3") : base("OpenGL3")
{ {
@ -120,6 +125,17 @@ namespace WindowsGame1
/// <param name="gameTime">Provides a snapshot of timing values.</param> /// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
{ {
elapsedLastSecond += (float)gameTime.ElapsedGameTime.TotalSeconds;
fpsCount++;
if (elapsedLastSecond >= 1f)
{
elapsedLastSecond -= 1f;
lastFps = fpsCount;
fpsCount = 0;
Window.Title = "FPS=" + lastFps;
}
// Allows the game to exit // Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit(); this.Exit();