weird smooth shading, vertex normals?

Depends on your hardware. But it is significantly slower. Use it in moderation.

I have a problem now when trying to load a model with texture and material.
I only get the material rendered but not the texture…
I will post the code which inicializes and uses the texture:


int num_texture=-1;
...
glEnable(GL_TEXTURE_2D);
...
objeto->tex_id=RS_Load_Bitmap(tex_name);
// then I load the u v coordinates for each vertex of each face
...
// And when I draw...
glBindTexture(GL_TEXTURE_2D, obj->tex_id);
...
if(obj->tex_id>-1)glTexCoord2f( obj->poligono[counter].ta.u, obj->poligono[counter].ta.v);
if(obj->tex_id>-1)glTexCoord2f( obj->poligono[counter].tb.u, obj->poligono[counter].tb.v);
if(obj->tex_id>-1)glTexCoord2f( obj->poligono[counter].tc.u, obj->poligono[counter].tc.v);
...
 

And this is the RS_Load_Bitmap function:

extern int RS_Load_Bitmap(char *filename) 
{ 

   unsigned char *l_texture;
   int i, j=0;
   FILE *l_file;
   BITMAPFILEHEADER fileheader; 
   BITMAPINFOHEADER infoheader;
   RGBTRIPLE rgb;
   num_texture++; 
   if( (l_file = fopen(filename, "rb"))==NULL) return (-1);
   fread(&fileheader, sizeof(fileheader), 1, l_file);
   fseek(l_file, sizeof(fileheader), SEEK_SET);
   fread(&infoheader, sizeof(infoheader), 1, l_file);
   l_texture = malloc(infoheader.biWidth * infoheader.biHeight * 4);
   memset(l_texture, 0, infoheader.biWidth * infoheader.biHeight * 4);
   for (i=0; i < infoheader.biWidth*infoheader.biHeight; i++)
   { 
      fread(&rgb, sizeof(rgb), 1, l_file); 

      l_texture[j+0] = rgb.rgbtRed; // Red component
      l_texture[j+1] = rgb.rgbtGreen; // Green component
      l_texture[j+2] = rgb.rgbtBlue; // Blue component
      l_texture[j+3] = 255; // Alpha value
      j += 4; // Go to the next position
   }
   fclose(l_file); // Closes the file stream
   glGenTextures( 1, &num_texture );
   glBindTexture(GL_TEXTURE_2D, num_texture);
   
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
   glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

   glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, l_texture);
   free(l_texture);
   return (num_texture);
}
 

Am I missing something?

Sorry for double posting, but I fixed the texture problem (I was enabling textures before setting them).

