Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

Texture Colors Not exact as Texture


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 dreamslayerx   Members   -  Reputation: 110

Like
0Likes
Like

Posted 01 September 2011 - 08:26 PM

Hi guys. I hope you can help. I am on using Windows 7. For some reason when I use SDL to load a bitmap image, then I display it on a cube the image isn't the same as the texture image. The coloring is off basically. Here is how it looks; its the Color cube attachment. The actual texture is the bricks image. Here is my code below. Can anyone tell me why the texture color isn't being displayed properly?

[size="2"][color="#008000"][size="2"][color="#008000"]/*Applying Textures to Surfaces: This Program Works Great

Good example video http://www.youtube.com/watch?v=2BsTOgABU1k



Program works, but coloring seems to be off a little. Look into that.

The linux computer at school contains SDL libraries, using -lSDL */

/* Rotating Cube with Textures and Mouse click interactions

Rendered images are stored in OpenGL pictures in the UAB School Folder

This program is a intro to texturing. It use a 3D cube and textures it's sureface with colors defined in the color array RGB.

This program also introcudes a simple way to interact with cube using the mouse buttons.*/

//****Important Note put SDL.dll file in operating directory and link libraries

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<stdlib.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<iostream>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<GL/glut.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<SDL/SDL.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]#include[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#a31515"][size="2"][color="#a31515"]<SDL_opengl.h>

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]using[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]namespace[/color][/size][/color][/size][size="2"] std;



[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Prototypes

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] loadTexture([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]char[/color][/size][/color][/size][size="2"]* filename); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//I had to put this in since the initRendering function was using loadTexture function before it was declared

//note: notice that the range is from -1 to 1, and colors are from 0 to 1, while textures are from 0 to 1

[/color][/size][/color][/size][size="2"]GLfloat vertices[][3] = {{-1.0,-1.0,1.0},{-1.0,1.0,1.0}, 

{1.0,1.0,1.0}, {1.0,-1.0,1.0},

{-1.0,-1.0,-1.0},{-1.0,1.0,-1.0},

{1.0,1.0,-1.0},{1.0,-1.0,-1.0}};

GLfloat colors[][3] = {{1.0,0.0,0.0},{0.0,1.0,1.0},

{1.0,1.0,0.0},{0.0,1.0,0.0},

{0.0,0.0,1.0},{1.0,0.0,1.0}};

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] tex; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//global variable to hold texture

//Added initialization function

//Initializes 3D rendering

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] initRendering() { [/size][size="2"][color="#008000"][size="2"][color="#008000"]//in this function I may need to add in the 

[/color][/size][/color][/size][size="2"]glEnable(GL_DEPTH_TEST);

glEnable(GL_COLOR_MATERIAL);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_LIGHTING); //enable lighting

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_LIGHT0); //enable light #0

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_LIGHT1); //enable light #1

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//glEnable(GL_NORMALIZE); //Automatically normalize normals

[/color][/size][/color][/size][size="2"]glShadeModel(GL_SMOOTH); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//enable smooth shading

[/color][/size][/color][/size][size="2"]glEnable(GL_TEXTURE_2D); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//Enables the use of 2D textures

[/color][/size][/color][/size][size="2"]tex = loadTexture([/size][size="2"][color="#a31515"][size="2"][color="#a31515"]"Bricks.bmp"[/color][/size][/color][/size][size="2"]); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//load up file

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//Adds ambient light

[/color][/size][/color][/size][size="2"]GLfloat ambientColor[] = {0.5, 0.5, 0.5, 1.0};

glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Adds positioned light

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]/*GLfloat lightColor0[] = {0.5, 0.5, 0.5, 1.0};

GLfloat lightPos0[] = {4.0, 0.0, 8.0, 1.0};

glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);

glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/

//Adds directed light

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]/*GLfloat lightColor1[] = {0.5, 0.2, 0.2, 1.0};

GLfloat lightPos1[] = {-1.0, 0.5, 0.5, 0.0};

glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);

glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);*/

[/color][/size][/color][/size][size="2"]}

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//***********************************Texture Function for Loading*************************************************************

/*Here I need to load a bitmap image, generate textures, bind texture, set glTextImage2D, glTextParameteri

Some ppl use programs such as SDL to read in bitmap images (just not to reinvent the wheel). Depending on the requirements of the Viscube

I can aslo us SDL. In this case I am trying to establish reading using SDL*/

