Anda di halaman 1dari 11

Nama : LUSIANA DIYAN NINGRUM (2210181051)

Kelas : 2 D4 Teknik Komputer B


PRAKTIKUM 2 WORKSHOP GRAFIKA KOMPUTER
INPUT HANDLER FUNCTION

Keyboard Program
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
std::cout << key << std::endl;

if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {


std::cout << "space key pressed" << std::endl;
}
}

int main(void) {
GLFWwindow* window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);

int screenWidth, screenHeight;


glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (!window) {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);

glfwSetKeyCallback(window, keyCallback);
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);

glViewport(0.0f, 0.0f, screenWidth, screenHeight);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);

glfwPollEvents();
}

glfwTerminate();

return 0;
}
HASIL PERCOBAAN

Ketika kode KeyCallback diubah seperti dibawah ini maka output akan berubah juga

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {

if (key == GLFW_KEY_SPACE && action == GLFW_PRESS) {


std::cout << "space key pressed" << std::endl;
}
}

ASSIGNMENT 2(1)
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {

if (key == GLFW_KEY_SPACE && action == GLFW_REPEAT) {


std::cout << "space key pressed" << std::endl;
}

int main(void) {
GLFWwindow* window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);


int screenWidth, screenHeight;
glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (!window) {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);

glfwSetKeyCallback(window, keyCallback);
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);

glViewport(0.0f, 0.0f, screenWidth, screenHeight);

glMatrixMode(GL_PROJECTION);

glLoadIdentity();

glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);

glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);

glfwPollEvents();
}

glfwTerminate();

return 0;
}

HASIL PERCOBAAN

Pada program keyboard yang pertama, fungsi keyCallback menggunakan fungsi input
GLFW_KEY_SPACE dan GLFW_PRESS. Outputnya adalah jika kita menekan space, akan muncul
teks “Space Key Pressed”. Sedangkan pada program keyboard assignment 2(1) menggunakan fungsi
input GLFW_REPEAT. Perbedaan dari fungsi sebelumnya yang menggunakan GLFW_PRESS yaitu
pada GLFW_REPEAT, saat menekan tombol space pada keyboard harus ditekan lebih lama agar
tulisan “Space Key Pressed” muncul.
ASSIGNMENT 2(2) - L key pressed
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {

if (key == GLFW_KEY_L && action == GLFW_REPEAT) {


std::cout << "L key pressed" << std::endl;
}
}

int main(void) {
GLFWwindow* window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);

int screenWidth, screenHeight;


glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (!window) {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, keyCallback);
glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
glViewport(0.0f, 0.0f, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);
glMatrixMode(GL_MODELVIEW);

glLoadIdentity();

while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT);
glfwSwapBuffers(window);
glfwPollEvents();
}

glfwTerminate();
return 0;
}

HASIL PERCOBAAN
MOUSE PROGRAM 1
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

static void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos);


int main(void) {
GLFWwindow* window;
if (!glfwInit())
{
return -1;
}
window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);
glfwSetCursorPosCallback(window, cursorPositionCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}

static void cursorPositionCallback(GLFWwindow* window, double xpos, double ypos) {


std::cout << xpos << " : " << ypos << std::endl;
}

HASIL PERCOBAAN
MOUSE PROGRAM 2
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void cursorEnterCallback(GLFWwindow *window, int entered);

int main(void) {

GLFWwindow* window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);

glfwSetCursorEnterCallback(window, cursorEnterCallback);

unsigned char pixels[16 * 16 * 4];


memset(pixels, 0xff, sizeof(pixels));
GLFWimage image;
image.width = 16;
image.height = 16;
image.pixels = pixels;
GLFWcursor *cursor = glfwCreateCursor(&image, 0, 0);
glfwSetCursor(window, cursor);

int screenWidth, screenHeight;


glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);

glfwPollEvents();
}

glfwTerminate();

return 0;
}

static void cursorEnterCallback(GLFWwindow* window, int entered) {


if (entered)
{
std::cout << "Entered Window" << std::endl;
}
else
{
std::cout << "Left Window" << std::endl;
}
}

HASIL PERCOBAAN
ASSIGNMENT 3(1)
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods);

int main(void) {

GLFWwindow* window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);

int screenWidth, screenHeight;


glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (!window) {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
glfwSetMouseButtonCallback(window, mouseButtonCallback);
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, GLFW_TRUE);

glViewport(0.0f, 0.0f, screenWidth, screenHeight);


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);

