Sign in to follow this  

OpenGL Texture Colors Not exact as Texture

This topic is 2245 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

[code]
[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]



[/code]

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
[attachment=5226:Fixed Texture Color Scheme GL_BGR.jpg][quote name='Yann L' timestamp='1314935243' post='4856583']
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).
[/quote]


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

Share this post


Link to post
Share on other sites
[quote name='Brother Bob' timestamp='1314971628' post='4856737']
BMP files are stored in BGR color order. If you read the image data directly from the file, that is the format you get.
[/quote]


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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites
This line needs attention drawn to it:[code] // BGR format. Now we could just use the GL_BGR extension[/code]
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.

Share this post


Link to post
Share on other sites
You shouldn't even be using gluBuild2DMipmaps anymore or "3" as an internal format
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,

Share this post


Link to post
Share on other sites
Not even mentioning that the code above invokes undefined behaviour and could possibly lead to memory leaks or even severe heap corruption.

[quote name='marcClintDion' timestamp='1315001078' post='4856946']
BYTE *bits = new BYTE[FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 3];

[..stuff..]

delete bits;
[/quote]

Share this post


Link to post
Share on other sites
[quote name='marcClintDion' timestamp='1321328755' post='4884044']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?[/quote]^^ Fixed that for you.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
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. ;)

Share this post


Link to post
Share on other sites
Well if you want any openGL help, then you're kind of pissing of the wrong people. Secondly a fact is not an attack:
[quote]Not even mentioning that the code above invokes undefined behaviour and could possibly lead to memory leaks or even severe heap corruption.[/quote]

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?

[quote]Send your pointless little snippet to the people who built freeImage and tell them that their software isn't written properly.[/quote]
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Ok fine I'm sorry for being grouchy. Thank you all for helping people fix their code. How's this look? //------------------------------------------------------------------------------------------------------------------------------------------------. void loadTexture(char *textureFileName, GLuint &textureMapID) { FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(textureFileName, 0); FIBITMAP *dib = FreeImage_Load(fifmt, textureFileName,0); FIBITMAP *temp = dib; dib = FreeImage_ConvertTo32Bits(temp); FreeImage_Unload(temp); if( dib != NULL ) { glGenTextures( 1, &textureMapID ); glBindTexture( GL_TEXTURE_2D, textureMapID ); BYTE *pixels = (BYTE*)FreeImage_GetBits(dib); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels); free(pixels); FreeImage_Unload(dib); } } //------------------------------------------------------------------------------------------------------------------------------------------------. Maybe now we are closer to having a proper image loader for people to make use of. The SGIS mip map generator appears to be hardware accelerated, almost twice as fast on a low end quadro, and about 1/3 faster on a budget intel graphics machine. Is a performance comparison like this inappropriate? Would it be considered bias or favoritism towards one or the other? I'm happy for both running faster now. There do not appear to be memory leaks. The dual core Intel with 2GB RAM and mobile 4 series graphics running on Win7 handled the loading of 100 1024x1024 images, then 200 1024x1024 images, then 300 1024x1024 images.

Share this post


Link to post
Share on other sites
Hidden
Maybe this is better for people who chance upon this topic.. I've chosen to comment out ---> //glGenerateMipmap(GL_TEXTURE_2D); because the documentation mentioned above gives the impression
that --->>>glGenerateMipmap(GL_TEXTURE_2D); is having some sort of version conflict nonsense. It is supported then not supported or some such nonsense. The --->>GL_GENERATE_MIPMAP_SGIS<<----works without adding extensions, seems to have no deprecation or version problems and offers a substantial performance boost on both nVidia as well as mobile Intel graphics(ATI, not tested yet) . Almost half the loading time for a mobile Quadro(NVS 130), and about 25% faster on budget mobile Intel(x4500).

I'm not sure if I've gotten things up to speed so far as all of the hints and tips provided goes, but things are at least, superficially, looking good. Two hundred and forty 1024 x 1024 textures loaded simultaneously without crashing either machine seems workable to me.

//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
//FreeImage_Save(FIF_JPEG, dib, "mybitmap.jpg", JPEG_QUALITYSUPERB);

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

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

FIBITMAP *temp = dib;
dib = FreeImage_ConvertTo32Bits(temp);
FreeImage_Unload(temp);

if( dib != NULL )
{
glGenTextures( 1, &textureMapID );
glBindTexture( GL_TEXTURE_2D, textureMapID );

BYTE *pixels = (BYTE*)FreeImage_GetBits(dib);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, GL_TRUE );
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, FreeImage_GetWidth(dib), FreeImage_GetHeight(dib), 0, GL_BGRA, GL_UNSIGNED_BYTE, pixels);
//glGenerateMipmap(GL_TEXTURE_2D);

free(pixels);
FreeImage_Unload(dib);

}

}
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Share this post


