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

View File

@ -240,21 +240,26 @@ namespace ANX.Framework.Windows.GL3
/// </summary>
/// <param name="type">XNA PrimitiveType.</param>
/// <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)
{
case PrimitiveType.LineList:
count = primitiveCount * 2;
return BeginMode.Lines;
case PrimitiveType.LineStrip:
count = primitiveCount + 1;
return BeginMode.LineStrip;
default:
case PrimitiveType.TriangleList:
count = primitiveCount * 3;
return BeginMode.Triangles;
case PrimitiveType.TriangleStrip:
count = primitiveCount + 2;
return BeginMode.TriangleStrip;
}
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -103,9 +103,19 @@ namespace ANX.Framework.Windows.GL3
ErrorHelper.Check("GenBuffers");
GL.BindBuffer(BufferTarget.ArrayBuffer, bufferHandle);
ErrorHelper.Check("BindBuffer");
IntPtr size = (IntPtr)(vertexDeclaration.VertexStride * setVertexCount);
GL.BufferData(BufferTarget.ArrayBuffer, size, IntPtr.Zero, usageHint);
int size = vertexDeclaration.VertexStride * setVertexCount;
GL.BufferData(BufferTarget.ArrayBuffer, (IntPtr)size, IntPtr.Zero,
usageHint);
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
@ -153,15 +163,16 @@ namespace ANX.Framework.Windows.GL3
}
#endregion
#region BufferData (private helper) (TODO)
#region BufferData (private helper)
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);
ErrorHelper.Check("BindBuffer");
GL.BufferSubData(BufferTarget.ArrayBuffer, (IntPtr)offset, size, data);
GL.BufferSubData<T>(BufferTarget.ArrayBuffer, (IntPtr)offset,
(IntPtr)size, data);
ErrorHelper.Check("BufferSubData");
}
#endregion
@ -169,37 +180,53 @@ namespace ANX.Framework.Windows.GL3
#region MapVertexDeclaration
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?
switch (element.VertexElementUsage)
{
case VertexElementUsage.Position:
GL.EnableClientState(ArrayCap.VertexArray);
int loc = GL.GetAttribLocation(programHandle, "pos");
ErrorHelper.Check("GetAttribLocation pos");
GL.EnableVertexAttribArray(loc);
ErrorHelper.Check("EnableVertexAttribArray pos");
GL.VertexAttribPointer(loc, 3, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check();
ErrorHelper.Check("VertexAttribPointer pos");
break;
case VertexElementUsage.Color:
GL.EnableClientState(ArrayCap.ColorArray);
int col = GL.GetAttribLocation(programHandle, "col");
ErrorHelper.Check("GetAttribLocation col");
GL.EnableVertexAttribArray(col);
GL.VertexAttribPointer(col, 4, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check();
ErrorHelper.Check("EnableVertexAttribArray col");
GL.VertexAttribPointer(col, 1, VertexAttribPointerType.UnsignedInt,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check("VertexAttribPointer col");
break;
case VertexElementUsage.TextureCoordinate:
GL.EnableClientState(ArrayCap.TextureCoordArray);
int tex = GL.GetAttribLocation(programHandle, "tex");
ErrorHelper.Check("GetAttribLocation tex");
GL.EnableVertexAttribArray(tex);
ErrorHelper.Check("EnableVertexAttribArray tex");
GL.VertexAttribPointer(tex, 2, VertexAttribPointerType.Float,
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check();
false, vertexDeclaration.VertexStride, element.Offset);
ErrorHelper.Check("VertexAttribPointer tex");
break;
// TODO
@ -226,6 +253,7 @@ namespace ANX.Framework.Windows.GL3
public void Dispose()
{
GL.DeleteBuffers(1, ref bufferHandle);
ErrorHelper.Check("DeleteBuffers");
}
#endregion
}

View File

@ -71,6 +71,11 @@ namespace WindowsGame1
float[] y = new float[] { 10f, 10f, 10f, 10f, 10f, 10f };
Random r = new Random();
private float elapsedLastSecond = 0f;
private int fpsCount = 0;
private int lastFps = 60;
public Game1()
: base("OpenGL3")
{
@ -120,6 +125,17 @@ namespace WindowsGame1
/// <param name="gameTime">Provides a snapshot of timing values.</param>
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
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();