【问题标题】:Opengl texture cylinder troubleOpengl纹理圆柱体问题
【发布时间】:2017-11-04 13:56:55
【问题描述】:

我有点困惑。

我想纹理一个“圆柱体”,所以我的第一种方法是(n 面数和 k 迭代次数)

void cylindrer(double r, int n,double h){
    double x[n],y[n];
    for(int k=0; k<n; k++){
        x[k]=r*cos(2*k*M_PI/n);
        y[k]=r*sin(2*k*M_PI/n);
    }


    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, texObject[1]);

    for(int k=0; k<n ;k++){
        int m= (k+1)%n;

        glBegin(GL_POLYGON);
            glTexCoord2f(1.0/n*(k),0.0);   glVertex3f( x[k], y[k], h/2);
            glTexCoord2f(1.0/n*(k),1);   glVertex3f( x[k], y[k], -h/2);
            glTexCoord2f(1.0/n*(k+1),1);   glVertex3f( x[m], y[m], -h/2);
            glTexCoord2f(1.0/n*(k+1),0.0);   glVertex3f( x[m], y[m], h/2);
        glEnd();
    }
    glDisable(GL_TEXTURE_2D);
}

纹理被应用但反向所以我改为

glBegin(GL_POLYGON);
    glTexCoord2f(1.0/n*(n-k), 0.0);   glVertex3f( x[k], y[k], h/2);
    glTexCoord2f(1.0/n*(n-k), 1.0);   glVertex3f( x[k], y[k], -h/2);
    glTexCoord2f(1.0/n*(n-k-1), 1.0);   glVertex3f( x[m], y[m], -h/2);
    glTexCoord2f(1.0/n*(n-k-1), 0.0);   glVertex3f( x[m], y[m], h/2);
glEnd();

它可以工作,看起来像这样:

当我使用这个纹理时:

但是现在我想将纹理旋转90度,所以我新建一个jpeg文件并旋转它。

所以纹理现在看起来像这样:

但这是我使用它时的结果:

纹理在圆柱体周围扭曲,我不明白为什么。

有什么想法吗?

我如何加载纹理:

#include <math.h>
#include <jpeglib.h>
#include <jerror.h>

GLuint texObject[2]; // textures

int main(int argc,char **argv){
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowPosition(200,200);
    glutInitWindowSize(1366,768);
    glutCreateWindow("");

    glClearColor(0.0,0.0,0.0,0.0);
    //glColor3f(1.0,1.0,1.0);
    glShadeModel(GL_FLAT);
    glPointSize(2.0);
    glEnable(GL_DEPTH_TEST);

    glGenTextures(2, texObject);

    loadJpegImage("./textureCorps5.jpg", 1);

    glutMainLoop();
    return 0;
}


void loadJpegImage(char *fichier, int i)
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    FILE *file;
    unsigned char *ligne;

    cinfo.err = jpeg_std_error(&jerr);
    jpeg_create_decompress(&cinfo);
    #ifdef __WIN32
        if (fopen_s(&file,fichier,"rb") != 0)
        {
            fprintf(stderr,"Error\n");
            exit(1);
        }
    #elif __GNUC__
        if ((file = fopen(fichier,"rb")) == 0)
        {
            fprintf(stderr,"Error\n");
            exit(1);
        }
    #endif
    jpeg_stdio_src(&cinfo, file);
    jpeg_read_header(&cinfo, TRUE);

    unsigned char image[cinfo.image_width*cinfo.image_height*3];

    jpeg_start_decompress(&cinfo);
    ligne=image;
    while (cinfo.output_scanline<cinfo.output_height)
    {
        ligne=image+3*cinfo.image_width*cinfo.output_scanline;
        jpeg_read_scanlines(&cinfo,&ligne,1);
    }
    jpeg_finish_decompress(&cinfo);
    jpeg_destroy_decompress(&cinfo);


    glBindTexture(GL_TEXTURE_2D, texObject[i]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, cinfo.image_width, cinfo.image_height, 0,
               GL_RGB, GL_UNSIGNED_BYTE, image);
    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}

【问题讨论】:

  • 显示MCVE。 x[k]是什么,y[k]是什么,纹理应该怎么看
  • x[k] 和 y[k] 是绘制圆柱的圆的坐标,我编辑帖子。

标签: c++ opengl textures


【解决方案1】:

GL_UNPACK_ALIGNMENT指定内存中每个像素行开头的对齐要求。默认情况下,GL_UNPACK_ALIGNMENT 设置为 4。 这意味着纹理的每一行都应该有 4*N 字节的长度。

您的纹理是一个 RGB 纹理,每个纹素需要 24 位或 3 个字节,并且您将纹素,尤其是纹理的线条紧密地打包在一起。这意味着您可以忽略 4 的对齐作为纹理的一行的开始(除了纹理宽度的 3 倍可以被 4 整除而没有剩余)。

为了解决这个问题,您必须将对齐方式更改为 1。这意味着您必须在 glTexImage2D 之前设置 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);,以便读取紧密包装的纹理。

否则在读取纹理时,您将获得每行 0-3 个字节的偏移量。这会导致纹理不断扭曲。


相反,您也可以在创建旋转纹理时处理线的对齐和对齐长度:

int bytesPerLine = cinfo.image_width * 3;
bytesPerLine += bytesPerLine % 4; 

unsigned char image[bytesPerLine * cinfo.image_height];

jpeg_start_decompress(&cinfo);
while (cinfo.output_scanline<cinfo.output_height)
{
    ligne = image + bytesPerLine*cinfo.output_scanline;
    jpeg_read_scanlines(&cinfo,&ligne,1);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多