Jump to content

  • Log In with Google      Sign In   
  • Create Account

nitro glycerine

Member Since 11 Jul 2011
Offline Last Active Apr 05 2012 07:50 PM

Topics I've Started

Issue with Opaque Objects Appearing Transluscent

26 March 2012 - 01:06 AM

Hello!

I'm looking for some help with a little issue I'm having rendering geometry in my OpenGL engine. Not entirely sure what the issue is, but objects appear translucent when they should be entirely opaque. I've quadruple checked my OpenGL init calls, and everything seems to be correct, even referencing it against other projects which use OpenGL.

Also worth noting that draw order seems to affect this, which makes no sense. Namely, if I draw object 1, then draw object 2, you can't see 2 through 1, but you can see 1 through 2. There are some screenshots in the following album for more clarification:

http://imgur.com/a/JsvL5

I thought it might be an issue with culling, blending or depth testing.. but all of that seems to be set up properly too. Disabling the Skybox fixes the behaviour on the first cube, but if you look through the second cube into the first, the issue reappears, as previously mentioned. Also worth noting that I'm using SDL with GLEW (although no shaders are currently active).

This is probably a simple issue (or at least I hope so and I'm just completely blind), so if anyone has any thoughts or can help in any way with respect to this issue, it would be greatly appreciated. Thank you!

OpenGL Initialization Calls:
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) < 0) { return false; }
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, settings->verticalSync ? 1 : 0);
if((m_graphics = SDL_SetVideoMode(settings->windowWidth, settings->windowHeight, 0, SDL_OPENGL | (settings->fullScreen ? SDL_FULLSCREEN : 0))) == NULL) { return false; }

glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glEnable(GL_CULL_FACE);
glEnable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, settings->windowWidth, settings->windowHeight);

Draw Function (with unnecessary code omitted):
void Game::draw() {
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_CULL_FACE);
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(70.0f, (GLfloat) settings->windowWidth / (GLfloat) settings->windowHeight, 0.1f, 10000.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
	// draw skybox
	// setup camera
	// draw cubes
	
	glDisable(GL_CULL_FACE);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0, settings->windowWidth, settings->windowHeight, 0);
	
	// draw text stuff
	
	SDL_GL_SwapBuffers();
}

[SDL] Super slow framerate on Windows 7, GLEW shaders unavailable

24 February 2012 - 10:38 PM

Hello!

I've been working on a project off and on in SDL for a number of months now, and I've had an issue from the start that I recently had some time to try and debug. I've been developing on Windows XP until this past week, so it was not really an issue until now.

Basically, my SDL bases game works perfectly fine on any Windows XP machine (ie. normal frame rate, shaders work no problem), but I initially noticed that if I ran the executable on Windows 7, the frame rate would only be 10-15 for no reason (on super high-end machines, ie. Phenom II X6 / 5970) but this was strictly a Windows 7 issue as indicated by testing on several different machines. Upon further investigation I realized that if I run the game from within Visual Studio 2008 on a Windows 7 machine it works perfectly (ie. 7000+ FPS w/o vsync), but as soon as I ran it from the executable instead, the frame rate would once again be between 10 and 15, and if shaders were being used via GLEW, the program would simply exit because GLEW 2.0 was unavailable.

tldr; SDL OpenGL game, works fine on XP, on Win7 works fine in VS2008 but EXE limited to 10 fps and shaders wont work

Honestly not sure what the issue could possibly be and has been baffling me for some time. If anyone has any suggestions or input, it would be greatly appreciated. It almost seems like SDL is somehow switching to software mode or some limited version of OpenGL. Also worth mentioning that the only things being rendered is some text (generated and rendered via lists) and a very simple skybox with 1024x1024 textures for each side.

My game is using SDLmain and a number of libraries, including:
SDL
SDL_ttf
GLEW
FreeImage
Qt
FMOD Ex
FBX SDK
Bullet Physics

Thanks for reading. Some code, if it helps any:

Main.cpp:
#include "Game.h"
int main(int argc, char * argv[]) {
	 Game * game = new Game();
	 if(!game->init()) { return -1; }
	 game->run();
	 delete game;
	 return 0;
}