//Reading the texture from an image file: OpenGL has no function to load an image

//Here I am going to use the SDL as my image uploader //SDL uploader test

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] loadTexture([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]const[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]char[/color][/size][/color][/size][size="2"]* filename)

{

SDL_Surface* img =SDL_LoadBMP(filename); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//figure out why color isn't coming in

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]unsigned[/color][/size][/color][/size][size="2"] [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] id; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//assigning an id for glGenTextures

[/color][/size][/color][/size][size="2"]glGenTextures(1, &id); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//idea assigned

[/color][/size][/color][/size][size="2"]glBindTexture(GL_TEXTURE_2D, id); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//binding texture to the id

[/color][/size][/color][/size][size="2"]glTexImage2D(GL_TEXTURE_2D,

0, 

GL_RGB, 

img->w, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//width of image

[/color][/size][/color][/size][size="2"]img->h, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//height of image

[/color][/size][/color][/size][size="2"]0,

GL_RGB, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//format

[/color][/size][/color][/size][size="2"]GL_UNSIGNED_BYTE, [/size][size="2"][color="#008000"][size="2"][color="#008000"]//what format is the image stored

[/color][/size][/color][/size][size="2"]img->pixels); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//pixel array, pixels

[/color][/size][/color][/size][size="2"]

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Setting Parameters

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]//Warping Parameters (how texture will be applied to the object)

[/color][/size][/color][/size][size="2"]glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//Many different parameters can be set her to manipulate the textures

[/color][/size][/color][/size][size="2"]glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Filtering Parameters

[/color][/size][/color][/size][size="2"]glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//2d texture with a minimum filter, and linear interpolation (averaging pixlels)

[/color][/size][/color][/size][size="2"]glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

SDL_FreeSurface(img); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//deletes the texture image out of the texture memory

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] id;

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] polygon([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] a, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] b, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] c, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] d)

{

[/size][size="2"][color="#008000"][size="2"][color="#008000"]/*draw a polygon via list of vertices */

[/color][/size][/color][/size][size="2"]

glBindTexture(GL_TEXTURE_2D, tex); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//This binds the textures to the polygon 

[/color][/size][/color][/size][size="2"]glBegin(GL_POLYGON); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//applying the textures are based on a square that is lower left(0,0), Upper left (0,1), lower right (1,0), upper right (1,1)

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#008000"][size="2"][color="#008000"]// glColor3fv(colors[a]); //These texture points are placed witht he corresponding vertices, to make sure that the texture is applied correctly

[/color][/size][/color][/size][size="2"]glTexCoord2f(0.0,0.0);

glVertex3fv(vertices[a]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glColor3fv(colors[b]);

[/color][/size][/color][/size][size="2"]glTexCoord2f(0.0,1.0);

glVertex3fv(vertices[b]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glColor3fv(colors[c]);

[/color][/size][/color][/size][size="2"]glTexCoord2f(1.0,1.0);

glVertex3fv(vertices[c]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//glColor3fv(colors[d]);

[/color][/size][/color][/size][size="2"]glTexCoord2f(1.0,0.0);

glVertex3fv(vertices[d]);

glEnd();

[/size][size="2"][color="#008000"][size="2"][color="#008000"]/*Here are the vertices that he used

glBegin(GL_QUADS);

glVertex3f(-2.0,2.0,0.0);

glVertex3f(-2.0,-2.0,0.0);

glVertex3f(2.0,-2.0,0.0);

glVertex3f(2.0,2.0,0.0);

glEnd();*/

[/color][/size][/color][/size][size="2"]}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] colorcube([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"])

{ [/size][size="2"][color="#008000"][size="2"][color="#008000"]//function is used to state which sections of the array for colors and vertext to use

/*map vertices to faces */

[/color][/size][/color][/size][size="2"]polygon(0, 3, 2, 1);

polygon(2, 3, 7, 6);

polygon(3, 0, 4, 7);

polygon(1, 2, 6, 5);

polygon(4, 5, 6, 7);

polygon(5, 4, 0, 1);

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]static[/color][/size][/color][/size][size="2"] GLfloat theta[] = {0.0,0.0,0.0}; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//declaring two static variables theta and axis

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]static[/color][/size][/color][/size][size="2"] GLint axis = 2;

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//display function

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] display([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"])

{

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

glRotatef(theta[0], 1.0,0.0,0.0);

glRotatef(theta[1], 0.0,1.0,0.0);

glRotatef(theta[2], 0.0,0.0,1.0);

colorcube(); [/size][size="2"][color="#008000"][size="2"][color="#008000"]//refers to the function that mapped the vertices to faces

[/color][/size][/color][/size][size="2"]glutSwapBuffers();

}

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//The below section contains the interaction component of the program

[/color][/size][/color][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] spinCube()

{

theta[axis] += 0.05;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](theta[axis] > 360.0)

theta[axis] -= 360.0;

glutPostRedisplay();

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] mouse([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] btn, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] state, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] x, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] y)

