Sign in to follow this  
Lorenzo Romero Guidi

OpenGL Problems with textures: environment mapping and object linear textures

Recommended Posts

I'm making a 3d car racing games with OpenGL/SDL.
The base core I started from was a simple formula1 car with some textures: environment(sphere) mapping for the skeleton and object linear texture mapping for the wheels.
I add a simple .off track and texture it with a simple 2d road texture with automatic generation of s and t using GL_TEXTURE_GEN_S&T, and setting s and t wraps at GL_REPEAT.
The track's texture renders with problems if I also render car's textures. I don't know how can I modify car's textures code to render track texture not distorted (it renders like lines of different gradients of track's texture color, not like a repetition of the texture I map).
If you need code, ask.
Thanks.

Share this post


Link to post
Share on other sites
can you please post the code here using the codeBB youll find it in the post widget. it sounds like you need to add a few more aprameters to your texturing process, but im not entirely sure without having a look at your code

Share this post


Link to post
Share on other sites
File car.cpp:

[code]
GLenum minfilter = GL_NEAREST;
GLenum magfilter = GL_NEAREST;
GLenum env = GL_MODULATE;
GLenum wraps = GL_REPEAT;
GLenum wrapt = GL_REPEAT;
extern bool useEnvmap;

....

// Funzione che prepara tutto per usare un env map
void SetupEnvmapTexture()
{
// facciamo binding con la texture 1
glBindTexture(GL_TEXTURE_2D,1);

glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S); // abilito la generazione automatica delle coord texture S e T
glEnable(GL_TEXTURE_GEN_T);
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE , GL_SPHERE_MAP); // Env map
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE , GL_SPHERE_MAP);
glColor3f(1,1,1); // metto il colore neutro (viene moltiplicato col colore texture, componente per componente)
glDisable(GL_LIGHTING); // disabilito il lighting OpenGL standard (lo faccio con la texture)
}

// funzione che prepara tutto per creare le coordinate tessitura (s,t) da (x,y,z)
// Mappo l'intervallo [ minY , maxY ] nell'intervallo delle T [0..1]
// e l'intervallo [ minZ , maxZ ] nell'intervallo delle S [0..1]
void SetupWheelTexture(Point3 min, Point3 max)
{
glBindTexture(GL_TEXTURE_2D,0);
glEnable(GL_TEXTURE_2D);

// ulilizzo le coordinate OGGETTO
// cioe' le coordnate originali, PRIMA della moltiplicazione per la ModelView
// in modo che la texture sia "attaccata" all'oggetto, e non "proiettata" su esso
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE , GL_OBJECT_LINEAR);
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE , GL_OBJECT_LINEAR);
float sz=1.0/(max.Z() - min.Z());
float ty=1.0/(max.Y() - min.Y());
float s[4]={0,0,sz, - min.Z()*sz };
float t[4]={0,ty,0, - min.Y()*ty };
glTexGenfv(GL_S, GL_OBJECT_PLANE, s);
glTexGenfv(GL_T, GL_OBJECT_PLANE, t);
}

.....

