Jump to content
  • Advertisement

Archived

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

Huge windows/OGL error..

This topic is 5152 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

In my KillGLWindow function (copied from NeHe), I'm getting a huge error (that I didn't used to get... I'm not sure what has triggered it).
Lvoid KillGLWindow(GLvoid)								// Properly Kill The Window

{
	if (fullscreen)										// Are We In Fullscreen Mode?

	{
		ChangeDisplaySettings(NULL,0);					// If So Switch Back To The Desktop

		ShowCursor(TRUE);								// Show Mouse Pointer

	}

	if (hRC)											// Do We Have A Rendering Context?

	{
		if (!wglMakeCurrent(NULL,NULL))					// Are We Able To Release The DC And RC Contexts?

		{
			MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		
		}
/*********************************ERROR HERE***************/	
	if (!wglDeleteContext(hRC))						// Are We Able To Delete The RC?

		{
			MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		}
		hRC=NULL;										// Set RC To NULL

	}

	if (hDC && !ReleaseDC(hWnd,hDC))					// Are We Able To Release The DC

	{
		MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hDC=NULL;										// Set DC To NULL

	}

	if (hWnd && !DestroyWindow(hWnd))					// Are We Able To Destroy The Window?

	{
		MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hWnd=NULL;										// Set hWnd To NULL

	}

	if (!UnregisterClass("OpenGL",hInstance))			// Are We Able To Unregister Class

	{
		MessageBox(NULL,"Could Not Unregister Class.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
		hInstance=NULL;									// Set hInstance To NULL

	}
}
If I exit my program before looking at any of my objects, the program deconstructs perfectly. If I try to exit after looking at one, however, I get an error at the marked line with the following message: "Unhandled exception in CurrentErikEngine.exe(NVOGLNT.DLL): 0xC0000005: Access violation." What is going on? [edited by - sirSolarius on April 10, 2004 2:15:27 PM]

Share this post


Link to post
Share on other sites
Advertisement
You call wglMakeCurrent with the device context and then NULL so that it stops using the rendering context.
so you should do:


wglMakeCurrent(hDC, NULL);


When you call it with both NULL it trys to take the rendering context from a null device context, that could be causing your access violation.

If that does not fix the problem check to be sure that hRC is not getting set to NULL or some other value that it should not be before its getting to:


if (!wglDeleteContext(hRC))


[edited by - Wolfdog on April 11, 2004 6:00:15 AM]

Share this post


Link to post
Share on other sites
Ok, this is really weird. I''m stepping through my program in debug mode, and hRC is set to the same thing every time, 0x00010000. The issue is that if I don''t move my camera so that this textured box is visible, hRC is still 0x00010000, but suddenly it doesn''t error on that line.

I have no idea what''s going on.

Share this post


Link to post
Share on other sites
Ok, I''ve narrowed it down to my camera... if I move my camera past a certain point, then suddenly I can''t exit. I''m going to try to get more details about exactly when this happens, but I''ll post my code for now:


// Thanks to DigiBen digiben@gametutorials.com //

//

// This is used so that we can use timeGetTime() for time based movement

#pragma comment(lib, "winmm.lib")

#include "Camera1.h"
#include "Vector3.h"
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <fstream.h>


// This is how fast our camera moves

#define kSpeed 50.0f

// Our global float that stores the elapsed time between the current and last frame

float g_FrameInterval = 0.0f;


///////////////////////////////// CCAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This is the class constructor

/////

///////////////////////////////// CCAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


//CCamera::CCamera(dWorldID *world)

CCamera::CCamera()
: m_vPosition(0.0f,0.0f,0.0f), // Init the position to zero

m_vView(0.0, 0.0, 1.0f), // Init the view to a std starting view

m_vUpVector(0.0, 1.0f, 0.0f), // Init the UpVector

m_vStrafe(1.0, 0.0f, 0.0f), // Init the Strafe Vector

NEAR_CLIP(0.25f), // Init the near clipping plane

FAR_CLIP(50.0f), // Init the far clipping plane

held(NULL) // Init the held object to NULL

{
}

CCamera::~CCamera()
{
if(held !=NULL)
delete held;
}

///////////////////////////////// POSITION CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This function sets the camera''s position and view and up vector.

/////

///////////////////////////////// POSITION CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::PositionCamera(float positionX, float positionY, float positionZ,
float viewX, float viewY, float viewZ,
float upVectorX, float upVectorY, float upVectorZ)
{
Vector3 vPosition = Vector3(positionX, positionY, positionZ);
Vector3 vView = Vector3(viewX, viewY, viewZ);
Vector3 vUpVector = Vector3(upVectorX, upVectorY, upVectorZ);

// The code above just makes it cleaner to set the variables.

// Otherwise we would have to set each variable x y and z.


m_vPosition = vPosition; // Assign the position

m_vView = vView; // Assign the view

m_vUpVector = vUpVector; // Assign the up vector


m_vStrafe=(m_vView-m_vPosition).cross(m_vUpVector);
}


///////////////////////////////// SET VIEW BY MOUSE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This allows us to look around using the mouse, like in most first person games.

