# OpenGL glEnd unhandled exeption?

This topic is 4927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So, I have got a very simple problem. (I am 11 so please try not to use so much trigonometry...) I have got an Opengl project, I call it "Paintball". It is supporsed to be an FPS. You have got a gun (MS3D model) and when you shoot, I find the intersection point of sector and the view vector and I render particles there (explosion from NeHe tutorials). So this is the code that renders the particle: #ifdef BILLBOARD glBegin(GL_TRIANGLE_STRIP); glTexCoord2d(0,0); glVertex3f(x + (-right.x - up.x), y + (-right.y - up.y), z + (-right.z - up.z)); glTexCoord2d(1,0); glVertex3f(x + ( right.x - up.x), y + ( right.y - up.y), z + ( right.z - up.z)); glTexCoord2d(1,1); glVertex3f(x + ( right.x + up.x), y + ( right.y + up.y), z + ( right.z + up.z)); glTexCoord2d(0,1); glVertex3f(x + (-right.x + up.x), y + (-right.y + up.y), z + (-right.z + up.z)); glEnd(); //CRASH 1 #else glBegin(GL_TRIANGLE_STRIP); glTexCoord2d(1,1); glVertex3f(x+0.2f,y+0.2f,z); glTexCoord2d(0,1); glVertex3f(x-0.2f,y+0.2f,z); glTexCoord2d(1,0); glVertex3f(x+0.2f,y-0.2f,z); glTexCoord2d(0,0); glVertex3f(x-0.2f,y-0.2f,z); glEnd(); //CRASH 2 #endif I call this code in a FOR cycle. If I define BILLBOARD, so the application crashes on the line //CRASH 1. If I dont, it crashes on the line //CRASH 2, but it doesnt crash in the first cycle. Doesnt anybody know how to solve this problem? Thanks.

##### Share on other sites
Just out of curiosity why are you using preproccessor?

##### Share on other sites
I use preprocessor because I have got two variations of my application: with billboarding (realistic) and without it (fast).

##### Share on other sites
I don't think the problem is with those lines.. Probably something you did just before caused it. I'd check the code preceding the part you posted, if I were you. 8)

I did.

##### Share on other sites
Ok... You could try removing those lines and see if the program crashes somewhere else.
Could you post the preceding lines too? (I.e. the for loop and the lines just before it.)

##### Share on other sites

void Font_drawtext( bool sada, int xpos, int ypos, int size, char *str, ... )
{
glDisable(GL_DEPTH_TEST); // Disables Depth Testing
char buffer[1024];
int i, L, ascii;
float cx, cy;
memset(buffer, '\0', 1024);
va_list argptr;
va_start( argptr, str );
_vsnprintf( buffer, 1024, str, argptr );
va_end( argptr );
L = strlen( buffer );
glCallList( fontsavestate );
glTranslatef( (float)xpos, (float)ypos, 0 );
for( i=0; i<L; i++ ) {
ascii = (int)buffer;
if( buffer == '&' && i+1<L )
{
glColor3f(1.0f,0.0f,0.0f);
continue;
}
else {
cx=float(ascii%16)/16.0f;
glTexCoord2f( cx,1-cy-0.0625f );
glVertex2i( 0,0 );
glTexCoord2f( cx+0.0625f,1-cy-0.0625f );
glVertex2i( size,0 );
glTexCoord2f( cx+0.0625f,1-cy );
glVertex2i( size,size );
glTexCoord2f( cx,1-cy );
glVertex2i( 0,size );
glEnd(); // <-- CRASH POINT!
glTranslatef( (float)size, 0, 0 );
}
}
glCallList( fontrestorestate );
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
}

There was another glEnd before this funcion, but it WASNT in pair with glBegin(GL_QUADS). Maybe the matter is the GL_QUADS.

##### Share on other sites
The best I can guess, is that you've managed to corrupt some memory, which causes these crashes. Maybe you've allocated too few bytes at one point, or maybe you've set a pointer wrong, both of which could mean you write to memory you shouldn't..
Check your memory allocations and writes to allocated memory.. That's about as specific I can get :-/

##### Share on other sites
So, this is the code rendering the particles. I am too lazy to remove the comments so please dont worry about non-english comments. They are in Czech. (a little country in center of Europe)

glBlendFunc(GL_SRC_ALPHA,GL_ONE);// Typ blendingu
glBindTexture(GL_TEXTURE_2D,texture_particles);// Vybere texturu

camera.Look();

glColor3f(1.0f,1.0f,1.0f);

#ifdef BILLBOARD
const float width = 0.1f;// Polovina velikosti
const float height = 0.1f;
float mat[16];
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
CVector3 right=CVector3(mat[0], mat[4], mat[8]);
right=Normalize(right);
right = right * width;
CVector3 up=CVector3(mat[1], mat[5], mat[9]);
up=Normalize(up);
up = up * height;
#endif

