Jump to content

  • Log In with Google      Sign In   
  • Create Account

Textures not working in OpenGL 3+


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 blueshogun96   Crossbones+   -  Reputation: 937

Like
0Likes
Like

Posted 17 January 2014 - 12:33 AM

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.


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


Sponsor:

#2 Sponji   Members   -  Reputation: 1256

Like
0Likes
Like

Posted 17 January 2014 - 03:49 AM

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.


Derp

#3 blueshogun96   Crossbones+   -  Reputation: 937

Like
0Likes
Like

Posted 17 January 2014 - 10:27 AM

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, 17 January 2014 - 10:39 AM.

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#4 cgrant   Members   -  Reputation: 661

Like
0Likes
Like

Posted 17 January 2014 - 12:00 PM

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 ?



#5 blueshogun96   Crossbones+   -  Reputation: 937

Like
0Likes
Like

Posted 17 January 2014 - 01:35 PM

Yes, I did. When my vertex and fragment programs are loaded, the uniform_tex variable has a valid value. I checked it against -1.

Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#6 Ohforf sake   Members   -  Reputation: 1832

Like
0Likes
Like

Posted 17 January 2014 - 02:18 PM

Is GL_RGB actually a valid internal format? I believe you have to use GL_RGBA8, even for 3 component textures.

#7 blueshogun96   Crossbones+   -  Reputation: 937

Like
0Likes
Like

Posted 17 January 2014 - 03:24 PM

Tried it, still didn't work.


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#8 haegarr   Crossbones+   -  Reputation: 4332

Like
0Likes
Like

Posted 17 January 2014 - 03:35 PM

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, 18 January 2014 - 04:06 AM.


#9 blueshogun96   Crossbones+   -  Reputation: 937

Like
0Likes
Like

Posted 17 January 2014 - 05:36 PM

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?


Follow Shogun3D on the official website: http://shogun3d.net

 

blogger.png twitter.png tumblr_32.png facebook.png

 

"Yo mama so fat, she can't be frustum culled." - yoshi_lol


#10 Sponji   Members   -  Reputation: 1256

Like
0Likes
Like

Posted 18 January 2014 - 06:26 AM

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.


Derp

#11 richardurich   Members   -  Reputation: 1187

Like
0Likes
Like

Posted 18 January 2014 - 08:40 AM

Are you using glGetError after every function when you say you're getting GL_INVALID_ENUM everywhere? The error doesn't clear until you call glGetError, so if you called 100 functions before calling glGetError the error could be for any one of the 100 functions. If so, it's probably only the first GL_INVALID_ENUM that matters since the others are likely just ripple effects.

 

The code you provided is way too long for me to try to debug without being able to compile and run it, so knowing which OpenGL call is the first one to generate an error would really help focus on a small enough amount of code to be manageable.

 

Also, it appears GL_RGB is a valid internal format for glTexImage2D since it's in table 1. People do normally use sized or compressed formats though.



#12 Charon   Members   -  Reputation: 476

Like
0Likes
Like

Posted 18 January 2014 - 09:50 AM

What about using ARB_debug_output or KHR_debug to pinpoint the place where something is going wrong?


chaos, panic and disorder - my work here is finished


#13 mark ds   Members   -  Reputation: 1285

Like
0Likes
Like

Posted 25 January 2014 - 10:17 AM

Is the value of icol (in your vertex shader) zero by any chance?

 

What are the values of attrib_v_pos, attrib_v_normal & attrib_v_tex?



#14 arka80   Members   -  Reputation: 941

Like
0Likes
Like

Posted 26 January 2014 - 03:28 PM

You are loading a bmp, according to these tutorials bmp stores bytes in BGR format, not RGB: glTexCoord2d call should be:

 

glTexImage2D(GL_TEXTURE_2D, 0,GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);

 

Maybe...






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS