Tuesday, 31 August 2010

Setting up an OpenGL texture for emulation

In this post, I will describe how I render graphics in my emulators using OpenGL, although, the idea of drawing to textures applies to any modern graphics library.

I interface the Windows API and OpenGL to create a GUI that I can render graphics to. To render the graphics, I create a texture in the following way.

// create a texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, x_res, y_res, 0, GL_RGB, GL_UNSIGNED_BYTE, screenData);

// set up the texture
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);   

and update the texture like this

// Update texture
glTexSubImage2D(GL_TEXTURE_2D, 0 ,0, 0, NTSC_WIDTH, NTSC_HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, screenData);

The pixel buffer i use is a 3 dimensional array, which is layed out in the following way.

u8 screenData[y][x][3];

The last 3 being the RGB value for each pixel
So, lets say I wanted to draw a blue pixel at coords(X:10, Y:15), i would do something like this.

// Draw blue pixel
screenData[15][10][0] = 0;        // R
screenData[15][10][1] = 0;        // G
screenData[15][10][2] = 255;    // B

I wont' explain too much more, instead, I'll provide the source to a small program I created to show you, which can be download from the following link. It can be compiled with Visual C++ 2010. This program creates a texture and draws some pixels to it, and also stretches the scene depending on the width and height of the window and creates a small menu, just for the sake of it.


Download here.

You will notice that in my DrawScene() function, I rotate and texture, this is because by default the bottom left is coord 0,0, when i want the top left to be 0,0.

If link goes down, make me aware please.
Peace

24 comments:

  1. Nah, not really, this is all just learned from trial and error.
    Thanks though :)

    ReplyDelete
  2. keep up the good work
    i can't program for shit

    ReplyDelete
  3. I'm sure you could if you put your mind to it, I admit programming ain't easy, but it can be great fun.

    ReplyDelete
  4. Son, I am NOT disappoint! Checking in to show some love ;)

    ReplyDelete
  5. Thanks for the love, I appreciate it I guess... no homo.
    ;)

    ReplyDelete
  6. damn, this makes my head hurt

    ReplyDelete
  7. Looks like this is going to go well.

    ReplyDelete
  8. Hexyn. It makes my head hurt too 0_o
    This is a good sign though, it shows that your brain is working, and eventually, it sinks in one way or another, depending on your learning style usually.

    ReplyDelete
  9. Cool stuff bro

    http://suptrollin.blogspot.com

    ReplyDelete
  10. looks confusing sir

    http://the-new-god.blogspot.com/

    but cool nonetheless i suppose..computers

    ReplyDelete
  11. I'll check out your blogs, thanks :)

    ReplyDelete
  12. Interesting...

    Followed
    -http://rogersroom001.blogspot.com/

    ReplyDelete
  13. Too confusing for me haha

    drockblock.blogspot.com

    ReplyDelete
  14. Lol most people wont understand shit you just posted! Love it! :D

    ReplyDelete
  15. Well, probably not, but I hope that it will eventually reach someone who could use the info.

    ReplyDelete
  16. I want to learn programming and emulators look like a nice, easy place to start. Thanks for the info, keep it up!

    soulaccelerant.blogspot.com

    ReplyDelete