/////

///////////////////////////////// SET VIEW BY MOUSE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::SetViewByMouse()
{
POINT mousePos; // This is a window structure that holds an X and Y

int middleX = 800 >> 1; // This is a binary shift to get half the width

int middleY = 600 >> 1; // This is a binary shift to get half the height

float angleY = 0.0f; // This is the direction for looking up or down

float angleX = 0.0f; // This will be the value we need to rotate around the Y axis (Left and Right)

static float currentRotX = 0.0f;

// Get the mouse''s current X,Y position

GetCursorPos(&mousePos);

// If our cursor is still in the middle, we never moved... so don''t update the screen

if( (mousePos.x == middleX) && (mousePos.y == middleY) ) return;

// Set the mouse position to the middle of our window

SetCursorPos(middleX, middleY);

// Get the direction the mouse moved in, but bring the number down to a reasonable amount

angleY = (float)( (middleX - mousePos.x) ) / 500.0f;
angleX = (float)( (middleY - mousePos.y) ) / 500.0f;

// Here we keep track of the current rotation (for up and down) so that

// we can restrict the camera from doing a full 360 loop.

currentRotX -= angleX;

// If the current rotation (in radians) is greater than 1.0, we want to cap it.

if(currentRotX > 1.0f)
currentRotX = 1.0f;
// Check if the rotation is below -1.0, if so we want to make sure it doesn''t continue

else if(currentRotX < -1.0f)
currentRotX = -1.0f;
// Otherwise, we can rotate the view around our position

else
{
// Rotate around our perpendicular axis and along the y-axis

RotateView(angleX, m_vStrafe.x, m_vStrafe.y, m_vStrafe.z);
}

// Rotate around the y axis no matter what the currentRotX is

RotateView(angleY, m_vUpVector.x, m_vUpVector.y, m_vUpVector.z);
}


///////////////////////////////// ROTATE VIEW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This rotates the view around the position using an axis-angle rotation

/////

///////////////////////////////// ROTATE VIEW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::RotateView(float angle, float x, float y, float z)
{
if(angle==0)
return;

m_Moved=true;
Vector3 vNewView;

// Get the view vector (The direction we are facing)

Vector3 vView = m_vView - m_vPosition;

// Calculate the sine and cosine of the angle once

float cosTheta = (float)cos(angle);
float sinTheta = (float)sin(angle);

// Find the new x position for the new rotated point

vNewView.x = (cosTheta + (1 - cosTheta) * x * x) * vView.x;
vNewView.x += ((1 - cosTheta) * x * y - z * sinTheta) * vView.y;
vNewView.x += ((1 - cosTheta) * x * z + y * sinTheta) * vView.z;

// Find the new y position for the new rotated point

vNewView.y = ((1 - cosTheta) * x * y + z * sinTheta) * vView.x;
vNewView.y += (cosTheta + (1 - cosTheta) * y * y) * vView.y;
vNewView.y += ((1 - cosTheta) * y * z - x * sinTheta) * vView.z;

// Find the new z position for the new rotated point

vNewView.z = ((1 - cosTheta) * x * z - y * sinTheta) * vView.x;
vNewView.z += ((1 - cosTheta) * y * z + x * sinTheta) * vView.y;
vNewView.z += (cosTheta + (1 - cosTheta) * z * z) * vView.z;

// Now we just add the newly rotated vector to our position to set

// our new rotated view of our camera.

m_vView = m_vPosition + vNewView;
}


///////////////////////////////// STRAFE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This strafes the camera left and right depending on the speed (-/+)

/////

///////////////////////////////// STRAFE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::StrafeCamera(float speed)
{
m_Moved=true;
// Add the strafe vector to our position

m_vPosition.x += m_vStrafe.x * speed;
m_vPosition.z += m_vStrafe.z * speed;

// Add the strafe vector to our view

m_vView.x += m_vStrafe.x * speed;
m_vView.z += m_vStrafe.z * speed;
}


///////////////////////////////// MOVE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This will move the camera forward or backward depending on the speed

/////

///////////////////////////////// MOVE CAMERA \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::MoveCamera(float speed)
{
m_Moved=true;
// Get the current view vector (the direction we are looking)

Vector3 vVector = m_vView - m_vPosition;
vVector.normalise();

m_vPosition.x += vVector.x * speed; // Add our acceleration to our position''s X

m_vPosition.y += vVector.y * speed; // Add our acceleration to our position''s Y

m_vPosition.z += vVector.z * speed; // Add our acceleration to our position''s Z

m_vView.x += vVector.x * speed; // Add our acceleration to our view''s X

m_vView.y += vVector.y * speed; // Add our acceleration to our view''s Y

m_vView.z += vVector.z * speed; // Add our acceleration to our view''s Z

}


//////////////////////////// CHECK FOR MOVEMENT \\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This function handles the input faster than in the WinProc()

/////

