# OpenGL Engine crashing due to camera angle?

## Recommended Posts

Hi all, I'm facing a strange problem I have been unable to figure out by myself... I have restarted a basic opengl engine, with support for 3ds files and bmp textures so far. On to the problem: I added some simple formulas to make the camera perform a certain fly-by of the default model. I used the sin and cos functions with timeGetTime, nothing special, but after a certain time (I THINK it's because of the angle of the camera) the screen freezes and goes black after which the system crashes. If it doesn't crash, the system resets the VPU (ATI Radeon 9700 Pro) so the screen goes black for 1 second, resumes the program only to still crash within the next minute. I have tried 2 completely different models, both with the same resutls. I have tried the program without the formulaes, i.e. a static camera, and then it works fine. What could be causing this?

##### Share on other sites
Just a guess, but could it be a tangent of 90 degrees? (e.g. did you check division by 0?)

##### Share on other sites
can you post the camera code?

##### Share on other sites
Check by outputting values after your sin / cos functions but before rendering where you are getting a divide-by-zero. I think it's a pretty safe bet that that is the problem here.

/edit: Darn, beaten! I opened this window and checked my e-mail before replying.

##### Share on other sites
Here's the function that renders the default model

void Update(void){	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);	glLoadIdentity();	if(myApp->GetKey(VK_ESCAPE)) 	{		myApp->Update_ptr = &Quit;		return;	}	gluLookAt(	400.0f,                 300.0f,                600.0f, 				0.0f, 0.0f, 0.0f,                 0.0f, 0.0f, 1.0f              );	GLfloat LightPosition[]= { 1.0f, 0.6f, 0.7f, 0.0f };	glLightfv(GL_LIGHT0, GL_POSITION, LightPosition);	glBegin(GL_TRIANGLES);	obj_Default.Render_3ds();	glEnd();}

This is the code that doesn't crash. If I alter the GLU_Lookat to...

	gluLookAt(	sin(timeGetTime() / 1500.0f) * 1200,                 cos(timeGetTime() / 1400.0f) * 1200,                600.0f, 				0.0f, 0.0f, 0.0f,                 0.0f, 0.0f, 1.0f              );

...things go wrong as described above.

##### Share on other sites
My guess is that you're getting denormalized values. Try setting your fp precision to round to zero mode or clamp your input values for sin && cos to "reasonable" values.

##### Share on other sites
cast the timegettime() function to a float: (float)timegettime()
because it returns a short integer.(I think dword is a short integer)
and change 1200 into 1200.0f

this are the two things where it could go wrong.

veel geluk

[Edited by - delta user on December 3, 2005 7:07:11 AM]

