CS116a
Chris Pollett
Sep 4, 2013
glutDisplayFunc(display); // display rendering callback glutReshapeFunc(reshape); // window reshape callback glutMotionFunc(motion); // mouse movement callback glutMouseFunc(mouse); // mouse click callback glutKeyboardFunc(keyboard);
static void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
if (state == GLUT_DOWN) {
// right mouse button has been clicked
g_leftClicked = true;
g_leftClickX = x;
g_leftClickY = g_height - y - 1;
}
else {
// right mouse button has been released
g_leftClicked = false;
}
}
if (button == GLUT_RIGHT_BUTTON) {
if (state == GLUT_DOWN) {
// right mouse button has been clicked
g_rightClicked = true;
g_rightClickX = x;
g_rightClickY = g_height - y - 1;
}
else {
// right mouse button has been released
g_rightClicked = false;
}
}
}
static void motion(int x, int y) {
const int newx = x;
const int newy = g_height - y - 1;
if (g_leftClicked) {
g_leftClickX = newx;
g_leftClickY = newy;
}
if (g_rightClicked) {
float deltax = (newx - g_rightClickX) * 0.02;
g_objScale += deltax;
g_rightClickX = newx;
g_rightClickY = newy;
}
glutPostRedisplay();
}
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'h':
cout << " ============== H E L P ==============\n\n"
<< "h\t\thelp menu\n"
<< "s\t\tsave screenshot\n"
<< "drag right mouse to change square size\n";
break;
case 'q':
exit(0);
case 's':
glFinish();
writePpmScreenshot(g_width, g_height, "out.ppm");
break;
}
}
struct SquareGeometry {
GlBufferObject posVbo[2], texVbo, colVbo;
SquareGeometry() {
static GLfloat sqPos[2][12] = {
{
-.5, -.5,
0., 0.,
0, -.5,
-.5, -.5,
-.5, .0,
0, .0
},
{
0., 0.,
.5, .5,
.5, 0,
0., 0.,
0., .5,
.5, .5
}
};
static GLfloat sqTex[12] = {
0, 0,
1, 1,
1, 0,
0, 0,
0, 1,
1, 1
};
static GLfloat sqCol[18] = {
1, 0, 0,
0, 1, 1,
0, 0, 1,
1, 0, 0,
0, 1, 0,
0, 1, 1
};
for (int i = 0; i < 2; i++) {
glBindBuffer(GL_ARRAY_BUFFER, posVbo[i]);
glBufferData(
GL_ARRAY_BUFFER,
12*sizeof(GLfloat),
sqPos[i],
GL_STATIC_DRAW);
checkGlErrors();
}
glBindBuffer(GL_ARRAY_BUFFER, texVbo);
glBufferData(
GL_ARRAY_BUFFER,
12*sizeof(GLfloat),
sqTex,
GL_STATIC_DRAW);
checkGlErrors();
glBindBuffer(GL_ARRAY_BUFFER, colVbo);
glBufferData(
GL_ARRAY_BUFFER,
18*sizeof(GLfloat),
sqCol,
GL_STATIC_DRAW);
checkGlErrors();
}
void draw(const ShaderState& curSS) {
int numverts=6;
safe_glEnableVertexAttribArray(curSS.h_aPosition);
safe_glEnableVertexAttribArray(curSS.h_aTexCoord0);
safe_glEnableVertexAttribArray(curSS.h_aTexCoord1);
safe_glEnableVertexAttribArray(curSS.h_aColor);
for (int i = 0; i < 2; i++) {
glBindBuffer(GL_ARRAY_BUFFER, posVbo[i]);
safe_glVertexAttribPointer(curSS.h_aPosition,
2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, texVbo);
safe_glVertexAttribPointer(curSS.h_aTexCoord0,
2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, texVbo);
safe_glVertexAttribPointer(curSS.h_aTexCoord1,
2, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, colVbo);
safe_glVertexAttribPointer(curSS.h_aColor,
3, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, numverts);
}
safe_glDisableVertexAttribArray(curSS.h_aPosition);
safe_glDisableVertexAttribArray(curSS.h_aColor);
safe_glDisableVertexAttribArray(curSS.h_aTexCoord0);
safe_glDisableVertexAttribArray(curSS.h_aTexCoord1);
}
};