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
Adam West    219
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
Adam West    219
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
InvalidPointer    1842
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
InvalidPointer    1842
[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
Yours3!f    1532
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  

  • Similar Content

    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      Question:
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
       
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
    • By markshaw001
      Hi i am new to this forum  i wanted to ask for help from all of you i want to generate real time terrain using a 32 bit heightmap i am good at c++ and have started learning Opengl as i am very interested in making landscapes in opengl i have looked around the internet for help about this topic but i am not getting the hang of the concepts and what they are doing can some here suggests me some good resources for making terrain engine please for example like tutorials,books etc so that i can understand the whole concept of terrain generation.
       
    • By KarimIO
      Hey guys. I'm trying to get my application to work on my Nvidia GTX 970 desktop. It currently works on my Intel HD 3000 laptop, but on the desktop, every bind textures specifically from framebuffers, I get half a second of lag. This is done 4 times as I have three RGBA textures and one depth 32F buffer. I tried to use debugging software for the first time - RenderDoc only shows SwapBuffers() and no OGL calls, while Nvidia Nsight crashes upon execution, so neither are helpful. Without binding it runs regularly. This does not happen with non-framebuffer binds.
      GLFramebuffer::GLFramebuffer(FramebufferCreateInfo createInfo) { glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); textures = new GLuint[createInfo.numColorTargets]; glGenTextures(createInfo.numColorTargets, textures); GLenum *DrawBuffers = new GLenum[createInfo.numColorTargets]; for (uint32_t i = 0; i < createInfo.numColorTargets; i++) { glBindTexture(GL_TEXTURE_2D, textures[i]); GLint internalFormat; GLenum format; TranslateFormats(createInfo.colorFormats[i], format, internalFormat); // returns GL_RGBA and GL_RGBA glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, createInfo.width, createInfo.height, 0, format, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); DrawBuffers[i] = GL_COLOR_ATTACHMENT0 + i; glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0 + i, textures[i], 0); } if (createInfo.depthFormat != FORMAT_DEPTH_NONE) { GLenum depthFormat; switch (createInfo.depthFormat) { case FORMAT_DEPTH_16: depthFormat = GL_DEPTH_COMPONENT16; break; case FORMAT_DEPTH_24: depthFormat = GL_DEPTH_COMPONENT24; break; case FORMAT_DEPTH_32: depthFormat = GL_DEPTH_COMPONENT32; break; case FORMAT_DEPTH_24_STENCIL_8: depthFormat = GL_DEPTH24_STENCIL8; break; case FORMAT_DEPTH_32_STENCIL_8: depthFormat = GL_DEPTH32F_STENCIL8; break; } glGenTextures(1, &depthrenderbuffer); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); glTexImage2D(GL_TEXTURE_2D, 0, depthFormat, createInfo.width, createInfo.height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, 0); glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthrenderbuffer, 0); } if (createInfo.numColorTargets > 0) glDrawBuffers(createInfo.numColorTargets, DrawBuffers); else glDrawBuffer(GL_NONE); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) std::cout << "Framebuffer Incomplete\n"; glBindFramebuffer(GL_FRAMEBUFFER, 0); width = createInfo.width; height = createInfo.height; } // ... // FBO Creation FramebufferCreateInfo gbufferCI; gbufferCI.colorFormats = gbufferCFs.data(); gbufferCI.depthFormat = FORMAT_DEPTH_32; gbufferCI.numColorTargets = gbufferCFs.size(); gbufferCI.width = engine.settings.resolutionX; gbufferCI.height = engine.settings.resolutionY; gbufferCI.renderPass = nullptr; gbuffer = graphicsWrapper->CreateFramebuffer(gbufferCI); // Bind glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo); // Draw here... // Bind to textures glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, textures[1]); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_2D, textures[2]); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, depthrenderbuffer); Here is an extract of my code. I can't think of anything else to include. I've really been butting my head into a wall trying to think of a reason but I can think of none and all my research yields nothing. Thanks in advance!
    • By Adrianensis
      Hi everyone, I've shared my 2D Game Engine source code. It's the result of 4 years working on it (and I still continue improving features ) and I want to share with the community. You can see some videos on youtube and some demo gifs on my twitter account.
      This Engine has been developed as End-of-Degree Project and it is coded in Javascript, WebGL and GLSL. The engine is written from scratch.
      This is not a professional engine but it's for learning purposes, so anyone can review the code an learn basis about graphics, physics or game engine architecture. Source code on this GitHub repository.
      I'm available for a good conversation about Game Engine / Graphics Programming
    • By C0dR
      I would like to introduce the first version of my physically based camera rendering library, written in C++, called PhysiCam.
      Physicam is an open source OpenGL C++ library, which provides physically based camera rendering and parameters. It is based on OpenGL and designed to be used as either static library or dynamic library and can be integrated in existing applications.
       
      The following features are implemented:
      Physically based sensor and focal length calculation Autoexposure Manual exposure Lense distortion Bloom (influenced by ISO, Shutter Speed, Sensor type etc.) Bokeh (influenced by Aperture, Sensor type and focal length) Tonemapping  
      You can find the repository at https://github.com/0x2A/physicam
       
      I would be happy about feedback, suggestions or contributions.

  • Popular Now