No difference :(

##### Share on other sites
Try to write your own code instead of gluLookAt. Then if there is division by 0 you'd get a runtime error, and maybe even a line number.

This is kinda weird, only tan usually causes problems, not sin and cos.

##### Share on other sites
I tried your code in one of my apps and it works fine.

It is verry weird because your app doesn't crash immediatly.

if it would be the camera code it should crash when your camera code gets executed for the first time and that would be when you start your app.

sorry for the code above. it was bad code so i removed it.

##### Share on other sites
Things like resetting VPUs are mostly caused by buffer overruns or invalid indices - you better double-check your 3DS rendering code. I really can't imagine how any messed up camera matrix could cause such problems.
Are you by any change using VAs or VBOs? If so, take a close look at those. If not , check your 3DS loader for invalid indices.

HTH,
Pat

##### Share on other sites
Try changing your up vector to (0,1,0). If that solves it then it is the angle you are using. Specifically you are hitting an angle where both sin and cos are returning zero. Not because the sin and cos of any given angle are both zero, but because you are so far out of the interval of convergence. You should use something like (((float)(timeGetTime() / A) % B) / B) * 2*PI to calculate the angle. The loop given above is wrong. It's wrong because it will most likely produce a huge negative angle and wrong numerically because you can't subtract your way back into range when you are so far out of range.

You should change how you calculate the angle regardless. What you're doing should be producing fairly irratic movement. If changing that up vector doesn't eliminate the crash then changing how you calculate the angle won't eliminate it either. Changing the up vector eliminates the possibility that you are generating (0,0,600) as the point you are looking at. The other major error you could make with LookAt is to look at the point the camera is at, but with the hardcoded 0 and 600 that isn't likely.

It is possible. How it can happen is addressing memory incorrectly. When you start stomping on memory all kinds of impossible things become possible. If you haven't commented out the call to render the scene then you might want to give it a try. It would make no sense that doing that would fix the problem so if it does then you most likely stomped on memory in that routine. If it stops the crash then obviously something in there was causing it.

##### Share on other sites
Changing the up-vector didnt help.

I'm using the 3ds-loader from www.morrowland.com. Used it before in a project and it worked fine then...

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627638
• Total Posts
2978330
• ### Similar Content

• By xhcao
Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness.
• By cebugdev
hi guys,
are there any books, link online or any other resources that discusses on how to build special effects such as magic, lightning, etc. in OpenGL? i mean, yeah most of them are using particles but im looking for resources specifically on how to manipulate the particles to look like an effect that can be use for games,. i did fire particle before, and I want to learn how to do the other 'magic' as well.
Like are there one book or link(cant find in google) that atleast featured how to make different particle effects in OpenGL (or DirectX)? If there is no one stop shop for it, maybe ill just look for some tips on how to make a particle engine that is flexible enough to enable me to design different effects/magic
let me know if you guys have recommendations.
• By dud3
How do we rotate the camera around x axis 360 degrees, without having the strange effect as in my video below?
Mine behaves exactly the same way spherical coordinates would, I'm using euler angles.
Tried googling, but couldn't find a proper answer, guessing I don't know what exactly to google for, googled 'rotate 360 around x axis', got no proper answers.

References:
Code: https://pastebin.com/Hcshj3FQ
The video shows the difference between blender and my rotation:

• By Defend
I've had a Google around for this but haven't yet found some solid advice. There is a lot of "it depends", but I'm not sure on what.
My question is what's a good rule of thumb to follow when it comes to creating/using VBOs & VAOs? As in, when should I use multiple or when should I not? My understanding so far is that if I need a new VBO, then I need a new VAO. So when it comes to rendering multiple objects I can either:
* make lots of VAO/VBO pairs and flip through them to render different objects, or
* make one big VBO and jump around its memory to render different objects.
I also understand that if I need to render objects with different vertex attributes, then a new VAO is necessary in this case.
If that "it depends" really is quite variable, what's best for a beginner with OpenGL, assuming that better approaches can be learnt later with better understanding?

• Hello all,

On my Windows 7 x64 machine I wrote the code below on VS 2017 and ran it.
#include <glad/glad.h>  #include <GLFW/glfw3.h> #include <std_lib_facilities_4.h> using namespace std; void framebuffer_size_callback(GLFWwindow* window , int width, int height) {     glViewport(0, 0, width, height); } //****************************** void processInput(GLFWwindow* window) {     if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)         glfwSetWindowShouldClose(window, true); } //********************************* int main() {     glfwInit();     glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);     glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);     glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);     //glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);     GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr);     if (window == nullptr)     {         cout << "Failed to create GLFW window" << endl;         glfwTerminate();         return -1;     }     glfwMakeContextCurrent(window);     if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))     {         cout << "Failed to initialize GLAD" << endl;         return -1;     }     glViewport(0, 0, 600, 480);     glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);     glClearColor(0.2f, 0.3f, 0.3f, 1.0f);     glClear(GL_COLOR_BUFFER_BIT);     while (!glfwWindowShouldClose(window))     {         processInput(window);         glfwSwapBuffers(window);         glfwPollEvents();     }     glfwTerminate();     return 0; }
The result should be a fixed dark green-blueish color as the end of here. But the color of my window turns from black to green-blueish repeatedly in high speed! I thought it might be a problem with my Graphics card driver but I've updated it and it's: NVIDIA GeForce GTX 750 Ti.
What is the problem and how to solve it please?

• 10
• 12
• 22
• 13
• 34