Hi there,
First of all sorry for the noobish question. I just want to render a model with a texture. Without the usage of *texture everything works fine, it I use a texture nothing shows up.
Here’s the relevant code:
In initBuffers
glGenBuffers(1, &vertexTextureCoordBuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexTextureCoordBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*loadedModel.textures.size(), &(loadedModel.positions[0]), GL_STATIC_DRAW);
vertexTextField.itemSize=2;
vertexTextField.numItems=loadedModel.textures.size() / 2;
Then somewhere
[code=auto:0] GLuint tex=loadBMP_custom(“mypath\green.bmp”);
Then in drawScene
*
glActiveTexture( GL_TEXTURE0 );
glBindTexture(GL_TEXTURE_2D, tex);
glUniform1i(program.samplerUniform, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexTextureCoordBuffer);
glVertexAttribPointer(
program.textureCoordAttribute,
vertexTextField.itemSize,
GL_FLOAT,
GL_FALSE,
0,
(void*)0
);
Whereas load_BMP is something found on opegl-tutorials.com and here it is:
GLuint loadBMP_custom(const char * imagepath){
printf("Reading image %s
", imagepath);
// Data read from the header of the BMP file
unsigned char header[54];
unsigned int dataPos;
unsigned int imageSize;
unsigned int width, height;
// Actual RGB data
unsigned char * data;
// Open the file
FILE * file = fopen(imagepath,"rb");
if (!file) {printf("%s could not be opened. Are you in the right directory ? Don't forget to read the FAQ !
", imagepath); return 0;}
// Read the header, i.e. the 54 first bytes
// If less than 54 byes are read, problem
if ( fread(header, 1, 54, file)!=54 ){
printf("Not a correct BMP file
");
return 0;
}
// A BMP files always begins with “BM”
if ( header[0]!=‘B’ || header[1]!=‘M’ ){
printf("Not a correct BMP file
");
return 0;
}
// Make sure this is a 24bpp file
if ( (int)&(header[0x1E])!=0 ) {printf("Not a correct BMP file
"); return 0;}
if ( (int)&(header[0x1C])!=24 ) {printf("Not a correct BMP file
"); return 0;}
// Read the information about the image
dataPos = *(int*)&(header[0x0A]);
imageSize = *(int*)&(header[0x22]);
width = *(int*)&(header[0x12]);
height = *(int*)&(header[0x16]);
// Some BMP files are misformatted, guess missing information
if (imageSize==0) imageSize=width*height*3; // 3 : one byte for each Red, Green and Blue component
if (dataPos==0) dataPos=54; // The BMP header is done that way
// Create a buffer
data = new unsigned char [imageSize];
// Read the actual data from the file into the buffer
fread(data,1,imageSize,file);
// Everything is in memory now, the file wan be closed
fclose (file);
// Create one OpenGL texture
GLuint textureID;
glGenTextures(1, &textureID);
// "Bind" the newly created texture : all future texture functions will modify this texture
glBindTexture(GL_TEXTURE_2D, textureID);
// Give the image to OpenGL
glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);
// OpenGL has now copied the data. Free our own version
delete [] data;
// Poor filtering, or ...
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// ... nice trilinear filtering.
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
// Return the ID of the texture we just created
return textureID;
}
The shaders are: (I know that something is deprecated, but I need those things)
#version 330 core
precision mediump float;
varying vec3 vTransformedNormal;
varying vec2 vTextureCoord;
varying vec4 vPosition;
uniform vec3 uPointLightingLocation;
uniform vec3 uPointLightingDiffuseColor;
uniform vec3 uAmbientColor;
uniform vec3 uColor;
uniform sampler2D uSampler;
void main(void){
vec3 lightWeighting;
vec3 lightDirection = normalize(uPointLightingLocation - vPosition.xyz);
vec3 normal = normalize(vTransformedNormal);
float diffuseLightWeighting=max(dot(normal, lightDirection),0.0);
lightWeighting=uAmbientColor+uPointLightingDiffuseColor*diffuseLightWeighting;
vec4 fragmentColor=vec4(uColor.rgb,1.0);
fragmentColor = texture(uSampler, vTextureCoord);
gl_FragColor=vec4(fragmentColor.rgb*lightWeighting, fragmentColor.a);
}
#version 330 core
attribute vec2 aTextureCoord;
attribute vec3 aVertexPosition;
attribute vec3 aVertexNormal;
uniform mat3 uNMatrix;
uniform mat4 uPMatrix;
uniform mat4 uMVMatrix;
varying vec4 vPosition;
varying vec3 vTransformedNormal;
varying vec2 vTextureCoord;
void main(void){
vPosition=uMVMatrixvec4(aVertexPosition, 1.0);
vTransformedNormal=uNMatrixaVertexNormal;
vTextureCoord = aTextureCoord;
gl_Position=uPMatrix*vPosition;
}
Thank you so much :D
EDIT: Solved, was a bug elsewhere