简体   繁体   中英

How to use texture in 3d graphic OpenGL

as tommy said, i make some changes:

So, I'd say steps would be:

  • make sure all of your arrays have at least 11 elements in them;
  • enable texturing.

Why this code won't show grass texture?

at first, load image of texture:

QImage img;
if(!img.load("../g.jpg")){
    qDebug("Texture not found!");
}
else {
    t = QGLWidget::convertToGLFormat(img);
    qDebug("Textture Converted to GLFormat.");
}

then call draw method that should draw an uneven ground with vertex array:

 GLfloat vertices2[] = {
    2,2,0 , 2,-2,0 , -2,-2,0 , -2,2,0,

    0.7,0.6,0.6 , 0.4,0.9,0.1 , 0.1,0.2,0.3 , 0.8,0.3,0.8 ,

    0.5,-0.5,0.12 , 0,-0.9,0.4 , -0.8,-0.2,0.6
};

GLfloat colors[] = {35 , 73 , 12 , 10 , 6 , 41 , 32 , 10 , 13 , 35 , 42 , 86 , 12 , 2 , 6 ,9};

GLfloat TexCoords[] = {
    0.449,0.911,0.056,
    0.911,0.056,0.17,
    0.056,0.17,0.902,
    0.17,0.902,0.873,
    0.902,0.873,0.061,
    0.873,0.061,0.557,
    0.061,0.557,0.517,
    0.557,0.517,0.131,
    0.517,0.131,0.855,
    0.131,0.855,0.533,
    0.855,0.533,0.896,
    0.533,0.896,0.178,
    0.896,0.178,0.303,
    0.178,0.303,0.358,
    0.303,0.358,0.064,
    0.358,0.064,0.156,
    0.064,0.156,0.199,
    0.156,0.199,0.32,
    0.199,0.32,0.636,
    0.32,0.636,0.306,
    0.636,0.306,0.445,
    0.306,0.445,0.166,
    0.445,0.166,0.572,
    0.166,0.572,0.249,
    0.572,0.249,0.29,
    0.249,0.29,0.389,
    0.29,0.389,0.79,
    0.389,0.79,0.567,
    0.79,0.567,0.692,
    0.567,0.692,0.202,
    0.692,0.202,0.913,
    0.202,0.913,0.498
};

GLfloat NormalP[] = {     0.383,0.29,0.746,
                          0.29,0.746,0.301,
                          0.746,0.301,0.675,
                          0.301,0.675,0.541,
                          0.675,0.541,0.677,
                          0.541,0.677,0.896,
                          0.677,0.896,0.915,
                          0.896,0.915,0.295,
                          0.915,0.295,0.423,
                          0.295,0.423,0.76,
                          0.423,0.76,0.89,
                    };

GLubyte indices[]  = {
    1,2,9 , 9,8,1 , 1,8,7 , 7,1,0 ,
    0,7,4 , 0,4,5 , 0,5,3 , 3,5,6 ,
    3,6,10 , 3,10,2 , 2,10,9 , 9,6,10 ,
    6,5,4 , 6,4,7 , 6,7,8 , 6,8,9
};

GLuint mHandle;
glGenTextures(1, &mHandle);
glBindTexture(GL_TEXTURE_2D, mHandle);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,t.width(),t.height(),0,GL_RGBA,GL_UNSIGNED_BYTE,t.bits());

glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glColorPointer(3,GL_FLOAT,0,colors);
glVertexPointer(3, GL_FLOAT, 0, vertices2);
glTexCoordPointer(3,GL_FLOAT,0,TexCoords);
glNormalPointer(GL_FLOAT,0,NormalP);

glDrawElements(GL_TRIANGLES,54, GL_UNSIGNED_BYTE, indices);
glDisableClientState(GL_VERTEX_ARRAY);

In the fixed pipeline texturing is applied only if enabled. You don't show a call to glEnable(GL_TEXTURE_2D) .

That being said, it also looks like your arrays are probably incorrectly sized.

The net effect of glTexCoordPointer(3,GL_FLOAT,0,TexCoords); ... glDrawElements(GL_TRIANGLES,54, GL_UNSIGNED_BYTE, indices); glTexCoordPointer(3,GL_FLOAT,0,TexCoords); ... glDrawElements(GL_TRIANGLES,54, GL_UNSIGNED_BYTE, indices); is that eleven sets of texture coordinates will be read from TexCoords since the greatest index in indices is 10 . However your texture coordinates array has only two texture coordinates in it, given that you're specifying them in 3d.

So, I'd say steps would be:

  • make sure all of your arrays have at least 11 elements in them;
  • enable texturing.

You probably also want to switch down to supplying texture coordinates in 2d if you're using only a 2d texture and not doing anything clever with the texture matrix stack.

As per jp, the blatant cribbing of which being why this is now marked as community wiki: you're also failing to bind your new texture ID after creating it. OpenGL is slightly odd because texture IDs were a bolt on after display lists proved not to have the properties originally hoped, so you can get away with doing everything without ever having generated a texture provided you also never bind one, assuming you have exactly one texture. But if you're going to be sensible about it and generate them then make sure you bind them. Generating does not implicitly bind.

Another problem is that you enable mipmapping as part of the sampling parameters:

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);

but the texture does not have mipmaps. To generate mipmaps, add the following call after glTexImage2D() :

glGenerateMipmap(GL_TEXTURE_2D);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM