glut Issues, final word, i guess.

Ok, I think the problem is in one of my libraries. The crash thing when away when i changed one bit of code, then when i added a brief glBegin()/glEnd() section, it started doing it again, in a completely different location (which is also pretty much completely unrelated to the section i’d changed). So i don’t think its my fault, i think i just have a problem elsewhere.

Thanks anyhow.

You know it is hard to say without seeing the code…

Originally posted by mhm27x5:
[b]Ok, I think the problem is in one of my libraries. The crash thing when away when i changed one bit of code, then when i added a brief glBegin()/glEnd() section, it started doing it again, in a completely different location (which is also pretty much completely unrelated to the section i’d changed). So i don’t think its my fault, i think i just have a problem elsewhere.

Thanks anyhow.[/b]

This is the code that works:

#include <stdio.h>
#include <glut.h>
#include <glTexFont.h>

int winW = 800;
int winH = 600;
float rot[3] = {0,0,0};
char *glExten;

float cube[3] =
{
{-10,-10,10},
{10,-10,10},
{10,10,10},
{-10,10,10},
{-10,-10,-10},
{10,-10,-10},
{10,10,-10},
{-10,10,-10},
{-10,-10,-10},
{-10,-10,10},
{-10,10,10},
{-10,10,-10},
{10,-10,-10},
{10,-10,10},
{10,10,10},
{10,10,-10},
{-10,10,10},
{10,10,10},
{10,10,-10},
{-10,10,-10},
{-10,-10,10},
{10,-10,10},
{10,-10,-10},
{-10,-10,-10}
};
float colors[3] =
{
{1,0,0},
{0,1,0},
{0,0,1},
{1,1,0},
{1,0,1},
{0,1,1}
};

void drawTest (void)
{
int i;

glBegin (GL_QUADS);
for (i = 0; i < 24; i ++)
{
glColor3fv (colors[(i % 7)]);
glVertex3fv (cube[i]);
}
glEnd ();
}
void glutDisplay (void)
{
int size;
float pink = {1, 0, 0.5};

glClearColor (0,0,0,1);

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
glTranslatef (0, 0, -50);
glRotatef (rot[0], 1.0, 0.0, 0.0);
glRotatef (rot[1], 0.0, 1.0, 0.0);
glRotatef (rot[2], 0.0, 0.0, 1.0);

drawTest ();

/* There is an unreasonable amount of text below */
fontSize (10);
fontShadow ();
fontShadowColor (0.5, 0.5, 0.5);
fontColor (1,0,0);
fontDrawString (0,winH - 10,"OpenGL Extensions:");

fontSize (10);
fontColor (1,1,1);
fontRegion (10, winH - 10, 10 * 50, 10 * 10);
// Please note that position is ignored with a region established
fontDrawString (-1,-1,glExten);

glutSwapBuffers();

}
void glutIdle (void)
{
int i;

rot[0] += 0.5;
rot[1] -= 0.25;
rot[2] += 1.5;

for (i = 0; i < 3; i ++)
if (rot[i] > 360 | | rot[i] < -360)
rot[i] = 0;
//glutPostRedisplay ();
}

void glInit (void)
{
glEnable (GL_DEPTH_TEST);
glExten = (char ) glGetString(GL_EXTENSIONS);
}
void glutVisible (int state)
{
if (state == GLUT_VISIBLE)
glutIdleFunc (glutIdle);
}
void glutKeyboard (unsigned char key, int x, int y)
{
switch (key)
{
case ‘q’:
case ‘Q’:
case 27:
exit (1);
break;
}
}
void main (int argc, char
argv)
{
glutInit(&argc,argv);
glutGameModeString(“800x600:16@60”);
glutEnterGameMode();
//glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(winW,winH);
//glutCreateWindow(“Font Test”);
glutDisplayFunc(glutDisplay);
//glutReshapeFunc(glutResize);
glutIdleFunc(glutIdle);
glutVisibilityFunc(glutVisible);
glutKeyboardFunc(glutKeyboard);
glInit();

// gray scale font
fontLoad (“fontGray.tga”);
glutFullScreen();
glutMainLoop();
}

If i replace the call to drawTest() with this, it then crashes with that error.

glBegin(GL_TRIANGLE_STRIP);
for (int i=0;i<15;i++) {
	glColor3fv(colors[i%7]);
    glVertex3fv(cube[i]);
}
glEnd();

This isn’t the only place where it will crash with that for no reason that I can see, but this is just the latest one. I’m Thinking that the library could be buggered up.

What is the error you get when the program crash’s?

the only diffrence is that you define ‘i’ twice in the code that does not work.
Try removeing the for( int i=1…) to for( i=0…)

The other odd thing is that you use the remander ‘%’ for the division of i and 7, an array must have an int value. I am not sure you are getting correct results because of it, maybe pointing you out of your array’s memory space. This would also cause a program crash.

int i;

glBegin(GL_TRIANGLE_STRIP);    for (int i=0;i&lt;15;i++) {    	glColor3fv(colors[i%7]);        glVertex3fv(cube[i]);    }    glEnd();

[QUOTE]Originally posted by mhm27x5:
[b]This is the code that works:

[This message has been edited by nexusone (edited 04-12-2002).]

[This message has been edited by nexusone (edited 04-12-2002).]

Here’s a problem I see.

You have 6 colors in your colors array.

float colors[3] ={
{1,0,0}, {0,1,0}, {0,0,1}, {1,1,0}, {1,0,1}, {0,1,1}};

When you try and use this line, you will be accessing colors[6] when i is 6. That goes beyond the bounds of the array, and thus you crash. (Your array indices go from 0-5, not 0-6. Change the 7 to a 6 and you should be ok.)

glColor3fv(colors[i%7]);

When you have an app that crashes, it is ALMOST ALWAYS from touching memory that you have no right touching. In this case, going beyond the bound of an arry.

You are quite right sir, there are only 6 colors. I hadn’t even noticed :stuck_out_tongue: However, changing that doesn’t prevent the error from happening.

The reason that I had put i % 7 there is because i pretty much just copied that code from within the drawTest() function, which used i % 7 to get its colors. I didn’t actually count the # of colors :stuck_out_tongue:

Hmm… if the error is still occuring, I’d suggest trying to step through the program to find out which line exactly is causing the problem. You shouldn’t be going out of the bounds of the cube array because that has a lot more than 15 entries.

Yeah… and honestly, I really don’t think that’s the problem - because i’ve typed in code from the Red Book word for word, and it gives me that error anyhow. I think that there must just be something wrong with my dll/lib files.

I’ve tried re-installing them, but i still get the errors though.