在openGL(版本>3.3)的屏幕上显示纹理时如何更改纹理的大小?

How to change the size of the texture when displayed it on the screen in openGL (version> 3.3)?

本文关键字:纹理 openGL 何更改 显示 gt 屏幕 版本      更新时间:2023-10-16

如何更改屏幕上纹理的大小?我想制作一个在OpenGL中学习的2d小游戏。:-)我试图改变顶点[]的几何结构,但纹理无法延伸到它。我使用的代码如下:

 tempz = IMG_Load("graphics/menu_start.png");
    SDL_InvertSurface(tempz);
    //setup quad geometry
        //setup quad vertices
        vertices[0] = Vector2D(0.0,0.0);
        vertices[1] = Vector2D(1.0,0.0);
        vertices[2] = Vector2D(1.0,1.0);
        vertices[3] = Vector2D(0.0,1.0);
        //fill quad indices array
        GLushort* id = &indices[0];
        *id++ =0;
        *id++ =1;
        *id++ =2;
        *id++ =0;
        *id++ =2;
        *id++ =3;
        //setup quad vao and vbo stuff
        glGenVertexArrays(1, &vaoID);
        glGenBuffers(1, &vboVerticesID);
        glGenBuffers(1, &vboIndicesID);
        glBindVertexArray(vaoID);
        glBindBuffer (GL_ARRAY_BUFFER, vboVerticesID);
            //pass quad vertices to buffer object
            glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
            //enable vertex attribute array for position
            glEnableVertexAttribArray(0);
            glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,0,0);
            //pass quad indices to element array buffer
            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);

        int texture_width = 0, texture_height = 0, channels=0;
        texture_width = tempz->w;
        texture_height = tempz->h;

    //setup OpenGL texture and bind to texture unit 0
        glGenTextures(1, &textureID);
            glActiveTexture(GL_TEXTURE0);
            glBindTexture(GL_TEXTURE_2D, textureID);
            //set texture parameters
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
            glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

            //allocate texture 
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texture_width, texture_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempz->pixels);

        programID = LoadShaders( "shaders/shader.vert", "shaders/shader.frag" );
        glUseProgram(programID);

如果发布其余部分(例如着色器)会有所帮助,但我觉得奇怪的是,顶点中没有纹理坐标。通常情况下,它们在拐角处为(0,0)、(0,1),(1,0)和(1,1)。这些是顶点中的属性,就像物理位置一样。然后,你可以在整个地方摆动物理位置,仍然可以看到纹理以同样的方式映射到脸上。