OpenGL lag problem

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

OpenGL lag problem

Post by N64vSNES »

Ok so I've basicly got a problem with openGL if I use glut and openGL I can render hundreds of polygons on my crappy pentium 4 PC but if I used SDL instead of glut I can only render 10-20 textured polygons before it slows down surverly.

Also with glut A incative window doing nothing ( but looping I.E. clear screen, flip screen ) runs at about 8% CPU at 60 FPS however SDL at 60 FPS completely blank window its at 30-40%

I'm using the SDL and openGL headers of SDL 1.2

Anyone got any ideas?
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

Can you post the entire SDL/OpenGL version's code?
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL lag problem

Post by N64vSNES »

X Abstract X wrote:Can you post the entire SDL/OpenGL version's code?
Define the entire versions code?
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

N64vSNES wrote:
X Abstract X wrote:Can you post the entire SDL/OpenGL version's code?
Define the entire versions code?
The code for the SDL/OpenGL textured polygon rendering app.
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL lag problem

Post by N64vSNES »

Code: Select all


#include "Rendering.h"

#include <iostream>

int NextPowerOfTwo(unsigned int value) {

	int number = 1;

	while ( number < value ) {
		number *= 2;
	}
	std::cout << number << std::endl;
	return number;
}


GLuint Image::GetGlTex() {
	return Texture;		// Return the texture ( makes it read only ) 
}

void Image::Render() {

     
glPushMatrix();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,Texture);
glTranslatef(Pos.x,Pos.y,0);
glRotatef(Pos.r,0,0,1);
glColor3f(r,g,b);
glBegin(GL_QUADS);

glTexCoord2f(Crop.x / Surface->w,Crop.y / Surface->h );

glVertex2f( - Pos.w / 2.0f,- Pos.h / 2.0f);

glTexCoord2f( ( Crop.x + Crop.w ) / Surface->w,Crop.y / Surface->h );

glVertex2f(Pos.w / 2.0f,- Pos.h / 2.0f );

glTexCoord2f(  ( Crop.x + Crop.w ) / Surface->w, ( Crop.y + Crop.h ) / Surface->h );

glVertex2f(Pos.w / 2.0f,Pos.h / 2.0f );

glTexCoord2f(Crop.x / Surface->w, ( Crop.y + Crop.h ) / Surface->h );

glVertex2f( - Pos.w / 2.0f,Pos.h / 2.0f );

glEnd();

glPopMatrix();

}

void Image::SetClip(float cropx,float cropy,float cropw,float croph) {
	Crop.x = cropx;
	Crop.y = cropy;
	Crop.w = cropw;
	Crop.h = croph;
}

void Image::SetWidthHeight(float w,float h) {
	Pos.w = w;
	Pos.h = h;
}


void Image::SetPosition(float x,float y) {
Pos.x = x;
Pos.y = y;
}

void Image::SetX(float x) {
Pos.x = x;
}

void Image::SetY(float y) {
Pos.y = y;
}

void Image::SetWidth(float w) {
Pos.w = w;
}

void Image::SetHeight(float h) {
Pos.h = h;
}

float Image::GetX() {
return Pos.x;
}

float Image::GetY() {
return Pos.y;
}

float Image::GetWidth() {
return Pos.w;
}

float Image::GetHeight() {
return Pos.h;
}

void Image::Load(char FILE[]) {

using namespace std;

glEnable(GL_TEXTURE_2D);

Surface = NULL;

SDL_Surface *TMP = IMG_Load(FILE);

if ( TMP == NULL ) {
	printf("Failed to load image- %s\n",FILE);
	return;
}

SDL_SetColorKey(TMP,SDL_SRCCOLORKEY,SDL_MapRGB(TMP->format,0xff,0x00,0xff));

Surface = SDL_DisplayFormatAlpha(TMP);

Surface->w = NextPowerOfTwo(Surface->w);
Surface->h = NextPowerOfTwo(Surface->h);

SDL_FreeSurface(TMP);

glGenTextures(1,&Texture);

glBindTexture(GL_TEXTURE_2D,Texture);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);


        if (Surface->format->Rmask == 0x000000ff)
{
glTexImage2D(GL_TEXTURE_2D,0,4,Surface->w,Surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Surface->pixels);
}
else {

 glTexImage2D(GL_TEXTURE_2D,0,4,Surface->w,Surface->h,0,GL_BGRA,GL_UNSIGNED_BYTE,Surface->pixels);

}


