Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

akdjr

camera problems

This topic is 6020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

ok, i'm following the first camera tutorial on http://www.gametutorials.com, and its not working for me. i followed the tutorial exactly, yet my camera refuses to move. the movement code for the camera is the same as on the tutorial, as are the initial position, view vector, and up vectors. here is my initialization code:
      
bool InitializeWindow(char *title, int width, int height, int bpp)
{
	outfile << endl;
	outfile << "*********InitializeWindow*********" << endl;
	// setup a window

	WNDCLASSEX windowClass;

	windowClass.cbClsExtra = 0;
	windowClass.cbSize = sizeof(WNDCLASSEX);
	windowClass.cbWndExtra = 0;
	windowClass.hbrBackground = NULL;
	windowClass.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
	windowClass.hIcon = (HICON)LoadIcon(NULL, IDI_APPLICATION);
	windowClass.hIconSm = (HICON)LoadIcon(NULL, IDI_APPLICATION);
	windowClass.hInstance = hInst;
	windowClass.lpfnWndProc = WndProc;
	windowClass.lpszClassName = "myGame Class";
	windowClass.lpszMenuName = NULL;
	windowClass.style = CS_HREDRAW | CS_VREDRAW;
	
	RegisterClassEx(&windowClass);

	outfile << "Switching to fullscreen mode..." << endl;
	DEVMODE dmScreenSettings;
	memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
	dmScreenSettings.dmSize = sizeof(dmScreenSettings);	
	dmScreenSettings.dmPelsWidth = width;
	dmScreenSettings.dmPelsHeight = height;
	dmScreenSettings.dmBitsPerPel = bpp;
	dmScreenSettings.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
	ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN);

	outfile << "Creating window..." << endl;
	hGlobalWnd = CreateWindowEx(WS_EX_APPWINDOW, "myGame Class", "myGame", WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, hInst, NULL);

	ShowWindow(hGlobalWnd, SW_SHOW);
	UpdateWindow(hGlobalWnd);

	ShowCursor(false);
	
	outfile << "*********InitializeWindow*********" << endl;
	outfile << endl;

	return true;
}

void InitializeOpenGL(int width, int height)
{
	// setup OpenGL

	outfile << "Setting up OpenGL..." << endl;
	glClearColor(0.0, 0.0, 0.0, 0.0);

	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);
	
	glViewport(0, 0, width, height);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.5f, 150.0f);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	outfile << "Succeeded!" << endl;
}

void SetupPixelFormat(HDC hDC)
{
	int pixelFormat;

	static PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof(PIXELFORMATDESCRIPTOR),
			1,
			PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER | PFD_TYPE_RGBA,
			32,
			0, 0, 0, 0, 0, 0,
			0,
			0,
			0,
			16,
			0,
			0,
			PFD_MAIN_PLANE,
			0,
			0, 0, 0
	};

	pixelFormat = ChoosePixelFormat(hDC, &pfd);
	SetPixelFormat(hDC, pixelFormat, &pfd);
}
  
my render function:
  
void Render()
{
	gluLookAt(camera.position[X], camera.position[Y], camera.position[Z], camera.lookPosition[X], camera.lookPosition[Y], camera.lookPosition[Z], 0.0f, 1.0f, 0.0f);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	angle = angle + 0.1f;
	if (angle >= 360.0f)					
		angle = 0.0f;

	//glRotatef(angle, 1.0f, 0.0f, 0.0f);

	//glRotatef(angle, 0.0f, 1.0f, 0.0f);

	//glRotatef(angle, 0.0f, 0.0f, 1.0f);


	glBegin(GL_TRIANGLES);
		glColor3ub(255, 0, 0);						// Make the top vertex RED

		glVertex3f(0, 1, -3);						// Here is the top point of the triangle

	
		glColor3ub(0, 255, 0);						// Make the left vertex GREEN

		glVertex3f(-1, 0, -3);						// Here is the left point of the triangle

	
		glColor3ub(0, 0, 255);						// Make the right vertex BLUE

		glVertex3f(1, 0, -3);						// Here is the right point of the triangle

	glEnd();

	glFlush();
}
  
here is my camera class:
        
class cCamera
{
public:
	cCamera();

	float position[3];
	float lookPosition[3];
	float upVector[3];

	void Update();

	void Move(float speed);
	void Strafe(float speed);
};