{

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Mouse button interaction

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)

axis = 0;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)

axis = 1;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)

axis = 2;

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]void[/color][/size][/color][/size][size="2"] myReshape([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] w, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] h)

{

glViewport(0,0,w,h);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]if[/color][/size][/color][/size][size="2"](w <= h)

glOrtho(-2.0, 2.0, -2.0* (GLfloat) h/ (GLfloat) w, 2.0 *(GLfloat) h/ (GLfloat) w, -10.0, 10.0);

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]else

[/color][/size][/color][/size][size="2"]glOrtho(-2.0* (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h/ (GLfloat) w, -2.0,2.0,-10.0,10.0);

glMatrixMode(GL_MODELVIEW);

}

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] main([/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] argc, [/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]char[/color][/size][/color][/size][size="2"] ** argv)

{

GLubyte image[64][64][3]; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//seems like 64 by 64 pixels taking a vertice made up of 3 points

[/color][/size][/color][/size][size="2"][/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]int[/color][/size][/color][/size][size="2"] i, j, r, c;

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]for[/color][/size][/color][/size][size="2"](i =0; i <64; i++)

{

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]for[/color][/size][/color][/size][size="2"](j=0; j<64; j++)

{

c = ((((i & 0x8)== 0)^((j & 0x8)) == 0))*255; [/size][size="2"][color="#008000"][size="2"][color="#008000"]//I think 0x8 has to always be written like this, not 0 x 8 or it will cause problems.

[/color][/size][/color][/size][size="2"]image[i][j][0] = (GLubyte) c;

image[i][j][1] = (GLubyte) c;

image[i][j][2] = (GLubyte) c;

}

}

glutInit(&argc, argv);

glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);

glutInitWindowSize(500,500);

glutCreateWindow([/size][size="2"][color="#a31515"][size="2"][color="#a31515"]"colorcube"[/color][/size][/color][/size][size="2"]);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]//Place initialization function here

[/color][/size][/color][/size][size="2"]initRendering();

glutReshapeFunc(myReshape);

glutDisplayFunc(display);

glutIdleFunc(spinCube);

glutMouseFunc(mouse);

[/size][size="2"][color="#008000"][size="2"][color="#008000"]

[/color][/size][/color][/size][size="2"]glutMainLoop();

[/size][size="2"][color="#0000ff"][size="2"][color="#0000ff"]return[/color][/size][/color][/size][size="2"] 0;

}

[/size]



Attached Thumbnails

  • OpenGL texture on Cube wrong color.jpg

Attached Files



Sponsor:

#2 Yann L   Moderators   -  Reputation: 1754

Like
2Likes
Like

Posted 01 September 2011 - 09:47 PM

The red and blue channels are probably swapped, ie. your image data comes in BGR component order.

Try GL_BGR as the format parameter to the glTexImage2D call (instead of GL_RGB).

#3 dreamslayerx   Members   -  Reputation: 110

Like
0Likes
Like

Posted 02 September 2011 - 07:19 AM

Fixed Texture Color Scheme GL_BGR.jpg

The red and blue channels are probably swapped, ie. your image data comes in BGR component order.

Try GL_BGR as the format parameter to the glTexImage2D call (instead of GL_RGB).



Thanks that was exactly what it was. Do you mind me asking how that happens? Is it just that specific image?

#4 Brother Bob   Moderators   -  Reputation: 4655

Like
1Likes
Like

Posted 02 September 2011 - 07:53 AM

BMP files are stored in BGR color order. If you read the image data directly from the file, that is the format you get.

