简体   繁体   English

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

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

How i can change size of texture on the screen? 我如何更改屏幕上的纹理大小? I'm want make a little 2d game for learning in OpenGL.:-) I try to change vertices[] geometry, but texture doesn't stretch to it. 我想做一个2D游戏在OpenGL中学习。:-)我试图改变vertices []的几何形状,但是纹理并没有延伸。 I use something like this code: 我使用类似下面的代码:

 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);

It would help if you posted the rest, eg the shaders, but what I find strange is that you have no texture coordinates in your vertices. 如果发布了其余部分(例如着色器)会有所帮助,但是我发现奇怪的是您的顶点中没有纹理坐标。 Usually they'd be (0,0), (0,1), (1,0) and (1,1) at the corners. 通常它们在拐角处分别是(0,0),(0,1),(1,0)和(1,1)。 These are attributes in your vertices just like the physical positions. 就像物理位置一样,这些是顶点中的属性。 Then you can wobble the physical position all over the place and still see the texture mapped the same way over the face. 然后,您可以摆动整个位置的物理位置,仍然可以看到纹理以相同的方式映射到整个面部。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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