使用c ++在opengl中创建正方形网格(2D)

时间:2015-10-16 09:25:40

标签: c++ image opengl image-segmentation

我想通过使用几个较小的正方形(如网格)来制作正方形(2D)。我需要这样做,因为我想在主要方块上纹理图像并将图片分成几个像素(所述较小的方块)。

以下是该计划: -

int h=1,w=1;
int res=25;// res is the number of smaller squares I want
float hratio=h/res;
float wratio=w/res;

void Draw()
{
float x,y;

for(y=-.5;y<=h;y+=h/res)
 {  
   for(x=-.5;x<=w;x+=w/res)
     {  
        glColor3f(1,1,1);
        glBegin(GL_QUADS); 
        glVertex3f(x,y+(h/res),0);
        glVertex3f(x+(w/res),y+(h/res),0);
        glVertex3f(x+(w/res),y,0);
        glVertex3f(x,y,0);
        glEnd();        
    }    
 }  
glFlush();
glutPostRedisplay();
}

void display(void)
{
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
 Draw();   
 glutSwapBuffers();
 glutPostRedisplay();
}


  int main(int iArgc, char** cppArgv)
   {
   glutInit(&iArgc, cppArgv);
   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);    
glutInitWindowSize(600, 600);
glutInitWindowPosition(200, 200);   
glutCreateWindow("PIXELS");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);   
glutMainLoop();
return 0;
}

当我运行这个程序时,我得到的只是黑屏。

1 个答案:

答案 0 :(得分:0)

尝试此显示功能:

void display() { 
    glClear(GL_COLOR_BUFFER_BIT); 
    glMatrixMode(GL_MODELVIEW); 

    //set viewport
    glViewport(0, 0, screen.width, screen.height);

    //set projection matrix using intrinsic camera params
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    float aspect = screen.width*1.0/screen.height;
    //gluPerspective is arbitrarily set, you will have to determine these values based
    //on the intrinsic camera parameters
    gluPerspective(60.0f, aspect, 0.1f, 100.0f); 

    //you will have to set modelview matrix using extrinsic camera params
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt(0, 0, 5, 0, 0, 0, 0, 1, 0); 

    glLoadIdentity();       
    glTranslatef(0.0, 0.0, -5.0);

    Draw(); 

    glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
    gluPostRedisplay();
}

在你的情况下,screen.width为600,screen.height为600。