Search code examples
openglstack-overflowpoint-clouds

Doing Point Cloud with OpenGL but keep getting stack overflow


I am trying to do point cloud but stack is always overflowed, please help! Where did I go wrong? (g_bmp.h includes the input image)

Or how else should I make point cloud from an input image with OpenGL?

#include <stdio.h>

#include "glut.h"
#include "g_bmp.h"

float ORG[3] = {0,0,0};

float XP[3] = {1,0,0}, YP[3] = {0,1,0}, ZP[3] = {0,0,1};

void display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

    glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective( 20, 1, 0.1, 10 );

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gluLookAt( 
     1,1,2,
     0 ,0 ,0,
     0,1,0 );

    glLineWidth (10.0);
    glBegin(GL_LINES);
        glColor3f (1,0,0); // X axis is red.
        glVertex3fv (ORG);
        glVertex3fv (XP ); 
        glColor3f (0,1,0); // Y axis is green.
        glVertex3fv (ORG);
        glVertex3fv (YP );
        glColor3f (0,0,1); // z axis is blue.
        glVertex3fv (ORG);
        glVertex3fv (ZP ); 
    glEnd();

    GBmp bm0;                   //the input image     
    bm0.load( "image.bmp" );    //the input image

    float imgdata[320][240][3]; //contains the three-dimensional coordinates
    float texture[320][240][3]; //contains the color values for each point
    float x, y, z;
    glPointSize (1);
    glBegin(GL_POINTS);
        for (int i=0; i<320; i++)
        {
            for (int j=0; j<240; j++)
            {
                glColor3f(texture[i][j][0]/255, texture[i][j][1]/255,                texture[i][j][2]/255);
                x=imgdata[i][j][0];
                y=imgdata[i][j][1];   
                z=imgdata[i][j][2];   
                glVertex3f(x,y,z);   
            }
        }   
    glEnd();
    glFlush();  
glutSwapBuffers();
}

void main()
{
  glutInitDisplayMode( GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB );
  glutInitWindowSize( 320, 240 );
  glutCreateWindow( "3D" );
  glutDisplayFunc(display);
  glutMainLoop();
}

Solution

  • these lines may be your problem:

    float imgdata[320][240][3]; //contains the three-dimensional coordinates
    float texture[320][240][3]; //contains the color values for each point
    

    that is 2*320*240*3*sizeof(float)=1 843 200 bytes allocated on the stack or 1.8 MB the stack was never meant to hold that much

    for a quick fix you can allocate them on the heap or make them globals, for a more thorough fix port to the non-fixed function pipeline and buffers to hold it all