• 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 DaniDesu
      #include "MyEngine.h" int main() { MyEngine myEngine; myEngine.run(); return 0; } MyEngine.h
      #pragma once #include "MyWindow.h" #include "MyShaders.h" #include "MyShapes.h" class MyEngine { private: GLFWwindow * myWindowHandle; MyWindow * myWindow; public: MyEngine(); ~MyEngine(); void run(); }; MyEngine.cpp
      #include "MyEngine.h" MyEngine::MyEngine() { MyWindow myWindow(800, 600, "My Game Engine"); this->myWindow = &myWindow; myWindow.createWindow(); this->myWindowHandle = myWindow.getWindowHandle(); // Load all OpenGL function pointers for use gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); } MyEngine::~MyEngine() { this->myWindow->destroyWindow(); } void MyEngine::run() { MyShaders myShaders("VertexShader.glsl", "FragmentShader.glsl"); MyShapes myShapes; GLuint vertexArrayObjectHandle; float coordinates[] = { 0.5f, 0.5f, 0.0f, 0.5f, -0.5f, 0.0f, -0.5f, 0.5f, 0.0f }; vertexArrayObjectHandle = myShapes.drawTriangle(coordinates); while (!glfwWindowShouldClose(this->myWindowHandle)) { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw something glUseProgram(myShaders.getShaderProgram()); glBindVertexArray(vertexArrayObjectHandle); glDrawArrays(GL_TRIANGLES, 0, 3); glfwSwapBuffers(this->myWindowHandle); glfwPollEvents(); } } MyShaders.h
      #pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> #include "MyFileHandler.h" class MyShaders { private: const char * vertexShaderFileName; const char * fragmentShaderFileName; const char * vertexShaderCode; const char * fragmentShaderCode; GLuint vertexShaderHandle; GLuint fragmentShaderHandle; GLuint shaderProgram; void compileShaders(); public: MyShaders(const char * vertexShaderFileName, const char * fragmentShaderFileName); ~MyShaders(); GLuint getShaderProgram(); const char * getVertexShaderCode(); const char * getFragmentShaderCode(); }; MyShaders.cpp
      #include "MyShaders.h" MyShaders::MyShaders(const char * vertexShaderFileName, const char * fragmentShaderFileName) { this->vertexShaderFileName = vertexShaderFileName; this->fragmentShaderFileName = fragmentShaderFileName; // Load shaders from files MyFileHandler myVertexShaderFileHandler(this->vertexShaderFileName); this->vertexShaderCode = myVertexShaderFileHandler.readFile(); MyFileHandler myFragmentShaderFileHandler(this->fragmentShaderFileName); this->fragmentShaderCode = myFragmentShaderFileHandler.readFile(); // Compile shaders this->compileShaders(); } MyShaders::~MyShaders() { } void MyShaders::compileShaders() { this->vertexShaderHandle = glCreateShader(GL_VERTEX_SHADER); this->fragmentShaderHandle = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(this->vertexShaderHandle, 1, &(this->vertexShaderCode), NULL); glShaderSource(this->fragmentShaderHandle, 1, &(this->fragmentShaderCode), NULL); glCompileShader(this->vertexShaderHandle); glCompileShader(this->fragmentShaderHandle); this->shaderProgram = glCreateProgram(); glAttachShader(this->shaderProgram, this->vertexShaderHandle); glAttachShader(this->shaderProgram, this->fragmentShaderHandle); glLinkProgram(this->shaderProgram); return; } GLuint MyShaders::getShaderProgram() { return this->shaderProgram; } const char * MyShaders::getVertexShaderCode() { return this->vertexShaderCode; } const char * MyShaders::getFragmentShaderCode() { return this->fragmentShaderCode; } MyWindow.h
      #pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> class MyWindow { private: GLFWwindow * windowHandle; int windowWidth; int windowHeight; const char * windowTitle; public: MyWindow(int windowWidth, int windowHeight, const char * windowTitle); ~MyWindow(); GLFWwindow * getWindowHandle(); void createWindow(); void MyWindow::destroyWindow(); }; MyWindow.cpp
      #include "MyWindow.h" MyWindow::MyWindow(int windowWidth, int windowHeight, const char * windowTitle) { this->windowHandle = NULL; this->windowWidth = windowWidth; this->windowWidth = windowWidth; this->windowHeight = windowHeight; this->windowTitle = windowTitle; glfwInit(); } MyWindow::~MyWindow() { } GLFWwindow * MyWindow::getWindowHandle() { return this->windowHandle; } void MyWindow::createWindow() { // Use OpenGL 3.3 and GLSL 3.3 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); // Limit backwards compatibility glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // Prevent resizing window glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); // Create window this->windowHandle = glfwCreateWindow(this->windowWidth, this->windowHeight, this->windowTitle, NULL, NULL); glfwMakeContextCurrent(this->windowHandle); } void MyWindow::destroyWindow() { glfwTerminate(); } MyShapes.h
      #pragma once #include <glad\glad.h> #include <GLFW\glfw3.h> class MyShapes { public: MyShapes(); ~MyShapes(); GLuint & drawTriangle(float coordinates[]); }; MyShapes.cpp
      #include "MyShapes.h" MyShapes::MyShapes() { } MyShapes::~MyShapes() { } GLuint & MyShapes::drawTriangle(float coordinates[]) { GLuint vertexBufferObject{}; GLuint vertexArrayObject{}; // Create a VAO glGenVertexArrays(1, &vertexArrayObject); glBindVertexArray(vertexArrayObject); // Send vertices to the GPU glGenBuffers(1, &vertexBufferObject); glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject); glBufferData(GL_ARRAY_BUFFER, sizeof(coordinates), coordinates, GL_STATIC_DRAW); // Dertermine the interpretation of the array buffer glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void *)0); glEnableVertexAttribArray(0); // Unbind the buffers glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); return vertexArrayObject; } MyFileHandler.h
      #pragma once #include <cstdio> #include <cstdlib> class MyFileHandler { private: const char * fileName; unsigned long fileSize; void setFileSize(); public: MyFileHandler(const char * fileName); ~MyFileHandler(); unsigned long getFileSize(); const char * readFile(); }; MyFileHandler.cpp
      #include "MyFileHandler.h" MyFileHandler::MyFileHandler(const char * fileName) { this->fileName = fileName; this->setFileSize(); } MyFileHandler::~MyFileHandler() { } void MyFileHandler::setFileSize() { FILE * fileHandle = NULL; fopen_s(&fileHandle, this->fileName, "rb"); fseek(fileHandle, 0L, SEEK_END); this->fileSize = ftell(fileHandle); rewind(fileHandle); fclose(fileHandle); return; } unsigned long MyFileHandler::getFileSize() { return (this->fileSize); } const char * MyFileHandler::readFile() { char * buffer = (char *)malloc((this->fileSize)+1); FILE * fileHandle = NULL; fopen_s(&fileHandle, this->fileName, "rb"); fread(buffer, this->fileSize, sizeof(char), fileHandle); fclose(fileHandle); buffer[this->fileSize] = '\0'; return buffer; } VertexShader.glsl
      #version 330 core layout (location = 0) vec3 VertexPositions; void main() { gl_Position = vec4(VertexPositions, 1.0f); } FragmentShader.glsl
      #version 330 core out vec4 FragmentColor; void main() { FragmentColor = vec4(1.0f, 0.0f, 0.0f, 1.0f); } I am attempting to create a simple engine/graphics utility using some object-oriented paradigms. My first goal is to get some output from my engine, namely, a simple red triangle.
      For this goal, the MyShapes class will be responsible for defining shapes such as triangles, polygons etc. Currently, there is only a drawTriangle() method implemented, because I first wanted to see whether it works or not before attempting to code other shape drawing methods.
      The constructor of the MyEngine class creates a GLFW window (GLAD is also initialized here to load all OpenGL functionality), and the myEngine.run() method in Main.cpp is responsible for firing up the engine. In this run() method, the shaders get loaded from files via the help of my FileHandler class. The vertices for the triangle are processed by the myShapes.drawTriangle() method where a vertex array object, a vertex buffer object and vertrex attributes are set for this purpose.
      The while loop in the run() method should be outputting me the desired red triangle, but all I get is a grey window area. Why?
      Note: The shaders are compiling and linking without any errors.
      (Note: I am aware that this code is not using any good software engineering practices (e.g. exceptions, error handling). I am planning to implement them later, once I get the hang of OpenGL.)

       
    • By KarimIO
      EDIT: I thought this was restricted to Attribute-Created GL contexts, but it isn't, so I rewrote the post.
      Hey guys, whenever I call SwapBuffers(hDC), I get a crash, and I get a "Too many posts were made to a semaphore." from Windows as I call SwapBuffers. What could be the cause of this?
      Update: No crash occurs if I don't draw, just clear and swap.
      static PIXELFORMATDESCRIPTOR pfd = // pfd Tells Windows How We Want Things To Be { sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor 1, // Version Number PFD_DRAW_TO_WINDOW | // Format Must Support Window PFD_SUPPORT_OPENGL | // Format Must Support OpenGL PFD_DOUBLEBUFFER, // Must Support Double Buffering PFD_TYPE_RGBA, // Request An RGBA Format 32, // Select Our Color Depth 0, 0, 0, 0, 0, 0, // Color Bits Ignored 0, // No Alpha Buffer 0, // Shift Bit Ignored 0, // No Accumulation Buffer 0, 0, 0, 0, // Accumulation Bits Ignored 24, // 24Bit Z-Buffer (Depth Buffer) 0, // No Stencil Buffer 0, // No Auxiliary Buffer PFD_MAIN_PLANE, // Main Drawing Layer 0, // Reserved 0, 0, 0 // Layer Masks Ignored }; if (!(hDC = GetDC(windowHandle))) return false; unsigned int PixelFormat; if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))) return false; if (!SetPixelFormat(hDC, PixelFormat, &pfd)) return false; hRC = wglCreateContext(hDC); if (!hRC) { std::cout << "wglCreateContext Failed!\n"; return false; } if (wglMakeCurrent(hDC, hRC) == NULL) { std::cout << "Make Context Current Second Failed!\n"; return false; } ... // OGL Buffer Initialization glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); glBindVertexArray(vao); glUseProgram(myprogram); glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_SHORT, (void *)indexStart); SwapBuffers(GetDC(window_handle));  
    • By Tchom
      Hey devs!
       
      I've been working on a OpenGL ES 2.0 android engine and I have begun implementing some simple (point) lighting. I had something fairly simple working, so I tried to get fancy and added color-tinting light. And it works great... with only one or two lights. Any more than that, the application drops about 15 frames per light added (my ideal is at least 4 or 5). I know implementing lighting is expensive, I just didn't think it was that expensive. I'm fairly new to the world of OpenGL and GLSL, so there is a good chance I've written some crappy shader code. If anyone had any feedback or tips on how I can optimize this code, please let me know.
       
      Vertex Shader
      uniform mat4 u_MVPMatrix; uniform mat4 u_MVMatrix; attribute vec4 a_Position; attribute vec3 a_Normal; attribute vec2 a_TexCoordinate; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { v_Position = vec3(u_MVMatrix * a_Position); v_TexCoordinate = a_TexCoordinate; v_Normal = vec3(u_MVMatrix * vec4(a_Normal, 0.0)); gl_Position = u_MVPMatrix * a_Position; } Fragment Shader
      precision mediump float; uniform vec4 u_LightPos["+numLights+"]; uniform vec4 u_LightColours["+numLights+"]; uniform float u_LightPower["+numLights+"]; uniform sampler2D u_Texture; varying vec3 v_Position; varying vec3 v_Normal; varying vec2 v_TexCoordinate; void main() { gl_FragColor = (texture2D(u_Texture, v_TexCoordinate)); float diffuse = 0.0; vec4 colourSum = vec4(1.0); for (int i = 0; i < "+numLights+"; i++) { vec3 toPointLight = vec3(u_LightPos[i]); float distance = length(toPointLight - v_Position); vec3 lightVector = normalize(toPointLight - v_Position); float diffuseDiff = 0.0; // The diffuse difference contributed from current light diffuseDiff = max(dot(v_Normal, lightVector), 0.0); diffuseDiff = diffuseDiff * (1.0 / (1.0 + ((1.0-u_LightPower[i])* distance * distance))); //Determine attenuatio diffuse += diffuseDiff; gl_FragColor.rgb *= vec3(1.0) / ((vec3(1.0) + ((vec3(1.0) - vec3(u_LightColours[i]))*diffuseDiff))); //The expensive part } diffuse += 0.1; //Add ambient light gl_FragColor.rgb *= diffuse; } Am I making any rookie mistakes? Or am I just being unrealistic about what I can do? Thanks in advance
    • By yahiko00
      Hi,
      Not sure to post at the right place, if not, please forgive me...
      For a game project I am working on, I would like to implement a 2D starfield as a background.
      I do not want to deal with static tiles, since I plan to slowly animate the starfield. So, I am trying to figure out how to generate a random starfield for the entire map.
      I feel that using a uniform distribution for the stars will not do the trick. Instead I would like something similar to the screenshot below, taken from the game Star Wars: Empire At War (all credits to Lucasfilm, Disney, and so on...).

      Is there someone who could have an idea of a distribution which could result in such a starfield?
      Any insight would be appreciated
    • By afraidofdark
      I have just noticed that, in quake 3 and half - life, dynamic models are effected from light map. For example in dark areas, gun that player holds seems darker. How did they achieve this effect ? I can use image based lighting techniques however (Like placing an environment probe and using it for reflections and ambient lighting), this tech wasn't used in games back then, so there must be a simpler method to do this.
      Here is a link that shows how modern engines does it. Indirect Lighting Cache It would be nice if you know a paper that explains this technique. Can I apply this to quake 3' s light map generator and bsp format ?
  • Popular Now