#5 dreamslayerx   Members   -  Reputation: 110

Like
0Likes
Like

Posted 02 September 2011 - 08:02 AM

BMP files are stored in BGR color order. If you read the image data directly from the file, that is the format you get.



Yeah thats the crazy thing. I used SDL to read in the texture, so I think that its kind of strange that the color format was reversed. Well now I am on to reading in .obj files to display my textures on models using blender and opengl. Thanks guys. Any tips?

#6 V-man   Members   -  Reputation: 785

Like
0Likes
Like

Posted 02 September 2011 - 09:45 AM

I don't use SDL but I imagine it is because SDL reads the data and doesn't convert the BGR to RGB automatically for you. You are probably suppose to make some function call into the SDL for it to convert for you.

Or, use SDL to know what the storage format is. If it says BGR then you send the data to OpenGL as BGR.
So, you need to know a little of SDL.
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#7 marcClintDion   Members   -  Reputation: 97

Like
1Likes
Like

Posted 02 September 2011 - 04:04 PM

The following code is from freeImage.cpp, it shows how to load an image and swap the RGB channels, I made a copy of the relevant code snippet and posted it first and added some comments to the swapping code.

for(int pix=0; pix<FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib); pix++)
{
bits[pix*3+0]=pixels[pix*3+2]; //<-------this line swaps Red with Blue
bits[pix*3+1]=pixels[pix*3+1]; //<-------Green stays the same
bits[pix*3+2]=pixels[pix*3+0]; //<-------this line swaps Blue with Red

}

What follows is the complete more or less original freeImage library image loading function. I say more or less because I have commented out the line that converts all images
to 24bits(line # 5). This line, if used, causes a duplicate image to be created and stored in memory which is not released until program shutdown, This doubles the amount of memory being
used by your texture maps. If you do use this code ,as is, you will have to always remember to only use 24bit images or your free Image will crash your program. To use freeImage you will also
need a copy of FreeImage.h(42.4 KB), FreeImage.dll(1 MB) and FreeImage.lib(52.5 KB), You should be able to use this together with SDL, I added the file sizes so you will have an easier time finding a matching set.


//----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


void loadTexture(char *textureFileName, GLuint &textureMapID)
{
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(textureFileName, 0);

FIBITMAP *dib = FreeImage_Load(fifmt, textureFileName,0);

//dib = FreeImage_ConvertTo24Bits(dib);

if( dib != NULL )
{
// This is important to note, FreeImage loads textures in
// BGR format. Now we could just use the GL_BGR extension
// But, we will simply swap the B and R components ourselves.
// Firstly, allocate the new bit data doe the image.
BYTE *bits = new BYTE[FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 3];

// get a pointer to FreeImage's data.
BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);

// Iterate through the pixels, copying the data
// from 'pixels' to 'bits' except in RGB format.
for(int pix=0; pix<FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib); pix++)
{
bits[pix*3+0]=pixels[pix*3+2];
bits[pix*3+1]=pixels[pix*3+1];
bits[pix*3+2]=pixels[pix*3+0];

}

// The new 'glTexImage2D' function, the prime difference
// being that it gets the width, height and pixel information
// from 'bits', which is the RGB pixel data..
//glTexImage2D( GL_TEXTURE_2D, 0, 3, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0, GL_RGB, GL_UNSIGNED_BYTE, bits );

glGenTextures( 1, &textureMapID );
glBindTexture( GL_TEXTURE_2D, textureMapID );

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri( GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), GL_RGB, GL_UNSIGNED_BYTE, bits);


// Unload the image.
// and free the bit data.
FreeImage_Unload(dib);
delete bits;
}

}

#8 mhagain   Members   -  Reputation: 3831

Like
3Likes
Like

Posted 02 September 2011 - 05:51 PM

This line needs attention drawn to it:
// BGR format. Now we could just use the GL_BGR extension
Substitute "could" with "should". Maybe in 1997 or 1998 there was a valid reason to swap in software, but every 3D card since then supports BGR - if you have OpenGL 1.2 or higher you have BGR, simple as that.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#9 V-man   Members   -  Reputation: 785

Like
3Likes
Like

Posted 03 September 2011 - 06:23 AM

You shouldn't even be using gluBuild2DMipmaps anymore or "3" as an internal format
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#10 Yann L   Moderators   -  Reputation: 1754

