Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用c+在opengl中创建正方形网格(2D)+;_C++_Image_Opengl_Image Segmentation - Fatal编程技术网

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

C++ 使用c+在opengl中创建正方形网格(2D)+;,c++,image,opengl,image-segmentation,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)

我想使用几个较小的正方形(如网格)制作一个正方形(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;
}
inth=1,w=1;
int res=25;//res是我想要的较小正方形的数量
浮动时间=小时/秒;
浮点数=w/res;
作废提款()
{
浮动x,y;

对于(y=-.5;y请尝试此显示功能:

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。

叹息,又一个“我得到了一个黑屏,为我做调试和计算”。禁用面剔除(
glDisable(GL\u CULL\u face)
)。禁用深度测试。查看是否有帮助。打印vertex提交到文件,验证它们是否正确。删除循环,尝试只绘制一个四元组。如果一个四元组有效,调试循环逻辑。启动OpenGL调试器并检查GL错误。是否尝试禁用深度测试-尝试注释主代码中显示glEnable的代码行(GL_DEPTH_TEST)并查看glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE)会发生什么情况;我认为问题是在深度0上绘制正方形。在初始化顶点的位置生成第三个参数…例如10。从gluPerspective()开始您可以指定相机值。在我上面发布的代码中,近剪裁帧为0.1,远剪裁帧为100.0,这意味着如果您想在屏幕上看到任何形状,您必须将其放置在近剪裁帧和远剪裁帧之间。您可以看到白色屏幕,因为您的正方形为白色:)