Jump to content
  • Advertisement
Sign in to follow this  
skuhtz

OpenGL Trouble with rendering system (c# and OpenGL)

This topic is 2641 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm following the book "C# Game Programming for Serious Game Creation" to learn c# and I've hit a wall... I've written Rendering and Sprite classes according to the books direction, but when I run the program nothing is rendering. I've checked for syntax errors and checked my code against the code in the book and even compared it to the code given on the CD bundled with the book. I'm fairly certain all of the needed references are accounted for, I'm really at a loss here x.X Any sort of guidance would be super duper

All of this should be the only relevant code...

Renderer Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenGl;

namespace GameStructure
{
public class Renderer
{
public Renderer()
{
Gl.glEnable(Gl.GL_TEXTURE_2D);
Gl.glEnable(Gl.GL_BLEND);
Gl.glBlendFunc(Gl.GL_SRC_ALPHA, Gl.GL_ONE_MINUS_SRC_ALPHA);
}

public void DrawImmediateModeVertex(Vector position, Color color, Point uvs)
{
Gl.glColor4f(color.Red, color.Green, color.Blue, color.Alpha);
Gl.glTexCoord2f(uvs.X, uvs.Y);
Gl.glVertex3d(position.X, position.Y, position.Z);
}

internal void DrawSprite(Sprite sprite)
{
Gl.glBegin(Gl.GL_TRIANGLES);
{
for (int i = 0; i < Sprite.VertexAmount; i++)
{
Gl.glBindTexture(Gl.GL_TEXTURE_2D, sprite.Texture.Id);
DrawImmediateModeVertex(
sprite.VertexPositions,
sprite.VertexColors,
sprite.VertexUVs);
}
}
Gl.glEnd();
}
}
}





Sprite Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace GameStructure
{
public class Sprite
{
internal const int VertexAmount = 6;
Vector[] _vertexPositions = new Vector[VertexAmount];
Color[] _vertexColors = new Color[VertexAmount];
Point[] _vertexUVs = new Point[VertexAmount];
Texture _texture = new Texture();

public Sprite()
{
InitVertexPositions(new Vector(0, 0, 0), 1, 1);
SetColor(new Color(1, 1, 1, 1));
SetUVs(new Point(0, 0), new Point(1, 1));
}

public Texture Texture
{
get { return _texture; }
set
{
_texture = value;
// By default the width and height is set
// to that of the texture
InitVertexPositions(GetCenter(), _texture.Width, _texture.Height);
}
}

public Vector[] VertexPositions
{
get { return _vertexPositions; }
}

public Color[] VertexColors
{
get { return _vertexColors; }
}

public Point[] VertexUVs
{
get { return _vertexUVs; }
}

private Vector GetCenter()
{
double halfWidth = GetWidth() / 2;
double halfHeight = GetHeight() / 2;


return new Vector(
_vertexPositions[0].X + halfWidth,
_vertexPositions[0].Y - halfHeight,
_vertexPositions[0].Z);
}

private void InitVertexPositions(Vector position, double width, double height)
{
double halfWidth = width / 2;
double halfHeight = height / 2;
// Clockwise creation of two triangles to make a quad.

// TopLeft, TopRight, BottomLeft
_vertexPositions[0] = new Vector(position.X - halfWidth, position.Y + halfHeight, position.Z);
_vertexPositions[1] = new Vector(position.X + halfWidth, position.Y + halfHeight, position.Z);
_vertexPositions[2] = new Vector(position.X - halfWidth, position.Y - halfHeight, position.Z);

// TopRight, BottomRight, BottomLeft
_vertexPositions[3] = new Vector(position.X + halfWidth, position.Y + halfHeight, position.Z);
_vertexPositions[4] = new Vector(position.X + halfWidth, position.Y - halfHeight, position.Z);
_vertexPositions[5] = new Vector(position.X - halfWidth, position.Y - halfHeight, position.Z);
}

public double GetWidth()
{
// topright - topleft
return _vertexPositions[1].X - _vertexPositions[0].X;
}

public double GetHeight()
{
// topleft - bottomleft
return _vertexPositions[0].Y - _vertexPositions[2].Y;
}

public void SetWidth(float width)
{
InitVertexPositions(GetCenter(), width, GetHeight());
}

public void SetHeight(float height)
{
InitVertexPositions(GetCenter(), GetWidth(), height);
}

public void SetPosition(double x, double y)
{
SetPosition(new Vector(x, y, 0));
}

public void SetPosition(Vector position)
{
InitVertexPositions(position, GetWidth(), GetHeight());
}

public void SetColor(Color color)
{
for (int i = 0; i < Sprite.VertexAmount; i++)
{
_vertexColors = color;
}
}

public void SetUVs(Point topLeft, Point bottomRight)
{
// TopLeft, TopRight, BottomLeft
_vertexUVs[0] = topLeft;
_vertexUVs[1] = new Point(bottomRight.X, topLeft.Y);
_vertexUVs[2] = new Point(topLeft.X, bottomRight.Y);

// TopRight, BottomRight, BottomLeft
_vertexUVs[3] = new Point(bottomRight.X, topLeft.Y);
_vertexUVs[4] = bottomRight;
_vertexUVs[5] = new Point(topLeft.X, bottomRight.Y);

}

}
}



Sprite Class Test State
(using this state to test the rendering system)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tao.OpenGl;

namespace GameStructure
{
class TestSpriteClassState : IGameObject
{
TextureManager _textureManager;
Renderer _renderer = new Renderer();
Sprite _testSprite = new Sprite();
Sprite _testSprite2 = new Sprite();

public TestSpriteClassState(TextureManager textureManager)
{
_textureManager = textureManager;
_testSprite.Texture = _textureManager.Get("face_alpha");
_testSprite.SetHeight(256 * 0.5f);

_testSprite2.Texture = _textureManager.Get("face_alpha");
_testSprite2.SetPosition(-256, -256);
_testSprite2.SetColor(new Color(1, 0, 0, 1));
}

public void Render()
{
Gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gl.glClear(Gl.GL_COLOR_BUFFER_BIT);
_renderer.DrawSprite(_testSprite);
_renderer.DrawSprite(_testSprite2);
Gl.glFinish();
}

public void Update(double elapsedTime)
{
}

}
}

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!