OpenGL Setup:
bool Game::init() {
	 if(m_initialized) { return false; }
	 settings->load();
	 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_JOYSTICK) == -1) { return false; }
	 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	 SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, settings->verticalSync ? 1 : 0);
	 m_graphics = SDL_SetVideoMode(settings->windowWidth, settings->windowHeight, 0, SDL_OPENGL | (settings->fullScreen ?	 SDL_FULLSCREEN : 0));
	 SDL_WM_SetCaption("Game", NULL);
	 QString iconPath;
	 iconPath.append(QString("%1/Icons/Block.bmp").arg(settings->dataDirectoryName));
	 QByteArray iconPathBytes = iconPath.toLocal8Bit();
	 m_icon = SDL_LoadBMP(iconPathBytes.data());
	 SDL_WM_SetIcon(m_icon, NULL);
	 glShadeModel(GL_SMOOTH);
	 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	 glClearDepth(1.0f);
	 glEnable(GL_DEPTH_TEST);
	 glEnable(GL_TEXTURE_2D);
	 glEnable(GL_BLEND);
	 glDepthFunc(GL_LEQUAL);
	 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	 glMatrixMode(GL_MODELVIEW);
	 glViewport(0, 0, settings->windowWidth, settings->windowHeight);
	 if(glewInit() != GLEW_OK) { return false; }
	 if(!GLEW_VERSION_2_0) { return false; }

	 etc.

Game Loop:
void Game::run() {
if(!m_initialized) { return; }
m_running = true;
SDL_Event event;
static unsigned int lastTime = SDL_GetTicks();
unsigned int currentTime = SDL_GetTicks();
do {
  if(SDL_PollEvent(&event)) {
   switch(event.type) {
	case SDL_KEYDOWN:
	 if(!console->isActive()) {
	  menu->handleInput(event);
	 }
	 if(!menu->isActive()) {
	  console->handleInput(event);
	 }
	 break;
	case SDL_MOUSEMOTION:
	 if(!console->isActive() &&
		!menu->isActive() &&
		SDL_GetAppState() & SDL_APPMOUSEFOCUS &&
		SDL_GetAppState() & SDL_APPINPUTFOCUS &&
		SDL_GetAppState() & SDL_APPACTIVE) {
	  camera->handleInput(event);
	 }
	 break;
	case SDL_QUIT:
	 m_running = false;
	 break;
	default:
	 break;
   }
  }
  if(m_running) {
   currentTime = SDL_GetTicks();
   update(currentTime - lastTime);
   draw();
   lastTime = currentTime;
  }
} while(m_running);
}

Issues with OpenGL / SDL / SDL_TTF

11 July 2011 - 11:59 AM

Okay, so I'm attempting to create dynamic font loading from TTF files using SDL_TTF / SDL / OpenGL. (Also note I am using the QtCore library as well.) The general idea of what my code is doing is as follows:

Loading:
1) Generate font path and attempt to load the font using SDL_TTF
2) Render 128 characters to individual SDL_Surfaces, from character 32 to 160
3) Keep track of the size of the largest characters
4) Create a new surface to render all 128 characters onto based on the largest width and height (also make sure it is a power of 2 to prevent scaling)
5) Iterate over all 128 of the characters, rendering to them to the full surface (16x8)
6) Create an OpenGL texture from the data contained in the full surface
7) Create an OpenGL list of all of the characters for ease of use when rendering text later

Rendering:
1) Generate output text data based on format string and arguments passed in
2) Use glCallLists to render the string using the previously generated font data

It's been somewhat successful so far, but I have hit two major snags:

1) SDL_Surfaces store pixel data upside down. I've read about this issue online, but have not found any simple solutions to this. I ultimately did find some code to flip the texture upside-down, but it seemed like an unnecessary amount of code to do so - and it still didn't fix the problem. In short, if I blit to an SDL_Surface, then attempt to make an OpenGL texture out of it, the image is upside-down when rendered. And since I'm generating the image on runtime, having an upside-down input file is not an option.

2) The text shows up yellow instead of white, as specified by the SDL_Color struct. I also found others online who had this same issue, but have not managed to find a solution. When the temp SDL_Surface is allocated, I had to change the RGBA masks to BGRA instead of the usual RGBA / ABGR depending on what endian SDL is using in order to have anything show up at all. I'm not sure if I horribly messed this up or not, but it's the only thing that produces any results thus far. I also tried SDL_DisplayFormat on the surface I was generating, but no matter what I change the RGBA masks to after that, there is no output.

I have attached a quad displaying the full image output at the bottom as well as relevant source code. If anyone has any advice / tips / hints / suggestions / solutions / etc. to this problem, they would be tremendously appreciated. I'll gladly surf around the forums and help other people out as well afterwards, seeing as how this is my first post. Thanks so much!

