[color="#008000"][color="#008000"]/*Applying Textures to Surfaces: This Program Works Great
Good example video<iframe allowfullscreen="true" frameborder="0" height="344" width="459" src="https://www.youtube.com/embed/2BsTOgABU1k?feature=oembed"></iframe>
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="#0000ff"][color="#0000ff"]#include [color="#a31515"][color="#a31515"]<stdlib.h>
[color="#0000ff"][color="#0000ff"]#include [color="#a31515"][color="#a31515"]<iostream>
[color="#0000ff"][color="#0000ff"]#include [color="#a31515"][color="#a31515"]<GL/glut.h>
[color="#0000ff"][color="#0000ff"]#include [color="#a31515"][color="#a31515"]<SDL/SDL.h>
[color="#0000ff"][color="#0000ff"]#include [color="#a31515"][color="#a31515"]<SDL_opengl.h>
[color="#0000ff"][color="#0000ff"]using [color="#0000ff"][color="#0000ff"]namespace std;
[color="#008000"][color="#008000"]//Prototypes
[color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]int loadTexture([color="#0000ff"][color="#0000ff"]const [color="#0000ff"][color="#0000ff"]char* filename); [color="#008000"][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
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}};
[color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]int tex; [color="#008000"][color="#008000"]//global variable to hold texture
//Added initialization function
//Initializes 3D rendering
[color="#0000ff"][color="#0000ff"]void initRendering() { [color="#008000"][color="#008000"]//in this function I may need to add in the
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
[color="#008000"][color="#008000"]//glEnable(GL_LIGHTING); //enable lighting
[color="#008000"][color="#008000"]//glEnable(GL_LIGHT0); //enable light #0
[color="#008000"][color="#008000"]//glEnable(GL_LIGHT1); //enable light #1
[color="#008000"][color="#008000"]//glEnable(GL_NORMALIZE); //Automatically normalize normals
glShadeModel(GL_SMOOTH); [color="#008000"][color="#008000"]//enable smooth shading
glEnable(GL_TEXTURE_2D); [color="#008000"][color="#008000"]//Enables the use of 2D textures
tex = loadTexture([color="#a31515"][color="#a31515"]"Bricks.bmp"); [color="#008000"][color="#008000"]//load up file
[color="#008000"][color="#008000"]//Adds ambient light
GLfloat ambientColor[] = {0.5, 0.5, 0.5, 1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
[color="#008000"][color="#008000"]//Adds positioned light
[color="#008000"][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="#008000"][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="#008000"][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="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]int loadTexture([color="#0000ff"][color="#0000ff"]const [color="#0000ff"][color="#0000ff"]char* filename)
{
SDL_Surface* img =SDL_LoadBMP(filename); [color="#008000"][color="#008000"]//figure out why color isn't coming in
[color="#0000ff"][color="#0000ff"]unsigned [color="#0000ff"][color="#0000ff"]int id; [color="#008000"][color="#008000"]//assigning an id for glGenTextures
glGenTextures(1, &id); [color="#008000"][color="#008000"]//idea assigned
glBindTexture(GL_TEXTURE_2D, id); [color="#008000"][color="#008000"]//binding texture to the id
glTexImage2D(GL_TEXTURE_2D,
0,
GL_RGB,
img->w, [color="#008000"][color="#008000"]//width of image
img->h, [color="#008000"][color="#008000"]//height of image
0,
GL_RGB, [color="#008000"][color="#008000"]//format
GL_UNSIGNED_BYTE, [color="#008000"][color="#008000"]//what format is the image stored
img->pixels); [color="#008000"][color="#008000"]//pixel array, pixels
[color="#008000"][color="#008000"]//Setting Parameters
[color="#008000"][color="#008000"]//Warping Parameters (how texture will be applied to the object)
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); [color="#008000"][color="#008000"]//Many different parameters can be set her to manipulate the textures
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
[color="#008000"][color="#008000"]//Filtering Parameters
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); [color="#008000"][color="#008000"]//2d texture with a minimum filter, and linear interpolation (averaging pixlels)
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
SDL_FreeSurface(img); [color="#008000"][color="#008000"]//deletes the texture image out of the texture memory
[color="#0000ff"][color="#0000ff"]return id;
}
[color="#0000ff"][color="#0000ff"]void polygon([color="#0000ff"][color="#0000ff"]int a, [color="#0000ff"][color="#0000ff"]int b, [color="#0000ff"][color="#0000ff"]int c, [color="#0000ff"][color="#0000ff"]int d)
{
[color="#008000"][color="#008000"]/*draw a polygon via list of vertices */
glBindTexture(GL_TEXTURE_2D, tex); [color="#008000"][color="#008000"]//This binds the textures to the polygon
glBegin(GL_POLYGON); [color="#008000"][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="#008000"][color="#008000"]// glColor3fv(colors[a]); //These texture points are placed witht he corresponding vertices, to make sure that the texture is applied correctly
glTexCoord2f(0.0,0.0);
glVertex3fv(vertices[a]);
[color="#008000"][color="#008000"]//glColor3fv(colors);
glTexCoord2f(0.0,1.0);
glVertex3fv(vertices);
[color="#008000"][color="#008000"]//glColor3fv(colors[c]);
glTexCoord2f(1.0,1.0);
glVertex3fv(vertices[c]);
[color="#008000"][color="#008000"]//glColor3fv(colors[d]);
glTexCoord2f(1.0,0.0);
glVertex3fv(vertices[d]);
glEnd();
[color="#008000"][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="#0000ff"][color="#0000ff"]void colorcube([color="#0000ff"][color="#0000ff"]void)
{ [color="#008000"][color="#008000"]//function is used to state which sections of the array for colors and vertext to use
/*map vertices to faces */
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);
}
[color="#0000ff"][color="#0000ff"]static GLfloat theta[] = {0.0,0.0,0.0}; [color="#008000"][color="#008000"]//declaring two static variables theta and axis
[color="#0000ff"][color="#0000ff"]static GLint axis = 2;
[color="#008000"][color="#008000"]//display function
[color="#0000ff"][color="#0000ff"]void display([color="#0000ff"][color="#0000ff"]void)
{
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(); [color="#008000"][color="#008000"]//refers to the function that mapped the vertices to faces
glutSwapBuffers();
}
[color="#008000"][color="#008000"]//The below section contains the interaction component of the program
[color="#0000ff"][color="#0000ff"]void spinCube()
{
theta[axis] += 0.05;
[color="#0000ff"][color="#0000ff"]if(theta[axis] > 360.0)
theta[axis] -= 360.0;
glutPostRedisplay();
}
[color="#0000ff"][color="#0000ff"]void mouse([color="#0000ff"][color="#0000ff"]int btn, [color="#0000ff"][color="#0000ff"]int state, [color="#0000ff"][color="#0000ff"]int x, [color="#0000ff"][color="#0000ff"]int y)
{
[color="#008000"][color="#008000"]//Mouse button interaction
[color="#0000ff"][color="#0000ff"]if(btn == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
axis = 0;
[color="#0000ff"][color="#0000ff"]if(btn == GLUT_MIDDLE_BUTTON && state == GLUT_DOWN)
axis = 1;
[color="#0000ff"][color="#0000ff"]if(btn == GLUT_RIGHT_BUTTON && state == GLUT_DOWN)
axis = 2;
}
[color="#0000ff"][color="#0000ff"]void myReshape([color="#0000ff"][color="#0000ff"]int w, [color="#0000ff"][color="#0000ff"]int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
[color="#0000ff"][color="#0000ff"]if(w <= h)
glOrtho(-2.0, 2.0, -2.0* (GLfloat) h/ (GLfloat) w, 2.0 *(GLfloat) h/ (GLfloat) w, -10.0, 10.0);
[color="#0000ff"][color="#0000ff"]else
glOrtho(-2.0* (GLfloat) h/ (GLfloat) w, 2.0* (GLfloat) h/ (GLfloat) w, -2.0,2.0,-10.0,10.0);
glMatrixMode(GL_MODELVIEW);
}
[color="#0000ff"][color="#0000ff"]int main([color="#0000ff"][color="#0000ff"]int argc, [color="#0000ff"][color="#0000ff"]char ** argv)
{
GLubyte image[64][64][3]; [color="#008000"][color="#008000"]//seems like 64 by 64 pixels taking a vertice made up of 3 points
[color="#0000ff"][color="#0000ff"]int i, j, r, c;
[color="#0000ff"][color="#0000ff"]for(i =0; i <64; i++)
{
[color="#0000ff"][color="#0000ff"]for(j=0; j<64; j++)
{
c = ((((i & 0x8)== 0)^((j & 0x8)) == 0))*255; [color="#008000"][color="#008000"]//I think 0x8 has to always be written like this, not 0 x 8 or it will cause problems.
image[j][0] = (GLubyte) c;
image[j][1] = (GLubyte) c;
image[j][2] = (GLubyte) c;
}
}
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE| GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow([color="#a31515"][color="#a31515"]"colorcube");
[color="#008000"][color="#008000"]//Place initialization function here
initRendering();
glutReshapeFunc(myReshape);
glutDisplayFunc(display);
glutIdleFunc(spinCube);
glutMouseFunc(mouse);
[color="#008000"][color="#008000"]
glutMainLoop();
[color="#0000ff"][color="#0000ff"]return 0;
}
Texture Colors Not exact as Texture
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?
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?
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.
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
// 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.
I took this part out..It really wasn't constructive at all.
mhagain -- I don't appreciate you pointing out the flaws in my code. Please refrain from posting suggestions relating to other people's posts.^^ Fixed that for you.
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?
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
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.
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. ;)
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?
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.
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.
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.