C++ Angles from integers

Hi Guys, I have a program below, I am basically trying to make the camera spin around the mock triangles (as if in orbit and always facing them). I am just butchering opengl tutorial programs to learn how things work, but I've hit my first (and somewhat familiar) snag.

Witness the FRAME, ANGLE and DISTANCE values. The angle value should provide a figure for the angle of the camera, and the DISTANCE is a set figure which is how far from the triangles the camera orbits. However something is wrong.

At first I thought i was getting ultra-low framerate, but i believe whats happening is it is rounding the angle figure to a whole number, which means a number of frames go by where the camera hasn't moved. The important lines I've cute out below:

These set the variables

int frame = 0;
float angle = 0;
float distance = 20;

Increase the frame counter by 1 and adjust the angle:

frame++;
angle = frame/100;

Spin the camera around the triagles (well not actually the triangles, but point 0,0,0 - where the triangles are:

glTranslatef(sin(angle)*distance, 1.0f, cos(angle)*distance );

I've a feeling the problem is because I'm telling it to divide a integer to get a float value. I have always been under the impression that because I am saying I need the result to be a float variable called ANGLE, it will not automatically round up the result just because there is an integer involved.

I have always found the only way to correct this, in whatever programming language (including here) is by changing FRAME to be a float value. However I don't like to do that because then float is something it doesn't need to be.

Is there another way of achieving this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <GL/glfw.h>
#include <math.h>

int main()
{
    int     width, height;
    float   frame = 0;
    float   angle = 0;
    float   distance = 20;

    bool    running = true;

    glfwInit();

    if( !glfwOpenWindow( 512, 512, 0, 0, 0, 0, 0, 0, GLFW_WINDOW ) )
    {
        glfwTerminate();
        return 0;
    }

    glfwSetWindowTitle("GLFW Application");

    while(running)
    {
        frame++;
        angle = frame/100;

        glfwGetWindowSize( &width, &height );
        height = height > 0 ? height : 1;

        glViewport( 0, 0, width, height );

        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
        glClear( GL_COLOR_BUFFER_BIT );

        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        gluPerspective( 95.0f, (GLfloat)width/(GLfloat)height, 1.0f, 100.0f );
        glTranslatef(sin(angle)*distance, 1.0f, cos(angle)*distance );

        // Draw some rotating garbage
        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();
        gluLookAt(0.0f, -10.0f, 0.0f,
                0.0f, 0.0f, 0.0f,
                0.0f, 0.0f, 1.0f );

        //glTranslatef( 1.0f, 1.0f, 0.0f );
        //glRotatef(frame*2, 2.00f, 0.00f, 0.75f);
        glBegin( GL_TRIANGLES );
          glColor3f(0.1f, 0.0f, 0.0f );
          glVertex3f(0.1f, 0.0f, -4.0f);
          glColor3f(1.0f, 1.0f, 0.0f );
          glVertex3f(0.1f, 0.0f, 4.0f);
          glColor3f(0.0f, 0.0f, 1.0f );
          glVertex3f(-2.0f, 0.0f, 0.0f);
        glEnd();
        glBegin( GL_TRIANGLES );
          glColor3f(1.0f, 0.1f, 0.0f );
          glVertex3f(-0.1f, 0.0f, -4.0f);
          glColor3f(0.0f, 0.0f, 1.0f );
          glVertex3f(-0.1f, 0.0f, 4.0f);
          glColor3f(1.0f, 0.0f, 0.0f );
          glVertex3f(-2.0f, 0.0f, 0.0f);
        glEnd();
        glfwSwapBuffers();

        // exit if ESC was pressed or window was closed
        running = !glfwGetKey(GLFW_KEY_ESC) && glfwGetWindowParam( GLFW_OPENED);
    }

    glfwTerminate();

    return 0;
}
If frame is supposed to be an integer, then declare it as such.
You could fix this by putting either
 
angle = float(frame)/100;
or better, make the other value a float:
 
angle = frame/100.0f;
Ahh i see, so essentially what happened is because 100 appears to be an integer, and FRAME IS an integer, its calculation returns an integer related value. I will remember that ".0f" trick. I've seen "f" already on the end of most values, but at least now I know it stands for "float"!
Yes, the f suffix means 'float'. Otherwise, 100.0 would be considered a double, which is ok too, but involves a conversion from double to float afterwards, when assigning the result to angle.

The possible suffixes and their meaning are described here:
http://www.cplusplus.com/doc/tutorial/constants/
Topic archived. No new replies allowed.