[color="#008000"][color="#008000"]/*Applying Textures to Surfaces: This Program Works Great
Good example video
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
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?
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).
Try GL_BGR as the format parameter to the glTexImage2D call (instead of GL_RGB).
[attachment=5226:Fixed Texture Color Scheme GL_BGR.jpg]
Thanks that was exactly what it was. Do you mind me asking how that happens? Is it just that specific image?
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.
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?
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.
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
This line needs attention drawn to it:
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.
// 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.
You shouldn't even be using gluBuild2DMipmaps anymore or "3" as an internal format
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,
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;
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement