简体   繁体   English

OpenGL 2D纹理映射问题

[英]OpenGL 2D Texture Mapping problem

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. 我是OpenGL的新手,在将图像渲染为QUAD的纹理(与图像的大小相同)时遇到一些问题。 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). (顺便说一句,图像尺寸为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 think I see you issue here. 我想我在这里看到你的问题。 glViewport defines the viewport size, not the window size. glViewport定义视口大小,而不是窗口大小。 So when you create your window (with glut, sdl, or wgl, or whatever), you have to specify the size. 因此,在创建窗口(使用glut,sdl或wgl或其他格式)时,必须指定大小。 To get a 1:1 mapping of the glViewport to this window, those two need to match. 为了获得glViewport到此窗口的1:1映射,这两个需要匹配。

First, your image dimensions must be powers of two, not just arbitrary 500x375. 首先,您的图片尺寸必须是2的幂,而不是任意的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. 其次, glTexImage2Dinternalformat (第三个)参数应该是一个符号常量,描述内部格式布局,而不仅仅是"4" ,因为自OpenGL 1.2开始不推荐使用它。 Third, your dataP and bufferP storage layout must correspond to the current GL_UNPACK_ALIGNMENT . 第三,您的dataPbufferP存储布局必须与当前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. 第四,要在其上放置纹理的等边四边形基本上是正方形的,因此非正方形纹理将以这种纹理坐标看起来像被挤压或拉伸。

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

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