with glew, my program never goes past 60fps

View: New views
4 Messages — Rating Filter:   Alert me  

with glew, my program never goes past 60fps

by felpa :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I use SDL/glew to create a simple OpenGL program. I don't do anything strange, just initialize SDL, create an OGL context, and initialize glew before the drawing loop. In the main loop, no matter what I do, whether it's just a few triangles or near a million, it always goes <=60FPS, but never past that.
If I only use SDL/OGL (by removing the glew initialization) I get all the range from 0 to infinity FPS. ¿Is there any reason why this would happen?

This is the code, if it helps:
#include <sdl.h>
#include <GL/glew.h>
//#include <sdl_opengl.h>

#include <tchar.h>
#include <math.h>

#include <iostream>

#include "Camera.h"
#include "Quadtree.h"
#include "common.h"


Camera myCamera;
bool up=false, down=false, left=false, right=false, a=false, z=false, w=false, s=false,
        l=false, o=false, lineMode=false;


                GLfloat *myMesh;
                GLuint *myMeshIndices;
                int myMeshXDim, myMeshYDim, myMeshNumberOfTriangles;


//
// Set up a purdy triangle as our vertex data.
GLfloat vertexData[] = {
                                                        0, 100, 0, //0
                                                        10, 100, 0,//1
                                                        20, 100, 0,//2
                                                        30, 100, 0,//3
                                                        40, 100, 0,//4

                                                        0, 90, 0,//5
                                                        10, 90, 0,//6
                                                        20, 90, 0,//7
                                                        30, 90, 0,//8
                                                        40, 90, 0,//9

                                                        0, 80, 0,//10
                                                        10, 80, 0,//11
                                                        20, 80, 0,//12
                                                        30, 80, 0,//13
                                                        40, 80, 0,//14
};

GLushort indexData[] = { 0, 5,  1, 6,  2, 7,  3, 8,  4, 9,
                                                9, 5,
                                                 5, 10, 6, 11, 7, 12, 8, 13, 9, 14
};

// Set a lovely teal with some Gouraud shading for
// our triangle.
GLfloat colourData[] = { 0.f, 0.f, 1.f
                                                , 0.f, 1.f, 1.f
                                                , 1.f, 1.f, 1.f
                                                };

// Create some 'ids' for the buffers.
GLuint vertexBuffer = 0;
GLuint colourBuffer = 0;
GLuint indexBuffer = 0;
//

void InitGraphics(Quadtree *quadtree)
{
    // Initialize SDL
    if (SDL_Init(SDL_INIT_VIDEO) < 0) exit(1);

    // Window Settings
    SDL_WM_SetCaption("SDL/OpenGL Sample", "SDL/OpenGL Sample");

    // Get Video Info
    const SDL_VideoInfo *video = SDL_GetVideoInfo();
    if (!video) { SDL_Quit(); exit(2); }

    // Create Video Surface
    SDL_Surface *screen = SDL_SetVideoMode(HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION,
                                                                                        video->vfmt->BitsPerPixel, SDL_OPENGL);
    if (!screen) { SDL_Quit(); exit(3); }



    // Size OpenGL to Video Surface
    glViewport(0, 0, HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(FOVY_DEGREES, (float)HORIZONTAL_RESOLUTION / (float)VERTICAL_RESOLUTION,
                                        NEAR_CLIPPING_PLANE, FAR_CLIPPING_PLANE);
    glMatrixMode(GL_MODELVIEW);


        //
        {
                int err;
                if (GLEW_OK != (err=glewInit()))
                {
                  // Problem: glewInit failed, something is seriously wrong.
                  throw glewGetErrorString(err);
                }
        }
        //

    // OpenGL Render Settings
    glClearColor(0, 0, 0, 1);
    //glClearDepth(1.0);
    //glEnable(GL_DEPTH_TEST);

        // other opengl settings
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);//GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);//GL_CLAMP_TO_EDGE);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
        glEnable(GL_TEXTURE_2D);

        //glFrontFace(GL_CCW);
        //glEnable(GL_CULL_FACE);
        //glPolygonMode(GL_FRONT, GL_FLAT);
        //glPolygonMode(GL_BACK, GL_LINE);
        //

        // set initial camera pos
        myCamera.setPosition(0, 10, -10);
        myCamera.setSpeed(50);
}

