Advertisement

GL_TRIANGLES and glMaterial???

Started by February 11, 2003 04:25 PM
8 comments, last by variant 22 years ago
I am working on a game using a 2D array of doubles to create a landscape. I just run it through a couple for loops using triangles. That works great. But when I began to specify the colors for the vertices based on their height using glMaterialfv, the triangles rendered strangely. Instead of coloring each vertex, it appeared to be coloring an entire side. I ultimately solved the problem by switching to glColorMaterial and glColor, but I am curious about the odd behavior. Has anyone seen this, or know something about glMaterial that I don''t?
Thats what glMaterialfv does. It changes surface properties not vertex colors.
Advertisement
If you''re calling glMaterial between glBegin and glEnd, I think it should work (unless glShadeModel is GL_FLAT of course).

Though, if I ever had to colorize a heightmap depending on the height I''d rather use a 1D texture with automatic texture coordinate generation. Just my 2c.
Hmmm....A lot of my confusion comes from the fact that I''ve run the program on two seperate machines, one with ME and Windows basic OpenGL implementation, and another with XP and an Intel Extreme card. The ME machine doesn''t have the problem, but the XP machine does. Does it sound like an implementation problem? MSDN says that you can call glMaterial between glBegin and glEnd, though they don''t say what will happen specifically.
This page does mention that glColorMaterial is "preferred" but they don''t mention that the result would be different :
quote:

The material parameters can be updated at any time. In
particular, glMaterial can be called between a call to
glBegin and the corresponding call to glEnd. If only a
single material parameter is to be changed per vertex,
however, glColorMaterial is preferred over glMaterial (see
glColorMaterial).


So, I think calling glColorMaterial or glMaterial is just a matter of performance, but the final image should be the same (assuming you''re calling glMaterial or glColor before the corresponding glVertex)
The MSDN is what prompted me to change to glColor, and that ultimately solved the problem, but you''re right, vincoof, in saying that it should have the same result, but instead it appears that of the three vertices, the first paints fine, but the next two are the same color no matter what, so I get a triangle with one whole side with the same color.
Advertisement
Could you please post a sample of glMaterial usage inside glBegin/glEnd ? (copy''n''paste from your source code would be great because it would reapeat mistakes if you are doing any)
Here's most of the source, without the extraneous details.


    glMaterialfv(GL_FRONT,GL_DIFFUSE,white);	//glColorMaterial(GL_FRONT,GL_DIFFUSE);	//glEnable(GL_COLOR_MATERIAL);	glBegin(GL_TRIANGLES);	for(i=0;i<(p_width-1);i++){		for(j=0;j<(p_height-1);j++){			point0[0] = (GLdouble)i * GRID_SIZE; 			point0[1] = board[i * p_height + j]* GRID_SIZE; 			point0[2] = (GLdouble)j * GRID_SIZE;			memcpy(&(normal0[0]),&(p_normals[(i * p_height + j) * 3]),sizeof(GLdouble) * 3);						point1[0] = (GLdouble)i * GRID_SIZE; 			point1[1] = board[i * p_height + j+1]* GRID_SIZE; 			point1[2] = (GLdouble)(j+1) * GRID_SIZE;			memcpy(&(normal1[0]),&(p_normals[(i * p_height + (j+1)) * 3]),sizeof(GLdouble) * 3);						point2[0] = (GLdouble)(i+1) * GRID_SIZE; 			point2[1] = board[(i+1) * p_height + j]* GRID_SIZE; 			point2[2] = (GLdouble)j * GRID_SIZE;			memcpy(&(normal2[0]),&(p_normals[((i+1) * p_height + j) * 3]),sizeof(GLdouble) * 3);						getColor(color,i,j);			//glColor4fv(color);			glMaterialfv(GL_FRONT,GL_DIFFUSE,color);			glNormal3dv(normal0);			glVertex3dv(point0);			getColor(color,i,j+1);			//glColor4fv(color);			glMaterialfv(GL_FRONT,GL_DIFFUSE,color);			glNormal3dv(normal1);			glVertex3dv(point1);			getColor(color,i+1,j);			//glColor4fv(color);			glMaterialfv(GL_FRONT,GL_DIFFUSE,color);			glNormal3dv(normal2);			glVertex3dv(point2);			/******************Second Triangle*****************/		        getColor(color,i+1,j);			//glColor4fv(color);			glMaterialfv(GL_FRONT,GL_DIFFUSE,color);			glNormal3dv(normal2);			glVertex3dv(point2);			getColor(color,i,j+1);			//glColor4fv(color);			glMaterialfv(GL_FRONT,GL_DIFFUSE,color);			glNormal3dv(normal1);			glVertex3dv(point1);			getColor(color,i+1,j+1);			//glColor4fv(color);			glMaterialfv(GL_FRONT,GL_DIFFUSE,color);			point0[0] = (GLdouble)(i+1) * GRID_SIZE; 			point0[1] = board[(i+1) * p_height + j+1]* GRID_SIZE; 			point0[2] = (GLdouble)(j+1) * GRID_SIZE;			memcpy(&(normal0[0]),&(p_normals[((i+1) * p_height + (j+1)) * 3]),sizeof(GLdouble) * 3);						glNormal3dv(normal0);			glVertex3dv(point0);		}	}	glEnd();	//glDisable(GL_COLOR_MATERIAL);    


I've commented out the glColor method, which does seem to work.

I know for a fact that the correct colors are being retrieved, and the grid gets displayed correctly, excepting the colors.

[edited by - variant on February 12, 2003 3:01:42 PM]
I really don''t see any problem. It may be a driver issue.
Thanks for looking into it!!

This topic is closed to new replies.

Advertisement