Only the last tile is rendered!

Started by
2 comments, last by Samsonite 17 years, 9 months ago
Ok. I have this program that currently creates two tiles and render them to screen with a texture. But then I changed it(so that each tile can have a different texture) and now it only renders the last tile: Free Image Hosting at www.ImageShack.us Here's some code: renderer.h decleration and definition

class RendererClass
{
public:
	void Render();
	void Setup();
	std::vector<TileClass*>RenderData;//Vector containing all the tiles to be rendered
};

/*Class definitions*/

/*Render class*/
void RendererClass::Setup()
{
    SDL_Init( SDL_INIT_VIDEO );

	SDL_WM_SetCaption("OpenGL!", NULL);
    
    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_DEPTH_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    
    SDL_Surface *screen;
    screen = SDL_SetVideoMode( 1024, 768, 24, SDL_OPENGL | SDL_HWSURFACE );

	glViewport(0, 0, 1024, 768);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0.0f, 1024.0f, 768.0f, 0.0f, 0.0f, -2.0f);

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
	glClearDepth(2.0f);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);

	
}

void RendererClass::Render()
{
	glEnable( GL_TEXTURE_2D );

	for(unsigned int i = 0; i < RenderData.size(); ++i)//loop through all tiles to be rendered and render them.
	{
		RenderData->Render();
	}

	SDL_GL_SwapBuffers();//swap buffers and display graphics.

}  
/*End Render class*/

/*END CLASS DEFINITIONS*/



cTile.h

class TileClass
{
public:
	//Constructor
	TileClass();

	void Setup();//For setting things up

	bool shouldRender;//Bool which determines if the current tile should be renderer
	void Render();//Function to render tile.
	float xPos;//X position
	float yPos;//Y position
	const char* TexturePath; //The filepath of the texture for this tile
	GLuint texture;
};

/*Class definitions*/

void TileClass::Setup()
{
	/*Texture generation*/
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);

	SDL_Surface* tex = SDL_LoadBMP(TexturePath);
	SDL_LockSurface(tex);

	glTexImage2D( GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tex->pixels );

	SDL_UnlockSurface( tex );
	SDL_FreeSurface( tex );

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

TileClass::TileClass()
{
	TexturePath = "test.bmp";
}

/*Tileclass*/
void TileClass::Render()
{
	glBindTexture( GL_TEXTURE_2D, this->texture );

	glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(xPos, yPos, 0.0f);

		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(xPos+64.0f, yPos, 0.0f);

		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(xPos+64.0f, yPos+64.0f, 0.0f);

		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(xPos, yPos+64.0f, 0.0f);
	glEnd();
}



main.cpp

#define GL_BGR GL_BGR_EXT

#include <windows.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <vector>
#include "cTile.h"
#include "renderer.h"
#include "cInput.h"


RendererClass* Renderer = new RendererClass();//Create new pointer to an instance of the renderer
InputHandler* IHandler = new InputHandler();//Create a new pointer to an instance of the input handler class
TileClass* tile = new TileClass();//Create a new pointer to an instance of a tile
TileClass* tile2 = new TileClass();//Create a new pointer to an instance of a tile


int main( int argc, char *argv[] )
{

	tile2->xPos = 64;
	tile2->yPos = 0;
	Renderer->RenderData.push_back(tile);//Put the new tile into the renderdata
	Renderer->RenderData.push_back(tile2);//Put the new tile into the renderdata
	Renderer->Setup();//Set up the screen
	
	tile->Setup();
	tile2->Setup();


	while(IHandler->HandleInput() == 0) Renderer->Render();

	SDL_Quit();

	//Let's clean up
	delete tile;
	delete tile2;
	delete Renderer;
	delete IHandler;
	//return success
	return 0; 

} // int main()



Thanks in advance! [Edited by - Samsonite on July 1, 2006 8:13:17 PM]
Hope I was helpful. And thank you if you were!
Advertisement
Well I am really tired, but change your tile2->xPos to 500; and your tile2->yPos to 300;

From a quick look at your code it doesn't look like tile is ever given a value for xPos or yPos (meaning it could have a garbage value). Where with tile2 you explicitly give them values. Perhaps, the constructor to the TileClass shoud give them some default vaues.

Also try calling glGetError() in the rendering and see what it returns.
No, didn't work. But thanks for trying [smile].

I wrote this little logging system that logged how many times the Renderer->Render() function was called, and it says 1. So this obviously means that the first tile doesn't get rendered. I wonder why.

EDIT: Oh, gotta try Will F's solution...
EDIT2: OMFG that did it! Thanks a lot! [smile]
Hope I was helpful. And thank you if you were!

This topic is closed to new replies.

Advertisement