OpenGL Insanely Slow

lochok
  • OpenGL Insanely Slow lochok

    I'm learning C++, and I'm writing my first OpenGL program. Unfortunately, it seems to be defaulting to Software Rendering (CPU uses bounces, GPU uses stays at 1%). I'm using SDL as the Windowing system. I've got no idea why, but when there is significant scaling, the program grinds to a halt. My PC should be able to handle it - I'm using an 8800GTX. My texture is 1024x1024, 32 bit RGBA and loaded with DevIL, and glGetString(GL_VENDOR) is returning "NVidia Corporation". Any ideas?

    I'm sorry to be dumping all the code, but I've got absolutely no idea at all what is doing it. I'm very new to OpenGL.

    #pragma once
    #include "SDL.h"
    #include "SDL_opengl.h"7
    #include "IL/il.h"
    #include "IL/ilut.h"
    #include <math.h>    
    
    const int SCREEN_WIDTH = 1024;
    const int SCREEN_HEIGHT = 768;
    const int SCREEN_BPP = 32;
    const int FPS = 60;
    const int XSIZE = 1024; 
    const int YSIZE = 768;
    
    int main( int argc, char *argv[] )
    {
        bool quit = false;
    
        SDL_Init(SDL_INIT_EVERYTHING);
        SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
        SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
        SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    
        SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE |  SDL_OPENGL);     
    
        glEnable(GL_TEXTURE_2D);
    
        const GLubyte * Vendor = glGetString(GL_VENDOR);
    
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red      
        glMatrixMode (GL_PROJECTION);
        glLoadIdentity ();
        glOrtho(0, XSIZE, YSIZE, 0, 0, 1);
        glMatrixMode (GL_MODELVIEW);
    
        glDisable(GL_DEPTH_TEST);
    
        GLuint image;
    
        ilInit();
        ilutRenderer(ILUT_OPENGL);  
    
        ILuint texid;
        ILboolean success;
        ilGenImages(1, &texid);
        ilBindImage(texid);
    
        success = ilLoadImage("Textures.png");
        if (success)
        {
            success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
            if (!success)
            {
                SDL_Quit();
                return -1;
            }
            glGenTextures(1, &image);
            glBindTexture(GL_TEXTURE_2D, image);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
            GL_NEAREST);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
            GL_NEAREST);
            glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
              ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
              ilGetData());
            ilDeleteImages(1, &texid);
        }
        else
        {
            SDL_Quit();
            return -2;
        }
    
    
        SDL_Event event;
    
        float x = 0.0f;
    
        while (!quit) 
        {
            while ( SDL_PollEvent(&event) )
            {
                switch (event.type)
                {
                    case SDL_QUIT:
                        quit = true;
                        break;
                }
            }
    
            glClear(GL_COLOR_BUFFER_BIT);
    
            glLoadIdentity();
            glTranslatef(200.0f, 200.0f, 0.0f);
            glRotatef(x,0.0f,0.0f, 1.0f);       
            glScalef(1.0f+x, 1.0f+x, 1.0f+x);
            glBegin(GL_QUADS);      
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(-100, -100); 
            glTexCoord2f(0.0f, 0.119140f);      
            glVertex2f(-100, 100); 
            glTexCoord2f(0.091797f, 0.119140f);
            glVertex2f(100, 100); 
            glTexCoord2f(0.091797f, 0.0f);
            glVertex2f(100, -100);
            glEnd();
    
            x += 0.1f;
    
            SDL_GL_SwapBuffers();
            glFlush;        
        }
    
        return 0;
    }
    

    Edit : (24/04/2011 9.22 PM AEST) When run on an ancient card, on an ancient computer, it maintains ~60FPS.

  • What do you mean by insanely slow? You have no timing in your code. How many frames per second?

    Your drawing very little, it's normal for the GPU to be mostly unused. You can't determine if it's software rendering just by looking at CPU/GPU usage.

  • Try to disable texturing before rendering (before while loop): glDisable(GL_TEXTURE_2D); - if it helps, there is problem with texturing. But I don't think it should help.

    You can also try to comment everything from glBegin(GL_QUADS); to glEnd(); - but you will not see if it helps. You would need some FPS counter.

    Also try to sleep while loop at the end for some time (let's say for 20 ms). But I don't think it will help anyway.

    And also try to change glOrtho(0, XSIZE, YSIZE, 0, 0, 1); to glOrtho(0, XSIZE, 0, YSIZE, 0, 1); Is your code rendering something?

  • You should not need to use glFlush(). Also it's advisable to use vertex arrays or VBOs instead of immediate mode, though it probably won't make a difference in a program as small as this.

  • Are your textures a power of 2 size? (4x4, 64x64 etc.) I think that could definitely slow things way down if they are not.

  • I've got no idea why, but it seems to have repaired itself. I even put a NPOT texture to test and I'm still getting very, very reasonable FPS rates. I don't think I've changed anything, and I've really got not idea at all why it is fixed. I'm sorry that this doesn't seem to be really answering the question. If anybody else can decipher why - you're doing better then me.

    My final source is below:

    #pragma once
    #include "SDL.h"
    #include "SDL_opengl.h"
    #include "IL/il.h"
    #include "IL/ilut.h"
    #include <math.h>    
    #include <fstream>
    
    const int SCREEN_WIDTH = 1024;
    const int SCREEN_HEIGHT = 768;
    const int SCREEN_BPP = 32;
    const int FPS = 60;
    const int XSIZE = 1024; 
    const int YSIZE = 768;
    
    using namespace std;
    
    int main( int argc, char *argv[] )
    {
    bool quit = false;
    
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
    SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
    
    SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_HWSURFACE |  SDL_OPENGL);     
    
    glEnable(GL_TEXTURE_2D);
    
    const GLubyte * Vendor = glGetString(GL_VENDOR);
    
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Clear the background of our window to red      
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    glOrtho(0, XSIZE, YSIZE, 0, 0, 1);
    glMatrixMode (GL_MODELVIEW);
    
    glDisable(GL_DEPTH_TEST);
    
    GLuint image;
    
    ilInit();
    ilutRenderer(ILUT_OPENGL);  
    
    ILuint texid;
    ILboolean success;
    ilGenImages(1, &texid);
    ilBindImage(texid);
    
    success = ilLoadImage("Textures.png");
    
        Uint32 startclock = 0;
    Uint32 deltaclock = 0;
    Uint32 currentFPS = 0;      
    startclock = SDL_GetTicks();
    deltaclock = SDL_GetTicks();
    
    fstream Log;
    Log.open("log.txt", ios::out | ios::trunc);
    Log.clear();    
    
    if (success)
    {
        success = ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
        if (!success)
        {
            SDL_Quit();
            return -1;
        }
        glGenTextures(1, &image);
        glBindTexture(GL_TEXTURE_2D, image);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
        GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
        GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH),
          ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE,
          ilGetData());
        ilDeleteImages(1, &texid);
    }
    else
    {
        SDL_Quit();
        return -2;
    }
    
    
    SDL_Event event;
    
    float x = 0.0f;
    
    while (!quit) 
    {
        while ( SDL_PollEvent(&event) )
        {
            switch (event.type)
            {
                case SDL_QUIT:
                    quit = true;
                    break;
            }
        }
    
        glClear(GL_COLOR_BUFFER_BIT);
    
        glLoadIdentity();
        glTranslatef(200.0f, 200.0f, 0.0f);
        glRotatef(x,0.0f,0.0f, 1.0f);       
        glScalef(1.0f+x, 1.0f+x, 1.0f+x);
        glBegin(GL_QUADS);      
        glTexCoord2f(0.0f, 0.0f);
        glVertex2f(-100, -100); 
        glTexCoord2f(0.0f, 0.119140f);      
        glVertex2f(-100, 100); 
        glTexCoord2f(0.091797f, 0.119140f);
        glVertex2f(100, 100); 
        glTexCoord2f(0.091797f, 0.0f);
        glVertex2f(100, -100);
        glEnd();
    
        x += 0.1f;
    
        SDL_GL_SwapBuffers();
    
        deltaclock = SDL_GetTicks() - startclock;
        startclock = SDL_GetTicks();
        if ( deltaclock != 0 )  currentFPS = 1000 / deltaclock; 
        static char buffer[20] = {0}; 
        sprintf( buffer, "%d ", currentFPS ); 
        SDL_WM_SetCaption( buffer,0 ); 
    
        if (currentFPS < 4) { quit = true ;}
    
        Log << buffer;
    
    }
    Log.close();
    return 0;
    }
    