void Car::RenderAllParts(bool usecolor) const
{
// disegna la carliga con una mesh
glPushMatrix();
glScalef(-0.05,0.05,-0.05); // patch: riscaliamo la mesh di 1/10
glTranslatef(px,-wheelBR1.bbmin.Y()+0.5,0);
if (!useEnvmap)
{
if (usecolor)
glColor3f(1,0,0); // colore rosso, da usare con Lighting
}
else
{
if (usecolor);
SetupEnvmapTexture();
}
carlinga.RenderNxV(); // rendering delle mesh carlinga usando normali per vertice
if (usecolor) glEnable(GL_LIGHTING);

for (int i=0; i<2; i++)
{
// i==0 -> disegno ruote destre.
// i==1 -> disegno ruote sinistre.
int sign;
if (i==0) sign=1; else sign=-1;
glPushMatrix();
if (i==1)
{
glTranslatef(0,+wheelFR1.Center().Y(), 0);
glRotatef(180, 0,0,1 );
glTranslatef(0,-wheelFR1.Center().Y(), 0);
}

glTranslate( wheelFR1.Center() );
glRotatef( sign*sterzo,0,1,0);
glRotatef(-sign*mozzoA,1,0,0);
glTranslate( -wheelFR1.Center() );

if (usecolor)
{
glColor3f(.6,.6,.6);
SetupWheelTexture(wheelFR1.bbmin,wheelFR1.bbmax);
}
wheelFR1.RenderNxF(); // la ruota viene meglio FLAT SHADED - normali per faccia
// provare x credere
glDisable(GL_TEXTURE_2D);
if (usecolor) glColor3f(0.9,0.9,0.9);
wheelFR2.RenderNxV();
glPopMatrix();

glPushMatrix();

if (i==1)
{
glTranslatef(0,+wheelBR1.Center().Y(), 0);
glRotatef(180, 0,0,1 );
glTranslatef(0,-wheelBR1.Center().Y(), 0);
}
glTranslate( wheelBR1.Center() );
glRotatef(-sign*mozzoA,1,0,0);
glTranslate( -wheelBR1.Center() );
if (usecolor)
{
glColor3f(.6,.6,.6);
SetupWheelTexture(wheelBR1.bbmin,wheelBR1.bbmax);
}
wheelBR1.RenderNxF();
glDisable(GL_TEXTURE_2D);
if (usecolor)
glColor3f(0.9,0.9,0.9);
wheelBR2.RenderNxV();
glPopMatrix();
}

glPopMatrix();
}

......

// disegna a schermo
void Car::Render() const
{
// sono nello spazio mondo

drawAxis(); // disegno assi spazio mondo
glPushMatrix();

glTranslatef(px,py,pz);
glRotatef(facing, 0,1,0);

// sono nello spazio MACCHINA
drawAxis(); // disegno assi spazio macchina

//fari auto
DrawHeadlight(-0.3,0,-1, 0); // accendi faro sinistro
DrawHeadlight(+0.3,0,-1, 1); // accendi faro destro

RenderAllParts(true);

// ombra!
glColor3f(0.4,0.4,0.4); // colore fisso
glTranslatef(0,0.045,0); // alzo l'ombra di un epsilon per evitare z-fighting con il pavimento
glScalef(1.01,0,1.01); // appiattisco sulla Y, ingrandisco dell'1% sulla Z e sulla X
glDisable(GL_LIGHTING); // niente lighing per l'ombra
RenderAllParts(false); // disegno la macchina appiattita

glEnable(GL_LIGHTING);

glPopMatrix();

glPopMatrix();
}

void SetRoadTexture()
{
glBindTexture(GL_TEXTURE_2D,2);
glEnable(GL_TEXTURE_2D);
glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glColor3f(1,1,1); // metto il colore neutro (viene moltiplicato col colore texture, componente per componente)
glDisable(GL_LIGHTING); // disabilito il lighting OpenGL standard (lo faccio con la texture)
}

void Circuit::Render() const
{
glPushMatrix();
glRotatef(90.0,-1,0,0); // glRotatef(90.0,-1,0,0);
glRotatef(90.0,0,0,-1); // glRotatef(90.0,0,0,-1);
glTranslatef(-130.0,-252.5,0.004); //glTranslatef(195.0,-230.0,0.004);
glScalef(63.0,36.0,1.0); //glScalef(26.0,42.0,1.0);
SetRoadTexture();
road.RenderNxV();
glPopMatrix();
}

[/code]


File main.cpp

[code]

......

bool LoadTexture(int textbind,char *filename)
{
SDL_Surface *s = IMG_Load(filename);
if (!s) return false;

glBindTexture(GL_TEXTURE_2D, textbind);
gluBuild2DMipmaps(
GL_TEXTURE_2D,
GL_RGB,
s->w, s->h,
GL_RGB,
GL_UNSIGNED_BYTE,
s->pixels
);
glTexParameteri(
GL_TEXTURE_2D,
GL_TEXTURE_MAG_FILTER,
GL_NEAREST ); //GL_LINEAR
glTexParameteri(
GL_TEXTURE_2D,
GL_TEXTURE_MIN_FILTER,
GL_LINEAR ); //GL_LINEAR_MIPMAP_LINEAR
return true;
}