cCamera::cCamera()
{
	position[X] = 0.0f;
	position[Y] = 0.0f;
	position[Z] = 0.0f;

	lookPosition[X] = 0.0f;
	lookPosition[Y] = 0.5f;
	lookPosition[Z] = 0.0f;

	upVector[X] = 0.0f;
	upVector[Y] = 1.0f;
	upVector[Z] = 0.0f;


	//gluLookAt(position[X], position[Y], position[Z], lookPosition[X], lookPosition[Y], lookPosition[Z], upVector[X], upVector[Y], upVector[Z]);

}

void cCamera::Update()
{
	// orient the camera

	gluLookAt(position[X], position[Y], position[Z], lookPosition[X], lookPosition[Y], lookPosition[Z], upVector[X], upVector[Y], upVector[Z]);
}

void cCamera::Strafe(float speed)
{
	// strafe the camera

}

void cCamera::Move(float speed)
{
	// move the camera

	float vector[3];

	// find out the direction we are looking at

	vector[X] = lookPosition[X] - position[X];
	vector[Y] = lookPosition[Y] - position[Y];
	vector[Z] = lookPosition[Z] - position[Z];

	// move the camera accordingly

	lookPosition[X] += vector[X] * speed;
	lookPosition[Z] += vector[Z] * speed;
	position[X] += vector[X] * speed;
	position[Z] += vector[Z] * speed;

	// update the camera

	gluLookAt(position[X], position[Y], position[Z], lookPosition[X], lookPosition[Y], lookPosition[Z], upVector[X], upVector[Y], upVector[Z]);
}
  
and here is my window procedure
  
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg)
	{
	case WM_CREATE:
		{
			return 0;
		}
	case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	case WM_DESTROY:
		{
			break;
		}
	case WM_SYSCOMMAND:
		{
			switch (wParam)
			{
			case SC_SCREENSAVE:
			case SC_MONITORPOWER:
				{
					return 0;
				}
			default:
				break;
			}
			break;
		}
		case WM_KEYDOWN:
			{
				switch(wParam)
				{
				case VK_ESCAPE:
					PostQuitMessage(0);						// Send a QUIT message

					break;

				case VK_UP:									// If we hit the UP arrow key

					camera.Move(0.03f);				// Move our camera forward by a positive SPEED

					outfile << "Camera position: " << camera.position[X] << " " << camera.position[Y] << " " << camera.position[Z] << endl;
					outfile << "Camera view    : " << camera.lookPosition[X] << " " << camera.lookPosition[Y] << " " << camera.lookPosition[Z] << endl;
					Render();							// Redraw the screen to reflect changes

					break;

				case VK_DOWN:								// If we hit the DOWN arrow key

					camera.Move(-0.03f);			// Move our camera backward by a negative SPEED

					outfile << "Camera position: " << camera.position[X] << " " << camera.position[Y] << " " << camera.position[Z] << endl;
					outfile << "Camera view    : " << camera.lookPosition[X] << " " << camera.lookPosition[Y] << " " << camera.lookPosition[Z] << endl;
					Render();						// Redraw the screen to reflect changes

					break;
				}
				break;
			}
		break;
	default:
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}
    
now the annoying part is that the camera doesn't move at all...plus my view window is kind of weird - its the exact same as the tutorial, yet looks way different - plus the fact that the camera code is the same, yet mine doesn't work... i'm stumped here... any ideas? thanks! [edited by - akdjr on April 20, 2002 8:14:25 PM]

Share this post


Link to post
Share on other sites
Advertisement
yeah, but i''ve tried it without, and it still doesn''t work. plus, thats the way it was in the tutorial... =/

i''m still stumped though, i''ve been looking at this for the past couple of days, now and can''t figure it out...

Share this post


Link to post
Share on other sites
In camera init code you are looking straight up!

Then when you move you only use the x/z components which are 0!

Try this to look straight ahead:

lookPosition[X] = 0.0f;
lookPosition[Y] = 0.0f;
lookPosition[Z] = -1f;

You may have to flip z depending on how your do the left/right hand thing.

zin

zintel.com - 3d graphics & more or less

Share this post


Link to post
Share on other sites
thanks for the reply, but i tried what you suggested, and the camera still refuses to move! i checked my log file, and the camera position does change - ie it movies along the z-axis, but there is no visible change! its as if the camera is moving, but gluLookAt() is doing nothing...

Share this post


Link to post
Share on other sites
In your Render funtion you load the identity matrix after the gluLookAt matrix transformation, basicaly you are reseting the changes to the camara every time. Like you said the gluLookAt is doing nothing.

[edited by - White Rabbit on April 24, 2002 12:21:59 AM]

Share this post


Link to post
Share on other sites
LOL!!!
i can''t believe i missed that....
*whaps himself over the head*
thanks a lot!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!