簡體   English   中英

使用openGl顯示pgm圖像

[英]Display a pgm image using openGl

所以我正在C ++上使用openGl編寫程序。我想加載圖像,然后以NxN網格形式顯示它。 我加載了圖像並將其數據存儲在數組中,然后繼續使用以下方法來實現我的目標:

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

    }

此函數將打開包含圖像數據的文件,首先將圖像加載到pixel數組中,然后再加載到我在glTexImage2D中使用的紙張數組中。 MyAtof()是我構建的用於將字符串轉換為浮點型的函數。 數據已正確地從文件傳遞到數組,我已經對其進行了測試。 fillGrid函數之后,以下函數被稱為t重新繪制:

void drawGrid2(void)
{

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

並且:

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

}

主要:

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

其他功能:

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

}

這是使用openGl創建紋理並顯示圖像的正確方法嗎?

該文件編譯,但結果不是我想要的。 我想要一個15x15的正方形網格,每個正方形都包含圖像。 在重新粉刷之前,結果是this

重新粉刷后的結果是this

我為第一次重塗和第二次重塗使用了不同的功能。 由於第一個工作,所以我沒有發布它。

OpenGL的默認紋理縮小過濾器是GL_NEAREST_MIPMAP_LINEAR 您的紋理不是mipmap完整的,因此紋理化在此模式下將不起作用。 您應該設置glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); (或GL_LINEAR )。

您似乎還嘗試在此處設置紋理放大濾鏡:

 glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT); 

對接GL_REPEAT根本不是有效的過濾器模式。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM