This project is read-only.

render 2D tiles

Sep 23, 2011 at 12:23 PM

hey, it seems its the first question ;}

is it possible to render 2D tiles from Bitmap and how to move them using mouse?

does this works on any video card nvidia/ati?

Sep 26, 2011 at 9:33 PM

Hi Radioman,

It's interesting that you ask this question as I have recently had a co-worker ask me about creating a 2D game using sharpgl. Currently this could be done with SharpGL 1.83 by using a polygon with four vertices and a texture - you'll get all of the mouse interactivity for free by using a SceneControl. This will work on any video card as it uses the native OpenGL driver for Windows. However, version 2.0 uses OpenGL 4.2 (or the best supported by your card) and is much faster - as it uses a higher version there are also some useful extensions available - for example allowing textured to be any size rather than a power-of-two.

If you give me a more descriptive example of what you are looking for, I could put together a basic example application as a starting point and check it in for you - any useful additions or tricks you come with with could be integrated into the main code. If you send over details we can look into this more :)

Sep 26, 2011 at 9:56 PM

...to be exact i have tile engine for maps(http://greatmaps.codeplex.com/), it uses only power-of-two textures, loads/unloads tiles while zooming/scrolling the maps, etc

hardware acceleration and cyber fast interaction would be just awesome, and that is my goal! ;}

basic demo with texture loading/unloading and drawing on the screen without scaling would be nice

thanks

 

Sep 27, 2011 at 10:52 AM

I'd be more than happy to put together something for you that you could use for greatmaps - greatmaps is an awesome project and it can only help SharpGL to be involved in it - it would really show that it can accelerate a serious project.

If I set up a demo app that does something along the lines of rendering a 3x3 grid of 2D textured squares would this be a good starting point?

One of the things that could potentially also be useful is that all rendering will be on the GPU, freeing the CPU for your application logic, but also you have the facility to do other things very fast - opacity, depth checks, pixel shaders, transparent overlays and so on.

So would a 3x3 tile window be a useful start? If so I'll knock one up over the next coupla evenings :)

Sep 27, 2011 at 11:51 AM

that would be more than perfect! Some ground base to experiment with. Thanks

Oct 13, 2011 at 3:42 PM
Edited Oct 13, 2011 at 3:42 PM
i load one texture set the viewport and 2d mode, but it seems i do something wrong because coordinates are referenced to the center ;/ any ideas?
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;

using SharpGL;

namespace Example2
{
   public partial class FormExample2 : Form
   {
      public FormExample2()
      {
         InitializeComponent();
         this.openGLControl1.DrawFPS = true;
         openGLControl1.Resized += new EventHandler(openGLControl1_Resized);
         FormClosing += new FormClosingEventHandler(FormExample2_FormClosing);
      }

      void FormExample2_FormClosing(object sender, FormClosingEventArgs e)
      {
         openGLControl1.OpenGL.DeleteTextures(1, textures);
      }

      void openGLControl1_Resized(object sender, EventArgs e)
      {
         OpenGL gl = openGLControl1.OpenGL;

         gl.Viewport(0, 0, openGLControl1.Width, openGLControl1.Height);
         gl.MatrixMode(OpenGL.GL_PROJECTION);
         gl.LoadIdentity();
         gl.Ortho2D(0.0, openGLControl1.Width, openGLControl1.Height, 0);
      }

      private void openGLControl1_OpenGLDraw(object sender, PaintEventArgs e)
      {
         SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

         gl.Clear(OpenGL.GL_COLOR_BUFFER_BIT);
         gl.MatrixMode(OpenGL.GL_PROJECTION);
         gl.LoadIdentity();

         gl.BindTexture(OpenGL.GL_TEXTURE_2D, textures[0]);

         gl.Begin(OpenGL.GL_QUADS);
         {
            // Top Left Of The Texture and Quad
            gl.TexCoord(0.0f, 0.0f);
            gl.Vertex(0.0, 1.0f, 0.0f);

            // Bottom Left Of The Texture and Quad
            gl.TexCoord(0.0f, 1.0f);
            gl.Vertex(0.0, 0.0, 0.0f);

            // Bottom Right Of The Texture and Quad
            gl.TexCoord(1.0f, 1.0f);
            gl.Vertex(1.0f, 0.0, 0.0f);

            // Top Right Of The Texture and Quad
            gl.TexCoord(1.0f, 0.0f);
            gl.Vertex(1.0f, 1.0f, 0.0f);
         }
         gl.End();

         gl.Flush();
      }

      private void openGLControl1_OpenGLInitialized(object sender, EventArgs e)
      {
         SharpGL.OpenGL gl = this.openGLControl1.OpenGL;

         gl.Enable(OpenGL.GL_BLEND);
         gl.Disable(OpenGL.GL_DEPTH_TEST);

         gl.Viewport(0, 0, openGLControl1.Width, openGLControl1.Height);
         gl.MatrixMode(OpenGL.GL_PROJECTION);
         gl.LoadIdentity();
         gl.Ortho2D(0.0, openGLControl1.Width, openGLControl1.Height, 0);

         // ...

         //  We need to load the texture from file.
         textureImage = new Bitmap("Crate.bmp");

         //  A bit of extra initialisation here, we have to enable textures.
         gl.Enable(OpenGL.GL_TEXTURE_2D);
         gl.TexEnv(OpenGL.GL_TEXTURE_ENV, OpenGL.GL_TEXTURE_ENV_MODE, OpenGL.GL_REPLACE);

         //  Get one texture id, and stick it into the textures array.
         gl.GenTextures(1, textures);

         //  Bind the texture.
         gl.BindTexture(OpenGL.GL_TEXTURE_2D, textures[0]);

         //  Tell OpenGL where the texture data is.
         gl.TexImage2D(OpenGL.GL_TEXTURE_2D, 0, 3, textureImage.Width, textureImage.Height, 0, OpenGL.GL_BGR, OpenGL.GL_UNSIGNED_BYTE,
             textureImage.LockBits(new Rectangle(0, 0, textureImage.Width, textureImage.Height),
             ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb).Scan0);

         //  Specify linear filtering.
         gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MIN_FILTER, OpenGL.GL_LINEAR);
         gl.TexParameter(OpenGL.GL_TEXTURE_2D, OpenGL.GL_TEXTURE_MAG_FILTER, OpenGL.GL_LINEAR);
      }

      //  The texture identifier.
      uint[] textures = new uint[1];

      //  Storage the texture itself.
      Bitmap textureImage;
   }
}
Oct 13, 2011 at 4:30 PM

Until I get back from work and have a proper look, have a glance over this:

 

http://basic4gl.wikispaces.com/2D+Drawing+in+OpenGL

 

It describes using glOrtho to do 2D drawing just like you need (the language is different but the calls are the same!) can you email me the code and I'll look over it( dwmkerr AT googlemail.com)

Oct 13, 2011 at 5:35 PM

..i've checked all these 2D pages before posting, but i don't know what is missing ;/ not much practice with openGl before, i was more on dx stuff...

May 22, 2012 at 10:52 PM

Did anything come of this? I'm having similar issues where my tiles are not being rendered correctly. Any examples would be great.

May 22, 2012 at 11:12 PM

indeed ;}