2D Rendering VERY slow

Started by
7 comments, last by ConorH 16 years, 6 months ago
This code renders at a solid 2fps lol. Any thoughts? Drawing code

	{
		 glPushMatrix();
		// Bind the texture to which subsequent calls refer to
		glBindTexture( GL_TEXTURE_2D, ogl );
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);

			glBegin( GL_QUADS );
		//Top-left vertex (corner)
		glTexCoord2f( 0, 0 );
		glVertex2d( x, y );
		
		//Bottom-left vertex (corner)
		glTexCoord2f( 1, 0 );
		glVertex2d( x+w, y );
		
		//Bottom-right vertex (corner)
		glTexCoord2f( 1, 1 );
		glVertex2d( x+w, y+h );
		
		//Top-right vertex (corner)
		glTexCoord2f( 0, 1 );
		glVertex2d( x, y+h );
			glEnd();
		glPopMatrix();
			
		}
Loading

// Have OpenGL generate a texture object handle for us
		glGenTextures( 1, &ogl );
 
		// Bind the texture object
		glBindTexture( GL_TEXTURE_2D, ogl );
 
		// Set the texture's stretching properties
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
        glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
 
		// Edit the texture object's image data using the information SDL_Surface gives us
		glTexImage2D( GL_TEXTURE_2D, 0, 3, sdl->w, sdl->h, 0,
	                    GL_RGBA, GL_UNSIGNED_BYTE, sdl->pixels );
Screen drawing code

SDL_GL_SwapBuffers();
		FrameDrawn();
		UpdateFps();
		if (SDL_GetError())
		{
			WriteLine(Error,SDL_GetError());
		}
Any ideas?
Advertisement
I don't think you need this in your drawing code, you have it when you make the texture. At least, I don't have it in my drawing code.

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


Also, in my loading code, I have them after the glTexImage2D. Don't think that would make a difference though.
-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage
How many times are you drawing?

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal

