UI 꾸밀 때 색 지정이 어려울 때 참고!

http://davidlab.net/google-design-ko/style/color.html#color-color-schemes


구글 디자인에서 색 조합을 친절하게 알려주고 있다.


여기서 참조하여 색을 지정하면 평균은 갈 것 같다.


예시!



OpenGL - 16개의 움직이는 랜덤 티팟

설명 : 16개의 teapot이 랜던한 색과 방향으로 회전한다.

이번에는 빛을 줘서 그림자 효과도 준다.


생략된 부분은 링크된 부분을 참고하세요(소스동일)

2017/09/12 - [SW/OpenGL] - OpenGL - 사각형 회전, 색바꾸기


선언부

GLUquadricObj *p; void getaxis(int index); double getcolor(); int axi[16]; double colR[16]; double colG[16]; double colB[16]; static float theta[3]; GLdouble angle;


렌더링 함수

void RenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(02.02.000001.00); //view point
 
	glEnable(GL_LIGHTING); //light on
	glEnable(GL_LIGHT0); //0th light
 
	GLfloat position0[4] = { 1001001001 };
	GLfloat ambient0[4] = { 000.6,0 };
	GLfloat diffuse0[4] = { 1.01.001 };
	GLfloat specular0[4] = { 1111 };
 
	glLightfv(GL_LIGHT0GL_POSITIONposition0);
	glLightfv(GL_LIGHT0GL_AMBIENTambient0);
	glLightfv(GL_LIGHT0GL_DIFFUSEdiffuse0);
	glLightfv(GL_LIGHT0GL_SPECULARspecular0);
 
	glColorMaterial(GL_FRONTGL_AMBIENT_AND_DIFFUSE);//light attributes is ambient, diffuse
	glEnable(GL_COLOR_MATERIAL);
 
	//set teapots attributes
	GLfloat mat_specular[4] = { 1,1,1,1 };
	GLfloat mat_shininess = 25.0f;
	glMaterialfv(GL_FRONTGL_SPECULARmat_specular);
	glMaterialf(GL_FRONTGL_SHININESSmat_shininess);
	double xp = -1.6;
	double yp = 2.4;
	int a = 0;
	int b = 0;
 
	for (int i = 0i < 4i++)
	{
		xp = -1.6// teapot move 1.6 with x-axis
		for (int k = 0k < 4k++)
		{
			glPushMatrix();//push
			glTranslatef(xpyp0.0);
 
			if (axi[a] == 0)
				glRotatef(angle100);
			else if (axi[a] == 1)
				glRotatef(angle010);
			else
				glRotatef(angle001);
			glColor3f(colR[b], colG[b], colB[b]);
			glutSolidTeapot(0.2);
			glPopMatrix();//pop
			a++;
			b++;
			xp = xp + 0.8;
		}
		yp = yp - 0.8;
	}
	glutSwapBuffers();
}


랜덤한 방향으로 회전하도록 x,y,z 방향을 제시해주는 함수

/// <summary>
/// set rotate direction random
/// </summary>
/// <param name="index">The index.</param>
void getaxis(int index)
{
	if (index == 0)
		glRotatef(angle100);
	else if (index == 1)
		glRotatef(angle010);
	else
		glRotatef(angle001);
}

주전자가 2도씩 회전함

/// <summary>
/// rotate teapot 2 degree
/// </summary>
/// <param name="value">The value.</param>
void rtimer(int value)
{
	angle += 2;
	glutTimerFunc(30rtimer0);
	glutPostRedisplay();
}


티팟에 줄 랜덤한 색을 제시

/// <summary>
/// Get random colors this instance.
/// </summary>
/// <returns></returns>
double getcolor(void)
{
	double c;
	c = (double)rand() / RAND_MAX;
	return c;
}


색과 방향 초기화

void init(void)
{
	srand(time(NULL));
	theta[0] = 0;
	theta[1] = 0;
	theta[2] = 0;
	for (int t = 0t < 16t++)
	{
		axi[t] = rand() % 3;
		colR[t] = (double)rand() / RAND_MAX;
		colG[t] = (double)rand() / RAND_MAX;
		colB[t] = (double)rand() / RAND_MAX;
	}
}


메인함수