for (loop=0;loop<MAX_PARTICLES;loop++)// Cyklus prochází každou èástici
{
if (particle[loop].active)// Pokud je èástice aktivní
{
// Vykreslení
float x=particle[loop].x;// x pozice
float y=particle[loop].y;// y pozice
float z=particle[loop].z;// z pozice + zoom

#ifdef BILLBOARD
// Vykreslení
glBegin(GL_TRIANGLE_STRIP);
glTexCoord2d(0,0);
glVertex3f(x + (-right.x - up.x),
y + (-right.y - up.y),
z + (-right.z - up.z));
glTexCoord2d(1,0);
glVertex3f(x + ( right.x - up.x),
y + ( right.y - up.y),
z + ( right.z - up.z));
glTexCoord2d(1,1);
glVertex3f(x + ( right.x + up.x),
y + ( right.y + up.y),
z + ( right.z + up.z));
glTexCoord2d(0,1);
glVertex3f(x + (-right.x + up.x),
y + (-right.y + up.y),
z + (-right.z + up.z));
glEnd();
#else
glBegin(GL_TRIANGLE_STRIP);// Vytvoøí obdélník pomocí triangle strip
glTexCoord2d(1,1); glVertex3f(x+0.2f,y+0.2f,z);// Horní pravý
glTexCoord2d(0,1); glVertex3f(x-0.2f,y+0.2f,z);// Horní levý
glTexCoord2d(1,0); glVertex3f(x+0.2f,y-0.2f,z);// Dolní pravý
glTexCoord2d(0,0); glVertex3f(x-0.2f,y-0.2f,z);// Dolní levý
glEnd();// Ukonèí triangle strip
#endif

particle[loop].x+=particle[loop].xi/(slowdown*1000);// Pohyb na ose x
particle[loop].y+=particle[loop].yi/(slowdown*1000);// Pohyb na ose y
particle[loop].z+=particle[loop].zi/(slowdown*1000);// Pohyb na ose z
particle[loop].xi+=1.8f;// Gravitaèní pùsobení na ose x
particle[loop].yi+=1.8f;// Gravitaèní pùsobení na ose y
particle[loop].zi+=1.8f;// Gravitaèní pùsobení na ose z
if (particle[loop].life<0.0f) particle[loop].active=false;
}
}

The particles are from NeHe lesson XX (Particle System). You can find there english comments.

##### Share on other sites
Quote:
 Original post by MiPIt crashed later, it was this font drawing function:

  void Font_drawtext( bool sada, int xpos, int ypos, int size, char *str, ... )  {    glDisable(GL_DEPTH_TEST);							// Disables Depth Testing    char buffer[1024];    int i, L, ascii;    float cx, cy;    memset(buffer, '\0', 1024);  	va_list argptr;  	va_start( argptr, str );  	_vsnprintf( buffer, 1024, str, argptr );  	va_end( argptr );    L = strlen( buffer );    glCallList( fontsavestate );    glTranslatef( (float)xpos, (float)ypos, 0 );    for( i=0; i<L; i++ ) {      ascii = (int)buffer;      if( buffer == '&' && i+1<L )      {        glColor3f(1.0f,0.0f,0.0f);         continue;      }      else {        cx=float(ascii%16)/16.0f;        cy=(float(ascii/16)/16.0f)+((sada)?(0.5):(0));        glBegin( GL_QUADS );        glTexCoord2f( cx,1-cy-0.0625f );        glVertex2i( 0,0 );        glTexCoord2f( cx+0.0625f,1-cy-0.0625f );        glVertex2i( size,0 );        glTexCoord2f( cx+0.0625f,1-cy );        glVertex2i( size,size );        glTexCoord2f( cx,1-cy );        glVertex2i( 0,size );        glEnd();                            // <-- CRASH POINT!        glTranslatef( (float)size, 0, 0 );     }      }    glCallList( fontrestorestate );    glEnable(GL_DEPTH_TEST);							// Enables Depth Testing  }
Quote:
 There was another glEnd before this funcion, but it WASNT in pair with glBegin(GL_QUADS). Maybe the matter is the GL_QUADS.

Please use the 'source' tag to contain your code, otherwise you lose all layout...

Anyway, how big is the string you pass to this function? If it is larger than 1023 bytes, you will corrupt memory. If that's not the problem, try using a debugger, I agree with TomasH that this looks like a memory corruption bug.

Tom

Edit:
And a coding tip: try replacing all hardcoded numbers (like for example the 0.0625f constant for the font quads or the 1024 buffer size) by constant variables. That is both more readable (i.e. if you use proper names of course) and more maintainable, as you need to replace the number in only one location if you wish to change it.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 31
• 16
• 11
• 10
• 12
• ### Forum Statistics

• Total Topics
634116
• Total Posts
3015598
×