Archived

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

Ruudje

More cellshading trouble

Recommended Posts

I have reimplemented my cellshading, because my previous model-class had some serious problems. With this new one, I get cellshading results that are confusing me. - If my model has NO texture, I get a shade-effect, but it looks wrong to me. Even more confusing is that the effect is threated as a normal texture, as rotating the object will rotate the shadow-effect as well. This shouldnt be, as now its just a normal texture. - If my model DOES have a texture, I dont get a shade-effect, but the texture in its whole gets darker, as if the entire object is shaded with the darkest shade-value... Heres my entire rendering-code for the model, texture and shader:
        float TmpShade; // Temporary shader value


        CMatrix TmpMatrix;
        CVector3 TmpVector, TmpNormal;

        glGetFloatv (GL_MODELVIEW_MATRIX, TmpMatrix.Data);	// Get the generated matrix


	// Since we know how many objects our model has, go through each of them.

	for(int i = 0; i < g_3DModel.numOfObjects; i++)
	{
		// Make sure we have valid objects just in case. (size() is in the vector class)

		if(g_3DModel.pObject.size() <= 0) break;

		// Get the current object that we are displaying

		t3DObject *pObject = &g_3DModel.pObject[i];

		// Check to see if this object has a texture map, if so bind the texture to it.

		if(pObject->bHasTexture)
                {

                        // 1. First Texture Unit - texture

                        glActiveTextureARB(GL_TEXTURE0_ARB);
                        glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
                        glEnable (GL_TEXTURE_2D);

                        // 2. Second Texture Unit - shader

                        glActiveTextureARB(GL_TEXTURE1_ARB);
                        glBindTexture(GL_TEXTURE_1D, shaderTexture[0]);
                        glEnable(GL_TEXTURE_1D);

			// Reset the color to normal again

			glColor3ub(255, 255, 255);
		}

                else
                {
			// Turn off texture mapping and turn on color

                        glActiveTextureARB(GL_TEXTURE0_ARB);
                        glBindTexture(GL_TEXTURE_2D, g_Texture[pObject->materialID]);
                        glDisable (GL_TEXTURE_2D);

                        glEnable(GL_TEXTURE_1D);

			// Reset the color to normal again

			glColor3ub(255, 255, 255);
		}

		glBegin(GL_TRIANGLES);					// Begin drawing with our selected mode (triangles or lines)


			// Go through all of the faces (polygons) of the object and draw them

			for(int j = 0; j < pObject->numOfFaces; j++)
			{
				// Go through each corner of the triangle and draw it.

				for(int whichVertex = 0; whichVertex < 3; whichVertex++)
				{
					// Get the index for each point of the face

					int index = pObject->pFaces[j].vertIndex[whichVertex];

                                        // Calculate the shader effect

                                        TmpNormal.x = pObject->pNormals[ index ].x;
                                        TmpNormal.y = pObject->pNormals[ index ].y;
                                        TmpNormal.z = pObject->pNormals[ index ].z;

                                        TmpVector.x = (TmpMatrix.Data[0] * TmpNormal.x) + (TmpMatrix.Data[4] * TmpNormal.y) + (TmpMatrix.Data[8] * TmpNormal.z);
                                        TmpVector.x = (TmpMatrix.Data[1] * TmpNormal.x) + (TmpMatrix.Data[5] * TmpNormal.y) + (TmpMatrix.Data[9] * TmpNormal.z);
                                        TmpVector.x = (TmpMatrix.Data[2] * TmpNormal.x) + (TmpMatrix.Data[6] * TmpNormal.y) + (TmpMatrix.Data[10] * TmpNormal.z);

                                        float M = sqrt(TmpVector.x * TmpVector.x + TmpVector.y * TmpVector.y + TmpVector.z * TmpVector.z);

                                        if(M != 0)
                                        {
                                                TmpVector.x /= M;
                                                TmpVector.y /= M;
                                                TmpVector.z /= M;
                                        }

                                        TmpShade = TmpVector.x * LightAngle.x + TmpVector.y * LightAngle.y + TmpVector.z * LightAngle.z;

			        	if (TmpShade < 0.0f)
				        	TmpShade = 0.0f;			// Clamp the value to 0 if negative


                                        // If the object has a texture pass its coordinates

                                        if(pObject->pTexVerts)
                                        {
                                                glMultiTexCoord2fARB(GL_TEXTURE0_ARB, pObject->pTexVerts[ index ].x, pObject->pTexVerts[ index ].y);
                                        }

                                        else
                                        {
						// Make sure there is a valid material/color assigned to this object.

						// You should always at least assign a material color to an object,

						// but just in case we want to check the size of the material list.

						// if the size is at least one, and the material ID != -1,

						// then we have a valid material.

						if(g_3DModel.pMaterials.size() && pObject->materialID >= 0)
						{
							// Get and set the color that the object is, since it must not have a texture

							BYTE *pColor = g_3DModel.pMaterials[pObject->materialID].color;

							// Assign the current color to this model

							glColor3ub(pColor[0], pColor[1], pColor[2]);
						}
					}

                                        // Set the shader

                                        glMultiTexCoord1fARB (GL_TEXTURE1_ARB, TmpShade);

					// Pass in the current vertex of the object (Corner of current face)

					glVertex3f(pObject->pVerts[ index ].x, pObject->pVerts[ index ].y, pObject->pVerts[ index ].z);
				}
			}

		glEnd();								// End the drawing

	}
Any help would be greatly appreciated

Share this post


Link to post
Share on other sites