// Draw the scene
void DrawGraphics(Quadtree *myQuadtree)
{

        if (lineMode)
                        glPolygonMode(GL_FRONT, GL_LINE);
        else
                        glPolygonMode(GL_FRONT, GL_FILL);
        //
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


    // Set location in front of camera
    glLoadIdentity();
        myCamera.SetLookAt();

    glColor3d(1, 1, 1);


        //glBegin(GL_TRIANGLES);
        //glVertex3f(10, 10, 10);
        //glVertex3f(20, 10, 10);
        //glVertex3f(30, 30, 10);
        //glEnd();


        //
        for (int i=0; i<2; i++)
        {
                //glTranslatef(i, i, i);
                // --- VBO's in action.
                // Set the vertexBuffer as the current buffer.
                glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
               
                // Specify that it's data is vertex data.
                glVertexPointer(3, GL_FLOAT, 0, 0);
               
                // Tell OpenGL to draw what it sees. ;)
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
                glDrawElements(GL_TRIANGLE_STRIP, myMeshNumberOfTriangles, GL_UNSIGNED_INT, NULL);
        }
        //

        glFlush();
        //glFinish();
    // Show the frame
    SDL_GL_SwapBuffers();

}

bool Events();


// Program entry point
int main(int argc, char **argv)
{
        Quadtree *myQuadtree;
        static char buffer[100]={0};
        int msCount=0;


        myQuadtree= new Quadtree(&myCamera);

        // esto tiene que ir antes de intentar cargar nada en el quadtree porque
        // OpenGL tiene que haber creado un contexto para empezar a reservar nombres
        // de texturas y otras cosas que se hacen en generateTree
        InitGraphics(myQuadtree);

        // cargamos y creamos la estructura del quadtree
        try
        {
                myMeshXDim=900; myMeshYDim=900;
                myMeshNumberOfTriangles=((myMeshYDim-2)*(2*(myMeshXDim+1))) + (2*myMeshXDim)+2;

                if ((myMesh=new GLfloat[myMeshXDim*myMeshYDim*3])==NULL) throw "nmem";

                int i, y, x;
                // puntos
                i=0;
                for (int y=0; y<myMeshYDim; y++)
                {
                        for (int x=0; x<myMeshXDim; x++)
                        {
                                myMesh[i+0]=x*10;
                                myMesh[i+1]=y*10;
                                myMesh[i+2]=0;
                                i+=3;
                        }
                }

                if ((myMeshIndices=new GLuint[myMeshNumberOfTriangles])==NULL) throw "nmem";

                // indices
                i=0;
                for (y=0; y<(myMeshYDim-1); y++)
                {
                        for (x=0; x<myMeshXDim; x++)
                        {
                                myMeshIndices[i]=((y*myMeshXDim)+x);
                                myMeshIndices[i+1]=(((y+1)*myMeshXDim)+x);
                                i+=2;
                        }
                        // triángulos nulos para dar la vuelta
                        x=(myMeshXDim-1);
                        myMeshIndices[i]=(((y+1)*myMeshXDim)+x);
                        x=0;
                        myMeshIndices[i+1]=(((y+1)*myMeshXDim)+x);
                        i+=2;
                }
                // corregir el último triángulo para que sea nulo
                myMeshIndices[i-1]=myMeshIndices[i-2];
                if (i!=myMeshNumberOfTriangles) throw "myMeshNumberOfTriangles";


                //
                // 1.- Generated a buffer name for our vertex buffer,
                glGenBuffers(1, &vertexBuffer);
                // 2.- Set our current 'in-use' buffer to our vertex buffer.
                glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
                // 3.- Fill out the data buffer using our initialized values.
                glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*myMeshXDim*myMeshYDim, myMesh, GL_STATIC_DRAW);
                //glBufferData(GL_ARRAY_BUFFER, sizeof(vertexData), vertexData, GL_STATIC_DRAW);

                // same for the index buffer
                glGenBuffers(1, &indexBuffer);
                glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBuffer);
                glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*myMeshNumberOfTriangles, myMeshIndices, GL_STATIC_DRAW);
                //glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexData), indexData, GL_STATIC_DRAW);
               
                // enable state
                glEnableClientState(GL_VERTEX_ARRAY);
        }

        catch (char* e)
        {
                SDL_SetError(e);
                FILE *f=fopen("c:\errors.txt", "w+"); fprintf(f, e); fclose(f);
                SDL_Quit();
                return -1;
        }



    // Event loop
    while (Events())
    {
                int ticksI=SDL_GetTicks();
                        DrawGraphics(myQuadtree);
                int ticksF=SDL_GetTicks();
                msCount+=(ticksF-ticksI);
                float FPS=(float)1/(float)(ticksF-ticksI); FPS*=1000;
                if (msCount>1000)
                {
                        sprintf(buffer, "%f FPS", FPS );
                        SDL_WM_SetCaption( buffer,0 );
                        msCount=0;
                }
    }

    // Clean up
    SDL_Quit();
    return 0;
}

