简体   繁体   English

使用openGl显示pgm图像

[英]Display a pgm image using openGl

So i am writing a program using openGl on C++.I want to load an image and then display it in a NxN grid form. 所以我正在C ++上使用openGl编写程序。我想加载图像,然后以NxN网格形式显示它。 I loaded the image and stored its data in an array and then proceeded to use the following method to achieve my goal: 我加载了图像并将其数据存储在数组中,然后继续使用以下方法来实现我的目标:

    void fillGrid(){
        ifstream myfile("paper.pgm");
        ofstream otherfile;
        otherfile.open("test.txt");
        string line;
        string buffer;
        fstream  afile;
        if (myfile.is_open())
        {
            int counter=0;
                while (getline(myfile, line))
            {
                if(counter>2){
                        buffer=buffer+line;
                        }
                    counter++;
                }
                pixels=new float[1600];
                int i=0;
                string delimiters = " ,";
            size_t current;
            size_t next = -1;
            do
            {
                current = next + 1;
                next = buffer.find_first_of( delimiters, current );
                if(i<=1600){
                pixels[i]=myAtof (buffer.substr(current));
                paper[i]=pixels[i];
                }
                i++;
            }
                    while (next != string::npos);


                    for(int j=0;j<=1600;j++){
                        otherfile<<paper[j]<<" "<< j<<endl;
                    }


       glEnable(GL_TEXTURE_2D);                     
            glEnable(GL_DEPTH_TEST);
            glBindTexture(GL_TEXTURE_2D, 1);
            glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 140, 140, 0, GL_RGB, GL_FLOAT, paper);
            glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);
        }

    }

This function opens the file containing the image data, loads the image in the pixels array at first and then at the paper array that i use in glTexImage2D. 此函数将打开包含图像数据的文件,首先将图像加载到pixel数组中,然后再加载到我在glTexImage2D中使用的纸张数组中。 MyAtof() is a function i built to convert a string into a float. MyAtof()是我构建的用于将字符串转换为浮点型的函数。 The data is properly passed from the file to the array, i've tested it. 数据已正确地从文件传递到数组,我已经对其进行了测试。 After ther fillGrid function the following function is called t do the repaint: fillGrid函数之后,以下函数被称为t重新绘制:

void drawGrid2(void)
{

    for(int i=-240;i<240;i+=40){
        for(int j=-300;j<=300;j+=40){   
            drawSquare2(i,j);
        }   
    }
}

And also: 并且:

void drawSquare2(int x,int y)
{


            glBindTexture(GL_TEXTURE_2D, 1);    
            glBegin(GL_QUADS); //Start drawing quad
            glVertex2f(x,y); //first coordinate x y
            glVertex2f(x+40,y); //second coordinate 
            glVertex2f(x+40,y+40); //third coordinate
            glVertex2f(x,y+40); //last coordinate
            glEnd(); //Stop drawing quads
            glFlush ();

}

Main: 主要:

int main (int argc, char** argv)
{
    glutInit (&argc, argv);                         // Initialize GLUT.
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);   // Set display mode.
    glutInitWindowPosition (0, 0);   // Set top-left display-window position.
    glutInitWindowSize (600, 500);      // Set display-window width and height.
    glutCreateWindow ("Main"); // Create display window.

    init ();                            // Execute initialization procedure.
    glutDisplayFunc (display);       // Send graphics to display window.
    glutKeyboardFunc(processEscKey);

    glutMainLoop ();                    // Display everything and wait.
    return 0;
}

Other functions: 其他功能:

void init (void)
{
 //   glClearColor (1.0, 1.0, 1.0, 0.0);  // Set display-window color to white.
    glClearColor (0.0, 0.0, 0.0, 1.0);//black
        glClear (GL_COLOR_BUFFER_BIT);
    glLoadIdentity();
        glMatrixMode (GL_PROJECTION);       // Set projection parameters.
        gluOrtho2D(-300,300,-300,300);//0,width,0,height

}

void processEscKey(unsigned char key, int x, int y)
{
    if(key==27){
        exit(0);
    }
    else if(key==98){
        fillGrid();
        drawGrid2();    
    }   

}

Is this the correct way to create a texture and display an image with openGl? 这是使用openGl创建纹理并显示图像的正确方法吗?

The file compiles but the result isn't what i want. 该文件编译,但结果不是我想要的。 I wanted an 15x15 grid of squares, with each square containing the image. 我想要一个15x15的正方形网格,每个正方形都包含图像。 Before the repaint the result was this . 在重新粉刷之前,结果是this

After the repaint the result is this . 重新粉刷后的结果是this

I used different functions for the first repaint and the the second one. 我为第一次重涂和第二次重涂使用了不同的功能。 Since the first one is working i didnt post it. 由于第一个工作,所以我没有发布它。

OpenGL's default texture minification filter is GL_NEAREST_MIPMAP_LINEAR . OpenGL的默认纹理缩小过滤器是GL_NEAREST_MIPMAP_LINEAR Your texture is not mipmap complete, so texturing will not work in this mode. 您的纹理不是mipmap完整的,因此纹理化在此模式下将不起作用。 You should set glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 您应该设置glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); (or GL_LINEAR ). (或GL_LINEAR )。

You also seem to try to set the texture maginification filter here: 您似乎还尝试在此处设置纹理放大滤镜:

 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT); 

butt GL_REPEAT is not a valid filter mode at all. 对接GL_REPEAT根本不是有效的过滤器模式。

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

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