开发者

Segmentation fault driving me insane [closed]

开发者 https://www.devze.com 2023-03-22 02:38 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in it开发者_运维百科s current form.
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in it开发者_运维百科s current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

It's a particle system I'm using. I wanted to make a cube out of particles but it now seems to be getting seg faults though it worked before. I am guessing it's the array but I cannot after 2 days of looking figure out the problem. GDB has been no help.

#include "particles.h"
using namespace std;

GLfloat texture1[10];
particle parts[50][50][50];
GLfloat angle = 1.0;

particles::particles()
{
    addTextures();
    cube();
}

particles::particles(int O)
{
    addTextures();
    cube();
}

void  particles::addTextures()
{
    texture1[0] = LoadTextureRAW("star_mask.bmp",256,256); //load texture
    texture1[1]  = LoadTextureRAW("star.bmp",256,256);
}

void particles::cube()
{
// for(int i =0; i<124999; i++)
// {
//  parts[i] = particle(1,1,1,1,1,1,1);
//  }

    float x=-5;
    float y=-5;
    float z=-5;

    for(int i =0 ; i <49 ;i++)
    {
        for(int k =0 ; k <49 ;k++)
        {
            for(int j =0 ; j <49 ;j++)
            {
            parts[i][k][j] = particle(i*0.01,k*0.01,j*0.01,1,1,1,0.09);
            }
        }
    }    
}

GLuint particles::LoadTextureRAW( const char * filename, int width,
                           int height )
{
    GLuint texture;
    unsigned char * data;
    FILE * file;

    file = fopen( filename, "rb" );
    if ( file == NULL ) return 0;

    data = (unsigned char *)malloc( width * height * 3 );

    fread( data, width * height * 3, 1, file );
    fclose( file );

    glGenTextures(1, &texture );

    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T,GL_REPEAT);
    glTexEnvi( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, 
        GL_LINEAR_MIPMAP_NEAREST );
    glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, 
        GL_LINEAR );
    gluBuild2DMipmaps(GL_TEXTURE_2D, 3, width, height, GL_RGB, 
        GL_UNSIGNED_BYTE, data);

    free( data );

    return texture;
}

void particles::FreeTexture( GLuint texture )
{
    glDeleteTextures( 1, &texture );
}

GLuint particles::LoadTextureRAW( const char * filename, int width, 
    int height);
void  particles::FreeTexture( GLuint texturez );

void  particles::square (void)
{
    glBindTexture( GL_TEXTURE_2D, texture1[0] );
    glBegin (GL_QUADS);
    glTexCoord2d(0.0,0.0);
    glVertex2d(-1.0,-1.0);
    glTexCoord2d(1.0,0.0);
    glVertex2d(1.0,-1.0);
    glTexCoord2d(1.0,1.0);
    glVertex2d(1.0,1.0);
    glTexCoord2d(0.0,1.0);
    glVertex2d(-1.0,1.0);
    glEnd();
}


Your texture IDs are the wrong data type. They should be GLint, but you're using GLfloat.


Another problem is that all your data appears to be held in global variables, but you're initializing it for each instance of the particles class. So you'll be leaking textures and possibly have other unexpected behavior as well.


Another potential problem is that if you have a VBO bound, the data pointer in gluBuild2DMipmaps will be interpreted as an offset. That could also cause trouble.


Besides all that, you should check the return values from malloc, fopen, and fread. Right now you're ignoring errors, which could lead to a segmentation fault.

0

精彩评论

暂无评论...
验证码 换一张
取 消