Applying texture to sphere turns it blue

Thread Solved

Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Applying texture to sphere turns it blue

 
0
  #11
Jul 7th, 2009
Ok i just did everything you just said to do and nothings really changed. This is so wierd, i don't know what the problem is. Earth is mapped out fine!

I'm about to upload an update of the code
Last edited by orangejuice2005; Jul 7th, 2009 at 3:27 am.
Attached Images
File Type: bmp venus1.bmp (58.6 KB, 2 views)
File Type: bmp mars.bmp (96.1 KB, 1 views)
File Type: bmp mercury.bmp (117.2 KB, 2 views)
File Type: bmp earth2.bmp (29.3 KB, 2 views)
File Type: bmp moon.bmp (96.1 KB, 1 views)
Attached Files
File Type: cpp Solar System.cpp (14.4 KB, 2 views)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Applying texture to sphere turns it blue

 
0
  #12
Jul 8th, 2009
Your palette is flipped!

Red is Blue and Blue is Red. Green is fine!

You're using a 24-bit BMP and those are oriented Blue, Green, Red, however the frame buffer is Red, Green, Blue!

I usually use TGA's so I'm not sure if you need to swap the texture or if Glut is suppose to do it!
Last edited by wildgoose; Jul 8th, 2009 at 4:59 am.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Applying texture to sphere turns it blue

 
1
  #13
Jul 8th, 2009
Okay looked at your BMP loader and noticed a big problem. I went into one of my full blown BMP loaders and twiddled yours specific to 24-bit RGB bitmaps.

Problem #1 - you were treating header data as pixel data!
Problem #2 - BGR vs RGB
Problem #3 - You had to hardcode your bitmap resolution instead of using the values set int the bitmap.

This is more like a hack but it'll fix your bitmap issue!

Note that I left your width, height, and depth to be passed in to the function but I actually ignore them as they are read from the file itself!

  1.  
  2.  
  3. //
  4. // BMP version 3.x header
  5. //
  6.  
  7. #pragma pack(1)
  8. typedef struct Bmp3XHead_Type
  9. {
  10. unsigned short ImageFileType; // File Identifier (always 0)
  11.  
  12. unsigned int FileSize; // Size of files in bytes
  13. unsigned short Reserved[2];
  14. unsigned int ImageDataOffset; // Start of image data offset
  15. } Bmp3XHead;
  16. #pragma pack()
  17.  
  18.  
  19.  
  20. //
  21. // BMP version 3.x informational header
  22. //
  23.  
  24. #pragma pack(1)
  25. typedef struct Bmp3XInfo_Type
  26. {
  27. unsigned int HeaderSize; // Size of this header
  28. unsigned int Width; // Width of image
  29. unsigned int Height; // Height of bitmap in scanlines
  30. unsigned short Planes; // # of color planes
  31. unsigned short Depth; // # of bits per pixel
  32. unsigned int Compression; // Type of compression
  33. unsigned int ImageSize; // Size of bitmap in bytes
  34. unsigned int HRes; // Horizontal Resolution in pixels/meter
  35. unsigned int VRes; // Vertical Resolution in pixels/meter
  36. unsigned int nColor; // # of colors in image
  37. unsigned int nColorCnt; // Number of important colors in palette
  38. } Bmp3XInfo;
  39. #pragma pack()
  40.  
  41.  
  42.  
  43.  
  44.  
  45. void load_texture ( char *filename, int width, int height, int depth, GLenum colour_type, GLenum filter_type )
  46. {
  47. GLubyte *texture ;
  48. FILE *file;
  49.  
  50. file = fopen( filename, "rb" );
  51. if ( file == NULL )
  52. printf("File not found: \n", filename);
  53.  
  54. Bmp3XHead head;
  55.  
  56. fread( &head, sizeof(head), 1, file );
  57. if ( 0x4d42 != head.ImageFileType )
  58. {
  59. fclose(file);
  60. return;
  61. }
  62.  
  63. Bmp3XInfo info;
  64.  
  65. fread( &info, sizeof(info), 1, file );
  66.  
  67. width = info.Width;
  68. height = info.Height;
  69. depth = info.Depth;
  70.  
  71. if (depth != 24)
  72. {
  73. fclose( file );
  74. return;
  75. }
  76.  
  77. depth >>= 3;
  78.  
  79. fseek( file, head.ImageDataOffset, SEEK_SET ); // Start of pixels!
  80.  
  81. texture = (GLubyte *) malloc ( width * height * depth * ( sizeof(GLubyte)) );
  82.  
  83. if (texture == NULL)
  84. {
  85. printf ( "Cannot allocate memory for texture\n" );
  86. fclose ( file);
  87. exit ( 1 );
  88. }
  89.  
  90. fread ( texture , width * height * depth, 1 , file );
  91. fclose ( file);
  92.  
  93. // Swap blue & red
  94. GLubyte *pix, *pixEnd, col;
  95. pix = texture;
  96. pixEnd = texture + width * height * depth;
  97.  
  98. while( pix < pixEnd )
  99. {
  100. col = *pix;
  101. *pix = *(pix+2);
  102. *(pix+2) = col;
  103. pix += 3;
  104. }
  105.  
  106. // Set Filtering type
  107. glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter_type );
  108. glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter_type );
  109.  
  110. // Set Texture Evironment
  111. glTexEnvf ( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
  112.  
  113. // Build Mipmaps
  114. gluBuild2DMipmaps ( GL_TEXTURE_2D, colour_type, width, height,
  115. colour_type, GL_UNSIGNED_BYTE, texture );
  116.  
  117. // Free up the array
  118. free ( texture );
  119. }