void main(int argccharargv[])
{
	glutInit(&argcargv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(800800);
	glutCreateWindow("Ex4");
	glutDisplayFunc(RenderScene);
	glutReshapeFunc(ChangeSize);
	glutTimerFunc(30rtimer1);
	init();
	SetupRc();
	glutMainLoop();
}


결과


OpenGL - 사각형 회전, 색바꾸기

OpenGL의 기초적인 함수를 사용해본다.


설명

r을 누르면 사각형의 회전방향이 바뀌고, 폼을 클릭하면 사각형의 색이 랜덤으로 바뀐다.


소스설명


키보드 입력등록

void menu(int item)
{
	keyboard((unsigned char)item00);
}


키보드 이벤트 등록

/// <summary>
/// key event registration
/// </summary>
/// <param name="key">The key.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
void keyboard(unsigned char keyint xint y)
{
	switch (key)
	{
	case 'q' | 'Q':
		exit(0); break;
	case VK_ESCAPE:
		exit(0); break;
	case 1:
		exit(0); break;
	case 2:
		exit(0); break;
	case 'R' | 'r'//change rotate direction
		rotate(); break;
	default:
		break;
	}
}


렌더링함수

/// <summary>
/// Renders the scene.
/// </summary>
void RenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT);
 
	float cos_th = cos(theta * 3.14159 / 180.0);
	float sin_th = sin(theta * 3.14159 / 180.0);
 
	glColor3f(abc);
 
	//draw polygon
	glBegin(GL_POLYGON);
	{
		glVertex2f(cos_thsin_th);
		glVertex2f(-sin_thcos_th);
		glVertex2f(-cos_th, -sin_th);
		glVertex2f(sin_th, -cos_th);
	}
	glEnd();
	glutSwapBuffers();
}


초기화함수

void init(void)
{
	mouseLeftDown = false;
 
	point[0][0] = 0;
	point[0][1] = 0;
	point[1][0] = 0;
	point[1][1] = 0;
 
	theta = 0;
}
void SetupRc(void)
{
	glClearColor(1.0f1.0f1.0f1.0f);
}


윈폼의 크기가 변할때를 계산

/// <summary>
/// Changes the size.
/// when form size change
/// </summary>
/// <param name="w">width</param>
/// <param name="h">height</param>
void ChangeSize(int wint h)
{
	GLfloat aspectRatio;
 
	if (h == 0)
		h = 1;
 
	glViewport(00wh);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
 
	aspectRatio = (GLfloat)w / (GLfloat)h;
	if (w <= h)
	{
		gluOrtho2D(-2.02.0, -2.0*(float)h / (float)w2.0*(float)h / (float)w);
	}
	else
	{
		gluOrtho2D(-2.0*(float)w / (float)h2.0*(float)w / (float)h, -2.02.0);
	}
 
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}


사각형 회전을 위한 타이머

void timer(int value)
{
	if (rot)//rotate right
	{
		theta += 2.0;
		if (theta >= 360.0)
			theta -= 360.0;
	}
	else//rotate left
	{
		theta -= 2.0;
		if (theta <= 360.0)
			theta += 360.0;
	}
	glutTimerFunc(1000 / 30timer1);
	glutPostRedisplay();
}
 
void rotate()//exchange rotate direction
{
	if (rot)
		rot = false;
	else
		rot = true;
}


마우스 위치 함수

/// <summary>
/// Mouses the motion.
/// </summary>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
void mouseMotion(int xint y)
{
	if (mouseLeftDown)
	{
		double viewport[4];
		glGetDoublev(GL_VIEWPORTviewport);
 
		point[1][0] = x / (float)viewport[2] * 500;
		point[1][1] = (viewport[3] - y) / (float)viewport[3] * 500;
	}
	glutPostRedisplay();
}


마우스 클릭 이벤트

/// <summary>
/// Mouse click event
/// </summary>
/// <param name="button">The button.</param>
/// <param name="state">button state.</param>
/// <param name="x">mouse's x</param>
/// <param name="y">mouse's y</param>
void mouseButton(int buttonint stateint xint y)
{
	int r;
	if (button == GLUT_LEFT_BUTTON//mouse left button
	{
		if (state == GLUT_DOWN)//clicked
		{
			if (!mouseLeftDown)
			{
			}
			r = rand() % 3;
			if (r == 0)
			{
				if (a == 0)
					a = 1;
				else
					a = 0;
				glutPostRedisplay();
			}
			else if (r == 1)
			{
				if (b == 0)
					b = 1;
				else
					b = 0;
				glutPostRedisplay();
			}
			else
			{
				if (c == 0)
					c = 1;
				else
					c = 0;
				glutPostRedisplay();
			}
		}
		else if (state == GLUT_UP)//unclicked
		{
			if (mouseLeftDown)
				mouseLeftDown = false;
		}
	}
}


가장 중요한 메인함수

/// <summary>
/// Mains the specified argc.
/// </summary>
/// <param name="argc">The argc.</param>
/// <param name="argv">The argv.</param>
void main(int argccharargv[])
{
	glutInit(&argcargv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
	glutInitWindowSize(600600);//form's size
	glutCreateWindow("ex1");//form's name
	glutDisplayFunc(RenderScene);//display function registration
	glutReshapeFunc(ChangeSize);//reshape function registration
 
	glutMouseFunc(mouseButton);//mouse click function
	glutMotionFunc(mouseMotion);//mouse move function
	glutTimerFunc(1000 / 30timer1); //timer
	glutKeyboardFunc(keyboard);//keyboard functiom
 
	glutCreateMenu(menu);//when right mouse clicked
	glutAddMenuEntry("1"1);
	glutAddMenuEntry("2"2);
	glutAttachMenu(GLUT_RIGHT_BUTTON);
 
	init();
	SetupRc();
	srand(time(NULL));
	glutMainLoop();
}




'색'에 해당되는 글 3건

1 →