texture and colors

i’m learning how to apply texture in openGL. i applyed a texture to one face of a cube and colored the other faces with primitive colors.
before i apply the texture the colors looked fine, and after i apply it they become darker.
what’s wrong?

Disable texturing after drawing first face, otherwise the other faces will get color from texture too.

i did disable it after the first face using glDisable(GL_TEXTURE_2D) but nohting changed

#include <SFML/Graphics.hpp>
#include <cstdio>

GLuint texture[1];

/* Image type - contains height, width, and data */
struct Image {
unsigned long sizeX;
unsigned long sizeY;
char *data;
};
typedef struct Image Image;

// quick and dirty bitmap loader…for 24 bit bitmaps with 1 plane only.
// See http://www.dcs.ed.ac.uk/~mxr/gfx/2d/BMP.txt for more info.
int ImageLoad(const char *filename, Image *image) {
FILE *file;
unsigned long size; // size of the image in bytes.
unsigned long i; // standard counter.
unsigned short int planes; // number of planes in image (must be 1)
unsigned short int bpp; // number of bits per pixel (must be 24)
char temp; // temporary color storage for bgr-rgb conversion.

// make sure the file is there.
if ((file = fopen(filename, "rb"))==NULL)
{
printf("File Not Found : %s

",filename);
return 0;
}

// seek through the bmp header, up to the width/height:
fseek(file, 18, SEEK_CUR);

// read the width
if ((i = fread(&image-&gt;sizeX, 4, 1, file)) != 1) {
printf("Error reading width from %s.

", filename);
return 0;
}
printf("Width of %s: %lu
", filename, image->sizeX);

// read the height
if ((i = fread(&image-&gt;sizeY, 4, 1, file)) != 1) {
printf("Error reading height from %s.

", filename);
return 0;
}
printf("Height of %s: %lu
", filename, image->sizeY);

// calculate the size (assuming 24 bits or 3 bytes per pixel).
size = image-&gt;sizeX * image-&gt;sizeY * 3;

// read the planes
if ((fread(&planes, 2, 1, file)) != 1) {
printf("Error reading planes from %s.

", filename);
return 0;
}
if (planes != 1) {
printf("Planes from %s is not 1: %u
", filename, planes);
return 0;
}

// read the bpp
if ((i = fread(&bpp, 2, 1, file)) != 1) {
printf("Error reading bpp from %s.

", filename);
return 0;
}
if (bpp != 24) {
printf("Bpp from %s is not 24: %u
", filename, bpp);
return 0;
}

// seek past the rest of the bitmap header.
fseek(file, 24, SEEK_CUR);

// read the data.
image-&gt;data = (char *) malloc(size);
if (image-&gt;data == NULL) {
printf("Error allocating memory for color-corrected image data");
return 0;
}

if ((i = fread(image-&gt;data, size, 1, file)) != 1) {
printf("Error reading image data from %s.

", filename);
return 0;
}

for (i=0;i&lt;size;i+=3) { // reverse all of the colors. (bgr -&gt; rgb)
temp = image-&gt;data[i];
image-&gt;data[i] = image-&gt;data[i+2];
image-&gt;data[i+2] = temp;
}

// we're done.
return 1;

}

// Load Bitmaps And Convert To Textures
void LoadGLTextures() {
// Load Texture
Image *image1;
// allocate space for texture
image1 = (Image *) malloc(sizeof(Image));
if (image1 == NULL) {
printf(“Error allocating space for image”);
exit(0);
}

if (!ImageLoad("dd.bmp", image1)) {
exit(1);
}

// Create Texture
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);   // 2d texture (x and y size)

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); // scale linearly when image bigger than texture
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); // scale linearly when image smalled than texture

// 2d texture, level of detail 0 (normal), 3 components (red, green, blue), x size from image, y size from image,
// border 0 (normal), rgb color data, unsigned byte data, and finally the data itself.
glTexImage2D(GL_TEXTURE_2D, 0, 3, image1-&gt;sizeX, image1-&gt;sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, image1-&gt;data);

};

int main()
{
sf::Window App(sf::VideoMode(600, 600, 32), “SFML OpenGL”, sf::Style::Close);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
LoadGLTextures();
glBindTexture(GL_TEXTURE_2D, texture[0]);
while (App.IsOpened())
{
App.SetActive();
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//back
glColor3f(0.0f,0.0f,0.0f); //cyan
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left
glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right
glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
glDisable(GL_TEXTURE_2D);
//bottom
glColor3f(0.0f,1.0f,0.0f); //green
glVertex3f(0.2f,-0.2f,0.2f); //back right
glVertex3f(-0.2f,-0.2f,0.2f); //back left
glVertex3f(-0.2f,-0.2f,-0.2f); //front left
glVertex3f(0.2f,-0.2f,-0.2f); //front right
//front
glColor3f(0.0f,0.0f,1.0f); //blue
glVertex3f(0.2f,-0.2f,-0.2f); //bottom right
glVertex3f(-0.2f,-0.2f,-0.2f); //bottom left
glVertex3f(-0.2f,0.2f,-0.2f); //top left
glVertex3f(0.2f,0.2f,-0.2f); //top right
//top
glColor3f(1.0f,1.0f,0.0f); //yellow
glVertex3f(0.2f,0.2f,-0.2f); //front right
glVertex3f(-0.2f,0.2f,-0.2f); //front left
glVertex3f(-0.2f,0.2f,0.2f); //back left
glVertex3f(0.2f,0.2f,0.2f); //back right
//left
glColor3f(0.0f,1.0f,1.0f); //pink
glVertex3f(-0.2f,-0.2f,-0.2f); //bottom front
glVertex3f(-0.2f,-0.2f,0.2f); //bottom back
glVertex3f(-0.2f,0.2f,0.2f); //top back
glVertex3f(-0.2f,0.2f,-0.2f); //top front
//right
glColor3f(1.0f,0.0f,0.0f); //red
glVertex3f(0.2f,-0.2f,-0.2f); //bottom front
glVertex3f(0.2f,-0.2f,0.2f); //bottom back
glVertex3f(0.2f,0.2f,0.2f); //top back
glVertex3f(0.2f,0.2f,-0.2f); //top front
glEnd();

    glFlush();

    App.Display();

    sf::Event Event;
    while (App.GetEvent(Event))
    {
        if (Event.Type == sf::Event::Closed)
            App.Close();
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
            App.Close();
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Right))
            glRotated(20.0d,0.0d,0.2d,0.0d);
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Left))
            glRotated(-20.0d,0.0d,0.2d,0.0d);
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Up))
            glRotated(20.0d,0.2d,0.0d,0.0d);
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Down))
            glRotated(-20.0d,0.2d,0.0d,0.0d);
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageUp))
            glRotated(20.0d,0.0d,0.0d,0.2d);
        if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::PageDown))
            glRotated(-20.0d,0.0d,0.0d,0.2d);
    }
}
return EXIT_SUCCESS;

}