Like
2Likes
Like

Posted 03 September 2011 - 08:56 AM

Not even mentioning that the code above invokes undefined behaviour and could possibly lead to memory leaks or even severe heap corruption.

BYTE *bits = new BYTE[FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 3];

[..stuff..]

delete bits;



#11 marcClintDion   Members   -  Reputation: 97

Like
-6Likes
Like

Posted 14 November 2011 - 09:45 PM

Wow, it's clear to me that having a large number under reputation doesn't actually mean much here. Thanks for the snarky no-it-all comments. This results of this code work on every computer with good performance
and the code is easy to use.
//--------------------------------------------------------------------------------------------------
mhagain->>>He asked a question, I answered it, simple as that.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------
V-man ->>> gluBuild2DMipmaps(GL_TEXTURE_2D, 3.......really? no explanation why, no alternative. Maybe you should keep your point-less comments to yourself. That's deprecated doesn't cut.
//---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Yann L ->> Once again, really? no explanation why, no alternative. Maybe you should keep your point-less comments to yourself. Or better yet. You send "[..stuff..]"

Send your pointless little snippet to the people who built freeImage and tell them that their software isn't written properly. I'm sure they would love to receive -----> [..stuff..] from you. They will probably contact gameDev so that your "reputation" can be boosted some more. Clearly you deserve it.


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
No doubt you'll scream and hollar murder so that my account is blocked, go right ahead. This is an antagonistic, and not very useful,environment. You are not here to help. You simply have damaged self=esteem and fell that you can get it by acting like condescending snide losers. If any one out there want to learn how to do what you will not learn on this site.
Goto,

http://nehe.gamedev.net/

http://www.codesampler.com/code/

Grab the CG-toolkit from nVidia as well as the accompany booklet.

Also visit http://www.songho.ca/opengl/

Grab the redbook -for openGl
the orangeBook for GLSL

http://www.clockworkcoders.com/oglsl/ <--------These people know what's up as well

Grab ----> bruce eckels thinking c++ volumes 1and2


Read the black art of Java Game Programming


There isn't much else that you will need, especially not around here.




#12 Hodgman   Moderators   -  Reputation: 13610

Like
2Likes
Like

Posted 14 November 2011 - 10:46 PM

mhagain -- I don't appreciate you pointing out the flaws in my code. Please refrain from posting suggestions relating to other people's posts.

V-man -- Can you please explain the modern alternative for producing mipmaps?

Yann L -- Can you please explain what part of that code evokes undefined behaviour, and why it's at risk of leaking memory?

^^ Fixed that for you.

#13 L. Spiro   Crossbones+   -  Reputation: 5187

Like
2Likes
Like

Posted 14 November 2011 - 11:21 PM

If someone asks me how to build a house and I suggest to him or her to go cut down trees, gather wood, and use a hammer and nails, yes, I have answered his question.
When someone else comes along and says, "You know, that way is deprecated; we use bricks and mortar now," he or she has given an answer that may be more suitable to the original person seeking advice.

In the pursuit of knowledge one must never be stuck in his or her own ways, and admit one's shortcomings with a smile as new things or ways are learned.

I would not be offended if someone suggested a better way than my own. Why were you?

One of the reasons I post here so often is because I can also learn when I give less-than-the-best advice and someone comes along and corrects it. I think I am pretty useful and help often, but I also say stupid things at times and make poor suggestions.

Your reaction to having been corrected is off-the-wall.

Also, Yann L showed you exactly where the error is.
You [or whomever] used new [] but not delete []. You [or whomever] used plain delete.
Instead of looking with an open mind and saying, "Oh I see, I made a mistake. Thank you for finding that because the quality of my software has increased because of it," you just screamed how correct the code is based on it working "on every computer", which is something you didn't even test, and stomped off with flawed code.


L. Spiro
It is amazing how often people try to be unique, and yet they are always trying to make others be like them. - L. Spiro 2011
I spent most of my life learning the courage it takes to go out and get what I want. Now that I have it, I am not sure exactly what it is that I want. - L. Spiro 2013
L. Spiro Engine: http://lspiroengine.com
L. Spiro Engine Forums: http://lspiroengine.com/forums

#14 marcClintDion   Members   -  Reputation: 97

Like
0Likes
Like

Posted 17 November 2011 - 04:31 PM

Actually L. Spiro. YOU told us exactly where this particular error is. The other person re-wrote the same mistake and said it was wrong without the correction that you presented. Maybe to you it was obvious that [] should have been added, but not to everyone. I do not get upset because I was corrected, I wasn't corrected, I was instead heckled for attempting to help. If I were building a house as you say and someone came by and critisized me for doing it wrong, then turned around and walked away without any clearly stated "help", I would have no respect for this person's opinion. If those people had put things nicely, and had given some useful advice I would have followed it. If that is how the "experts" around here behave then it is clear that the "experts" need to be told how to behave properly. It rubs me the wrong way when no-it-alls use what they "know" to make themselves feel superior. Simply put, I overreacted, but what I reacted to was most certainly something to react to. And by the way, I've tested that code extensively on many different computers with very different hardware and it works beautifully. I re-compile sometimes half-a-dozen times every few minutes. I have also used this code to load 240 pictures at 1024x1024 all at the same time, if there really were memory leaks and corruption it would have been very obvious, these computers would crash. Maybe the Dev-Cpp compiler fixes errors like these automatically, I will however take you advice on this matter and make the appropriate correction:delete[]. I am aware that Nvidia has posted a document called "fast texture transfers" and in this document it is pointed out that BGR should be used but in most cases even they say that it has little or no performance increase. The response I received was snarky and overblown. I escalated things and I'm sorry.







#15 mhagain   Members   -  Reputation: 3831

Like
0Likes
Like

Posted 17 November 2011 - 09:01 PM

We;; for my part i wasn't trying to make little of your work; my motive was to point out to the OP that on a more modern GPU using BGR is preferable to swapping the bytes manually. I can see how it could have came across otherwise, and I could have picked my words better.

I have done quite a bit of benchmarking of texture uploads using different format and type parameters, and GL_BGRA is actually the one to use. ;)

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.


#16 dpadam450   Members   -  Reputation: 537

Like
2Likes
Like

Posted 18 November 2011 - 12:56 AM

Well if you want any openGL help, then you're kind of pissing of the wrong people. Secondly a fact is not an attack:

Not even mentioning that the code above invokes undefined behaviour and could possibly lead to memory leaks or even severe heap corruption.


The code posted was bad. For instance why make a new array instead of just swapping the r,g,b values in the original array?

Send your pointless little snippet to the people who built freeImage and tell them that their software isn't written properly.

Just because someone gave away free code doesn't mean it is good. It really was bad code, and was a memory leak.

The problem was already solved anyway and then you posted a ton of code and more work than the standard solution of GL_BGR. You can also google things like gluBuild2DMipMaps and find out why he is saying not to use it.


#17 Hodgman   Moderators   -  Reputation: 13610

Like
0Likes
Like

Posted 18 November 2011 - 01:57 AM

ok

#18 V-man   Members   -  Reputation: 785

Like
2Likes
Like

Posted 18 November 2011 - 06:58 AM

From the Wiki
http://www.opengl.org/wiki/Common_Mistakes#GL_TEXTURE_MAG_FILTER

and also
http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

#19 marcClintDion   Members   -  Reputation: 97

Like
1Likes
Like

Posted 19 November 2011 - 05:20 PM

Thanks hodgman for reminding me to be polite. Thanks Vman for the useful links. Also, so far as all the code I posted goes. I was simply trying to illustrate a point. The original poster did not understand why his colors were inverted and I posted code that showed how this takes place. All of the rest of it was included so this fellow would know WHERE it takes place. A little snippet with no indication of where this takes place can be frustrating for a person who is not familiar with the big picture. I included the rest to avoid confusion and created a big stink. Now dpadam450 is running his mouth(fingers). Well, I'm out of here. That code could have been corrected and refined so that anyone who showed up here could see exactly how it should be done and that simply has not happened here. This thread is a mess and now a new heckler has shown up with no useful advice. This is not a healthy or productive environment. I'm going stick with google. For the hecklers and no-it-alls with snide remarks: don't bother posting anything else for me to read, I won't be visiting this site again.

#20 dpadam450   Members   -  Reputation: 537

Like
-1Likes
Like

Posted 19 November 2011 - 06:24 PM

Secondly a fact is not an attack:






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS