I've been trying to make the transition to OpenGL 3.2 from OpenGL 2.1, and no matter what I do, textures just won't work!
My code for using textures has never failed me using OpenGL 2.1, and I have no idea why it's not working here. I've checked various tutorials, guides, and similar questions. Tried everything I could possibly think of, nothing fixed it. I've checked for error codes, but I get GL_INVALID_ENUM everywhere! Geometry works fine, all of the uniforms and attributes are located without problems. So, I'm really lost here.
As much as I hate to just dump a load of code on you all, I have a very short amount of time to get this working, and I've already spent 2 hours trying to fix this.
vertex program:
#version 150
in vec4 ipos; /* Vertex position */
in vec3 inor; /* Vertex normal */
in vec4 icol; /* Vertex diffuse colour */
in vec2 itex; /* Vertex texture coordinates */
uniform mat4 mvp; /* Modelview Projection matrix */
//uniform mat3 nm; /* Normal matrix */
out vec4 ocol; /* Output colour */
out vec2 otex; /* Output texture coordinates */
void main()
{
/* Transform vertices */
gl_Position = mvp * ipos;
/* Transform normals */
mat3 nm = mat3(mvp);
vec3 normal = normalize( nm * inor );
/* Calculate light direction */
vec3 light_dir = vec3( 1.0, 1.0, -1.0 );
light_dir = normalize( light_dir );
/* Calculate the angle between the normal and the light's direction */
float n_dot_l = max( dot( normal, light_dir ), 0.0 );
/* Multiply vertex colour with the result */
ocol = icol * n_dot_l;
/* Pass through texture coordinates */
otex = itex;
}
fragment program:
#version 150
in vec4 ocol;
in vec2 otex;
out vec4 frag;
uniform sampler2D tex;
void main()
{
frag = ocol * texture( tex, otex );
}
Relevant code:
bool create_texture( const char* filename, unsigned int* tex )
{
int x, y, comp, req_comp = STBI_rgb_alpha;
stbi_uc* p = stbi_load( filename, &x, &y, &comp, req_comp );
if(p)
{
GLenum format = ( comp == 3 ) ? GL_RGB : GL_RGBA;
glGenTextures( 1, tex );
glBindTexture( GL_TEXTURE_2D, *tex );
glTexImage2D( GL_TEXTURE_2D, 0, format, x, y, 0, format, GL_UNSIGNED_BYTE, p );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glGenerateMipmap( GL_TEXTURE_2D );
stbi_image_free(p);
}
else
{
return false;
}
return true;
}
bool init_vbo()
{
float* vertices = NULL;
float* normals = NULL;
float* texcoords = NULL;
object_type* obj = new object_type();
int vertex_count;
if( !create_texture( "/Users/admin/shogun3d/luna/media/mesh/dolphin/DOLPHIN.bmp", &texture ) )
return false;
if(! obj->objloader( "/Users/admin/shogun3d/luna/media/mesh/dolphin/DOLPHIN.obj" ) )
return false;
vertex_count = obj->getvertices( &vertices, &normals, &texcoords );
prim = vertex_count/3;
printf( "Primitive count: %d\n", vertex_count );
delete obj;
glGenVertexArrays( 1, &vao );
glBindVertexArray(vao);
glGenBuffers( 3, vbo );
glBindBuffer( GL_ARRAY_BUFFER, vbo[0] );
glBufferData( GL_ARRAY_BUFFER, vertex_count*3*sizeof(float), vertices, GL_STATIC_DRAW );
glVertexAttribPointer( (GLuint) attrib_v_pos, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glEnableVertexAttribArray(attrib_v_pos);
glBindBuffer( GL_ARRAY_BUFFER, vbo[1] );
glBufferData( GL_ARRAY_BUFFER, vertex_count*3*sizeof(float), normals, GL_STATIC_DRAW );
glVertexAttribPointer( (GLuint) attrib_v_normal, 3, GL_FLOAT, GL_FALSE, 0, 0 );
glEnableVertexAttribArray(attrib_v_normal);
glBindBuffer( GL_ARRAY_BUFFER, vbo[2] );
glBufferData( GL_ARRAY_BUFFER, vertex_count*2*sizeof(float), texcoords, GL_STATIC_DRAW );
glVertexAttribPointer( (GLuint) attrib_v_tex, 2, GL_FLOAT, GL_FALSE, 0, 0 );
glEnableVertexAttribArray(attrib_v_tex);
glBindVertexArray(0);
//glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
delete [] vertices;
delete [] normals;
delete [] texcoords;
return true;
}
void draw_vbo()
{
extern Matrix4 projection_matrix;
Matrix4 translation_matrix, rotation_matrix, modelview_matrix, mvp_matrix;
VmathVector3 translation_vector = { 0.0f, 0.0f, -15.0f };
static float angle = 0;
translation_matrix = M4MakeTranslation( translation_vector );
rotation_matrix = M4MakeRotationY( angle ); angle += 0.04f;
modelview_matrix = M4Mul( translation_matrix, rotation_matrix );
mvp_matrix = M4Mul( projection_matrix, modelview_matrix );
glUseProgram(shader);
glUniform1i( uniform_tex, 0 );
glEnable( GL_TEXTURE_2D );
glActiveTexture( GL_TEXTURE0 );
glBindTexture( GL_TEXTURE_2D, texture );
glUniformMatrix4fv( uniform_mvp, 1, false, &mvp_matrix.col0.x );
glBindVertexArray(vao);
glVertexAttrib4f( attrib_v_colour, 1.0, 1.0, 1.0, 1.0 );
glDrawArrays( GL_TRIANGLES, 0, prim*3 );
}
I've checked this over so many times, and can't find a single thing wrong with it. All I know is that it isn't working, and I'm actually in the middle of preparing for a coding competition on Saturday. Good thing I'm not running into this during the competition. But still, this sucks. Any ideas? Thanks.
Shogun.