#### Archived

This topic is now archived and is closed to further replies.

# Light + MSVC++ = broke, Light + MSVC++ + Debugger = working?!

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

## Recommended Posts

I've never, ever seen something similar to this before. I'm just trying to render a heightmap inside of a skybox. First, before anyone asks, yes, they both work fine with glEnable(GL_LIGHTING) commented out. Whenever I include the following (the rendering part of my heightmap) I get a completely black screen:
	glEnable(GL_LIGHTING);
glBegin(GL_TRIANGLE_STRIP);

for(int j = 0; j < height - scale; j+=scale){  // ROWS = J
glTexCoord2f(0.0f, j/float(height));
glNormal3f(normals[(j+scale) * width].x, normals[(j+scale) * width].y, normals[(j+scale) * width].z);
glVertex3f(verts[(j+scale) * width].x, verts[(j+scale) * width].y, verts[(j+scale) * width].z);
glNormal3f(normals[(j+scale) * width].x, normals[(j+scale) * width].y, normals[(j+scale) * width].z);
glVertex3f(verts[(j+scale) * width].x, verts[(j+scale) * width].y, verts[(j+scale) * width].z);

for(int i = 0; i < width - scale; i+=scale) { // COLS = I
glTexCoord2f((i+scale)/float(width), (j)/float(height));
glNormal3f(normals[(j) * width + i].x, normals[(j) * width + i].y, normals[(j) * width + i].z);
glVertex3f(verts[(j) * width + i].x, verts[(j) * width + i].y, verts[(j) * width + i].z);

glTexCoord2f((i+scale)/float(width), (j+scale)/float(height));
glNormal3f(normals[(j+scale) * width + i].x, normals[(j+scale) * width + i].y, normals[(j+scale) * width + i].z);
glVertex3f(verts[(j+scale) * width + i].x, verts[(j+scale) * width + i].y, verts[(j+scale) * width + i].z);
}
glTexCoord2f(1.0f, (j)/float(height));

int index = (j) * width - scale;
glNormal3f(normals[index].x, normals[index].y, normals[index].z);
glVertex3f(verts[index].x, verts[index].y, verts[index].z);
glNormal3f(normals[index].x, normals[index].y, normals[index].z);
glVertex3f(verts[index].x, verts[index].y, verts[index].z);
}

glEnd();

glDisable(GL_LIGHTING);

My order of rendering is: Skybox, Heightmap, Text. Whenever I comment out the above code, it works perfectly fine... the skybox and text are not affected by lighting. Now here's the really wierd part: if I compile the whole thing, nothing commented, it runs as a black screen. However, if I hit f-5 to go into the debugger - without changing anything - it works fine. I've never seen anything like this in my life. Anyone have any ideas? ___ So many little things to do, so much time wasted... [edited by - Skyfire360 on June 4, 2004 1:23:37 PM]

##### Share on other sites
You probably assume some variables are initialized 0.0 which is often the case in debug mode but not when you are in release mode or running with debug disabled, be sure to set all variables to zero when you declare them.

EDIT: look for variables you use in loops or light position/diffuse color.

[edited by - Tree Penguin on June 4, 2004 1:37:57 PM]

##### Share on other sites
Thanks, I''m looking into that now.

Interesting note: If I just draw the "basic triangle" (GL_TRI, the one from NEHE #1) there are no problems with the lighting.

##### Share on other sites
Ok, one thing i don't really think is right is the placement of the glBegin call, try placing it inside the 1st (j) loop.
The second thing i don't really understand is that you call the first and last vertex twice, when i have that (i) loop i draw 2 vertices at the time and none before or after, i don't get that code in your example.

Another thing i don't get is that you test (i) to be less than width-scale which results in two triangles not being drawn (because it stops one vertex in the width direction too early).

[edited by - Tree Penguin on June 5, 2004 5:28:50 AM]

##### Share on other sites
lol. I think MS's debugger has a DWIM somewhere in there (do what I mean). I've often had troubles like this. If lighting's the problem, make sure you:

glEnable (GL_LIGHTING);
glEnable (GL_LIGHTx);

x is the light you're using. Make sure it's set up properly too (i.e. colour and direction).

just out of interest, do you mean that when you compile in debug mode it works, or when you compile in debug mode AND run the debugger? Have you tried it in Release mode?

a rather more drastic measure that seemed to clear up these random bugs was to delete all of VC's project files and create a new project and import all your original files.

##### Share on other sites
Thanks for the replies guys, was out of town for a few days and couldn't get access to the internet.

Tree_Penguin:

The placement of the begin/end statement was that way on purpose: instead of using multiple triangle strips to render it, I decided to use a single strip with degenerate triangles - which explains the repeated verticies (to kill the triangle and "reposition" back to the beginning all in the same strip).

Mental:

I ran the program in debug mode and the program drew nothing. Then I ran the program in debug with the debugger and it drew everything correctly. Apparently what was happening is that when you use MSVC++'s debugger, it allocates a whole lot of room for your program (zeros memory) to catch it if it decides to crash. However, in straight debug mode it doesn't allocate any extra space, which means when one of my verticies that I was using happened to be undefined, instead of going to zero it was shot out to some far place in memory and read a very wrong number. ::shrugs:: That's what I get for being a sloppy coder... I think faster than I type!

Anyway, I'm now using vertex arrays and it's working just great. I've got texture and vertex working, and now I'm going to try normal. However, I've got a slight problem: I'm not sure how to calculate the per-vertex normals! If I have a point, say point 5 below...
 1-----2-----3| /| /|| / | / || / | / || / | / ||/ |/ |4-----5-----6| /| /|| / | / || / | / || / | / ||/ |/ |7-----8-----9

How would I go about calculating the normal? Would I average...

1) the 6 normals gotten from 452,253,356,658,857 and 754
2) the 4 normals gotten from 256,658,854, and 452
3) the 8 normals gotten from 451,152,253,356,659,958,857, and 754

Or should I go about it another way entirely?

Thanks again for the responses (hope the ascii works...

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 10
• 9
• 13
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007289

×