Adding texture to a plane made from vertices

I created a plane in OpenGL using this code:

glBegin(GL_TRIANGLE_STRIP);
glColor4f(0.8, 0.8, 0.5, 1.0);
glVertex3f(startlen, height, startwid);    
glVertex3f(startlen, height, startwid + width);
glVertex3f(startlen + length, height, startwid);
glVertex3f(startlen + length, height, startwid + width);
glEnd();

Now I want to apply a texture to this plane.

I read this tutorial here , but I focused on the part “Using Textures in OpenGL” because I do not have UV coordinates.

I know that we need to import a texture file, in my case a raster file.
The bootloader returns Glint, which is the textureID, is global? In the example, if I need to load this texture, do I just need to call the texture binding function with the specified identifier?

Anyway, how can I apply a texture to a plane?


. # 1: .
, , , OpenGL.
, :)

, . , , , ( , ).

, , : enter image description here

, 2 :

  • , , .
  • , ( ? ?) )

# 2:

​​, ! . : enter image description here

, :
enter image description here
- 256x256 ( )


# 3:
glTexCoord2f ( ):

glTexCoord2f(0.0f, 0.0f);   glVertex3f(startlen, height, startwid);
glTexCoord2f(0.0f, 1.0f);   glVertex3f(startlen, height, startwid - width/2);
glTexCoord2f(1.0f, 0.0f);   glVertex3f(startlen + length, height, startwid);
glTexCoord2f(1.0f, 1.0f);   glVertex3f(startlen + length, height, startwid - width/2);
+5
2

, textureID, , , , . .

, glBindTexture() , :

glBindTexture(GL_TEXTURE_2D, id); // where id is GLuint returned by your loader

, , . glTexCoord2f(). :

glBegin(GL_QUADS);
    // Bottom left
    glTexCoord2f(0.0f, 1.0f);                   
    glVertex2i(0.0f, 10.0f);

    // Top left
    glTexCoord2f(0.0f, 0.0f);
    glVertex2i(0.0f, 0.0f);

    // Top right
    glTexCoord2f(1.0f, 0.0f);
    glVertex2i(10.0f, 0.0f);

    // Bottom right
    glTexCoord2f(1.0f, 1.0f);
    glVertex2i(10.0f, 10.0f);
glEnd();

, "". OpenGL, .

, , , .

, , 0.0 1.0. 1.0 (, ).

, , 0.0 1.0 :

enter image description here

PS. , , :

glEnable(GL_TEXTURE_2D);

, . .


  • : , , - , . :

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    :

    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
    

    GL_REPEAT 1,0. , 2.0, , 1.0, .

  • , , (, glEnable(GL_TEXTURE_2D);), , , .

    , - , , , :

    glDisable(GL_TEXTURE_2D);
       // Draw your second plane here
    glEnable(GL_TEXTURE_2D);
    

EDIT2

, , . , 1.0, . , , :

enter image description here

- .

, , , ( 1.0), . , - , , .

, 1.0, - 2.0 .

, "" , . .

+10

uv . glTexCoord2f();
, - v. :

glBegin(GL_TRIANGLE_STRIP);
glColor4f(0.8, 0.8, 0.5, 1.0);
glTexCoord2f(0.0f, 0.0f); glVertex3f(startlen, height, startwid);
glTexCoord2f(0.0f, 1.0f); glVertex3f(startlen, height, startwid + width);
glTexCoord2f(1.0f, 1.0f); glVertex3f(startlen + length, height, startwid);
glTexCoord2f(1.0f, 0.0f); glVertex3f(startlen + length, height, startwid + width);
glEnd();

, , . u x , 0.0 = left 1.0 = right, v y , 0.0 = top 1.0 = bottom.
, , :

glEnable(GL_TEXTURE_2D); //call this once, during initialization

, :

glGenTextures(1, &MyTexture);
glBindTexture(GL_TEXTURE_2D, MyTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); //data is a gluByte array of your image data

:

glBindTexture(GL_TEXTURE_2D, MyTexture);

, , .

+1

All Articles