Welcome Guest ( Log In | Register )

[ Big| Medium| Small] -



Post new topic Reply to topic  [ 9 posts ] 
    Amy
  Sat Apr 30, 2011 9:14 pm
User avatar
Staff

Big Dumb Guy
This has been bugging me for ages but I finally worked it out.

Cameras in OpenGL (C++)

So you have a 3D scene drawn out with some nice cubes and cylinders - and it looks pretty nice! But you don't know how to control the camera.

In OpenGL there is no camera - and there doesn't need to be.

Take the Futurama principle: the ship doesn't move; it merely moves the rest of the Universe in the opposite direction.

With this in mind, we simply need to add a rotation to the beginning of the draw() function, like so:

glRotatef(rotateX, 1, 0, 0);
glRotatef(rotateY, 0, 1, 0);


Two variables: rotateX and rotateY.

rotateX is our rotation around the X axis - imagine you are folding a piece of paper towards you. rotateY is our rotation around the Y axis, like turning a mug of coffee in place.

We just need to assign this to keys! I'm using the arrow keys.

if (Event.Type == sf::Event::KeyPressed)
{
switch (Event.Key.Code)
{
case sf::Key::Left:
rotateY -= 10;
break;
case sf::Key::Right:
rotateY += 10;
break;
case sf::Key::Up:
rotateX -= 10;
break;
case sf::Key::Down:
rotateX += 10;
break;
}
}


NB: better to replace 10 with a preset constant so you can change it later.

Now that we know this, it's simple to move the camera in any way we want. To move forwards and backwards, we just have to do the same but with a translation instead of a rotation. So in our draw() function we add:

glTranslatef(x, y, z);


Top Top
Profile      
 

    Amy
  Sat Apr 30, 2011 9:51 pm
User avatar
Staff

Big Dumb Guy
How to zoom the camera in OpenGL

Like before, to zoom in and out all we are doing is drawing objects closer or further away.

I used page up and page down as they were convenient to my setup.

At the beginning of your draw() method, place a translate. That is, moving everything drawn next by x, y, z.

glTranslatef(0, 0, zoom);


Now manipulate the
zoom
variable (a float) to zoom in and out.

I attached these to two keys like so:


case sf::Key::PageUp:
zoom += 5;
break;
case sf::Key::PageDown:
zoom -= 5;
break;


Again, setting 5 to be a preset constant is preferable.



How to pan the camera in OpenGL

Panning, as strange as it sounds, is just a zoom in a different direction.

Let's look at our transformation;

glTranslatef(0, 0, zoom);


By "zooming" in x and y we are in effect panning the camera.

glTranslatef(panX, panY, zoom);


You can attach these to keys as before, or perhaps use for an animation.


Top Top
Profile      
 

    Ceiling_Cat
  Sat Apr 30, 2011 11:26 pm
.byte $1a
User avatar
Member