// Handle events
bool Events()
{
    SDL_Event event;

    while (SDL_PollEvent(&event))
    {
        switch (event.type)
        {
                        case SDL_KEYUP:
                        if (event.key.keysym.sym == SDLK_UP)
                        {
                                up=false;
                        }
                        if (event.key.keysym.sym == SDLK_DOWN)
                        {
                                down=false;
                        }
                        if (event.key.keysym.sym == SDLK_LEFT)
                        {
                                left=false;
                        }
                        if (event.key.keysym.sym == SDLK_RIGHT)
                        {
                                right=false;
                        }
                        if (event.key.keysym.sym == SDLK_a)
                        {
                                a=false;
                        }
                        if (event.key.keysym.sym == SDLK_z)
                        {
                                z=false;
                        }
                        if (event.key.keysym.sym == SDLK_w) w=false;
                        if (event.key.keysym.sym == SDLK_s) s=false;
                        if (event.key.keysym.sym == SDLK_l) l=false;
                        if (event.key.keysym.sym == SDLK_p) o=false;

                        break;

        case SDL_KEYDOWN:
            // Exit when ESC is pressed
            if (event.key.keysym.sym == SDLK_ESCAPE) return false;

                        if (event.key.keysym.sym == SDLK_UP) up=true;
                        if (event.key.keysym.sym == SDLK_DOWN) down=true;
                        if (event.key.keysym.sym == SDLK_LEFT) left=true;
                        if (event.key.keysym.sym == SDLK_RIGHT) right=true;
                        if (event.key.keysym.sym == SDLK_a) a=true;
                        if (event.key.keysym.sym == SDLK_z) z=true;
                        if (event.key.keysym.sym == SDLK_w) w=true;
                        if (event.key.keysym.sym == SDLK_s) s=true;
                        if (event.key.keysym.sym == SDLK_l) l=true;
                        if (event.key.keysym.sym == SDLK_p) o=true;
                        break;

        case SDL_QUIT:
            return false;
        }
    }

        if (right) myCamera.rotateInY(-1);
        if (left) myCamera.rotateInY(1);

        if (down) myCamera.moveBackwards();
        if (up) myCamera.moveForward();

        if (a) myCamera.move(0, 0, 1);
        if (z) myCamera.move(0, 0, -1);

        if (w) myCamera.move(0, 1, 0);
        if (s) myCamera.move(0, -1, 0);

        if (o) lineMode=false;
        if (l) lineMode=true;

    return true;
}


Re: with glew, my program never goes past 60fps

by Nigel Stewart-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

My first suggestion would be to eliminate glFlush and/or glFinish,
assuming that SDL_GL_SwapBuffers takes care of that.

- Nigel

> glFlush();
> //glFinish();
>     // Show the frame
>     SDL_GL_SwapBuffers();


-----------------------------------------------------------------------------------
This email message is for the sole use of the intended recipient(s) and may contain
confidential information.  Any unauthorized review, use, disclosure or distribution
is prohibited.  If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
-----------------------------------------------------------------------------------

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
glew-users mailing list
glew-users@...
https://lists.sourceforge.net/lists/listinfo/glew-users

Re: with glew, my program never goes past 60fps

by Juan Linietsky :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Fri, Aug 14, 2009 at 12:50 PM, felpa<christopher.orihuela@...> wrote:
>
> I use SDL/glew to create a simple OpenGL program. I don't do anything
> strange, just initialize SDL, create an OGL context, and initialize glew
> before the drawing loop. In the main loop, no matter what I do, whether it's
> just a few triangles or near a million, it always goes <=60FPS, but never
> past that.
> If I only use SDL/OGL (by removing the glew initialization) I get all the
> range from 0 to infinity FPS. ¿Is there any reason why this would happen?
>

Yes,  you are syncing to the screen refresh, fingure out how to not do
it, as glew has nothing to do with that...

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
glew-users mailing list
glew-users@...
https://lists.sourceforge.net/lists/listinfo/glew-users

Re: with glew, my program never goes past 60fps

by felpa :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

In case anyone ever googles this I thought I should post the way I fixed it:

Somehow, by not including <sdl_opengl.h> the vertical refresh sync got enabled, in the end it was just a matter of disabling it before creating the screen, like this:

// Create Video Surface
        SDL_GL_SetAttribute( SDL_GL_SWAP_CONTROL, 0 ); // vertical retrace sync off
    SDL_Surface *screen = SDL_SetVideoMode(HORIZONTAL_RESOLUTION, VERTICAL_RESOLUTION,
                                                                                        video->vfmt->BitsPerPixel, SDL_OPENGL);

As sugested, I also removed glFlush() before SDL_GL_SwapBuffers().