Link to post
Sign in to follow this  

  • Similar Content

    • By _OskaR
      Hi,
      I have an OpenGL application but without possibility to wite own shaders.
      I need to perform small VS modification - is possible to do it in an alternative way? Do we have apps or driver modifictions which will catch the shader sent to GPU and override it?
    • By xhcao
      Does sync be needed to read texture content after access texture image in compute shader?
      My simple code is as below,
      glUseProgram(program.get());
      glBindImageTexture(0, texture[0], 0, GL_FALSE, 3, GL_READ_ONLY, GL_R32UI);
      glBindImageTexture(1, texture[1], 0, GL_FALSE, 4, GL_WRITE_ONLY, GL_R32UI);
      glDispatchCompute(1, 1, 1);
      // Does sync be needed here?
      glUseProgram(0);
      glBindFramebuffer(GL_READ_FRAMEBUFFER, framebuffer);
      glFramebufferTexture2D(GL_READ_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
                                     GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, texture[1], 0);
      glReadPixels(0, 0, kWidth, kHeight, GL_RED_INTEGER, GL_UNSIGNED_INT, outputValues);
       
      Compute shader is very simple, imageLoad content from texture[0], and imageStore content to texture[1]. Does need to sync after dispatchCompute?
    • By Jonathan2006
      My question: is it possible to transform multiple angular velocities so that they can be reinserted as one? My research is below:
      // This works quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); quat quaternion2 = GEMultiplyQuaternions(quaternion1, GEQuaternionFromAngleRadians(angleRadiansVector2)); quat quaternion3 = GEMultiplyQuaternions(quaternion2, GEQuaternionFromAngleRadians(angleRadiansVector3)); glMultMatrixf(GEMat4FromQuaternion(quaternion3).array); // The first two work fine but not the third. Why? quat quaternion1 = GEQuaternionFromAngleRadians(angleRadiansVector1); vec3 vector1 = GETransformQuaternionAndVector(quaternion1, angularVelocity1); quat quaternion2 = GEQuaternionFromAngleRadians(angleRadiansVector2); vec3 vector2 = GETransformQuaternionAndVector(quaternion2, angularVelocity2); // This doesn't work //quat quaternion3 = GEQuaternionFromAngleRadians(angleRadiansVector3); //vec3 vector3 = GETransformQuaternionAndVector(quaternion3, angularVelocity3); vec3 angleVelocity = GEAddVectors(vector1, vector2); // Does not work: vec3 angleVelocity = GEAddVectors(vector1, GEAddVectors(vector2, vector3)); static vec3 angleRadiansVector; vec3 angularAcceleration = GESetVector(0.0, 0.0, 0.0); // Sending it through one angular velocity later in my motion engine angleVelocity = GEAddVectors(angleVelocity, GEMultiplyVectorAndScalar(angularAcceleration, timeStep)); angleRadiansVector = GEAddVectors(angleRadiansVector, GEMultiplyVectorAndScalar(angleVelocity, timeStep)); glMultMatrixf(GEMat4FromEulerAngle(angleRadiansVector).array); Also how do I combine multiple angularAcceleration variables? Is there an easier way to transform the angular values?
    • By dpadam450
      I have this code below in both my vertex and fragment shader, however when I request glGetUniformLocation("Lights[0].diffuse") or "Lights[0].attenuation", it returns -1. It will only give me a valid uniform location if I actually use the diffuse/attenuation variables in the VERTEX shader. Because I use position in the vertex shader, it always returns a valid uniform location. I've read that I can share uniforms across both vertex and fragment, but I'm confused what this is even compiling to if this is the case.
       
      #define NUM_LIGHTS 2
      struct Light
      {
          vec3 position;
          vec3 diffuse;
          float attenuation;
      };
      uniform Light Lights[NUM_LIGHTS];
       
       
    • By pr033r
      Hello,
      I have a Bachelor project on topic "Implenet 3D Boid's algorithm in OpenGL". All OpenGL issues works fine for me, all rendering etc. But when I started implement the boid's algorithm it was getting worse and worse. I read article (http://natureofcode.com/book/chapter-6-autonomous-agents/) inspirate from another code (here: https://github.com/jyanar/Boids/tree/master/src) but it still doesn't work like in tutorials and videos. For example the main problem: when I apply Cohesion (one of three main laws of boids) it makes some "cycling knot". Second, when some flock touch to another it scary change the coordination or respawn in origin (x: 0, y:0. z:0). Just some streng things. 
      I followed many tutorials, change a try everything but it isn't so smooth, without lags like in another videos. I really need your help. 
      My code (optimalizing branch): https://github.com/pr033r/BachelorProject/tree/Optimalizing
      Exe file (if you want to look) and models folder (for those who will download the sources):
      http://leteckaposta.cz/367190436
      Thanks for any help...

  • Popular Now