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);

}

 

 

lab2

Lab2-GeometryPositionColor (using Model-View-Projection matrix)

lab2-moglclassGeometryPositionColor

 

Geometry – Triangle, 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();

}

 

// using Uniform variables to shader
float g_fovy = 45.0f;
float g_aspect = 1.0f;
float g_zNear = 1.0f;
float g_zFar = 100.f;

glm::vec3 g_eye(0, 0, 3.0f);
glm::vec3 g_at(0, 0, 0);
glm::vec3 g_up(0, 1, 0);

// Model View Projection matrix
glm::mat4 World(1.0f);
glm::mat4 View(1.0f);
glm::mat4 Projection(1.0f);
glm::mat4 MVP(1.0f);

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

// set drawmode
if (g_wiremode)
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);  // draw wireframe
else
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // draw filled

// Model-View-Projection matrix
Projection = glm::perspective(g_fovy, g_aspect, g_zNear, g_zFar);
View = glm::lookAt(g_eye, g_at, g_up);
World = yRotation;
MVP = Projection * View * World;
spMain.useProgram();
spMain.setUniform(“gMVP“, MVP);

}

void reshape(int w, int h)
{
g_aspect = (float) (w/h);
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glutPostRedisplay();
}

void specialkey(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
g_eye.z += 2;
break;
case GLUT_KEY_DOWN:
g_eye.z -= 2;
break;
case GLUT_KEY_LEFT:
g_at.x -= 1;
g_eye.x -= 1;
break;
case GLUT_KEY_RIGHT:
g_at.x += 1;
g_eye.x += 1;
break;
}
glutPostRedisplay();
}

 

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

simple3.vs

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

uniform mat4 gMVP;

in vec3 vPosition;
in vec3 inColor;
out vec4 Color;

void
main()
{
gl_Position = gMVP * vec4(vPosition, 1);
Color = vec4(inColor, 1);
}

 

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;
}

HW1

lab1 코드 실행하고 실행 및 코드분석 리포트 (init() & draw() 함수 중심으로, 장수 제한 없음. 그리고 본인의 geometry primitive를 추가한다. (Due by 9/21)

-triangle

-square

-circle (primitive type)

-indexed square

your geometry (eg. star, flower) 추가

 

glVertexAttribPointer

glVertexAttribPointer — define an array of generic vertex attribute data

https://www.opengl.org/sdk/docs/man/html/glVertexAttribPointer.xhtml

void glVertexAttribPointer( GLuint index,
GLint size,
GLenum type,
GLboolean normalized,
GLsizei stride,
const GLvoid * pointer);

Parameters

index
Specifies the index of the generic vertex attribute to be modified.
size
Specifies the number of components per generic vertex attribute. Must be 1, 2, 3, 4. Additionally, the symbolic constant GL_BGRA is accepted by glVertexAttribPointer. The initial value is 4.
type
Specifies the data type of each component in the array. The symbolic constants GL_BYTE, GL_UNSIGNED_BYTE, GL_SHORT, GL_UNSIGNED_SHORT, GL_INT, and GL_UNSIGNED_INT are accepted by glVertexAttribPointer and glVertexAttribIPointer. Additionally GL_HALF_FLOAT, GL_FLOAT, GL_DOUBLE, GL_FIXED, GL_INT_2_10_10_10_REV, GL_UNSIGNED_INT_2_10_10_10_REV and GL_UNSIGNED_INT_10F_11F_11F_REV are accepted by glVertexAttribPointer. GL_DOUBLE is also accepted by glVertexAttribLPointer and is the only token accepted by the type parameter for that function. The initial value is GL_FLOAT.
normalized
For glVertexAttribPointer, specifies whether fixed-point data values should be normalized (GL_TRUE) or converted directly as fixed-point values (GL_FALSE) when they are accessed.
stride
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
pointer
Specifies a offset of the first component of the first generic vertex attribute in the array in the data store of the buffer currently bound to the GL_ARRAY_BUFFER target. The initial value is 0.