Crop.w = (float)Surface->w;
Crop.h = (float)Surface->h;
Crop.x = 0;
Crop.y = 0;
Pos.w = (float)Surface->w;
Pos.h = (float)Surface->h;
Pos.x = Pos.y = Pos.r = 0; // assign everything to default 0
Alpha = 1;


}

GLuint ENG_LoadImage(char *FILE) {
       
 glEnable(GL_TEXTURE_2D);

GLuint Texture;

SDL_Surface *Surface = NULL;

SDL_Surface *TMP = IMG_Load(FILE);


SDL_SetColorKey(TMP,SDL_SRCCOLORKEY,SDL_MapRGB(TMP->format,0xff,0x00,0xff));

Surface = SDL_DisplayFormatAlpha(TMP);

SDL_FreeSurface(TMP);

glGenTextures(1,&Texture);

glBindTexture(GL_TEXTURE_2D,Texture);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);


        if (Surface->format->Rmask == 0x000000ff)
{
glTexImage2D(GL_TEXTURE_2D,0,4,Surface->w,Surface->h,0,GL_RGBA,GL_UNSIGNED_BYTE,Surface->pixels);
}
else {

 glTexImage2D(GL_TEXTURE_2D,0,4,Surface->w,Surface->h,0,GL_BGRA,GL_UNSIGNED_BYTE,Surface->pixels);

}


SDL_FreeSurface(Surface);

return Texture;
}


void Image::SetColor(float R,float G,float B) {
	r = R;
	g = G;
	b = B;
}

Image::Image() {
	Alpha = 0.0f;
	r = g = b = 1.0f;
}

Image::~Image() {
	SDL_FreeSurface(Surface);
	glDeleteTextures( 1,&Texture );
}

He's my rendering system (cpp)

Code: Select all


struct Image {

public:

Image();

~Image();

ENG_Rectangle Pos; // Position of sprite
ENG_Rectangle Crop; // Crop of sprite

GLuint Texture;      // Gl texture object

void Render();

void SetColor(float R,float G,float B);

void SetClip(float cropx,float cropy,float cropw,float croph );

void SetPosition(float x,float y);

void SetWidthHeight(float w,float h);

void SetX(float x);

void SetY(float y);

float GetX();

float GetY();

void SetWidth(float w);

void SetHeight(float h);

float GetWidth();

float GetHeight();

GLuint GetGlTex();

void Load(char FILE[]);

float Alpha;

private:
   

	float r;
	float g;
	float b;

unsigned int Type;
SDL_Surface *Surface; // Surface ( Later change to a read only Get() Func! ) 

}; 

He's the header basicly I'm rendering 10-20 32x32 tiles from one image object that has a 512x512 sheet
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

I can't see anything that would cause such slow speeds but, I do notice that you write to the SDL_Surface's width and height, which is read-only data according to the SDL doc. I don't know if that's dangerous or not. You aren't loading the image every frame, right? How do you initialize OpenGL?
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL lag problem

Post by N64vSNES »

X Abstract X wrote:I can't see anything that would cause such slow speeds but, I do notice that you write to the SDL_Surface's width and height, which is read-only data according to the SDL doc. I don't know if that's dangerous or not. You aren't loading the image every frame, right? How do you initialize OpenGL?

Nope I don't load the image every frame I've even tried for when tiling to bind the texture before I even enter the loop because I thought that might have been slowing it down but no.

Also I don't think this is dangerouse all I'm doing is converting it the next power of 2, I've tried removing it and the is no diffrence in the FrameRate

Here's how I Initialize openGL

Code: Select all

void System::Initialize(bool FLSCR) {

SDL_Init ( SDL_INIT_VIDEO );	// Initialize SDL

SDL_WM_SetCaption("Window",NULL);

Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT,2,4096);

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,0);

if ( FLSCR == false )
buffer = SDL_SetVideoMode( 640,480,16,SDL_OPENGL );	
else
buffer = SDL_SetVideoMode( 640,480,16,SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL );
SDL_ShowCursor(0);