I always wanted to tackle OpenGL but never had a reason to. Looks rather simple (or you're just good at explaining it).

_________________
Image


Top Top
Profile      
 

    Amy
  Sat Apr 30, 2011 11:30 pm
User avatar
Staff

Big Dumb Guy
It's a lot easier to use than I thought it would be to be honest.


Top Top
Profile      
 

    DeM0nFiRe
  Sun May 01, 2011 12:07 am
Member

I think it's cool that we are getting some OpenGL tutorials here BUT I would like to mention that those are the old OpenGL 1 way of doing things. It would be a good idea to learn how to do things the shader way! It's a bit tougher to get started with the shader way, but you can do some cool stuff once you get set up with it.

_________________
_          )         &          ^    ( ^%       +     +  *   + ^ $      )    %
+ * % %$( ( % ( *
) ಠ_ಠ ^ % (
) # $ ) ) #
^ $ & % $ ( # @*
# ) * % ) $


Top Top
Profile      
 

    Ceiling_Cat
  Sun May 01, 2011 2:42 pm
.byte $1a
User avatar
Member

That's exactly what I was going to ask about... is it hard to work with meshes and materials in OGL? (provided someone is willing to learn to code shaders in HLSL)

_________________
Image


Top Top
Profile      
 

    Amy
  Sun May 01, 2011 3:29 pm
User avatar
Staff

Big Dumb Guy
Yeah i worked this out myself so there's almost certainly a better way. I don't even know what a shader is!


Top Top
Profile      
 

    DeM0nFiRe
  Sun May 01, 2011 5:30 pm
Member

Heh, well, essentially a shader is a program that runs on the GPU. It originally comes from the term in like art when you talk about shading, but now you can do pretty much anything with shaders on the GPU, not just shading!
There are 5 major types of shaders:

Vertex shader is what you use to change things like position and normal vectors of each vertex. That way, instead of doing glTranslate or glRotate, you would do that stuff in the vertex shader. The same goes for any matrix operations instead of doing things like glMatrixMode or anything like that. (glTranslate and glRotate are also actually matrix operations).

Fragment shader (called pixel shader in DirectX) is what you use to change the colors of fragments/pixels (OpenGL uses the term fragment for this, because in reality the size of a fragment that is shaded is not a unit size like a pixel is. The same goes for pixel shaders in DirectX, I'm not sure why they are called pixels there, I guess just to piss you off)

Realistically, those are the only two types you need to know about to start working with shaders, as those are the two that directly replace the stuff you usually do in glMatrix operations and glBegin/glEnd. However, there are other things you can do that you may not think can be done on the GPU.

The geometry shader (Only in DirectX 10+/OpenGL 3+(I think it's 3, might be 3.1)) is used to create geometry! Basically, it can take the vertex data you passed to the vertex shader and make new vertices from it. That way you can do things like hardware instancing (IE, pass one instance of a mesh to the GPU and the GPU renders it multiple times)

The tessellation shader(Only in DirectX 11/ OpenGL 4+) is what is used to tesselate the polygons, and that basically just makes more polygons inside the polygons you've already created. If you have more polygons, you can add more detail to things, basically. The tessellation shader lets you create those extra polygons on the fly according to some rules and guidelines you give it. Good for having dynamic level of detail on meshes and terrains.

Then the last type of shader is a compute shader (Again, DirectX 11/ OpenGL 4+). The compute shader does anything! Basically, it lets you run operations on the GPU without necessarily working with any polygon or texture data. In DirectX, you write your compute shaders in DirectCompute. In OpenGL, they decided to just use OpenCL. If you are using OpenGL 4, then there are some things they did to make it easier to have OpenCL and OpenGL work together.

Oh, as for Ceiling Cat's question, it's actually pretty tough to work with meshin in OGL if you use HLSL. That's because HLSL is the DirectX shader language :P. For OpenGL you use GLSL! It's not difficult to work with meshes in GLSL, however it's a bit harder to learn at first than doing it immediate mode (I.E., without shaders) but once you get yourself thinking about the data as data, it's not too tough.

_________________
_          )         &          ^    ( ^%       +     +  *   + ^ $      )    %
+ * % %$( ( % ( *
) ಠ_ಠ ^ % (
) # $ ) ) #
^ $ & % $ ( # @*
# ) * % ) $


Top Top
Profile      
 

    Ceiling_Cat
  Sun May 01, 2011 9:18 pm
.byte $1a
User avatar
Member

The GLSL thing was a typo. Thanks for the info.

_________________
Image


Top Top
Profile      
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 


Who is online

Users browsing this forum: No users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

We are an independent, not-for-profit game making community.
Homepage
Board Index
About Us
Downloadable Games
Free Browser Games
Games in Development
RPG Maker Support
Game Maker Support
Construct 2 Support
HBGames the eZine
Advanced RPG Maker
Site Announcements
Powered by phpBB © phpBB Group