Sign in to follow this  
gluefactory

OpenGL A mess of a 3rd person camera attempt

Recommended Posts

Hi all, I'm new here so I'll just introduce myself, my name is rob i'm a games development student based in the uk. I'm currently working on a 3rd person camera (in 'c' and OpenGL) using some lecture notes for guidance. However It's proving rather fidly and if I'm honest im a little lost. I was wondering if anyone on here could shed some light on this for me. The set-by-step I have been given is as follows: 1) Get transform matrix to move avatar (car) (same as camera transform in 1st person, minus the inversion) 2) Save this matrix as car matrix 3) Use this to find a camera position n units directly behind the car 4) Load identity matrix (start again) 5) Use gluLookAt() to build camera view matrix --(Eye position found behind car) --(Target is position of car (xpos, ypos, zpos)) 4) Get and save camera view matrix 5) Add car transformations onto camera matrix and save as new car matrix 6) Use camera matrix to transform terrain and other static objects 7) Use car matrix to transform car Now i've had a go at this, but it is somewhat messy and a lil incomplete, ill attatch my code with notes of what section is which step:
void move_car(char direction[], int scalar)
{
	static float scale = 10.0;
	static float heading, pitch, roll;

	float m[16];
	float cmatrix[16];
	float cam[16];
	float xpos, ypos, zpos;
	float camx, camy, camz;
	float carx, cary, carz;
	float nextcamx, nextcamy, nextcamz;
	float xinc, yinc, zinc ;
	int i = 0;
	
	xpos = 1280.0f ;
	zpos = -1280.0f;
	ypos = 200.0f;

// Step 1: the directions etc. are imputs from keyboard or gamepad and are
// to move the car around

	glLoadIdentity() ;

	if (direction[2] == 'a') heading = heading + 3 * scalar;
	if (direction[3] == 'd') heading = heading - 3 * scalar;
	if (direction[5] == 'f') pitch = pitch - 1;
	if (direction[4] == 'r') pitch = pitch + 1;
	if (direction[10] == 'z') roll = roll - 1;
	if (direction[11] == 'x') roll = roll + 1;

	glPushMatrix();
	glLoadIdentity();
	glRotatef(heading, 0.0f, 1.0f, 0.0f);
	glRotatef(pitch, 1.0f, 0.0f, 0.0f);
	glRotatef(roll, 0.0f, 0.0f, 1.0f);
	glGetFloatv(GL_MODELVIEW_MATRIX, m);
	glPopMatrix();

	xinc = -m[8] * scale;
	yinc = -m[9] * scale;
	zinc = -m[10] * scale;
 
	if(direction[0] == 'w')
	{
		xpos = xpos + xinc;
		ypos = ypos + yinc;
		zpos = zpos + zinc;
	}
	if(direction[1] == 's')
	{
		xpos = xpos - xinc;
		ypos = ypos - yinc;
		zpos = zpos - zinc;	  
	}

	xinc = -m[0] * scale;
	yinc = -m[1] * scale;
	zinc = -m[2] * scale;

	if(direction[6] == 'q')
	{
		xpos = xpos - xinc;
		ypos = ypos - yinc;
		zpos = zpos - zinc;	  
	}
	if(direction[7] == 'e')
	{
		xpos = xpos + xinc;
		ypos = ypos + yinc;
		zpos = zpos + zinc; 
	}

	xinc = -m[4]  * scale;
	yinc = -m[5]  * scale;
	zinc = -m[6] * scale;

	if(direction[8] == 'p')
	{
		xpos = xpos - xinc;
		ypos = ypos - yinc;
		zpos = zpos - zinc;	  
	}
	if(direction[9] == 'o')
	{
		xpos = xpos + xinc;
		ypos = ypos + yinc;
		zpos = zpos + zinc; 
	}

	glLoadIdentity();
	glRotatef(roll, 0.0f, 0.0f, 1.0f);
	glRotatef(pitch, 1.0f, 0.0f, 0.0f);
	glRotatef(heading, 0.0f,1.0f, 0.0f);
	glTranslatef(xpos, ypos, zpos);

// Step 2: saving the matrix as car matrix - cmatrix

	glGetFloatv(GL_MODELVIEW_MATRIX, cmatrix);

// Step 3: This is supposed to calculate the cam position behind the car
// I've added in a little bit to program some kind of camera lag

	nextcamx = (-18 * cmatrix[8] + cmatrix[12]);
	nextcamy = (-18 * cmatrix[9] + cmatrix[13]);
	nextcamz = (-18 * cmatrix[10] + cmatrix[14]);
	
        //camera lag
	camx = camx + ((nextcamx - camx) * 0.1f);
	camy = camy + ((nextcamy - camy) * 0.1f);
	camz = camz + ((nextcamz - camz) * 0.1f);

// Step 4: Load the identity

	glLoadIdentity();

// Step 5: the gluLookAt command

	gluLookAt(camx, camy, camz, xpos, ypos, zpos, 0, 1, 0);

// Step 6: geting the camera matrix - cam

	glGetFloatv(GL_MODELVIEW_MATRIX, cam);

// Step 7: Adding the car transformations onto the camera matrix and saving
// as a new car matrix... I'm really not sure this is right at all

	for(i = 0;i<=15;i++)
	{
		cmatrix[i] = cam[i] + cmatrix[i];
	}

// Step 8: Using the camera matrix to transform terrain and static objects
// again I'm not sure if this is right

	glLoadIdentity();
	glRotatef(-roll, 0.0f, 0.0f, 1.0f);
	glRotatef(-pitch, 1.0f, 0.0f, 0.0f);
	glRotatef(-heading, 0.0f,1.0f, 0.0f);
	glTranslatef(-camx, -camy, -camz);

// Step 9: Use car matrix to transform car... now I'm really lost here and 
// havent attempted much. rendercar() is where I render the car using an md2
// model which is working fine.

	carx = cmatrix[8];
	cary = cmatrix[9];
	carz = cmatrix[10];

	//glTranslatef(carx, cary, carz);
	//rendercar(carx, cary, carz);	
}
As you can see from the comments, I'm a little lost, any help at all is greatly appreciated. Thank you

