View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Avoiding unnessecary if/else choice in loop for Virtual Camera

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

6 replies to this topic

### #1tmason  Members

Posted 14 August 2014 - 03:23 PM

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.

### #2Nypyren  Members

Posted 14 August 2014 - 03:36 PM

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.

### #3aregee  Members

Posted 14 August 2014 - 03:52 PM

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.

### #4tmason  Members

Posted 14 August 2014 - 03:57 PM

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.

### #5tmason  Members

Posted 14 August 2014 - 03:59 PM

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.

### #6Aliii  Members

Posted 14 August 2014 - 11:05 PM

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.

### #7aregee  Members

Posted 23 August 2014 - 06:32 PM

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.

Edited by aregee, 23 August 2014 - 06:33 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.