Advertisement Jump to content
Sign in to follow this  
blueshogun96

OpenGL Textures not working in OpenGL 3+

This topic is 1818 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

So, have you checked where those invalid enums come from? At least glEnable(GL_TEXTURE_2D) shouldn't be used, but I don't think that's the problem.

Share this post


Link to post
Share on other sites

I have checked (that was my first course of action), and they come randomly it looks like.  Even after moving on from that error, it continues to give me that, while other functions still work.

 

Shogun.

 

EDIT: Got rid of glEnable( GL_TEXTURE_2D ); and that stopped the errors.  Still no textures though.

Edited by blueshogun96

Share this post


Link to post
Share on other sites

I'm assuming uniform_tex is the uniform location associated with the sampler in your fragment shader..If so did you check that you are getting back a valid location ?

Share this post


Link to post
Share on other sites

This line in the fragment shader

frag = ocol * texture( tex, otex );

need to be 

frag = ocol * texture2D( tex, otex );

The driver should have told you an error like "function texture not declared" or so. Please check the state of compiled scripts before linking.

 

 

EDIT: Ups :(

Edited by haegarr

Share this post


Link to post
Share on other sites

texture2D is deprecated.  

 

Keep in mind that I'm using GLSL 1.50, not 1.20.  Using texture2D will automatically generate errors.  Using texture in GLSL 1.50+ will automatically detect the sampler type.

 

Any more ideas?

Share this post


Link to post
Share on other sites

Have you tried simplifying your shaders? That's usually one of the first things I do when something isn't working, commenting "unnecessary" stuff out. Like, are you sure your normals are correct? Is the light position is fine? Maybe try doing just simple out = texture(texmap, texcoord) in the fragment shader. And if it still wouldn't work, I'd use some OpenGL debugger (gDebugger) to see if it would tell me something.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!