[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]
Texture Colors Not exact as Texture
#1 Members - Reputation: 110
Posted 01 September 2011 - 08:26 PM
#3 Members - Reputation: 110
Posted 02 September 2011 - 07:19 AM
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?
#5 Members - Reputation: 110
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 Members - Reputation: 785
Posted 02 September 2011 - 09:45 AM
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.
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 Members - Reputation: 97
Posted 02 September 2011 - 04:04 PM
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 Members - Reputation: 3831
Posted 02 September 2011 - 05:51 PM
// BGR format. Now we could just use the GL_BGR extensionSubstitute "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 Members - Reputation: 785
Posted 03 September 2011 - 06:23 AM
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
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 Moderators - Reputation: 1754
Posted 03 September 2011 - 08:56 AM
BYTE *bits = new BYTE[FreeImage_GetWidth(dib) * FreeImage_GetHeight(dib) * 3];
[..stuff..]
delete bits;
#11 Members - Reputation: 97
Posted 14 November 2011 - 09:45 PM
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 Moderators - Reputation: 13610
Posted 14 November 2011 - 10:46 PM
^^ Fixed that for you.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?
#13 Crossbones+ - Reputation: 5187
Posted 14 November 2011 - 11:21 PM
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
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 Members - Reputation: 97
Posted 17 November 2011 - 04:31 PM
#15 Members - Reputation: 3831
Posted 17 November 2011 - 09:01 PM
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 Members - Reputation: 537
Posted 18 November 2011 - 12:56 AM
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?
Just because someone gave away free code doesn't mean it is good. It really was bad code, and was a memory leak.Send your pointless little snippet to the people who built freeImage and tell them that their software isn't written properly.
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.
#18 Members - Reputation: 785
Posted 18 November 2011 - 06:58 AM
http://www.opengl.org/wiki/Common_Mistakes#GL_TEXTURE_MAG_FILTER
and also
http://www.opengl.org/wiki/Common_Mistakes#Automatic_mipmap_generation
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 Members - Reputation: 97
Posted 19 November 2011 - 05:20 PM