Last edited by wildgoose; Jul 8th, 2009 at 5:50 am. Reason: tab adjustments
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 79
Reputation: orangejuice2005 is an unknown quantity at this point 
Solved Threads: 0
orangejuice2005 orangejuice2005 is offline Offline
Junior Poster in Training

Re: Applying texture to sphere turns it blue

 
0
  #14
Jul 8th, 2009
Mad respect wildgoose. I talked to my professor last night about my issue and we were on to what you managed to figure out. The problem definately lied with the images, more specifically the color indexes. When it stopped being an issue for me was when i decided not to use images in bmp format but rather use raw images. I mentioned I initially had an issue with images in said format and that was because the program i was using to convert my images was not doing it properly. I decided to use photoshop, and i should have done that in the first place, and i had working textures.

I'll definately try out the great fix, genius material btw, you posted for me when i get back from work and see how that turns out. Thanks a lot for analyzing my issue and helping me fix it!

+1!
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 830
Reputation: wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all wildgoose is a name known to all 
Solved Threads: 94
wildgoose's Avatar
wildgoose wildgoose is offline Offline
Practically a Posting Shark

Re: Applying texture to sphere turns it blue

 
0
  #15
Jul 8th, 2009
no problem, just send the check to............

When I looked at it late last night I noticed again that the image seemed inverted blue vs brown. I scaled mars large, and when I applied the earth texture onto mars, water was brown, ground was blue. Then applied the moon texture onto mars, it appeared correct. That was clue #1 & 2.

I created a solid red texture and it showed up blue. I knew what was wrong but then tested the blue texture and it showed up red confirming my suspicion. And of course green was green. Thus the first posting of red and blue were swapped. I don't use BMP's I use TGA's but I remembered the BMP's are in BGR order, not RGB.

I then wrote the red-Blue color swapper and it didn't work. That's when I looked at your BMP loader more closely and then noticed the other problem. NO HEADERS. You were trying to treat the BMP like a raw file. I cut in the headers, overrode your inputs {w, h, d} with those from the file, and it came up like it was suppose to!

By the way. Move the image types within the planet functions themselves, don't keep them outside. And unless you're planning on trying to show earth textures on Jupiter, etc. Don't pass in the texture enum to get the texture name. Hard code them within the individual planet function's texture name lookup.

There are other cleanup issues, but just think, "How can I make this look more clean!" Once functionality is working. Save a copy of the project, then continue making changes. Archiving the project has to do with having a backup if cleaning goes terrible wrong. You'll have a fall back point to the last know good working version.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Game Development Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC