• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
EngineProgrammer

OpenGL
3D algorithm

20 posts in this topic

Hey everyone, has been a while I've programmed but I'm back. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

I know that re-writing existing code is useless. But I don't mind to re-write some code, I want to learn how it's done.
I want to make a 3D polygon and draw it in C++, without using DirectX or OpenGL.
I've been looking around after source codes or tutorials but haven't found a single tutorial that helps me.

Very simple example of what I want to do:
[CODE]
m_Polygon = new Polygon(vertex1, vertex2, vertex3, vertex4); // where the vertexes have a X,Y,Z
m_Polygon->Draw();
[/CODE]

So I want to draw the 3D polygon in my 2D screen. Can someone help me with this. [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]


Thank you and kind regards,
Jonathan
0

Share this post


Link to post
Share on other sites
Look up coordinate transformations, perspective projection, and rasterization. Basically, you'll be reimplementing parts of the GPU pipeline, in the form of a software renderer. Knowledge of linear algebra helps a lot if you want to understand half of the code you're writing.

It typically goes roughly like this:
- transform each vertex to world coordinates, if it's not already done, this is useful if you want to have multiple instances of the same mesh without duplicating vertices, for instance suppose you want to have two bunnies side to side, you could duplicate each vertex to make two separate bunnies, or you could instead just reuse the same mesh, but simply moving each vertex in the second bunny off to the side (translation)
- transform each vertex to camera coordinates (rotating the world around the camera, so that you're facing what you want to face)
- transform each of those vertices to perspective coordinates (so that vertices further away from the camera tend to the point at infinity, giving the illusion of depth)
- from this, work out where each vertex would appear on the screen, in normalized screen coordinates (with coordinates ranging from -1 to +1 in X and Y dimensions) this is the step where you project your 3D vertices on your screen
- upscale these vertex locations to your desired resolution (e.g. 1024 * 768)
- for each pixel on the screen, work out which triangle it belongs to (depending on how you defined triangles, e.g. triangle list or triangle strip, or even using indices) this is the rasterization step
- shade the pixel (often you interpolate important data like normals, etc.. from the three vertices of the triangle)

As you can see, it can be a lot of work, and that's a very high-level overview (I ignored the depth and stencil buffers, as well as a lot of other stuff) but it's a good learning exercise. Make sure to take it step by step so that you don't get overwhelmed! Edited by Bacterius
2

Share this post


Link to post
Share on other sites
A billion percent thank you!

In Win32 there are some standard functions for example: SetWorldTransform, SetGrapicsMode, etc. and structs like XFORM.
Should I make any use of those functions and structs or should I rather make my own ones?


Kind regards,
Jonathan
0

Share this post


Link to post
Share on other sites
[quote name='EngineProgrammer' timestamp='1354769016' post='5007635']
In Win32 there are some standard functions for example: SetWorldTransform, SetGrapicsMode, etc. and structs like XFORM.
Should I make any use of those functions and structs or should I rather make my own ones?
[/quote]
I remember those functions, they seem to be remnants of some past era of software rendering using GDI (or they might be hardware-accelerated and used to draw desktop controls, I'm not sure). I wouldn't use them especially if you want to learn how transformation matrices work, since they hide all of that. All you really need is a form to display the results on, and a 2D array of pixels to render to, but of course you can use them if you find they are helpful. But you can absolutely do 100% of the rendering and computations without using built-in functions. You can even make your own vector and matrix class and roll with that, and I feel this is important if you've never done it before and are interested in low-level rendering.

It's up to you how deep you want to go. If you want to build on Win32 GDI to create your software renderer, you can absolutely do that and it's still fun (though you'll have to deal with the API, and you might not find as many tutorials as you'd like), or if you prefer to do everything from scratch, that's fun too but be ready to do a lot of linear algebra math!

By the way, re-writing existing code is not useless. It's important if you want to learn. Nobody would tell a beginner not to code Pong because it's already been done! It's only bad to reinvent the wheel if you want to get stuff done quickly, like a game. But If you're doing it for fun or for experience, reinventing the wheel is [i]the right thing to do[/i].

EDIT: The GDI API seems to be limited to 2D, so you might not find much use for GDI beyond accelerated pixel manipulation. I could be wrong though. Edited by Bacterius
1

Share this post


Link to post
Share on other sites
I'm looking up some information what GDI exactly is and this is a quote from wikipedia.
[quote]Simple games that do not require fast graphics rendering use GDI. However, GDI is relatively hard to use for advanced animation, and lacks a notion for synchronizing with individual video frames in the video card, lacks hardware rasterization for 3D, etc. Modern games usually use DirectX or OpenGL instead, which let programmers exploit the features of modern hardware.[/quote]

What makes DirectX a fast graphics renderer? How you decide the speed of rendering?
I just want to get low-level what programming concerns. I really want to get the knowledge into my head how it's done.
GDI only limited to 2D, how do I get the 3D in my program then? I'm assuming I need to calculate every pixel and draw them manually, please correct me if I'm wrong.

I want to achieve real-time rendering. Dragging a 3D model into your window and be able to translate, scale and rotate in your project.
But that is not for now, first the baby steps. But I can't do this alone sadly enough. Need some help from people who have experience in this because I can't find almost anything that helps me. maybe I'm not looking in the right place. [img]http://public.gamedev.net//public/style_emoticons/default/unsure.png[/img]


Kind regards,
Jonathan
0

Share this post


Link to post
Share on other sites
Your graphics card already has all the steps I highlighted in my first post implemented, in hardware (not software). This means you can pump ridiculous amounts of triangles in it, and shade them in complex ways with multiple texturing and stuff and it'll still run in realtime. Furthermore, your graphics card is the component that's connected to your display monitor, and since the frame that's about to be rendered exists on the graphics card, it's very easy to send it to the screen. On the other hand, if you're using software rendering, then the frame is in system memory, and must be sent to the graphics card before being displayed, which is quite expensive (CPU to GPU transfers and vice versa are quite slow).

If you want to achieve realtime rendering, that is [i]possible[/i] to some extent on a software renderer, but you certainly will not achieve the same amount of speed that can be achieved on a dedicated graphics card, and not with ease. The hardware is just superior in terms of performance. Unfortunately, you cannot really learn from hardware, so if you really want to know how it's done, you want to look at software rendering, which is great for learning, but remember it'll probably be quite slow without using a lot of optimization tricks and not using too many triangles.

Now it depends on your definition of "real-time" and "3D model". If you're talking about white triangles on black screen, on a model with like 10000 triangles, then sure - easily real-time. But if you want to draw a nice 1M polygon model with texturing and lighting... it's going to be harder for your software renderer than for the graphics card.

This is not to discourage you - graphics cards are [i]designed[/i] with graphics performance in mind and are meant to be faster than processors for these kinds of operations, that's why you don't see a lot of software rendering around. But to learn the inner workings of graphics cards, there's nothing better.
0

Share this post


Link to post
Share on other sites
I would like to write a small copy of DirectX.[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] How do they handle the vertices and rendering of 3D models? How should it be handled for an optimal framework/engine?
If my GPU can handle all the math, how can I render my 3D model with it? I still need to use some math from your first post or don't I?

I'm looking up coordinate transformations and it's quite fun. [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]
And how about particles? Like when I have 1 million particles. How should I best handle them? Do I need to do all calculations and rendering on the GPU? Or are there better/faster ways?

You are helping me already but I'm still a bit confused about how it all works with the gpu / cpu thing. I want to get an optimal render system for low poly models with materials, shaders, lightning, bouncings, etc. I know the pipeline for the graphics rendering but I can't go any further than the first step at the moment. For example I want to be able to load a whole environment like COD with all players, bullets, effects in it.


Kind regards,
Jonathan
0

Share this post


Link to post
Share on other sites
[quote name='EngineProgrammer' timestamp='1354813004' post='5007805']
I would like to write a small copy of DirectX.[img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] How do they handle the vertices and rendering of 3D models? How should it be handled for an optimal framework/engine?
If my GPU can handle all the math, how can I render my 3D model with it? I still need to use some math from your first post or don't I?

I'm looking up coordinate transformations and it's quite fun. [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]
And how about particles? Like when I have 1 million particles. How should I best handle them? Do I need to do all calculations and rendering on the GPU? Or are there better/faster ways?

You are helping me already but I'm still a bit confused about how it all works with the gpu / cpu thing. I want to get an optimal render system for low poly models with materials, shaders, lightning, bouncings, etc. I know the pipeline for the graphics rendering but I can't go any further than the first step at the moment. For example I want to be able to load a whole environment like COD with all players, bullets, effects in it.


Kind regards,
Jonathan
[/quote]

So basically you want to write a software renderer which runs on the GPU right?
Remember that DirectX and OpenGL can communicate with your graphics driver, and I'm afraid you as a developer won't have that luxury (if you want to call it that). This only leaves you with the option to resort to GPGPU solutions, and while it's completely possible to write a software renderer with these, you probably won't be able to beat or even come near the performance a library like DirectX or OpenGL will give you. The reason for this is that DirectX and OpenGL are able to use the rasterizer hardware available on your GPU, while GPGPU solutions aren't able to do so.


Now the essential question here (before I ramble on) is: Why do you want do write your own renderer?

If this is just for learning I'd say try to do a very simple CPU-based renderer and leave the GPU out of the picture (except for maybe presenting your final result to the screen), this will teach you a lot about how the entire rasterization process works without having to worry about CPU-GPU communication and all that stuff. Try to implement each of the steps Bacterius laid out for you in his first post completely by yourself, from writing your own structures for managing vertices and indices to writing systems which can transform and shade these for you to get a final texture which you can then present to the screen.
Writing a simple software renderer which can do all of this is a very rewarding and fun experience which will teach you a lot. If you implement this well you could even use it do some very basic 3D games.

If this is about writing a production-quality renderer I'm going to be harsh and say: don't bother.
You're talking about rendering millions of particles, and rendering a huge amount of objects, so I'm guessing that this is actually what you want to do. I know re-inventing the wheel can be a lot of fun sometimes, but trying to implement something like this will put you in a lot of nasty sticky situations, and in the end you'll have a system which tries to sub-optimally solve a problem for which we already have 2 fast and proven solutions (being DirectX and OpenGL), if you even manage to complete your renderer at all (and I'm going to be harsh again and say that this is very unlikely as you don't have any previous experience writing software renderers). Edited by Radikalizm
0

Share this post


Link to post
Share on other sites
If you want to do simple 3d lines and such, an easy way to get going is to try simple perspective. This is what I played around with before I went into matrix math. You will probably get into matrices at some point, but if you just want something basic, you can start with this.

3d without rotation or translation:

A single perspective transform is as easy as this:

//sw is screen width, sh is screen height
//zcut is z plane cutoff (don't render anything closer to or behind z) WHY? z=0 is forbidden. z > 1 means point is in front of camera. z=-1 means point is behind camera. zcutoff of .1, .01 etc are reasonable.

[code]
int perspective(float x, float y, float z, int* sx, int* sy)
{
if (z < zcutoff)
return 0; //refuse to transform

sx = x* sw / z; //scale X by screen height and distance.
//So, an object sw pixels wide at Z=1 is the width of the screen.
//Z=2, half the widht of the screen. Z=.5, twice the width of the screen. etc
sy = y* sh / z;
return 1;
}
[/code]

To draw a line in 3d:
[code]
void line3d( float x, float y, float z, float x2, float y2, float z2)
{
int sx, sy, sx2, sy2;
//only draws if both points are in front of camera
//later, if you want to get fancy, if one is in front, and the other is behind, you clip at z=zcutoff

if (perspective(x,y,z, &sx, &sy) && perspective(x2,y2,z2,&sx2, &sy2))
draw_line(sx,sy,sx2,sy2);
}
[/code]

With the above snippets, you should be able to draw a 3d perspective object from the point of view of the origin.

To move the camera around, just subtract the camera position from the coordinates:

[code]
int perspective(float x, float y, float z, int* sx, int* sy)
{

x -= camera_x;
y -= camera_y
z -= camera_z;

if (z < zcutoff)
return 0; //refuse to transform
sx = x* sw / z; //scale X by screen height and distance.
//So, an object sw pixels wide at Z=1 is the width of the screen.
//Z=2, half the widht of the screen. Z=.5, twice the width of the screen. etc
sy = y* sh / z;
return 1;
}
[/code]

With that, you should be able to move around the 3d environment, but view is constrained to always looking down the Z axis. But, its a start.

The last thing you can do, is allow camera rotation along the y axis (like wolf3d). It's been a while but if [url="http://www.siggraph.org/education/materials/HyperGraph/modeling/mod_tran/2drota.htm"]http://www.siggraph....tran/2drota.htm[/url] is correct, then:

[code]
int perspective(float x, float y, float z, int* sx, int* sy)
{

float xr, yr, zr;
//translate to camera position

x -= camera_x;
y-= camera_y
z -= camera_z;


//rotate 2D about y axis:

xr = x * cos(camera_angle) - z * sin(camera_angle);
zr = z* cos(camera_angle) + x * sin(camera_angle);
yr = y; // height does not change



if (zr < zcutoff)
return 0; //refuse to transform
sx = xr* sw / zr; //scale X by screen height and distance.
//So, an object sw pixels wide at Z=1 is the width of the screen.
//Z=2, half the widht of the screen. Z=.5, twice the width of the screen. etc
sy = yr* sh / zr;
return 1;
}

[/code]

That should give you 5 degrees of freedom: You can move up/down, left/right, forward/back and rotate about Y. So, it's 'DOOM' controls. You can add another rotation to look up/down, but at the point you should consider trying to understand matrices. Edited by DracoLacertae
1

Share this post


Link to post
Share on other sites
I wrote a software renderer to the OpenGL 1 Spec last year, compiled it and linked it to QuakeGL. It worked pretty well.

This is the book that made software rendering click for me: http://www.amazon.com/Building-3D-Game-Engine-C/dp/0471123269/ref=sr_1_6?ie=UTF8&qid=1354824709&sr=8-6&keywords=3d+game+engine+programming+C%2B%2B

It's pretty concise and straight forward. Not one of those books that you want to copy the code verbatim out of due to it's age. You really need to read it and understand it. This is what i had after a few weeks with the book: http://www.youtube.com/watch?v=Xz4k37mQsyU&feature=player_embedded
0

Share this post


Link to post
Share on other sites
That's pretty cool. Writing a software renderer can be fun. I wrote my first 3d programs in dos qbasic using the simple perspective and rotation transforms I posted up above. Just lines and such. Made little wireframe cube mazes to walk through. Later I moved onto Borland C and messed around with filling triangles with solid colors, and tried some texturing, which I could get on the walls but never worked properly on the floors. I made the jump to OpenGL, and had to learn matrices. I did eventually play around with software rendering again, and figured out the floors, but it was so much slower than hardware.

I resisted matrices for a while because they didn't make sense: 4x4 for 3d? 4 dimensions? It didn't make sense. When I first started using matrices, I started with translation + 3x3 matrices. A lot of my code still uses 3x3 matrices to track rotations. They make sense for 3d: for each axis in the rotated frame, there are 3 components in 'world' coordinates that make up those axes vectors. Now add in a translation. Now you have a 3x3 matrix and an awkward random 'add' for the translation. But, it just so happens if you put that 3x3 matrix inside a 4x4 matrix, put the translation on the side, and then pad out the rest of it with 0's or 1's, doing the 4x4 matrix multiply will do the same thing as the 3x3 matrix multiply, and then add the translation on. It's just a really cheap trick. Now you just get your CPU or GPU todo 4x4 matrix multiplies really fast, and then all your transform operations can be done by one heavy optimized math routine.
0

Share this post


Link to post
Share on other sites
Thanks everyone!

Radikalizm, I'm not trying to make a commercial software renderer [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img] DirectX and OpenGL can't be beaten so I won't try to.
It's all learning purpose. You say that I first need to work with the CPU, how do I do that? How can I choose which one I can use to get my project running? I know the difference between both tho, cpu only does 1 thing at the time while a gpu does multiple things at the same time. But no idea when I'm using a cpu or a gpu.

DracoLacertae, thanks for the examples! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img] So drawing lines isn't that hard to get in 3D [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img] Before I get on to it, I bought a math book which covers every mathematics what game programming concerns. So I hope to get a line or a 3D polygon on my screen today!

uglybdavis, nicely done! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
[quote]It's all learning purpose. You say that I first need to work with the CPU, how do I do that? How can I choose which one I can use to get my project running? I know the difference between both tho, cpu only does 1 thing at the time while a gpu does multiple things at the same time. But no idea when I'm using a cpu or a gpu.[/quote]
If you're not sure, you're definitely using a CPU :)