Rendering in software mode ?
Not sure if Im rendering in Software mode, whats the command? Or the one for hardware mode. Im using it with SDL btw. Im only drawing one image to the screen :(

Edit:
I changed the image im rendering, so it now renders a 128x128 image and things are normal (900+). The old image was some arbitrary number like 511x412. I do intend to use non poer-of-2 textures in my prog, so is there a way around this?
Quote:Edit:
I changed the image im rendering, so it now renders a 128x128 image and things are normal (900+). The old image was some arbitrary number like 511x412. I do intend to use non poer-of-2 textures in my prog, so is there a way around this?


What I do is when loading a picture, I figure out the width and height of the image, then go up to the next power of 2 (checking the numbers to make sure the video card can handle it). Then, with all the extra pixels (if any), I fill them with alpha 0 padding pixels. I believe there's a way without doing that to draw non-powers of 2, but not all video cards support it iirc, which is why I took that method.

-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage
@Blackstream
Yea thats what I assumed Id have to do. Would you be so kind as to share a peek at ur code for it? or point me in the direction of some? Im not lazy Id just rather not think for myself
Here's my entire sprite loading function. I can make no claims as to the quality of this code.

EDIT: My code broke the forums, it was too long. Changed the conversion part to 2 lines.

EDIT 2: Finally unbroke the forums. Had way too many huge lines. Had to make them all 2 or even 3 lines. Be on the look out for compiler errors if you are copy pasting, although I think it should be okay.

bool bstSprite::loadImg(char *filename, maskflags flags ) {	int tempPosNew;	int tempPosOld;	//Use SDL_Image to load our picture	SDL_Surface *sdl_data;	sdl_data = IMG_Load(filename);	if(!sdl_data) {		//Problem loading		return false;	}	//Convert our surface to a true color RGBA surface for easy processing	//This byte swapping thing is untested, but in theory, if this compiles on a system	//that uses big endian, it'll still show pictures correctly.	if(SDL_BYTEORDER == SDL_BIG_ENDIAN) {		SDL_PixelFormat format = {NULL, 32, 4, 0, 0, 0, 0, 0, 8, 16, 24, 			SDL_Swap32(0x000000FF), SDL_Swap32(0x0000FF00), SDL_Swap32(0x00FF0000), 			SDL_Swap32(0xFF000000), 0, 255};		sdl_data = SDL_ConvertSurface(sdl_data, &format, SDL_SWSURFACE);	} else {		SDL_PixelFormat format = {NULL, 32, 4, 0, 0, 0, 0, 0, 8, 16, 24, 			0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000, 0, 255};		sdl_data = SDL_ConvertSurface(sdl_data, &format, SDL_SWSURFACE);	}		//Now we use this sdl_data to make an array of our own data, in order to modify it	//to our wishes.  First, we need to know what size to make it (has to be a power of 2)		//Get and save our dimensions	width = (float)sdl_data->w;	height = (float)sdl_data->h;	trueWidth = 2;	trueHeight = 2;	//Stop at the first power of 2 that's equal to or greater than the width/height	while(trueWidth < width)   trueWidth  *= 2;	while(trueHeight < height) trueHeight *= 2;	//Check our width and height and make sure they don't exceed the max texure size	int maxsize;	glGetIntegerv( GL_MAX_TEXTURE_SIZE, &maxsize );	if(trueWidth > maxsize || trueHeight > maxsize) {		//Our picture is too big for our user's system		return false;	}	//Now create the "real" texture.  Basically the original texture but with the padded pixels	SDL_LockSurface( sdl_data );	rgbaByte *rBP = new rgbaByte[(int)(trueWidth * trueHeight)];	rgbaByte temp;	//Get the top left pixel if we are in TOP_LEFT mask mode	rgbaByte invis;	if(flags == TOP_LEFT) {		SDL_GetRGBA(((Uint32 *)sdl_data->pixels)[0], (SDL_PixelFormat *)sdl_data->format, 			&invis.red, &invis.green, &invis.blue, &invis.alpha);	}	for(int y = 0; y < trueHeight; y++) {		for(int x = 0; x < trueWidth; x++) {			tempPosNew = (int)(y*trueWidth + x);			tempPosOld = (int)(y*width + x);			if(y < height && x < width) {				//Get pixel data from the SDL surface				SDL_GetRGBA(((Uint32 *)sdl_data->pixels)[tempPosOld], 					(SDL_PixelFormat *)sdl_data->format, 					&temp.red, &temp.green, &temp.blue, &temp.alpha);				rBP[tempPosNew].red   = temp.red;				rBP[tempPosNew].green = temp.green;				rBP[tempPosNew].blue  = temp.blue;				//If we are in top left mode, see if we have an invisible pixel or not				if(flags == TOP_LEFT && temp.red == invis.red && 					temp.blue == invis.blue && temp.green == invis.green) {					rBP[tempPosNew].alpha = 0;				} else {					rBP[tempPosNew].alpha = temp.alpha;				}			} else {				//Put in a padded pixel				rBP[tempPosNew].red = 0;				rBP[tempPosNew].green = 0;				rBP[tempPosNew].blue = 0;				rBP[tempPosNew].alpha = 0;			}		}	}	SDL_UnlockSurface( sdl_data );	SDL_FreeSurface( sdl_data );	//Now, just create our surface!	glGenTextures(1, &texSprite);	glBindTexture(GL_TEXTURE_2D, texSprite);	glTexImage2D(GL_TEXTURE_2D, 0, 4, (GLsizei)trueWidth, (GLsizei)trueHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, rBP);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);	//Check for OpenGL errors, if there are any, return failure.	GLenum err = glGetError();	if(err != GL_NO_ERROR) {		//Clear all the other errors out, then return failure		while(glGetError() != GL_NO_ERROR);		return false;	}	delete[] rBP;	return true;}
-Blackstream Will you, won't you, will you, won't you, won't you take my virus?-The Mad HackerBlackstream's Webpage
Cheers BlackStream, Ill add it to the archives

This topic is closed to new replies.

Advertisement