OpenGL 2D 纹理映射问题

发布于 2024-08-25 07:24:39 字数 2043 浏览 6 评论 0原文

我对 OpenGL 比较陌生,当我将图像渲染为与图像大小相同的 QUAD 的纹理时,我遇到了一些问题。这是我的代码。如果有人帮助我解决这个问题,我将非常感激。图像显得小得多并且被压扁。 (顺便说一句,图像尺寸为 500x375)。

glGenTextures( 1, &S_GLator_InputFrameTextureIDSu );
        glBindTexture(GL_TEXTURE_2D, S_GLator_InputFrameTextureIDSu);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

        glTexImage2D( GL_TEXTURE_2D, 0, 4, S_GLator_EffectCommonData.mRenderBufferWidthSu, S_GLator_EffectCommonData.mRenderBufferHeightSu, 0, GL_RGBA, GL_UNSIGNED_BYTE, dataP);
glBindTexture(GL_TEXTURE_2D, S_GLator_InputFrameTextureIDSu);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, S_GLator_EffectCommonData.mRenderBufferWidthSu, S_GLator_EffectCommonData.mRenderBufferHeightSu, GL_RGBA, GL_UNSIGNED_BYTE, bufferP);
//set the matrix modes
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        //gluPerspective( 45.0, (GLdouble)widthL / heightL, 0.1, 100.0 );
        glOrtho (0, 1, 0, 1, -1, 1);
        // Set up the frame-buffer object just like a window.
        glViewport( 0, 0, widthL, heightL );
        glDisable(GL_DEPTH_TEST);
        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();


        glBindTexture( GL_TEXTURE_2D, S_GLator_InputFrameTextureIDSu );


        //Render the geometry to the frame-buffer object


        glBegin(GL_QUADS); //input frame
            glColor4f(1.f,1.f,1.f,1.f);
            glTexCoord2f(0.0f,0.0f);    
            glVertex3f(0.f ,0.f ,0.0f);
            glTexCoord2f(1.0f,0.0f);    
            glVertex3f(1.f ,0.f,0.0f);
            glTexCoord2f(1.0f,1.f); 
            glVertex3f(1.f ,1.f,0.0f);
            glTexCoord2f(0.0f,1.f); 
            glVertex3f(0.f ,1.f,0.0f);
        glEnd();

I am relatively new to OpenGL and I am having some issues when I am rendering an image as a texture for a QUAD which is as the same size of the image. Here is my code. I would be very grateful if someone helps me to solve this problem. The image appears way smaller and is squished. (BTW, the image dimensions are 500x375).

glGenTextures( 1, &S_GLator_InputFrameTextureIDSu );
        glBindTexture(GL_TEXTURE_2D, S_GLator_InputFrameTextureIDSu);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);

        glTexImage2D( GL_TEXTURE_2D, 0, 4, S_GLator_EffectCommonData.mRenderBufferWidthSu, S_GLator_EffectCommonData.mRenderBufferHeightSu, 0, GL_RGBA, GL_UNSIGNED_BYTE, dataP);
glBindTexture(GL_TEXTURE_2D, S_GLator_InputFrameTextureIDSu);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, S_GLator_EffectCommonData.mRenderBufferWidthSu, S_GLator_EffectCommonData.mRenderBufferHeightSu, GL_RGBA, GL_UNSIGNED_BYTE, bufferP);
//set the matrix modes
        glMatrixMode( GL_PROJECTION );
        glLoadIdentity();
        //gluPerspective( 45.0, (GLdouble)widthL / heightL, 0.1, 100.0 );
        glOrtho (0, 1, 0, 1, -1, 1);
        // Set up the frame-buffer object just like a window.
        glViewport( 0, 0, widthL, heightL );
        glDisable(GL_DEPTH_TEST);
        glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
        glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );


        glMatrixMode( GL_MODELVIEW );
        glLoadIdentity();


        glBindTexture( GL_TEXTURE_2D, S_GLator_InputFrameTextureIDSu );


        //Render the geometry to the frame-buffer object


        glBegin(GL_QUADS); //input frame
            glColor4f(1.f,1.f,1.f,1.f);
            glTexCoord2f(0.0f,0.0f);    
            glVertex3f(0.f ,0.f ,0.0f);
            glTexCoord2f(1.0f,0.0f);    
            glVertex3f(1.f ,0.f,0.0f);
            glTexCoord2f(1.0f,1.f); 
            glVertex3f(1.f ,1.f,0.0f);
            glTexCoord2f(0.0f,1.f); 
            glVertex3f(0.f ,1.f,0.0f);
        glEnd();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

十年九夏 2024-09-01 07:24:39

我想我在这里看到你的问题。 glViewport 定义视口大小,而不是窗口大小。因此,当您创建窗口(使用 glut、sdl 或 wgl 或其他)时,您必须指定大小。要获得 glViewport 到此窗口的 1:1 映射,这两者需要匹配。

I think I see you issue here. glViewport defines the viewport size, not the window size. So when you create your window (with glut, sdl, or wgl, or whatever), you have to specify the size. To get a 1:1 mapping of the glViewport to this window, those two need to match.

思念绕指尖 2024-09-01 07:24:39

首先,您的图像尺寸必须是 2 的幂,而不仅仅是任意 500x375。其次,glTexImage2Dinternalformat(第三个)参数应该是一个符号常量,描述内部格式布局,而不仅仅是 "4",如下自 OpenGL 1.2 起已弃用。第三,您的 dataPbufferP 存储布局必须与当前的 GL_UNPACK_ALIGNMENT 相对应。第四,您尝试在其上放置纹理的等边四边形本质上是方形的,因此您的非方形纹理将在这样的纹理坐标下显得被压扁或拉伸。

First, your image dimensions must be powers of two, not just arbitrary 500x375. Second, the internalformat (third) argument to glTexImage2D should be a symbolic constant, describing the internal format layout, not just "4", as it's deprecated since OpenGL 1.2. Third, your dataP and bufferP storage layout must correspond to the current GL_UNPACK_ALIGNMENT. Fourth, your equilateral quad, on which you're trying to put the texture on, is essentially square, and thus your non-square texture will appear squashed or stretched with such a texture coordinates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文