You only "use" the GPU (at least the current generation) by programming little programs called shaders (in HLSL, or GLSL) which perform very specific tasks (e.g. transform vertices, or shade pixels). All the rest is done by the driver automatically. You don't actually write complete software on it. Everything that you code in C++ or C# or Java or whatever language, really, is done on your CPU.
0

Share this post


Link to post
Share on other sites
[quote name='Bacterius' timestamp='1354853787' post='5007989']
[quote]It's all learning purpose. You say that I first need to work with the CPU, how do I do that? How can I choose which one I can use to get my project running? I know the difference between both tho, cpu only does 1 thing at the time while a gpu does multiple things at the same time. But no idea when I'm using a cpu or a gpu.[/quote]
If you're not sure, you're definitely using a CPU [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

You only "use" the GPU (at least the current generation) by programming little programs called shaders (in HLSL, or GLSL) which perform very specific tasks (e.g. transform vertices, or shade pixels). All the rest is done by the driver automatically. You don't actually write complete software on it. Everything that you code in C++ or C# or Java or whatever language, really, is done on your CPU.
[/quote]
Damn, I messed up my mind then. Thank you for clearing this out! [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
0

Share this post


Link to post
Share on other sites
That's why they introduced technologies like C++ AMP and CUDA which are basically extensions on top of C++, so that you don't have to bother with DirectX and OpenGL and their shaders. With these "extensions" you can just write your whole code in C++ and launch parallel kernels which are executed on the GPU.

But just start with a standard programming language running on solely the CPU and get your software rasterizer working on the CPU before you bother using a GPU to increase the performance. Edited by CryZe
0

Share this post


Link to post
Share on other sites
Is Direct3D a separate set of API calls still or is it just all part of the same interface to DirectX? I would think getting a polygon on the screen in DirectX would be fairly simple.

I do recognize "SetGraphicsMode" as possibly a WinG call from back when keyboards were made with dinosaur bones. [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
-1

Share this post


Link to post
Share on other sites
[quote name='EngineProgrammer' timestamp='1354850861' post='5007980']
I know the difference between both tho, cpu only does 1 thing at the time while a gpu does multiple things at the same time.
[/quote]

A CPU can do multiple things at a time when using multiple cores (and it can 'fake' doing multiple things on only one core), but it does indeed do this differently than a GPU :)
Modern GPUs are designed to handle wide SIMD instructions (doing the same operation on a larger set of data in parallel), hence their ability to do extremely fast floating point operations when compared to CPUs. Modern CPUs have SIMD instructions too though (like SSE on x86 architectures), but these are for smaller sets of data (mostly 128 bits).

Keep us posted on your progress by the way, I'm always very interested in these kinds of projects.
0

Share this post


Link to post
Share on other sites
I will gladly post my progress but it will take a while, today I realized I better learn matrices through 2D and when I understand everything I'll change to 3D.

I'm making my own matrix class which will be supported with the function SetWorldTransform. This function works with XFORM but I'm not using this one. It's better to write my own calculations self so I can learn from it. When I got it all calculated I just put everything into an XFORM and transform the world. So when I understand the matrix and transform concept I go to 3D. [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Or is there a better way to transform my object(rectangle, bitmap, etc)?
I know matrices needs to be used to transform such an object, but I mean, is there something else to transform my world with?
0

Share this post


Link to post
Share on other sites
You might want to consider decoupling your renderer from systems such as GDI and use it purely as a back-end to present your final image, this will give you a cleaner system which is somewhat future-proof as GDI can be considered old right now.
You could eventually use a system like D2D as a back-end to present your image to the screen as well without having to make any changes to your renderer.

Have you ever written shaders before? If you haven't I'd advise to try and do some simple shaders in DirectX or OpenGL to get a feel for transformations and rasterization in general. They're a great instant-gratification kind of way to learn about all of this stuff, and it will probably make it easier for your to understand the steps you should take for a software rasterizer.
With a knowledge of shaders the concept of how to build systems for setting your own transformation matrices for your renderer becomes a piece of cake as well.

I would however be careful about writing your own matrix and vector implementations. They can be a great learning experience, but it's very easy to make small errors here an there when it comes to operations like matrix-matrix multiplications or matrix-vector multiplications, and those errors can introduce huge bugs in your renderer.
So if you intend to take the DIY route when it comes to math, be sure to write a ton of test cases before you actually start working with it, you want to make sure your math library completely works as intended.
2

Share this post


Link to post
Share on other sites
Thanks for the help, I've made a colorshader last year, but I didn't understood a single line of code what I was writing. Shaders are quite hard to understand for me.

How does DirectX render his transformations?
I just made a matrix class that can translate, scale and rotate. Tested it and it works.
Don't mind how I use the methods. It's just a testing project.
[CODE]
Matrix matTranslate, matRotate, matScale, matWorld;
matTranslate.SetAsTranslate(-0.7f, -0.4f);
matRotate.SetAsRotate(80.0);
matScale.SetAsScale(0.5,0.5);
matWorld = matRotate * matScale * matTranslate;
Matrix::SetAsWorld(hDC, matWorld);

Rectangle(hDC, -1, -1, 1, 1);
[/CODE]

This is what I use for my transformations:
[CODE]
Matrix::Matrix():
eM11(0.0f), eM12(0.0f), eM13(0.0f),
eM21(0.0f), eM22(0.0f), eM23(0.0f),
eM31(0.0f), eM32(0.0f), eM33(0.0f)
{
}

void Matrix::SetAsTranslate(float x, float y)
{
eM11 = 1.0f;
eM12 = 0.0f;
eM13 = 0.0f;
eM21 = 0.0f;
eM22 = 1.0f;
eM23 = 0.0f;
eM31 = x;
eM32 = y;
eM33 = 1.0f;
}

void Matrix::SetAsRotate(float radians)
{
eM11 = (float)cos(radians);
eM12 = (float)sin(radians);
eM13 = 0.0f;
eM21 = (float)-sin(radians);
eM22 = (float)cos(radians);
eM23 = 0.0f;
eM31 = 0.0f;
eM32 = 0.0f;
eM33 = 1.0f;
}

void Matrix::SetAsRotate(double degrees)
{
float radians = (float)(degrees/180 * M_PI);
eM11 = (float)cos(radians);
eM12 = (float)sin(radians);
eM13 = 0.0f;
eM21 = (float)-sin(radians);
eM22 = (float)cos(radians);
eM23 = 0.0f;
eM31 = 0.0f;
eM32 = 0.0f;
eM33 = 1.0f;
}

void Matrix::SetAsScale(float x, float y)
{
eM11 = x;
eM12 = 0.0f;
eM13 = 0.0f;
eM21 = 0.0f;
eM22 = y;
eM23 = 0.0f;
eM31 = 0.0f;
eM32 = 0.0f;
eM33 = 1.0f;
}

Matrix operator*(const Matrix& ref1, const Matrix& ref2)
{
Matrix mat;
mat.eM11 = ref1.eM11 * ref2.eM11 + ref1.eM12 * ref2.eM21 + ref1.eM13 * ref2.eM31;
mat.eM12 = ref1.eM11 * ref2.eM12 + ref1.eM12 * ref2.eM22 + ref1.eM13 * ref2.eM32;
mat.eM13 = ref1.eM11 * ref2.eM13 + ref1.eM12 * ref2.eM23 + ref1.eM13 * ref2.eM33;
mat.eM21 = ref1.eM21 * ref2.eM11 + ref1.eM22 * ref2.eM21 + ref1.eM23 * ref2.eM31;
mat.eM22 = ref1.eM21 * ref2.eM12 + ref1.eM22 * ref2.eM22 + ref1.eM23 * ref2.eM32;
mat.eM23 = ref1.eM21 * ref2.eM13 + ref1.eM22 * ref2.eM23 + ref1.eM23 * ref2.eM33;
mat.eM31 = ref1.eM31 * ref2.eM11 + ref1.eM32 * ref2.eM21 + ref1.eM33 * ref2.eM31;
mat.eM32 = ref1.eM31 * ref2.eM12 + ref1.eM32 * ref2.eM22 + ref1.eM33 * ref2.eM32;
mat.eM33 = ref1.eM31 * ref2.eM13 + ref1.eM32 * ref2.eM23 + ref1.eM33 * ref2.eM33;

return mat;
}

void Matrix::SetAsWorld(HDC hDC, const Matrix& mat)
{
XFORM form;
form.eM11 = mat.eM11;
form.eM12 = mat.eM12;
form.eM21 = mat.eM21;
form.eM22 = mat.eM22;
form.eDx = mat.eM31;
form.eDy = mat.eM32;

SetWorldTransform(hDC, &form);
}
[/CODE]

The calculations I've written on paper first with help from a book. So I didn't copied anything from the internet. I'm getting the hang of matrices very fast with this book, next chapter is linear transformations! [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]

But indeed, I'm using GDI and it's getting pretty classic. But I really don't want to use a single external library. Because it's too "easy" then.. [img]http://public.gamedev.net//public/style_emoticons/default/laugh.png[/img] I'm not trying to be stubborn. Any suggestions how I avoid the classic functions? DirectX also must use a classic way or are they working very low level and code all their rendering themselves?
0

Share this post


Link to post
Share on other sites
Take your mind off of GDI for now, completely forget it exists as you won't be needing it if you want to build your renderer from the ground up (unless you want to use it to present your final image to a window).

Let's imagine this situation:
You have a 3D model you want to render, let's make it a cube to keep things simple. Let's assume your model is completely made up out of vertices, we're leaving indices behind for the time being. To make things even simpler we're just going to assume that your vertices are just plain old positions (being 3D vectors), we're not going to worry about things such as normals, UV-coords, colors, etc., just plain old positions.

Now let's say you want to render this model at a certain position with a certain rotation and a certain scale. You also want all the pixels occupied by this model on-screen to have a certain color, let's take red for example.

Let's have a look at the requirements to realize all of this, you'll need:[list=1]
[*]A data structure which can contain your model's info. In our simple setting here this can be a plain old array containing vertices, and as mentioned our vertices are regular positions right now. We're going to assume each 3 following vertices will make up a triangle.
[*]A way of representing where you want your model to end up relative to the world center, and which rotation and scale it should have. This is your [b]world transformation [/b]as you have probably already figured out by yourself. This world transformation is a single matrix containing all of these 3 aspects at once.
[*]A way of representing where [b]you[/b] are in the world and how you're looking at the world. This is mostly abstracted away behind the concept of a [b]camera[/b], which is a data structure which holds its own world position, look-at vector and a vector which defines which way is up. These 3 vectors will be used to generate our second important transformation matrix, being the [b]view transformation[/b].
[*]A way of projecting what we 'see' through our camera onto our final image. Projections can be done in a lot of ways to achieve different results, but what you probably want is a [url="http://en.wikipedia.org/wiki/3D_projection#Perspective_projection"]perspective projection[/url]. Such a perspective projection is defined mostly by a field of view (FoV) and an aspect ratio. This information will be stored in our final important transformation matrix, being the [b]projection transformation[/b]. This projection transformation will map a 3D position to a 2D position where each component ranges between [-1, 1]
[*]A way of storing our final image in full color. This can be done by creating a [b]texture[/b] data structure, which basically is just a 2D array of arbitrary values with a resolution of your choosing (this will be the resolution of your final image). How wide these values are, and how many values you need to define a single color-element is defined by the color format you're going to be using. For simple applications the R8G8B8 format, which is a format which defines 3 color channels per color element (red, green and blue) each with 8-bits (being 1 byte) per channel, will do just fine. You'll be creating such a texture which will act as a screen buffer for you to render to.
[/list]

Ok so, now we've defined our requirements, the only thing we need now is an overview of how we're going to use these to get our final image from our model.
I'll provide you with a simplified overview of what you should do:[list]
[*]Tell our renderer that we want to render to our screen buffer (see #5 in our previous overview). The renderer could've created this screen buffer itself, or you could create it yourself and pass it on (eg. renderer->setRenderTarget(some_texture))
[*]We now want to get our screen-coordinates for each vertex in our model, this in itself happens in a few steps. I'm going to give you some pseudo-code to explain the process:
[/list]
[source lang="plain"]for each vertex in our model do
position = transform(vertex_position, world_transformation) // This transforms our vertex from local space to world space
position = transform(position, view_transformation) // This transforms our world space position to a view space (camera or eye space) position
position = transform(position, projection_transformation) // This transforms our view space position to a screen space position
[/source][list]
[*]This gives us a bunch of positions of which we only want the first 2 components (X and Y) right now for our simple setup. As mentioned X and Y will both be in the range [-1, 1], but this won't do if we want to determine which pixels we want to write to. To fix this we're going to apply 2 simple transformations. The first one will transform our range from [-1, 1] to [0, 1]; you do this by applying this formula: n = (n + 1) * 0.5. Our second transformation will scale up our [0, 1] range to our chosen screen buffer resolution, so this is just a simple multiplication of your X and Y components by your screen buffer width and height respectively.
[*]We now have a bunch of screen coordinates which directly map to pixels in our screen buffer, this means we can now set colors for the pixels we want to write to. We assumed that our vertices would be ordered so they would make up triangles, so for each group of 3 positions we'll first have to create a triangle. Once we have this triangle we only need to go over it's surface to determine which pixels the triangle should cover. Remember that we just wanted to color everything red, so for all pixels making up our triangle we'll set the red channel to 255 (we're working in RGB8!) in our screen buffer.
[*]Once you've done this for every group of 3 transformed vertices your screen buffer will now contain your projected image of your cube model. The only thing left to do now is to present it to the screen, which is where a library like GDI or D2D can come into play.
[/list]

That about covers it I think, could be that I left out a few details or that I made some errors, but please don't shoot me for that.

EDIT:

I want to make note of some things I left out which were not needed for such an extremely simple example, but which will play a major part once you get further in your renderer. To name a few:
-Usage of a Z-buffer for depth testing (really important for ordering and avoiding overdraw when rendering multiple objects)
-Indices (all kinds of uses, from determining triangle winding order to reducing vertex buffer footprints)
-Materials, lighting, texturing and all that stuff
-Culling and clipping
-Probably a million more things which I can't immediately think of right now Edited by Radikalizm
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By sandor_deak
      Hi,
      I started to learn OpenGL about 3 months ago and I've been working on a game engine which uses tools from Riemannian geometry to display curved spaces. Here is a link to a video. (It has a lot to improve yet.) I'm a mathematician but I would like to work in the game industry in a programmer role and I'm making this engine to build my portfolio. I would appreciate some feedback and opinions about the engine, how "convincing" it is as a portfolio, and also some advice about the game industry. Thanks a lot!
    • By Toastmastern
      So it's been a while since I took a break from my whole creating a planet in DX11. Last time around I got stuck on fixing a nice LOD.
      A week back or so I got help to find this:
      https://github.com/sp4cerat/Planet-LOD
      In general this is what I'm trying to recreate in DX11, he that made that planet LOD uses OpenGL but that is a minor issue and something I can solve. But I have a question regarding the code
      He gets the position using this row
      vec4d pos = b.var.vec4d["position"]; Which is then used further down when he sends the variable "center" into the drawing function:
      if (pos.len() < 1) pos.norm(); world::draw(vec3d(pos.x, pos.y, pos.z));  
      Inside the draw function this happens:
      draw_recursive(p3[0], p3[1], p3[2], center); Basically the 3 vertices of the triangle and the center of details that he sent as a parameter earlier: vec3d(pos.x, pos.y, pos.z)
      Now onto my real question, he does vec3d edge_center[3] = { (p1 + p2) / 2, (p2 + p3) / 2, (p3 + p1) / 2 }; to get the edge center of each edge, nothing weird there.
      But this is used later on with:
      vec3d d = center + edge_center[i]; edge_test[i] = d.len() > ratio_size; edge_test is then used to evaluate if there should be a triangle drawn or if it should be split up into 3 new triangles instead. Why is it working for him? shouldn't it be like center - edge_center or something like that? Why adding them togheter? I asume here that the center is the center of details for the LOD. the position of the camera if stood on the ground of the planet and not up int he air like it is now.

      Full code can be seen here:
      https://github.com/sp4cerat/Planet-LOD/blob/master/src.simple/Main.cpp
      If anyone would like to take a look and try to help me understand this code I would love this person. I'm running out of ideas on how to solve this in my own head, most likely twisted it one time to many up in my head
      Thanks in advance
      Toastmastern
       
       
    • By fllwr0491
      I googled around but are unable to find source code or details of implementation.
      What keywords should I search for this topic?
      Things I would like to know:
      A. How to ensure that partially covered pixels are rasterized?
         Apparently by expanding each triangle by 1 pixel or so, rasterization problem is almost solved.
         But it will result in an unindexable triangle list without tons of overlaps. Will it incur a large performance penalty?
      B. A-buffer like bitmask needs a read-modiry-write operation.
         How to ensure proper synchronizations in GLSL?
         GLSL seems to only allow int32 atomics on image.
      C. Is there some simple ways to estimate coverage on-the-fly?
         In case I am to draw 2D shapes onto an exisitng target:
         1. A multi-pass whatever-buffer seems overkill.
         2. Multisampling could cost a lot memory though all I need is better coverage.
            Besides, I have to blit twice, if draw target is not multisampled.
       
    • By mapra99
      Hello

      I am working on a recent project and I have been learning how to code in C# using OpenGL libraries for some graphics. I have achieved some quite interesting things using TAO Framework writing in Console Applications, creating a GLUT Window. But my problem now is that I need to incorporate the Graphics in a Windows Form so I can relate the objects that I render with some .NET Controls.

      To deal with this problem, I have seen in some forums that it's better to use OpenTK instead of TAO Framework, so I can use the glControl that OpenTK libraries offer. However, I haven't found complete articles, tutorials or source codes that help using the glControl or that may insert me into de OpenTK functions. Would somebody please share in this forum some links or files where I can find good documentation about this topic? Or may I use another library different of OpenTK?

      Thanks!
    • By Solid_Spy
      Hello, I have been working on SH Irradiance map rendering, and I have been using a GLSL pixel shader to render SH irradiance to 2D irradiance maps for my static objects. I already have it working with 9 3D textures so far for the first 9 SH functions.
      In my GLSL shader, I have to send in 9 SH Coefficient 3D Texures that use RGBA8 as a pixel format. RGB being used for the coefficients for red, green, and blue, and the A for checking if the voxel is in use (for the 3D texture solidification shader to prevent bleeding).
      My problem is, I want to knock this number of textures down to something like 4 or 5. Getting even lower would be a godsend. This is because I eventually plan on adding more SH Coefficient 3D Textures for other parts of the game map (such as inside rooms, as opposed to the outside), to circumvent irradiance probe bleeding between rooms separated by walls. I don't want to reach the 32 texture limit too soon. Also, I figure that it would be a LOT faster.
      Is there a way I could, say, store 2 sets of SH Coefficients for 2 SH functions inside a texture with RGBA16 pixels? If so, how would I extract them from inside GLSL? Let me know if you have any suggestions ^^.
  • Popular Now