//////////////////////////// CHECK FOR MOVEMENT \\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::CheckForMovement()
{
// Once we have the frame interval, we find the current speed

float speed = kSpeed * g_FrameInterval;

// Check if we hit the Up arrow or the ''w'' key

if(GetKeyState(VK_UP) & 0x80 || GetKeyState(''W'') & 0x80) {

// Move our camera forward by a positive SPEED

MoveCamera(speed);
}

// Check if we hit the Down arrow or the ''s'' key

if(GetKeyState(VK_DOWN) & 0x80 || GetKeyState(''S'') & 0x80) {

// Move our camera backward by a negative SPEED

MoveCamera(-speed);
}

// Check if we hit the Left arrow or the ''a'' key

if(GetKeyState(VK_LEFT) & 0x80 || GetKeyState(''A'') & 0x80) {

// Strafe the camera left

StrafeCamera(-speed);
}

// Check if we hit the Right arrow or the ''d'' key

if(GetKeyState(VK_RIGHT) & 0x80 || GetKeyState(''D'') & 0x80) {

// Strafe the camera right

StrafeCamera(speed);
}
}


///////////////////////////////// UPDATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// This updates the camera''s view and strafe vector

/////

///////////////////////////////// UPDATE \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::Update()
{
// Move the camera''s view by the mouse

SetViewByMouse();

// This checks to see if the keyboard was pressed

CheckForMovement();

// Initialize a variable for the cross product result

Vector3 vCross = (m_vView - m_vPosition);
vCross= vCross.cross(m_vUpVector);

// Normalize the strafe vector

vCross.normalise();
m_vStrafe = vCross;
}


///////////////////////////////// DRAW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// Required by the object class

/////

///////////////////////////////// DRAW \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CCamera::draw()
{

// Draw the held object


if(held!=NULL)
{
glPushMatrix();
held->draw();
glPopMatrix();
}


// Give openGL our camera position, then camera view, then camera up vector

gluLookAt(m_vPosition.x, m_vPosition.y, m_vPosition.z,
m_vView.x, m_vView.y, m_vView.z,
m_vUpVector.x, m_vUpVector.y, m_vUpVector.z);

if(m_Moved) // Recalculate the frustum if moved

{
viewFrust.getFrustum();
m_Moved=false;
}
}


///////////////////////////////// PLACE/MOVE/CALCBOUNDING \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// Required by the object class

/////

///////////////////////////////// PLACE/MOVE/CALCBOUNDING \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*


void CCamera::place(GLfloat newX, GLfloat newY, GLfloat newZ)
{
m_Moved=true;
Vector3 newPos(newX, newY, newZ);

m_vView+=(m_vPosition-newPos); // Move the view vector for change in position


m_vPosition=newPos;
}

void CCamera::move(GLfloat newX, GLfloat newY, GLfloat newZ)
{
m_Moved=true;

Vector3 newPos(newX, newY, newZ);

m_vPosition+=newPos;
m_vView+=newPos; // Move the view and up vectors to correspond with

}

void CCamera::calcBounding()
{
boundingRadius=6.0f;
boundingBox.setVals(-1.2, 1.2, -1.2, 1.2, -1.2, 1.2);
orientation.transformBoundingBox(&boundingBox);
}

///////////////////////////////// SPIN/ROT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// Required by the object class

/////

///////////////////////////////// SPIN/ROT \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CCamera::spin(GLfloat xRot, GLfloat yRot, GLfloat zRot)
{
m_Moved=true;
RotateView(xRot, m_vStrafe.x, m_vStrafe.y, m_vStrafe.z);
RotateView(yRot, m_vUpVector.x, m_vUpVector.y, m_vUpVector.z);
RotateView(zRot, m_vView.x, m_vView.y, m_vView.z);
}

void CCamera::rot(GLfloat xRot, GLfloat yRot, GLfloat zRot)
{
m_Moved=true;
// Reset the rotations

m_vView=m_vPosition; // Set the view equal to the position

m_vView.z+=1.0f; // And push the view forward

m_vUpVector.x=0.0; m_vUpVector.y=1.0; m_vUpVector.z=0.0; // Reset the up vector

m_vStrafe.x=1.0; m_vStrafe.y=0.0f; m_vStrafe.z=0.0f; // Reset the strafe vector


// Apply the new rotations now

RotateView(xRot, 1.0f, 0.0f, 0.0f);
RotateView(yRot, 0.0f, 1.0f, 0.0f);
RotateView(zRot, 0.0f, 0.0f, 1.0f);
}

///////////////////////////////// SetClip \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// Sets the clipping planes

/////

///////////////////////////////// SetClip \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CCamera::SetClip(GLfloat closePlane, GLfloat farPlane)
{
if(closePlane > 0 && farPlane > 0)
{
NEAR_CLIP=closePlane;
FAR_CLIP=farPlane;
m_Moved=true;
}
}


///////////////////////////////// Held \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

/////

///// Sets the held object

/////

///////////////////////////////// Held \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\*

void CCamera::setHeld(Object *newHeld)
{
if(newHeld == NULL && held != NULL) // If I want to remove the old item in my hands

delete held;
held=newHeld;
}

Object *CCamera::getHeld()
{ return held;}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!