Edit: I just realized the texture co-ordinates are semi wrong when generating the lists, they're for a 16x16 grid, but the one I'm using is 16x8. They also don't account for non-perfect fit textures (ie. whitespace on the "top" and "right" to make the texture a power of 2, to prevent automatic scaling).

Font Loading:
bool Font::load() {
	if(m_loaded) { return true; }

	QString fontPath = QString(Game::settings->dataDirectory);
	fontPath.append("/Fonts/");
	fontPath.append(m_filename);

	QFileInfo fontFile(fontPath);
	if(!fontFile.exists()) { return false; }

	QByteArray bytes = fontPath.toLocal8Bit();

	TTF_Font * fontData;
	if(!(fontData = TTF_OpenFont(bytes.data(), m_size))) {
		return false;
	}

	SDL_Color colour = {255, 255, 255, 255};

	SDL_Surface * character;
	QVector<SDL_Surface *> characters;
	int maxWidth = -1;
	int maxHeight = -1;
	char data[2];
	data[1] = '\0';
	for(int i=0;i<128;i++ ) {
		data[0] = (char) (i + 32);
		character = TTF_RenderText_Blended(fontData, data, colour);
		characters.push_back(character);
		if(maxWidth < 0 || maxWidth < character->w) { maxWidth = character->w; }
		if(maxHeight < 0 || maxHeight < character->h) { maxHeight = character->h; }
	}

	int width = 1, tempWidth = maxWidth * 16;
	int height = 1, tempHeight = maxHeight * 8;
	while(width < tempWidth) { width *= 2; }
	while(height < tempHeight) { height *= 2; }

	SDL_Surface * temp = SDL_CreateRGBSurface(0, width, height, 32, 0x0000ff00, 0x00ff0000, 0xff000000, 0x000000ff);

	SDL_Rect sourceArea, destArea;
	int x = 0;
	int y = 0;
	for(int i=0;i<128;i++) {
		sourceArea.x = 0;
		sourceArea.y = 0;
		sourceArea.w = characters[i]->w;
		sourceArea.h = characters[i]->h;

		destArea.x = maxWidth * x;
		destArea.y = maxHeight * y;
		destArea.w = characters[i]->w;
		destArea.h = characters[i]->h;

		SDL_BlitSurface(characters[i], &sourceArea, temp, &destArea);

		x++;
		if(x == 16) {
			x = 0;
			y++;
		}
	}

	glGenTextures(1, &m_texture);
	glBindTexture(GL_TEXTURE_2D, m_texture);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, temp->w, temp->h, 0, GL_BGRA, GL_UNSIGNED_BYTE, temp->pixels);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	
	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);

	m_font = glGenLists(128);

	float cx, cy;
	for(int i=0;i<128;i++) {
		cx = (float) (i % 16) / 16.0f;
		cy = (float) (i / 16) / 16.0f;

		glNewList(m_font + i, GL_COMPILE);

			glBegin(GL_QUADS);
				glTexCoord2f(cx,   		1 - cy - 0.0625f); glVertex2i(0,  	m_size);
				glTexCoord2f(cx + 0.0625f, 1 - cy - 0.0625f); glVertex2i(m_size, m_size);
				glTexCoord2f(cx + 0.0625f, 1 - cy      	); glVertex2i(m_size, 0 	);
				glTexCoord2f(cx,   		1 - cy      	); glVertex2i(0,  	0 	);
				
			glEnd();

			glTranslated(10, 0, 0);

		glEndList();
	}

	for(int i=0;i<128;i++) {
		SDL_FreeSurface(characters[i]);
	}
	SDL_FreeSurface(temp);

	m_loaded = true;

	return true;
}

Text Drawing:
void Font::drawText(int x, int y, const char * format, ...) const {
	if(!m_loaded) { return; }

	char text[256];
	va_list args;
	va_start(args, format);
		vsprintf_s(text, 256, format, args);
	va_end(args);

	glEnable(GL_TEXTURE_2D);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE);

	glBindTexture(GL_TEXTURE_2D, m_texture);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();

		glLoadIdentity();

		glTranslated(x, y, 0);
		glListBase(m_font - 32);
		glCallLists(strlen(text), GL_BYTE, text);

	glPopMatrix();
}

Output:
Attached File  font_texture.png   16.99KB   44 downloads

PARTNERS