PDA

View Full Version : Don't see my texture on cube, SOIL



Schieby
12-01-2013, 01:59 AM
Hey guys,

I have a problem with my texture. I coded a cube and uses the SOIL library. But the texture will be not shown. I don't know where my bug is :/

Here is the code:

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <GLUT/glut.h>
#include "SOIL.h"


GLfloat lightPos[4] = {50.0, 30.0 ,0.0, 0.0};
GLfloat lightAmb[3] = {0.1, 0.1, 0.1};
GLfloat lightDiff[3] = {1.0, 1.0, 1.0};

/* Variables for gluLookAt */
GLdouble eyeX = 0.0;
GLdouble eyeY = 5.0;
GLdouble eyeZ = 25.0;
GLdouble centerX = 0.0;
GLdouble centerY = 0.0;
GLdouble centerZ = 0.0;
GLdouble upX = 0.0;
GLdouble upY = 1.0;
GLdouble upZ = 0.0;

GLdouble angleLookAt = 0.0;
GLdouble transX = 0.0;
GLdouble transY = 0.0;
GLdouble transZ = -1.0;

/* Global variables */
GLfloat angle = 0.0;
GLfloat ratio = 0.0;
GLfloat fraction = 0.3;
GLint active = 0;
GLfloat deltaAngle = 0.0;
GLint mouseCoordX = 0;
GLint mouseCoordY = 0;

GLuint texture[1]; // Storage For One Texture ( NEW )



int LoadGLTextures()
{

texture[0] = SOIL_load_OGL_texture
(
"NeHe.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);

if(texture[0] == 0)
return false;


// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTE R,GL_LINEAR);

return true; // Return Success
}



void changeSize(int w, int h) {
if(h == 0) h = 1;
float ratio = 1.0* w / h;

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(25, ratio, 1, 1000);
glMatrixMode(GL_MODELVIEW);
}


void renderScene(void) {
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
centerX = eyeX + transX;
centerY = eyeY + transY;
centerZ = eyeZ + transZ;
glLoadIdentity();
gluLookAt( eyeX, eyeY, eyeZ, centerX, centerY, centerZ, upX, upY, upZ);

glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiff);

glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

glPushMatrix();
glTranslatef(0.0f,4.0f,-5.0f);

glRotatef(0.3f,1.0f,0.0f,0.0f);
glRotatef(0.2f,0.0f,1.0f,0.0f);
glRotatef(0.4f,0.0f,0.0f,1.0f);

glBindTexture(GL_TEXTURE_2D, texture[0]);

glBegin(GL_QUADS);
// Front Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
// Back Face
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
// Top Face
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
// Bottom Face
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, -1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f);
// Left Face
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, -1.0f);
glEnd();
glPopMatrix();

glutSwapBuffers();
}

int main(int argc, char **argv) {
// por inicializacao aqui
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(0,0);
glutInitWindowSize(1440,790);
glutCreateWindow("Texture");

// por registo de funcoes aqui
glutDisplayFunc(renderScene);

glutIdleFunc(renderScene);
glutReshapeFunc(changeSize);

// alguns settings para OpenLG
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);

glutMainLoop();

return 0;
}

Thanks!!

uwi2k2
12-01-2013, 03:53 AM
hi there,

i canīt see where you are calling the func:

LoadGLTextures()

do you ?


If you do not call this function you will never ever have a texture.

cu
uwi

Schieby
12-01-2013, 01:40 PM
Oh yes, that was my fault! Thanks!