Tags
opengl c++ sdl
Related questions and answers
  • , temporarySurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temporarySurface->pixels); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG...I am making a game using OpenGL, with SDL_Image employed to load the textures. When drawn on screen, there's no alpha -- in an exact square around the opaque contents of the texture, black is drawn; outside that space, white is drawn. Here is my OpenGL init code... SDL_SetVideoMode(windowWidth, windowHeight, 16, SDL_HWSURFACE|SDL_GL_DOUBLEBUFFER|SDL_OPENGL); glClearColor(0, 0, 0, 0

  • (); glOrtho(0, Width, Height, 0, -1, 1); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); Just in-case, here's my image loading code, I think that perhaps this may be were the problem lies somehow..., bmask, amask); SDL_BlitSurface(LoadedImage, NULL, image, NULL); glGenTextures(1, &amp;texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL..._MIN_FILTER, GL_LINEAR); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels); return texture; }

  • is the init code for the FBO and it's texture: glGenTextures(1,&amp;fboimg); glBindTexture(GL_TEXTURE_2D,fboimg); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 512, 512, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindTexture(GL_TEXTURE_2D, 0); glGenFramebuffers(1,&amp;fboHandle); glBindFramebuffer(GL_FRAMEBUFFER,fboHandle

  • ; unsigned int m_TextureID; void initFBO() { m_FBOWidth = screen_width; m_FBOHeight = screen_height; glGenRenderbuffers(1, &amp;depth_buffer); glBindRenderbuffer(GL_RENDERBUFFER...); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_FBOWidth, m_FBOHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindTexture(GL

  • (1, &amp;shaderTexture[0]); // Get A Free Texture ID ( NEW ) glBindTexture (GL_TEXTURE_1D, shaderTexture[0]); // Bind This Texture. From Now On It Will Be 1D ( NEW ) // For Crying Out Loud Don't Let OpenGL Use Bi/Trilinear Filtering! ( NEW ) glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri (GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage1D (GL_TEXTURE_1D, 0, GL_RGB, 32, 0, GL_RGB , GL_FLOAT, shaderData); // Upload ( NEW ) } This is the drawing for the animated MD2 model: void

  • ; scd.BufferDesc.Width = SCREEN_WIDTH; scd.BufferDesc.Height = SCREEN_HEIGHT; scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; scd.OutputWindow = hWnd; scd.SampleDesc.Count = 1; scd.SampleDesc.Quality = 0...(zbd)); zbd.Width = SCREEN_WIDTH; // set the width of the depth buffer zbd.Height = SCREEN_HEIGHT; // set the height of the depth buffer zbd.ArraySize = 1; // we are only creating one texture... viewport.Width = SCREEN_WIDTH; // set the width to the window's width viewport.Height = SCREEN_HEIGHT; // set the height to the window's height viewport.MinDepth = 0; // the closest an object can

  • ;fstream> #include <exception> MD2::MD2(const std::string& Path) : m_TextureID(0), m_Scale(1.0f) { std::ifstream File(Path.c_str(), std::iostream::in | std::ifstream::binary); if(!File.is_open()) { Debugging::Debugf("Unable to open the file: %s", Path.c_str()); exit(0); } // Get the filesize. File.seekg (0, std::ios::end); int Size..._Commands, m_MD2Header.num_glcommands * sizeof(int)); // Read all the data. for(int i = 0; i < m_MD2Header.num_frames; ++i) { md2_frame_t* Frame = (md2_frame_t*)&amp;Buffer

  • is identified, so that's all good. I'm writing the game in C++ with OpenGl/GLFW The drawing function is: int win_width; int win_height; glfwGetWindowSize(&amp;win_width, &amp;win_height); float win_aspect = (float)win_width / (float)win_height; glViewport(0, 0, win_width, win_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90, win_aspect, 1, 100.0); glMatrixMode(GL_MODELVIEW...(GL_LIGHT0); glEnable(GL_LIGHT1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(0.0f, 0.0f, 0.0f); int vertexIndex = 0, normalIndex; glRotatef(90, 0, 1, 0); glPushMatrix(); for(int

  • = xyCoords.y; m_font->DrawTextA(NULL, stateStream.str().c_str(), -1, &amp;rct, DT_LEFT|DT_NOCLIP , D3DXCOLOR(1,0,0,1)); This all works perfectly. Where am I going wrong with the first bit of code... bubble D3DXVECTOR2 origin = D3DXVECTOR2( (0.0f / SCREEN_WIDTH) * 2.0f - 1.0f, (0.0f / SCREEN_HEIGHT) * 2.0f - 1.0f); D3DXVECTOR2 headPos = D3DXVECTOR2( (xyCoords.x / SCREEN_WIDTH) * 2.0f - 1.0f, (xyCoords.y / SCREEN_HEIGHT) * 2.0f - 1.0f); lineRenderer.Draw2DLine(origin, headPos, D3DXCOLOR(1, 0, 0, 1), D3DXCOLOR(1, 1, 0, 1)); origin has a value of -1, -1 which is fine