0

I have been trying to load 6 images onto each face of the cube, and I tried to search for a way to do it, and found out that using SOIL I can load the texture images. Currently I want to load an array of images (6), and use that to set the textures of each of the cube's sides to one of those textures.

Here is my code so far, it loads a cube, and keeps on rotating it without the textures if you comment the LoadGLTextures() function:

#include <windows.h>  // for MS Windows
#include <GL/glut.h>  // GLUT, include glu.h and gl.h
#include "SOIL.h""
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <Imagehlp.h>
#include <image_DXT.h>


using namespace std;
/* Global variables */
char title[] = "3D Shapes";
float _angle = 0.0;
GLuint texture[6];

/* Initialize OpenGL Graphics */
void initGL() {
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
    glClearDepth(1.0f);                   // Set background depth to farthest
    glEnable(GL_DEPTH_TEST);   // Enable depth testing for z-culling
    glDepthFunc(GL_LEQUAL);    // Set the type of depth-test
    glShadeModel(GL_SMOOTH);   // Enable smooth shading
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);  // Nice perspective corrections
    GLuint tex;
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

float color[] = { 1.0f, 0.0f, 0.0f, 1.0f };
glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_BORDER_COLOR, color);
}



void LoadGLTextures() {     
    // Load Texture
    Image *image[6];    //PROBLEM HERE
int i;
for (i = 0; i<6; i++){
    // allocate space for texture
    image[i] = (Image *)malloc(sizeof(Image));
    if (image[i] == NULL) {
        printf("Error allocating space for image");
        exit(0);
    }
}
if ((!ImageLoad("1.png", image[0])) || (!ImageLoad("2.png", image[1])) ||     (!ImageLoad("3.png", image[2])) || (!ImageLoad("4.png", image[3])) || (!ImageLoad("5.png", image[4])) || (!ImageLoad("6.png", image[5]))) {
    exit(1);
}
for (i = 0; i<6; i++){
    // Create Texture   
    glGenTextures(1, &texture[i]);
    glBindTexture(GL_TEXTURE_2D, texture[i]);

    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

    glTexImage2D(GL_TEXTURE_2D, 0, 3, image[i]->sizeX, image[i]->sizeY, 0,     GL_RGB, GL_UNSIGNED_BYTE, image[i]->data);
}
};


/* Handler for window-repaint event. Called back when the window first appears and
whenever the window needs to be re-painted. */
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth         buffers
glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix

// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity();                 // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f);  // Move right and into the screen

//Rotates, making sure it rotates around the center of the cube
glTranslatef(0.5, 1.0, 0.0);
glRotatef(_angle, 1.0, 1.0, 1.0);
glTranslatef(-0.5, -1.0, 0.0);

glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f);     // Green
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);

// Bottom face (y = -1.0f)
glColor3f(1.0f, 0.5f, 0.0f);     // Orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);

// Front face  (z = 1.0f)
glColor3f(1.0f, 0.0f, 0.0f);     // Red
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);

// Back face (z = -1.0f)
glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);

// Left face (x = -1.0f)
glColor3f(0.0f, 0.0f, 1.0f);     // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);

// Right face (x = 1.0f)
glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);

// Black/white checkerboard
float pixels[] = {
    0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
    1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 0.0f
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);

glEnd();  // End of drawing color-cube

//// Render a pyramid consists of 4 triangles
//glLoadIdentity();                  // Reset the model-view matrix
//glTranslatef(-1.5f, 0.0f, -6.0f);  // Move left and into the screen

//glBegin(GL_TRIANGLES);           // Begin drawing the pyramid with 4 triangles
//// Front
//glColor3f(1.0f, 0.0f, 0.0f);     // Red
//glVertex3f(0.0f, 1.0f, 0.0f);
//glColor3f(0.0f, 1.0f, 0.0f);     // Green
//glVertex3f(-1.0f, -1.0f, 1.0f);
//glColor3f(0.0f, 0.0f, 1.0f);     // Blue
//glVertex3f(1.0f, -1.0f, 1.0f);

