Hi all
I´m trying to put two different textures in a cube
the cube appears with two textures but with the same image.
i´m doing the right thing binding with two different index one for each image
How can i resolve this problem?
this is the class with draws everything
#include "sg3d.h"
#include <SDL/SDL.h>
#include "gl_util.h"
#include "immediate.h"
#include "DisplayList.h"
#include "vertex_buffer_object.h"
#include "loader.h"
#include "Texture.h"
#include "FBO.h"
#include <iostream>
#define w 800
#define h 600
namespace topicos
{
SG3D::SG3D()
{
_end = false;
_rotation = 0;
start();
}
SG3D::~SG3D()
{
for (unsigned i = 0; i < _sceneObject.size(); ++i)
{
delete _sceneObject[i];
}
}
void SG3D::start()
{
_sdl.videoMode(w, h);
GLUtil::initGL();
GLUtil::setView(w, h);
GLUtil::initGlew();
initObjects();
// initLight();
// initShader();
mainLoop();
}
/**
* Inicia aqui os diferentes objetos
**/
void SG3D::initObjects()
{
Texture* tex = new Texture("data/texture1.jpg");
_index1 = tex->genTexture(true);
Texture* tex2 = new Texture("data/wisp.png");
_index2 = tex2->genTexture(true);
std::cout << "loading obj" << std::endl;
Mesh m = Loader::loadObj("data/legoGordo.obj");
std::cout << " end." << std::endl;
std::cout << "loading obj" << std::endl;
Mesh m2 = Loader::loadObj("data/boneco.obj");
std::cout << " end." << std::endl;
std::cout << "creating FBO 1" << std::endl;
fbo1 = new FBO(m,w,h);
std::cout << " end." << std::endl;
std::cout << "creating FBO 2" << std::endl;
fbo2 = new FBO(m,w,h);
std::cout << " end." << std::endl;
std::cout << "creating FBO 3" << std::endl;
fbo3 = new FBO(m2,w,h);
std::cout << " end." << std::endl;
std::cout << "creating FBO 4" << std::endl;
fbo4 = new FBO(m2,w,h);
std::cout << " end." << std::endl;
}
/**
* Para evitar que o sistema de eventos
* fique complicado ele nao vai ficar na classe MediaControl,
* mas deveria ficar lah. Assim evitaria o include de SDL
* nesta classe.
**/
void SG3D::mainLoop()
{
SDL_Event e;
while(!_end)
{
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
_end = true;
break;
case SDL_KEYDOWN:
switch(e.key.keysym.sym)
{
case SDLK_ESCAPE:
_end = true;
break;
} //END keydow check
break;
default:
break;
} //END type check
}//END while poll
update();
draw();
}//END while end
_sdl.quit();
}
/**
* Funcao responsavel por atualizacoes.
*
**/
void SG3D::update()
{
//Esse update eh para bonito. Nao seria legal ficar fazendo esse tipo de verificacao dentro do update.
if (_sceneObject.size() == 0)
{
std::cout << "Voce nao pode acessar o elemento zero. O tamanho da sua lista eh zero" << std::endl;
}
else
{
Object* obj = _sceneObject[0]; //Pegando o elemento zero (Erro caso não tenhamos objetos)
obj->rotate((_rotation++), Vec3(1, 0, 0)); //Rodando o elemento zero.
}
}
/**
* Funcao responsavel por chamar a lista de objetos que serao desenhados.
*
**/
void SG3D::draw()
{
GLUtil::resetDraw();
glLoadIdentity ( );
glPushMatrix();
glTranslatef ( 0.0, 0.0, -5.0 );
//glRotatef ( xrot, 1.0, 0.0, 0.0 );
glRotatef ( yrot, 0.0, 1.0, 0.0 );
// glRotatef ( zrot, 0.0, 0.0, 1.0 );
glEnable ( GL_TEXTURE_2D );
glGenerateMipmap(GL_TEXTURE_2D);
// define qual das texturas usar
for (std::vector<Object*>::iterator it = _sceneObject.begin(); it != _sceneObject.end(); ++it)
{
(*it)->draw();
}
//imagem opaca
glBindTexture ( GL_TEXTURE_2D, _index1);
glBegin ( GL_QUADS );
// Front Face
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
// Back Face
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
//imagem transparente
// Back Face
glBegin ( GL_QUADS );
glBindTexture ( GL_TEXTURE_2D, _index2);
// Back Face
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
//boneco opaco
glBindTexture ( GL_TEXTURE_2D, fbo1->getTextureIndex());
glBegin ( GL_QUADS );
// Top Face
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
//boneco2 opaco
glBindTexture ( GL_TEXTURE_2D, fbo2->getTextureIndex());
glBegin ( GL_QUADS );
// Bottom Face
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f);
glVertex3f( 1.0f, -1.0f, -1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
//legoGordo transparente
glBindTexture ( GL_TEXTURE_2D, fbo3->getTextureIndex());
glBegin ( GL_QUADS );
// Right face
glTexCoord2f(1.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, -1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, -1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glTexCoord2f(0.0f, 0.0f);
glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
//legoGordo2 transparente
glBindTexture ( GL_TEXTURE_2D, fbo4->getTextureIndex());
glBegin ( GL_QUADS );
// Left Face
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Texture and Quad
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glPopMatrix();
xrot+=0.5f;
yrot+=0.5f;
zrot+=0.5f;
_sdl.swap();
}
}
And this is texture class with contains the method that generate the texture and returns me an index.
#include <GL/glew.h>
#include "Texture.h"
#include <iostream>
#include <string>
#include <assert.h>
Texture::Texture(const std::string& path)
{
SDL_Surface* surface = IMG_Load(path.c_str());
assert(surface && "path not found");
this->width = surface->w; //tamanhos.
this->height = surface->h;
this->component = surface->format->BytesPerPixel;
this->pixels = surface->pixels;
SDL_FreeSurface(surface);
}
Texture::Texture(unsigned with,unsigned height)
{
this->width = with;
this->height = height;
}
unsigned Texture::genTexture(bool image)
{
glGenTextures(1, &_index);
glBindTexture(GL_TEXTURE_2D, _index);
int glMode = GL_RGB;
if(component == 4)
{
glMode = GL_RGBA;
}
if(image == true)
{
glTexImage2D(GL_TEXTURE_2D, 0, this->component, this->width, this->height, 0, glMode, GL_UNSIGNED_BYTE, this->pixels);
}
else
{
glTexImage2D(GL_TEXTURE_2D,0, GL_RGBA, this->width, this->height, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0 );
}
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE); // automatic mipmap generation included in OpenGL v1.4
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
return _index;
}
Texture::~Texture()
{
//termina o uso da textura.
glBindTexture(GL_TEXTURE_2D, 0);
}