Category Archives: OpenGL
OpenGL Shadow Reflection
Planar Shadow
Projection Shadow
// create a shadow matrix that will project the desired shadow
void buildShadowMatrix(GLfloat shadowMat[16], GLfloat plane[4], GLfloat lightpos[4])
{
GLfloat dot; // dot product of light position and ground plane normal
dot = plane[0]*lightpos[0] + plane[1]*lightpos[1] +
plane[2]*lightpos[2] + plane[3]*lightpos[3];
shadowMat[0] = dot – lightpos[0] * plane[0];
shadowMat[1] = – lightpos[0] * plane[1];
shadowMat[2] = – lightpos[0] * plane[2];
shadowMat[3] = – lightpos[0] * plane[3];
shadowMat[4] = – lightpos[1] * plane[0];
shadowMat[5] = dot – lightpos[1] * plane[1];
shadowMat[6] = – lightpos[1] * plane[2];
shadowMat[7] = – lightpos[1] * plane[3];
shadowMat[8] = – lightpos[2] * plane[0];
shadowMat[9] = – lightpos[2] * plane[1];
shadowMat[10] = dot – lightpos[2] * plane[2];
shadowMat[11] = – lightpos[2] * plane[3];
shadowMat[12] = – lightpos[3] * plane[0];
shadowMat[13] = – lightpos[3] * plane[1];
shadowMat[14] = – lightpos[3] * plane[2];
shadowMat[15] = dot – lightpos[3] * plane[3];
}
Reflection
// create a reflection matrix that will project the desired shadow
void buildReflectionMatrix(GLfloat reflectionMat[16], GLfloat plane[4])
{
reflectionMat[0] = 1 – 2 * plane[0] * plane[0];
reflectionMat[1] = – 2 * plane[0] * plane[1];
reflectionMat[2] = – 2 * plane[0] * plane[2];
reflectionMat[3] = – 2 * plane[0] * plane[3];
reflectionMat[4] = – 2 * plane[1] * plane[0];
reflectionMat[5] = 1 – 2 * plane[1] * plane[1];
reflectionMat[6] = – 2 * plane[1] * plane[2];
reflectionMat[7] = – 2 * plane[1] * plane[3];
reflectionMat[8] = – 2 * plane[2] * plane[0];
reflectionMat[9] = – 2 * plane[2] * plane[1];
reflectionMat[10] = 1 – 2 * plane[2] * plane[2];
reflectionMat[11] = – 2 * plane[2] * plane[3];
reflectionMat[12] = 0.0;
reflectionMat[13] = 0.0;
reflectionMat[14] = 0.0;
reflectionMat[15] = 1.0;
}
http://dis.dankook.ac.kr/lectures/cg11/entry/OpenGL-Shadow-Reflection
OpenGL Camera
town
-LEFT/RIGHT/UP/DOWN – x축/z축으로 카메라의 위치이동
glLoadIdentity();
gluLookAt(cameraPos[0], cameraPos[1]+2, cameraPos[2]+25,
cameraPos[0], cameraPos[1], cameraPos[2],
0, 1, 0);
drawScene();
uptown
– LEFT/RIGHT/UP/DOWN 키와 LEFT/MIDDLE/RIGHT 마우스버튼으로 카메라이동
LEFT/RIGHT/UP/DOWN – y축/x축으로 물체를 회전시킴
LEFT MOUSEBUTTON MOTION – x축과 y축으로 물체를 회전시킴
MIDDLE MOUSEBUTTON MOTION – x축과 y축으로 물체를 이동시킴
RIGHT MOUSEBUTTON MOTION – x축과 z축으로 물체를 이동시킴
glLoadIdentity();
glTranslatef(0, 0, -viewDistance);
glTranslatef(viewTransX, viewTransY, 0);
glRotatef(viewRotX, 1.0, 0.0, 0.0);
glRotatef(viewRotY, 0.0, 1.0, 0.0);
drawScene();
navtown
– camera class를 사용하여 x/y/x축 카메라의 위치이동과 x/y/x축 카메라의 방향이동
F1&F2 – x축 카메라 위치이동
F3&F4 – y축 카메라 위치이동
F5&F6 – z축 카메라 위치이동
F7&F8 – x축 카메라 방향이동
F9&F10 – y축 카메라 방향이동
HOME&END – z축 카메라 방향이동
camera1.strafe(0.5);
camera1.fly(0.5); camera1.walk(0.5);
camera1.yaw(2.5);
camera1.pitch(2.5);
camera1.roll(2.5);
camera1.apply();
drawScene();
OpenGL Projection Matrix
OpenGL Transformation
Object Coordinates
Eye Coordinates
Clip Coordinates
Normalized Device Coordinates (NDC)
Window Coordinates (Screen Coordinates)
OpenGL Transformation
OpenGL Transformation
http://dis.dankook.ac.kr/lectures/cg08/entry/transformation-예제
OpenGL Cube Transformation
http://dis.dankook.ac.kr/lectures/cg08/entry/cube3d-예제
OpenGL Transformation
// OpenGL 행렬는 column-order이고, 벡터와 행렬의 곱은 Post-multiply 방식 사용
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 0.0, 1.0);
glutWireCube(1);
glPushMatrix();
glRotatef(45.0, 0.0, 0.0, 1.0);
glTranslatef(3.0, 0.0, 0.0);
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
// p’= T R p (green) => rotate, and then translate
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
glColor3f(0.0, 1.0, 0.0);
glutWireCube(1);
glPopMatrix();
// p’= T R S p (blue) => scale, and then rotate, and then translate
glPushMatrix();
glTranslatef(3.0, 0.0, 0.0);
glRotatef(45.0, 0.0, 0.0, 1.0);
glScalef(0.5, 0.7, 1.0);
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1);
glPopMatrix();
////////////////////////////////////////////
// USING MATRIX
///////////////////////////////////////////
matrix4x4 M1, M2, M3, R, T, S;
R.rotate(45.0, ‘z’);
T.translate(vector3(1.5, 0.0, 0.0));
S.scale(vector3(0.5, 0.7, 1.0));
// p’= R T p (red) => translate, and then rotate
M1 = R * T;
glPushMatrix();
glMultMatrixf( M1 );
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
// p’= T R p (green) => rotate, and then translate
M2 = T * R;
glPushMatrix();
glMultMatrixf( M2 );
glColor3f(0.0, 1.0, 0.0);
glutWireCube(1);
glPopMatrix();
// p’= T R S p (blue) => scale, and then rotate, and then translate
M3 = T * R * S;
glPushMatrix();
glMultMatrixf( M3 );
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1);
glPopMatrix();
////////////////////////////////////////////
// USING glMultMatrixf
///////////////////////////////////////////
// p’= R T p (red) => translate, and then rotate
glPushMatrix();
glMultMatrixf( R );
glMultMatrixf( T );
glColor3f(1.0, 0.0, 0.0);
glutWireCube(1);
glPopMatrix();
// p’= T R p (green) => rotate, and then translate
glPushMatrix();
glMultMatrixf( T );
glMultMatrixf( R );
glColor3f(0.0, 1.0, 0.0);
glutWireCube(1);
glPopMatrix();
// p’= T R S p (blue) => scale, and then rotate, and then translate
glPushMatrix();
glMultMatrixf( T );
glMultMatrixf( R );
glMultMatrixf( S );
glColor3f(0.0, 0.0, 1.0);
glutWireCube(1);
glPopMatrix();
glutSwapBuffers();
}
Vector/Matrix/Plane Class
Vector3/4 Class
Matrix4x4 Class
Plane Class
Quaternion Class
http://dis.dankook.ac.kr/lectures/cg10/entry/Vector-Matrix-Plane-Class
OpenGL Single vs Double Buffer
OpenGL single buffer vs double buffer – draw geometry primitives
http://www.opengl.org/resources/code/samples/glut_examples/examples/sb2db.c
OpenGL single buffer vs double buffer – draw a square