Jump to content
  • Advertisement


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


More cellshading trouble

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

If you intended to correct an error in the post then please contact us.

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.


                        // 1. First Texture Unit - texture

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

                        // 2. Second Texture Unit - shader

                        glBindTexture(GL_TEXTURE_1D, shaderTexture[0]);

			// Reset the color to normal again

			glColor3ub(255, 255, 255);

			// Turn off texture mapping and turn on color

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


			// 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

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

						// 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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!