Avoiding unnessecary if/else choice in loop for Virtual Camera

Started by
5 comments, last by aregee 9 years, 8 months ago

Hello,

So I have more of a best practice question that I am wondering what is the best approach for.

I have a working virtual "camera" for OpenGL that builds the correct view and projection matrices for upload into OpenGL.

Let's represent that with the psuedo-function UpdateCamera(). This camera is inside a class OpenGLCamera which also has another function to set whether we want to display a perspective view or orthogonal view. Let's call this function SwitchView() and the flag isPerspectiveOn.

Now, inside the update camera function, I have code that looks something like this:


void OpenGLCamera::UpdateCamera() {
	/*
	
	Calculate position, direction, determine rotation, etc.
	
	View Matrix is built.
	
	*/
	
	if (isPerspectiveOn == true)
	{
		ProjectionMatrix = glm::perspective(FieldOfView, AspectRatio, NearPlane, FarPlane);
	} else {
		Projection = glm::ortho(CurrentOrthoParameters.LeftPlane, CurrentOrthoParameters.RightPlane,
		                        CurrentOrthoParameters.BottomPlane, CurrentOrthoParameters.TopPlane);
	}
	
	ProjectionViewMatrix = ProjectionMatrix * ViewMatrix;

}

What I am wondering is if there is a way to simply switch which projection function I am using without needing to perform if/else test all of the time?

In other words, perhaps a way to set the isPerspectiveOn flag somewhere else and with a change in how I write the code I don't have to perform this test with every loop or per frame.

I feel it may seem trivial but the least amount of testing I am doing per frame the better.

Thank you for your time and if I need to clarify further let me know.

Advertisement
Store the projection matrix as a member variable and move the projection matrix setup into a function that's only run when initializing the class or when changing the isPerspectiveOn variable.

I feel it may seem trivial but the least amount of testing I am doing per frame the better.

Apart from Nypren's answer, if you are doing that if-test just once every frame, assuming 60 frames per second, it won't slow down code much at all. There might be smarter ways to do things, like Nypyren's suggestion, but premature optimisation is not a good idea.

Store the projection matrix as a member variable and move the projection matrix setup into a function that's only run when initializing the class or when changing the isPerspectiveOn variable.


Good catch; I don't update the Projection Matrix every frame so that is a good idea.

I feel it may seem trivial but the least amount of testing I am doing per frame the better.


Apart from Nypren's answer, if you are doing that if-test just once every frame, assuming 60 frames per second, it won't slow down code much at all. There might be smarter ways to do things, like Nypyren's suggestion, but premature optimisation is not a good idea.


I see; I felt I post the question before making any changes to see what other minds would think.

Since this seems simple enough I can make the change but I also see where you are coming from and can agree with that as well.

If you are concerned about branch misprediction put the less likely case into the "else" block. But things that are done once-per-frame usually have no effect on performance, I would choose the solution that gives the cleanest code.

I feel it may seem trivial but the least amount of testing I am doing per frame the better.


Apart from Nypren's answer, if you are doing that if-test just once every frame, assuming 60 frames per second, it won't slow down code much at all. There might be smarter ways to do things, like Nypyren's suggestion, but premature optimisation is not a good idea.


I see; I felt I post the question before making any changes to see what other minds would think.

Since this seems simple enough I can make the change but I also see where you are coming from and can agree with that as well.

Yes, I agree. I am trying to be the kind of positive thinker, but sometimes I slip. Thinking about it I kind of do optimise in various ways myself all the time, when I get ideas or find places that seem just bad. Somehow, I even feel that refactoring can be some kind of optimisations too, and I do THAT a lot... Sorry. :)

This topic is closed to new replies.

Advertisement