Now I have three questions which I am still wondering:

  1. Why doesn’t my models make shadows on other models when being in front or over them?
  2. Why do all the faces with the same vertex normals looks the same? (like if they weren`t taking the position of light into account)
  3. If I load a textured model, the texture is not afected by light or material, It looks like if using texture disables lighting. Why?

Thanks and sorry for this long thread about stupid questions hehe.

  1. casts shadows are quite advanced stuff. Two main ways to do them properly : shadow maps or stenciled shadow volumes.
  2. GL_LIGHT_MODEL_LOCAL_VIEWER defaults to GL_FALSE in glLightModel(), this means it simulates infinitely far light like the sun. You want it to be GL_TRUE.
    http://www.opengl.org/sdk/docs/man/
  3. strange, do you glDisable lighting ?
  1. Thanks, I’ll search some information about it, maybe I can use that in the future
  2. Thanks again, That was the problem =)
  3. The lighting is enabled. For example, look at the following screenshot (both the sphere and the earth were rendered at the same time, but when I move the lighting it only affects the sphere)

Have a read around 21.030 : http://www.opengl.org/resources/faq/technical/texture.htm

Do you wanna be my best friend?
hahaha :stuck_out_tongue:
Now seriously, thanks very much, you replied so quickly, and it worked!

I’m posting in this thread like twice a day, I’m happy to see that some people work hard to help others, this way learning to use OpenGl is easier

Thanks again

lol no problem

it won’t be any slower unless you embed the shader source in the file.As noted, display performance may be slightly decreased since hardware must calculate the lighting equation for every pixel instead of every vertex, but the result is worth it! Make sure you have OpenGL 2.0 capable hardware before trying. In any case you must write the shaders for the material yourself. I know that blender already supports real-time materials in the 3D view through shaders so if you dig into the blender source you may even find a shader that looks exactly like the blender rendering. But, after all why not write your own shader. It won’t take long if you know, and even if you don’t know you should consider learning.

Nice work by the way! I’ve worked on some custom made binary format and it’s not easy to import every blender attribute in OpengGL. For shadows you will need some extra work and some reading. As stated, shadow maps and shadow volumes are the alternatives but, hey! Look into nvidia and ATI developer sites to find tons of info!

I don’t mind if it’s too easy or too hard, I would like to learn. Honestly, I love programming. In fact, all I know is what I learned from internet and people like you. I’m 16 now but next year I’m entering to a university in order to study computer engineering.

This loader may end up as a game, which I would like to show in a game developers exposition this year in my country (as a house made game, nothing too serius)

Now I will clean some of my code. Then I’ll try to start with the shaders, and once I’m done with that, maybe start with the game (I think I’ll need a physics library or something like that to do physics calculations, won´t I?

Thanks for all, I think I’ll be posting my problems soon :stuck_out_tongue:

EDIT: Before starting with the shader stuff, how can i calculate and draw reflections in opengl? (for example, a metal-like ball reflecting light and objects)

Reflections (and a bunch of other things) are much easier to do with shaders, so start GLSL as soon as you can.

This series of tutorials is a very good start :
http://www.lighthouse3d.com/opengl/glsl/

Then if can have access to “The Orange Book” second edition, you are set. The book website have code and tools, but no tutorials :
http://www.3dshaders.com/home/

I’ve been testing my program with lot of models today, and I found out that when I move an object, the lighting doesn’t change. I mean, the object is lighted as if it were in the original position.
This is what I do to move the object:

  1. call glPushMatrix();
  2. translate
  3. rotate
  4. draw
  5. call glPopMatrix();

And where do you define your light ?
Read this page, especially 18.050 :
http://www.opengl.org/resources/faq/technical/lights.htm

If you define a directional light this is normal. Directionnal light is set if you put 0 as the 4th coordinate in the light position. Put 1 for an omnidirectional light.

I’m fixing the light position after calling glLoadIdentity(), so that the light ìsn`t moved or rotated with the objects. In fact, when I rotate an object, the light stay still (works well), but the problem appears when I move an object, so I think this is not the problem.

If you define a directional light this is normal. Directionnal light is set if you put 0 as the 4th coordinate in the light position. Put 1 for an omnidirectional light.

I’m using an array of 4 floats variables which I pass to glLightfv every frame. The 4th float of the array is 1.0, so I think it’s an omnidirectional light :S

Well, if any of you have a little time here is my source code if you wanna check it

  • in the “func.c” file is the light fixing function RS_Light_Fix, and also the drawing function RS_Render_Object, which is still slow because I’m not using VBO yet
  • in the “init.c” file are the inicializations of light variables (function RS_GL_Init)

Thanks to both

http://www.megaupload.com/?d=8J10AHT0

I have looked into your code very quicky and seen that you set a camera transformation. Since your light is an object like any any of your meshes drawn in the scene you have to transform it at least by the camera matrix.

So you have to set light position after computing camera transformation to transform the light properly.
If it is not the problem, I will check out your code more longer when I would have time.

That’s not the problem :S. I’ve already checked that.

Thanks anyway

Ah, that’s a shame, I have no more ideas right now, it must be something in your code that might be not related to opengl. I can’t find it your program is quite complicated, it is hard to find out. You should try to simplify it to isolate the source of your issue.
Anyway, I maintain what I said, you have to transform the light by the camera transformation matrix. This is normal since the camera movement is like a transformation that affects the whole scene including the lights.

I really understand you, my code IS a mess, I haven’t documented anything yet. Maybe simplifying it is a good idea, I will try to do that. I don’t know why but I feel like if my light is still directional and not omnidirectional (even if I’m doing the things the right way to make it be omnidirectional)

thanks for all

I started school so I’ve been busy, but I could made some time to check my code today, and I found the error. Like I suposed, it was the light. I was not inicializing it the right way (a really stupid error not related to OpenGL, I’m so ashamed that I can’t even tell it).

Now I have one question: Which is the best way to control the speed of a game/program using OpenGL in order to make it run at the same speed no matter at how much FPS it runs? (I think most of you will get what I mean)
Thanks