glClearColor(0,0,1,0);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0,640,480,0,-1,1);		// Orthographic projection
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();


glEnable ( GL_TEXTURE_2D );
glEnable ( GL_COLOR_MATERIAL );
glEnable ( GL_BLEND );
glBlendFunc( GL_ONE,GL_ONE_MINUS_SRC_ALPHA );	// the blending setup

CurrentTick = SDL_GetTicks();
CurrentFrame = 0;

Lua_Players::InitLuaPlayers();
LuaConsole::Init();
LuaItems::Init();

}
Ignore the lua stuff thats just for intializing lua functions, I get a feeling that you're going to ask if I'm calling any scripts or anything and no all I'm doing is rendering textured quads
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

I'm kind of surprised the Surface's pixel array doesn't go out of bounds when you call glTexImage2D() but, whatever. Anyway, your rendering 32 bit textures with a 16 bit video mode, maybe this is causing slow rendering?
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: OpenGL lag problem

Post by GroundUpEngine »

hmm Looks fine to me but also, shouldn't this

Code: Select all

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,0);

if ( FLSCR == false )
buffer = SDL_SetVideoMode( 640,480,16,SDL_OPENGL );   
else
buffer = SDL_SetVideoMode( 640,480,16,SDL_FULLSCREEN | SDL_HWSURFACE | SDL_OPENGL );
be

Code: Select all

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER,1); // enable OpenGL double buffering

if ( FLSCR == false )
buffer = SDL_SetVideoMode( 640,480,16,SDL_OPENGL );   
else
buffer = SDL_SetVideoMode( 640,480,16, SDL_OPENGL | SDL_FULLSCREEN );
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL lag problem

Post by N64vSNES »

Yeah its 16 bit becuase I was seeing if it effected the speed any and it dosen't wether its 8, 16, 24, 32 or whatever its back at 32 now.

And I don't need to double buffer but I guess if you set doublebuffer artibute to 1 commented out glFlush and put SDL_GL_SwapBuffers() that would be fine too but I've tried this and that makes no diffrence
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

If all else fails, you could throw together a small compilable example that demonstrates the really poor rendering speed and other people and I could try to run it and debug it. Sorry though, can't find the culprit :(
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL lag problem

Post by N64vSNES »

I remember seeing someone having the same problem as me it turned out he had a old microsoft opengl DLL in his directory, do you think this could be the case with me?
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

I really don't know, that's why I suggested writing a minimal app that other people could try. It sounds like a pain in the ass but it might be a faster solution than looking for problems in your code that might not exist.

Also, this function below proved to be 2x faster than your implementation on my PC. Of course this is just an extremely minor optimization, nothing noticeable. I don't take credit for writing it.

Code: Select all

unsigned int nextPowerOfTwo(unsigned int x) {
    if ((x > 2147483648) || (x == 0)) //2^31, avoid overflow
        return 0;

    --x;

    x = (x >> 1) | x;
    x = (x >> 2) | x;
    x = (x >> 4) | x;
    x = (x >> 8) | x;
    x = (x >> 16) | x;
    
    return ++x;
}
N64vSNES
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Thu Aug 12, 2010 11:25 am

Re: OpenGL lag problem

Post by N64vSNES »

Cool well I'll try that out but here is a link to the build I'm using so could people tell me how it runs, its not outputting FPS or anything but you will know if its faster than it is for me because my framerate is reaaaaaaaaly bad

http://rapidshare.com/files/412566843/strain.zip

Press 1 to activate the first player ( I was playing with multiple characters )
W,S,D,A moves the camera for the map

Its just some mario and chrono trigger graphics all its doing is rendering a few items the map and the player

Oh and by the way if it does run like shit then try using one of you're openGL dll's see if that has any effect.

And don't hesitiate to tell me what you think of my engine in general :mrgreen:
X Abstract X
Chaos Rift Regular
Chaos Rift Regular
Posts: 173
Joined: Thu Feb 11, 2010 9:46 pm

Re: OpenGL lag problem

Post by X Abstract X »

Missing lua51.dll.
Post Reply