Lab4

Lab4-GeometryPositionColorComposeAnimation (“사” using Parallelepiped & geometry keyframe animation using Catmull-Rom Curve Animation)

lab4-GeometryPositionColorComposeAnimation

Sa::Sa(glm::vec3 p_) : GeometryPositionColor()
{
p = p_;
for (int i=0; i<4; i++) pipe[i] = Parallelepiped();
init();

// keyframe animation
 std::vector<KeyFrame> keyframes;
 keyframes.push_back(KeyFrame(glm::vec3(0, 0, 0), 0));
 keyframes.push_back(KeyFrame(glm::vec3(1, 1, 0), 2000));
 keyframes.push_back(KeyFrame(glm::vec3(-1, 2, 0), 4000));
 keyframes.push_back(KeyFrame(glm::vec3(2, 3, 0), 6000));
 setKeyframeAnimation(keyframes);
}

void Sa::init() // “사”
{
glm::vec3 p0 = p;
pipe[0].set(p0, glm::vec3(0.3f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.3f), glm::vec3(0.7f, 1.0f, 0.0f)); // ‘ㅅ’의 첫번 째 획
glm::vec3 p1 = p + glm::vec3(1.4f, 0.0f, 0.0f);
pipe[1].set(p1, glm::vec3(0.3f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.3f), glm::vec3(-0.7f, 1.0f, 0.0f)); // ‘ㅅ’의 두번 째 획
glm::vec3 p2 = p + glm::vec3(2.0f, 0.0f, 0.0f);
pipe[2].set(p2, glm::vec3(0.3f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.3f), glm::vec3(0.0f, 1.0f, 0.0f)); // ‘ㅏ’의 첫번 째 획
glm::vec3 p3 = p + glm::vec3(2.3f, 0.35f, 0.0f);
pipe[3].set(p3, glm::vec3(0.5f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.3f), glm::vec3(0.0f, 0.3f, 0.0f)); // ‘ㅏ’의 두번 째 획

void Sa::draw(bool wireframe)
{
for (int i=0; i<4; i++) pipe[i].draw();
}

bool Sa::update(float elapsedTime)
{
if (active)
{
if (curve)
{
curve->updatePosition(elapsedTime);
p = curve->getPosition();

// if time is greater than duration, then set time to duration and set active to false
if (!curve->getLoop() && elapsedTime >=  curve->getDuration())
{
done = true;
active = false;
}
}
init();
}
return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

void GeometryPositionColor::setKeyframeAnimation(std::vector<KeyFrame> frames_)
{
curve = new CatmullRomCurveAnimation(frames_, true);
init();
}

 

 

Lab3

Lab3-GeometryPositionColorMouse (using mouse & motion & geometry animation)

geometryPositionColor class restructured (adding center position & animation routine)

lab3-GeometryPositionColorMouse

std::vector<GeometryPositionColor*> geoList;

void mouse(int button, int state, int mx, int my)
{
if (button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
exit(0);
if (button==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
g_mousemove = true;
else if (button==GLUT_LEFT_BUTTON && state==GLUT_UP)
g_mousemove = false;
}

void motion(int mx, int my)
{
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);

// 0~600(x+ right) x 0~600(y+ down) => -5~5(x+ right) x -5~5(y+ up)
float x = (float) 10 * (mx – w*0.5) / w;
float y = (float) 10 * (h*0.5 – my) / h;

if (g_mousemove)
{
//printf(“mx=%d my=%d x=%f y=%f\n”, mx, my, x, y);
geoList[g_geometrymode]->setPosition(glm::vec3(x, y, 0));
}

glutPostRedisplay();
}

void keyboard( unsigned char key, int x, int y )
{
switch ( key ) {
case 27:
exit( EXIT_SUCCESS );
break;
case ‘c’:
geoList[g_geometrymode]->setColor(glm::vec3(1, 0, 0));
break;
case ‘w’:
g_wiremode = !g_wiremode;
break;
case ‘a’:
g_animationmode = !g_animationmode;
break;
case ‘g’:
g_geometrymode = (g_geometrymode + 1) % 8;
printf(“g_geometrymode=%d\n”, g_geometrymode);
break;
case ‘m’:
   startTime = glutGet(GLUT_ELAPSED_TIME);
   geoList[g_geometrymode]->activate();
printf(“object animation active?=%d\n”, (int)geoList[g_geometrymode]->isActive());
break;
}
glutPostRedisplay();
}

void update()
{

// each geometry update (triggered by ‘m’-key)
 geoList[g_geometrymode]->update(elapsedTime);

glutPostRedisplay();
}

void display( void )
{

 geoList[g_geometrymode]->draw();

}

void setData()
{
 geoList.push_back(new Quad());
 geoList.push_back(new Triangle());
 geoList.push_back(new Circle());

}

void init( void )
{

setData();

 geoList[0]->setAnimation(glm::vec3(-1, 0, 0), glm::vec3(1, 0, 0), 5000); // start, end, duration
 geoList[1]->setAnimation(glm::vec3(-2, -2, 0), glm::vec3(2, 2, 0), 5000);
 geoList[2]->setAnimation(glm::vec3(2, -2, 0), glm::vec3(-2, 2, 0), 5000);

}

 

 

PARALLELEPIPED

p

void Parallelepiped::init()
{
glm::vec3 pu = p + u;
glm::vec3 pv = p + v;
glm::vec3 pw = p + w;
glm::vec3 puv = p + u + v;
glm::vec3 pvw = p + v + w;
glm::vec3 puw = p + u + w;
glm::vec3 puvw = p + u + v + w;

// Front face
vbo.addData(&p[0], sizeof(glm::vec3));
vbo.addData(&pu[0], sizeof(glm::vec3));
vbo.addData(&puw[0], sizeof(glm::vec3));
vbo.addData(&p[0], sizeof(glm::vec3));
vbo.addData(&puw[0], sizeof(glm::vec3));
vbo.addData(&pw[0], sizeof(glm::vec3));

// Back face
vbo.addData(&puv[0], sizeof(glm::vec3));
vbo.addData(&pv[0], sizeof(glm::vec3));
vbo.addData(&pvw[0], sizeof(glm::vec3));
vbo.addData(&puv[0], sizeof(glm::vec3));
vbo.addData(&pvw[0], sizeof(glm::vec3));
vbo.addData(&puvw[0], sizeof(glm::vec3));

 

// Left face
vbo.addData(&pv[0], sizeof(glm::vec3));
vbo.addData(&p[0], sizeof(glm::vec3));
vbo.addData(&pw[0], sizeof(glm::vec3));
vbo.addData(&pv[0], sizeof(glm::vec3));
vbo.addData(&pw[0], sizeof(glm::vec3));
vbo.addData(&pvw[0], sizeof(glm::vec3));

 

// Right face
vbo.addData(&pu[0], sizeof(glm::vec3));
vbo.addData(&puv[0], sizeof(glm::vec3));
vbo.addData(&puvw[0], sizeof(glm::vec3));
vbo.addData(&pu[0], sizeof(glm::vec3));
vbo.addData(&puvw[0], sizeof(glm::vec3));
vbo.addData(&puw[0], sizeof(glm::vec3));

 

// Top face
vbo.addData(&pw[0], sizeof(glm::vec3));
vbo.addData(&puw[0], sizeof(glm::vec3));
vbo.addData(&puvw[0], sizeof(glm::vec3));
vbo.addData(&pw[0], sizeof(glm::vec3));
vbo.addData(&puvw[0], sizeof(glm::vec3));
vbo.addData(&pvw[0], sizeof(glm::vec3));

 

// Bottom face
vbo.addData(&puv[0], sizeof(glm::vec3));
vbo.addData(&pu[0], sizeof(glm::vec3));
vbo.addData(&p[0], sizeof(glm::vec3));
vbo.addData(&puv[0], sizeof(glm::vec3));
vbo.addData(&p[0], sizeof(glm::vec3));
vbo.addData(&pv[0], sizeof(glm::vec3));

 

numVertices = 36;

 

// create VBO
vbo.createVBO();
vbo.bindVBO();
vbo.uploadDataToGPU(GL_STATIC_DRAW);

 

// create a VAO
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

 

isLoaded = true;
}

Quad

q

void Quad::init()
{
glm::vec3 left = glm::cross(n, u);
glm::vec3 uppercenter = (u * height/2.0f) + p;
glm::vec3 tl = uppercenter + (left * width/2.0f);
glm::vec3 tr = uppercenter – (left * width/2.0f);
glm::vec3 bl = tl – (u * height);
glm::vec3 br = tr – (u * height);

// face 1
vbo.addData(&bl[0], sizeof(glm::vec3)); // vertex position
vbo.addData(&color[0], sizeof(glm::vec3)); // vertex color
vbo.addData(&br[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));
vbo.addData(&tr[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));

// face 2
vbo.addData(&bl[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));
vbo.addData(&tr[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));
vbo.addData(&tl[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));

numVertices = 6;

// VAO & VBOs
vbo.createVBO();
vbo.bindVBO();
vbo.uploadDataToGPU(GL_STATIC_DRAW);

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

int iDataStride = 2 * sizeof(glm::vec3); // vertex & color only
int iDataOffset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, iDataStride, (void*)iDataOffset);
iDataOffset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, iDataStride, (void*)iDataOffset);

isLoaded = true;
}

TRIANGLE

void Triangle::init()
{
 glm::vec3 t1 = p + v1;
glm::vec3 t2 = p + v2;
glm::vec3 t3 = p + v3;

// face 1
vbo.addData(&t1[0], sizeof(glm::vec3)); // vertex position
vbo.addData(&color[0], sizeof(glm::vec3)); // vertex color
vbo.addData(&t2[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));
vbo.addData(&t3[0], sizeof(glm::vec3));
vbo.addData(&color[0], sizeof(glm::vec3));

numVertices = 3;

// VAO & VBOs
vbo.createVBO();
vbo.bindVBO();
vbo.uploadDataToGPU(GL_STATIC_DRAW);

glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

int iDataStride = 2 * sizeof(glm::vec3); // vertex & color only
int iDataOffset = 0;
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, iDataStride, (void*)iDataOffset);
iDataOffset += sizeof(glm::vec3);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, iDataStride, (void*)iDataOffset);