Share this post


Link to post
Share on other sites
I think you're making it more complicated than you need.

Is something like this what you want?

Multiperson camera

Here's my code:
vpos = car pos
vtarget = car heading
vup = up vector (you can use (0,1,0))


void CCamera::update(const CVector3 &vpos, const CVector3 &vtar, const CVector3 &vup )
{
//set target as player pos
target = vpos;
position = vpos;
up = vup;

CVector3 f, r, u;
CVector3 temp(vtar);
//forward = target - pos
//r = f x u
f = temp - vpos;
u = vup;
r = vcross(f, u);

//normalize 'em
f.normalize();
u.normalize();
r.normalize();

//rotate on right axis to set cam just above the player
Matrix.rotate_on_axis (-15*PI/180.0f, r.x, r.y, r.z);
Matrix.transform_vector (f);


//adjust camera's pos just behind the player 6 units
position -= (f * 5);
target += (f * 5);

}





What this does basically is to get the right angle from your forward vector, move the camera 5 units from the car, and rotate the camera on the right axis.

The effect is looking "just above and behind the car"


Oops.

Here's how you use it...


glPushMatrix();
glLoadIdentity();
//set the player view vectors and update the cam depending on
//what the viewer sees.
set_view(Object,Camera);
//Apply lookat transform
gluLookAt ( Camera.position.x,Camera.position.y, Camera.position.z,
Camera.target.x,Camera.target.y,Camera.target.z,
Camera.up.x,Camera.up.y,Camera.up.z );

//draw some noce grids
draw_terrain()

//draw and orient the object, note there's a glpushmatrix in the sub draw_player

draw_player (Object);

glPopMatrix();




The link I posted has the whole source and exe.

Share this post


Link to post
Share on other sites
Oh sorry 'bout that.

Did you try this?


// Step 9: Use car matrix to transform car... now I'm really lost here and
// havent attempted much. rendercar() is where I render the car using an md2
// model which is working fine.

carx = cmatrix[8];
cary = cmatrix[9];
carz = cmatrix[10];


glPushMatrix();
glTranslatef(carx, cary, carz);
rendercar(carx, cary, carz);
glPopMatrix();
}



Share this post


Link to post
Share on other sites
Cheers :)

Doesn't work but is along the right lines, using the push and pop will stop it moving all the static objects again when I want it to just move the car. Think the answer lies in using the car matrix to transform the car, feeling rather 'thick' but I just can't get my head around how I'd do that :P

I'm sure once I get the answer it'll be a "duh!" moment for me :)

thanks,

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  

  • Announcements

  • Forum Statistics

    • Total Topics
      628348
    • Total Posts
      2982208
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now