//// Right
//glColor3f(1.0f, 0.0f, 0.0f);     // Red
//glVertex3f(0.0f, 1.0f, 0.0f);
//glColor3f(0.0f, 0.0f, 1.0f);     // Blue
//glVertex3f(1.0f, -1.0f, 1.0f);
//glColor3f(0.0f, 1.0f, 0.0f);     // Green
//glVertex3f(1.0f, -1.0f, -1.0f);

//// Back
//glColor3f(1.0f, 0.0f, 0.0f);     // Red
//glVertex3f(0.0f, 1.0f, 0.0f);
//glColor3f(0.0f, 1.0f, 0.0f);     // Green
//glVertex3f(1.0f, -1.0f, -1.0f);
//glColor3f(0.0f, 0.0f, 1.0f);     // Blue
//glVertex3f(-1.0f, -1.0f, -1.0f);

//// Left
//glColor3f(1.0f, 0.0f, 0.0f);       // Red
//glVertex3f(0.0f, 1.0f, 0.0f);
//glColor3f(0.0f, 0.0f, 1.0f);       // Blue
//glVertex3f(-1.0f, -1.0f, -1.0f);
//glColor3f(0.0f, 1.0f, 0.0f);       // Green
//glVertex3f(-1.0f, -1.0f, 1.0f);
//glEnd();   // Done drawing the pyramid

glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
}

/* Handler for window re-size event. Called back when the window first appears and
whenever the window is re-sized with its new width and height */
void reshape(GLsizei width, GLsizei height) {  // GLsizei for non-negative integer
// Compute aspect ratio of the new window
if (height == 0) height = 1;                // To prevent divide by 0
GLfloat aspect = (GLfloat)width / (GLfloat)height;

// Set the viewport to cover the new window
glViewport(0, 0, width, height);

// Set the aspect ratio of the clipping volume to match the viewport
glMatrixMode(GL_PROJECTION);  // To operate on the Projection matrix
glLoadIdentity();             // Reset
// Enable perspective projection with fovy, aspect, zNear and zFar
gluPerspective(45.0f, aspect, 0.1f, 100.0f);
}

void update(int value) { //function for increasing the angle variable smoothly, keeps     it <=360
_angle += 1.0f;
if (_angle > 360) {
    _angle -= 360;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);
}

/* Main function: GLUT runs as a console application starting at main() */
int main(int argc, char** argv) {
glutInit(&argc, argv);            // Initialize GLUT
glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
glutInitWindowSize(640, 480);   // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title);          // Create window with the given title
glutDisplayFunc(display);       // Register callback handler for window re-paint     event
glutReshapeFunc(reshape);       // Register callback handler for window re-size event
initGL();                       // Our own OpenGL initialization
glutTimerFunc(25, update, 0); //Add a timer
glutMainLoop();                 // Enter the infinite event-processing loop
return 0;
}
kebs
  • 6,387
  • 4
  • 41
  • 70
terasky
  • 13
  • 4
  • `Image` is a remarkably generic class/struct name. I cannot imagine any quality library uses an unqualified name like that... they will almost always prefix names with something more descriptive to avoid collisions with other libraries. Long story short, this leads me to believe that `Image` is something proprietary. If you followed a tutorial, then the tutorial probably defines this as a custom struct. – Andon M. Coleman May 09 '14 at 17:46
  • `` has nothing to do with pictures or textures or GL, it's about a totally different kind of images. Perhaps you should not try to `#include` any random file that has `Image` in its name. – n. m. could be an AI May 09 '14 at 18:22
  • @n.m. I only added '' after trying to identify that "Image" struct name from several different libraries, yet none included it.. It is probably a variable made from GLuint, but even trying that didn't help. – terasky May 09 '14 at 18:43
  • @Andon M. Coleman I have gathered several different code parts to get to that, mainly I am trying to make a cube that has the six die faces on it; so it is not from a certain tutorial. – terasky May 09 '14 at 18:43
  • Where did this `Image` identifier come from in the first place? – n. m. could be an AI May 10 '14 at 10:40

0 Answers0