glfwPollEvents();
}

glfwTerminate();
return 0;
}

static void mouseButtonCallback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS)
{
std::cout << "Right Button Pressed" << std::endl;
}
if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
{
std::cout << "Left Button Pressed" << std::endl;
}
}

HASIL PERCOBAAN

Pada mouse program di module, saat kursor berada dalam window Hello World, maka akan muncul
teks “Entered Window”. Saat kursor di luar window Hello World, akan muncul teks “Left Window”.
Dengan fungsi cursorEnterCallback, hanya dengan menggeser kursor ke arah window atau keluar
window, kita bisa memunculkan tulisan tersebut. Sedangkan dalam program assignment 3,
perbedaannya adalah kita harus menekan klik kanan pada mouse, lalu akan muncul tulisan “Right
Button Pressed”. Hal itu bisa dilakukan dengan membuat fungsi mouseButtonCallback yang
didalamnya terdapat GLFW_MOUSE_BUTTON_RIGHT untuk klik kanan dan action GLFW_PRESS
untuk bisa menampilkan action saat mouse ditekan, yaitu memunculkan tulisan “Right Button
Pressed”. Dan GLFW_MOUSE_BUTTON_LEFT untuk klik kiri dan action GLFW_PRESS untuk
menampilkan action saat mouse ditekan dan menampilkan tulisan “Left Button Pressed”.

ASSIGNMENT 3(2)
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void scrollCallback(GLFWwindow* window, double xoffset, double yoffset);

int main(void) {

GLFWwindow* window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);

int screenWidth, screenHeight;


glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (!window) {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
glfwSetScrollCallback(window, scrollCallback);
glfwSetInputMode(window, GLFW_STICKY_MOUSE_BUTTONS, GLFW_TRUE);
glViewport(0.0f, 0.0f, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);

glfwSwapBuffers(window);

glfwPollEvents();
}

glfwTerminate();

return 0;
}

static void scrollCallback(GLFWwindow* window, double xoffset, double yoffset) {


if (yoffset < 0)
{
yoffset = -1;
std::cout << "Scrolling : X[" << xoffset << "] Y[" << yoffset << "]" <<
std::endl;
}
else
{
yoffset = 1;
std::cout << "Scrolling : X[" << xoffset << "] Y[" << yoffset << "]" <<
std::endl;
}
}

HASIL PERCOBAAN

Output program mouse assignment 3(2) adalah jika kita melakukan scroll up pada mouse, akan keluar
nilai 1 pada sumbu y, dan jika kita melakukan scroll down pada mouse, akan keluar nilai -1 pada
sumbu y. Perbedaan dari program sebelumnya adalah pada program sebelumnya kita harus mengklik
kanan atau kiri pada mouse terlebih dahulu baru muncul output “Right button pressed” atau “Left
button pressed”, sedangkan program ini hanya perlu menscroll ke atas atau ke bawah saja untuk
menampilkan nilai pada Y apakah 1 atau -1.

TEXT INPUT PROGRAM


#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
#include <math.h>
#include <iostream>

#define SCREEN_WIDTH 640


#define SCREEN_HEIGHT 480

void character_callback(GLFWwindow* window, unsigned int codepoint) {


std::cout << codepoint << std::endl;
}
void charmods_callback(GLFWwindow* window, unsigned int codepoint, int mods) {
std::cout << codepoint << std::endl;

if (mods == 1) {
std::cout << "Shift key pressed aswell" << std::endl;
}
}

int main(void) {

GLFWwindow *window;

if (!glfwInit())
{
return -1;
}

window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "Hello World", NULL, NULL);


glfwSetCharModsCallback(window, charmods_callback);

int screenWidth, screenHeight;


glfwGetFramebufferSize(window, &screenWidth, &screenHeight);

if (!window) {
glfwTerminate();
return -1;
}

glfwMakeContextCurrent(window);
glViewport(0.0f, 0.0f, screenWidth, screenHeight);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

while (!glfwWindowShouldClose(window))
{
glClear(GL_COLOR_BUFFER_BIT);
double xpos, ypos;
glfwGetCursorPos(window, &xpos, &ypos);
glfwSwapBuffers(window);
glfwPollEvents();
}

glfwTerminate();
return 0;
}

Hasil Percobaan
Pada program diatas ketika kita menekan tombol selain shift maka akan muncul angka saja, tetapi
ketika kita menekan tombol shift atau perpaduan shift+tombol keyboard lain maka akan muncul tulisan
“Shift Key Pressed Aswell”