......

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

.......

glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE); // opengl, per favore, rinormalizza le normali
// prima di usarle
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW); // consideriamo Front Facing le facce ClockWise
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_POLYGON_OFFSET_FILL); // caro openGL sposta i
// frammenti generati dalla
// rasterizzazione poligoni
glPolygonOffset(1,1); // indietro di 1

if (!LoadTexture(0,"textures/logo.jpg")) return 0;
if (!LoadTexture(1,"textures/envmap_flipped.jpg")) return 0;
if (!LoadTexture(2,"textures/road.jpg")) return 0;

....... (renders of car(and wheels) and circuit)

}[/code]

Share this post


Link to post
Share on other sites
i would recommend putting the texture loader into a class so you can multi-thread your objects with different texture objects.
very simple and efficient, plus your textures wont get mixed up. if your lazy with a texture loader, you shoul dtry out SOIL, [url="http://www.lonesock.net/soil.html"]http://www.lonesock.net/soil.html[/url]. i use it and its really awesome

Share this post


Link to post
Share on other sites
[quote name='Adam West' timestamp='1333625773' post='4928443']
i would recommend putting the texture loader into a class so you can multi-thread your objects with different texture objects.
very simple and efficient, plus your textures wont get mixed up. if your lazy with a texture loader, you shoul dtry out SOIL, [url="http://www.lonesock.net/soil.html"]http://www.lonesock.net/soil.html[/url]. i use it and its really awesome
[/quote]

So I have to put the LoadTexture function in a Class TextureLoader, and add a textureLoader object in every object of the 3d game so I can call LoadTexture inside the object I like to texture? And this won't get textures mixed up? Sorry and thanks.

Share this post


Link to post
Share on other sites
You could, though I'm not really sure what that has to do with multithreading, anyways. GL contexts are, as far as I am aware, entirely thread-unsafe, and I don't know if your image loader(s) use any static/global variables. So long as everything's contained to stack variables and OS calls you'd be fine so long as the GL state modifications are kept to one thread.

On-topic: Got some screenshots? I'm not quite visualizing what you're getting here.

Share this post


Link to post
Share on other sites
This is what I should get.

[attachment=8061:What I Should Get.png]


And this is what I got.

[attachment=8062:What I Got.png]

While the game begins, it renders like it should be, but a second later and all the following rendering loops, it renders badly. And I don't understand why.

Share this post


Link to post
Share on other sites
[left]I don't see you changing the texturing behavior back in your SetRoadTexture() function, and I'd bet good money that this is your problem.[/left]

[left]EDIT: Or at least part of it.[/left]

[left]EDIT 2: And unless you have strong reason to (read: hardware flat out doesn't support it) I'd definitely suggest the absolutely archaic fixed-function graphics pipeline and using shaders.[/left]

Share this post


Link to post
Share on other sites
[quote name='InvalidPointer' timestamp='1333751365' post='4928920']
[left]I don't see you changing the texturing behavior back in your SetRoadTexture() function, and I'd bet good money that this is your problem.[/left]

[left]EDIT: Or at least part of it.[/left]

[left]EDIT 2: And unless you have strong reason to (read: hardware flat out doesn't support it) I'd definitely suggest the absolutely archaic fixed-function graphics pipeline and using shaders.[/left]
[/quote]

I'm making this game for my last university exam so I'm not interested to obtain a beautiful work, but a good work taking also care of the time passed on it. So I'll try shader implementation only if it's not possible to obtain what I like without it.
Sorry but I'm a deb to 3d game programming and also in set textures, so can you please tell me what's wrong with the texturing behavior of my road texture and what I should add/change to get what I like?
Thank you very much and sorry again!
Sorry also for my not perfect english, five years since last english lesson! :-)

Share this post


Link to post
Share on other sites
probably rendering the road fais because you set some texturing parameters that modify the texturing of the road as well. For example when you render the road you enable texture coordinate generation but you don't set the mode, so the mode is what is currently set. Try using it with object linear. You can check if you loaded the texture correctly with gDEBugger.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this