Color And Shading

Introduction

Color and ShadingIn the previous tutorial, you learnt how to draw primitives. These primitives were a little dull as they were all white. This tutorial will show you how to add color and shading to your primitives.

All colors in OpenGL are represented by 4 values. The first 3 values are red, green and blue intensities. The last value is the alpha. This specifies how transparent a color is. This will be dealt with in greater detail in a future tutorial.

Recall that we said there were a number of arrays that can be used. One of these is thecolor array.

Contents of main.cpp :


We will be drawing one triangle in this tutorial. The 3 points of the triangle are given below.

GLfloat triangle[] = {
	0.25f, 0.25f, 0.0f,
	0.75f, 0.25f, 0.0f,
	0.25f, 0.75f, 0.0f
};

Below, we create the color array. We want to give each vertex a different color. These colors are specified in groups of 4 given below. The first color is red, the second is green and the third is blue.

GLfloat colors[] = {
	1.0f, 0.0f, 0.0f, 1.0f,
	0.0f, 1.0f, 0.0f, 1.0f,
	0.0f, 0.0f, 1.0f, 1.0f
};

shaded boolean variable is created to keep track if the shape is shaded or not. We will be using this to switch between a shaded and a non-shaded shape.

bool shaded = false;

Once again we setup an orthographic view.

void init()
{
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrthof(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);

The vertex array is setup in the same way as the previous tutorial.

	glVertexPointer(3, GL_FLOAT, 0, triangle);

As we said above, there is also a color array at our disposal. The glColorPointer is used to setup the color array. This works in the exact same way as the glVertexPointer function. We pass a 4 as the first parameter to indicate that there are 4 floats per vertex.

	glColorPointer(4, GL_FLOAT, 0, colors);

We need to enable both the vertex and color arrays.

	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_COLOR_ARRAY);

We are adding both color and shading to our primitives. There are two types of shading. This is specified using theglShadeModel function. This function takes one parameter specifying what type of shading will be used. The two values are GL_FLAT and GL_SMOOTHGL_SMOOTH is set by default.

GL_FLAT causes the color of the shape to have a single color, being the last color that was specified.

GL_SMOOTH enabled smooth shading. This takes the color of every vertex and colors the primitive by interpolating the color values of all pixels in the primitive. You will see this more clearly when running the program.

We will first draw the shape using flat shading.

	glShadeModel(GL_FLAT);
}

The display method remains the same. The only difference is seen in the glDrawArrays function call. We are now drawing primitives using the GL_TRIANGLES flag and 3 vertices need to be read. This will create a single triangle.

void display()
{
   glClear(GL_COLOR_BUFFER_BIT);

   glDrawArrays(GL_TRIANGLES, 0, 3);

   glFlush();
   glutSwapBuffers();
}

Our menu function remains the same except we now allow the shading to be toggled. Also, remember to add another option to the menu in the main function i.e. (glutAddMenuEntry("Shading", 2);)

void menu(int entry)
{
	switch(entry)
	{
	case 1 : exit(0); break;

Here we change the shading mode, alternating between GL_SMOOTH and GL_FLAT.

	case 2 :
		shaded = !shaded;
		glShadeModel(shaded ? GL_SMOOTH : GL_FLAT);
	}
}

Once the change has been made, we need to redraw the screen and show a new frame to reflect the change made in the shading mode. Redrawing the window is done by calling the glutPostRedisplay function. If you are using the UG library, you will need to use the ugPostRedisplay function. The UG function takes one parameter, being the handle to the window that you want refreshed, while the GLUT|ES function does not need any parameters.

		glutPostRedisplay();
		break;
	}
}

You should now be able to make your programs more colorful by adding color and shading to your primitives. Upon executing the program, you will be presented with a blue triangle. If you select the shading menu option, you will change the shading mode.

Please let me know of any comments you may have : Contact Me

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: color shading的校正方法包括以下几种: 1. 使用校正板:将校正板放在拍摄场景中,拍摄一张照片,然后使用软件将校正板的颜色值作为参考,对其他照片进行校正。 2. 使用白平衡:在拍摄时使用白平衡功能,将白色或灰色物体作为参考,调整色温和色彩平衡。 3. 使用色彩校正工具:使用专业的色彩校正工具,对照片进行校正,调整色彩平衡和色温。 4. 手动调整:根据经验和感觉,手动调整照片的色彩平衡和色温,使其更加自然和真实。 以上是color shading的校正方法,根据实际情况选择合适的方法进行校正。 ### 回答2: 所谓的color shading是指由于相机模组中传感器区域之间响应差异等因素导致的图像颜色渐变现象。在许多照片中,会出现颜色在照片周围的边缘降低的情况,使其外观不真实,色调不鲜明。为了消除这种不必要的鹅卵石细节,许多照片编辑软件都提供校正方法。 基于计算机图像处理的技术,颜色渐变的矫正可以通过为每个像素计算校正因子来实现。这个校正因子就是每个像素的颜色;多边形拟合算法可以随着图像的变化来调整位置,并修正前后相邻像素之间的显著颜色差异。这是一项技术密集型工作,通常需要密集的算法和复杂的编程来完成。 另外,一些更高端的图像编辑软件还提供了高级的颜色校正选项。这些选项可以更精细地控制渐变现象,对图像进行更精确的调整。例如, 像曲线调整器和色彩校正铃将直接修改像素颜色值,以逐渐改变整个图像的颜色而不影响对比度。 总体来说,调整方法的成功取决于所需的具体校正水平和是校正单个照片还是一整个图像序列。对于较小的调整,一般可以使用基本校正方法,但对于大规模照片处理项目,可能需要自定义算法和流程来确保结果的准确性。无论是使用基本或高级方法,都需要仔细的图像分析和处理,以确保校正效果良好。 ### 回答3: Color shading是指图像中不同位置和角度的像素所呈现出的颜色不均匀现象。它是由于相机镜头或传感器不完美造成的,严重时会影响图像质量。为了解决这个问题,需要对图像进行色彩校正。以下是一些常用的彩色遮挡矫正方法: 1.基于灰度卡比较法(Gray-Reference Comparison Method) 该方法从图像中选取灰度卡,将图像中不同位置的灰度值与灰度卡的灰度值进行比较,计算出颜色偏差,进而校正颜色。 2.基于灰度反转法(Gray Scale Inversion Method) 该方法将灰度图像取反,使颜色区分更明显,然后对反转后的图像进行颜色校正。 3.基于光谱反演法(Spectral Inversion Method) 该方法基于调整不同颜色的光谱分布比例,使得图像中不同位置的颜色更加均匀。 4.基于模型预测法(Model-Based Prediction Method) 该方法通过建立数学模型来预测图像中不同位置的颜色,从而校正颜色偏差。 这些校正方法各有优缺点,根据图像特性和应用需求选取合适的方法来进行彩色遮挡矫正。而在实际应用中,通常需要将多种方法相结合,以达到更好的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值