isLoaded = true;
}

Lab2

Lab2-GeometryPositionColor (using depth buffer & single buffer)

lab2-moglclassGeometryPositionColor

GeometryTriangle, Quad, Circle, Sphere, Cube, Cylinder, Torus, Parallelpiped

 

int main( int argc, char **argv )
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGBA | GLUT_DEPTH );

}

 

void init( void )
{

glEnable(GL_DEPTH_TEST);
glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background
}

void display( void )
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); // clear the window

 glFlush();

}

 

HW1

단국대학교 멀티미디어공학전공 그래픽스프로그래밍 (2016년 봄학기) 실습

 

과목코드 : 514780

강사 : 박경신

이름:                                 날짜: 2016년 9월 23 일

학번:

– 실습번호 : HW1 (Due by 10/7)

– 실습제목 : Draw Primitives

– 실습요약 : 정점 버퍼와 인덱스 버퍼를 이용하여 간단한 2차원 기본 요소(primitives)와 3차원 객체 (3D objects)를 만들고 그린다. Lab2를 이용한다.

 

– 실습문제

  1. lab2 프로그램은 삼각형(triangle), 사각형(quad), 원(circle), 입방체(cube), 구(sphere) 등을 그리는 프로그램이다. 본인이 원하는 2차원 모양 3가지 (예: 나무, 꽃, 집)와 본인이 원하는 3차원 객체 (예: 미키 마우스 등) 1개 이상을 작성하라. 이 객체에 여러 가지 색(color)를 추가한다. 각 도형 별로 다양한 OpenGL Primitive Type을 사용하여 그려본다.

GL_GeometricPrimitives

  1. 그리고, 이 객체들을 여러 개 합쳐서 배치하여 “가을 풍경”의 장면을 구성해준다.

fall-season

  1. 완성된 장면을 구성하는 프로그램을 작성하고, 실행 화면과 코드를 첨부하시오.