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
wow you sure do know a lot about this stuff
ReplyDeleteNah, not really, this is all just learned from trial and error.
ReplyDeleteThanks though :)
keep up the good work
ReplyDeletei can't program for shit
I'm sure you could if you put your mind to it, I admit programming ain't easy, but it can be great fun.
ReplyDeleteBackup has arrived. ;)
ReplyDeleteYay :D
ReplyDeletethere you go luv :)
ReplyDeleteJust giving back the love!
ReplyDeleteThanks for the love, I appreciate it I guess... no homo.
ReplyDelete;)
damn, this makes my head hurt
ReplyDeleteLooks like this is going to go well.
ReplyDeleteHexyn. It makes my head hurt too 0_o
ReplyDeleteThis 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.
Cool stuff bro
ReplyDeletehttp://suptrollin.blogspot.com
looks confusing sir
ReplyDeletehttp://the-new-god.blogspot.com/
but cool nonetheless i suppose..computers
I'll check out your blogs, thanks :)
ReplyDeleteInteresting...
ReplyDeleteFollowed
-http://rogersroom001.blogspot.com/
amazing bro!
ReplyDeleteToo confusing for me haha
ReplyDeletedrockblock.blogspot.com
Lol most people wont understand shit you just posted! Love it! :D
ReplyDeleteWell, probably not, but I hope that it will eventually reach someone who could use the info.
ReplyDeleteI want to learn programming and emulators look like a nice, easy place to start. Thanks for the info, keep it up!
ReplyDeletesoulaccelerant.blogspot.com