Long code for other people is hard enough to read, so please at least use the [ code ] my long code here[ /code ] (without the spaces) tags …

Never use glDisable between glBegin and glEnd functions.

http://www.opengl.org/sdk/docs/man/xhtml/glBegin.xml

Also,

glColor3f(0.0f,0.0f,0.0f); //cyan

This is not cyan, this is black. This is why your textured face could look dark.

you are absolutely right, thanks arts :slight_smile:

this is the correct code now :


        glEnable(GL_TEXTURE_2D);
        glBindTexture(GL_TEXTURE_2D, texture);
        glBegin(GL_QUADS);
        //back
        glColor3f(1.0f,1.0f,1.0f); //white
        glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.2f,0.2f,0.2f); //top left
        glTexCoord2f(1.0f, 1.0f); glVertex3f(0.2f,0.2f,0.2f); //top right
        glTexCoord2f(1.0f, 0.0f); glVertex3f(0.2f,-0.2f,0.2f); //bottom right
        glTexCoord2f(0.0f, 0.0f); glVertex3f(-0.2f,-0.2f,0.2f); //bottom left
        glEnd();
        glDisable(GL_TEXTURE_2D);
        glBegin(GL_QUADS);
        //bottom
        glColor3f(0.0f,1.0f,0.0f); //green
        glVertex3f(0.2f,-0.2f,0.2f); //back right
        glVertex3f(-0.2f,-0.2f,0.2f); //back left
        glVertex3f(-0.2f,-0.2f,-0.2f); //front left
        glVertex3f(0.2f,-0.2f,-0.2f); //front right
        //front
        glColor3f(0.0f,0.0f,1.0f); //blue
        glVertex3f(0.2f,-0.2f,-0.2f); //bottom right
        glVertex3f(-0.2f,-0.2f,-0.2f); //bottom left
        glVertex3f(-0.2f,0.2f,-0.2f); //top left
        glVertex3f(0.2f,0.2f,-0.2f); //top right
        //top
        glColor3f(1.0f,1.0f,0.0f); //yellow
        glVertex3f(0.2f,0.2f,-0.2f); //front right
        glVertex3f(-0.2f,0.2f,-0.2f); //front left
        glVertex3f(-0.2f,0.2f,0.2f); //back left
        glVertex3f(0.2f,0.2f,0.2f); //back right
        //left
        glColor3f(0.0f,1.0f,1.0f); //pink
        glVertex3f(-0.2f,-0.2f,-0.2f); //bottom front
        glVertex3f(-0.2f,-0.2f,0.2f); //bottom back
        glVertex3f(-0.2f,0.2f,0.2f); //top back
        glVertex3f(-0.2f,0.2f,-0.2f); //top front
        //right
        glColor3f(1.0f,0.0f,0.0f); //red
        glVertex3f(0.2f,-0.2f,-0.2f); //bottom front
        glVertex3f(0.2f,-0.2f,0.2f); //bottom back
        glVertex3f(0.2f,0.2f,0.2f); //top back
        glVertex3f(0.2f,0.2f,-0.2f); //top front
        glEnd();