GLFW05 Objek Tiga Dimensi dan Transformasi Tiga Dimensi - auziasfarian/CG-IPB GitHub Wiki
#include <stdio.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
static void grid(int x, int y)
{
int i;
glColor3ub(237, 29, 36);
glBegin(GL_LINES);
for(i=-x; i<=x; i++)
{
glVertex2f(i,-y);
glVertex2f(i,y);
}
for(i=-y; i<=y; i++)
{
glVertex2f(-x,i);
glVertex2f(x,i);
}
/*glColor3f(0.7,1,0.7);
glVertex2f(-x,0);
glVertex2f(x,0);
glVertex2f(0,-y);
glVertex2f(0,y);*/
glEnd();
}
void display()
{
grid(10,10);
}
int main(void) {
//Window
GLFWwindow* window;
// glfwSetErrorCallback(error_callback);
if (!glfwInit())exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Grafika Komputer", NULL, NULL);
if (!window){
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
// glfwSetKeyCallback(window, key_callback);
while (!glfwWindowShouldClose(window)){
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
display();
glfwSwapBuffers(window);
glfwPollEvents();
}
//Fungsi Exit
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}
Ikuti langkah berikut untuk menjalankan kode program objek 3D dan transformasinya di Codeblocks 17.12:
-
Unduh library GL / glew.h pada link berikut,
-
Compress file yang telah diunduh, kemudian pindahkan file di dalam folder bin, include, lib ke folder directory instalasi codeblocks pada PC mu,
-
Buka aplikasi Codeblocks, kemudian pilih menu Setting > Compiler > tab Linker Settings, lalu tambahkan library bernama "libglu32.a" pada folder directory instalasi Codeblocks > MinGW > Lib.
-
Salin dan jalankan kode program C++ dibawah
Ikuti langkah berikut untuk menjalankan kode program objek 3D dan transformasinya di Codeblocks 20.03:
-
Unduh library GL / glew.h pada link berikut,
-
Compress file yang telah diunduh, kemudian pindahkan file di dalam folder bin, include, lib ke folder directory instalasi Codeblocks > MinGW > x86_64-w64-mingw32 pada PC mu
-
Copy file di dalam folder include dan lib ke folder directory yang menyimpan GLFW 3.3.4 kamu. Misalnya di Program Files > glfw-3.3.4.bin.WIN64
-
Buka aplikasi Codeblocks, kemudian pilih menu Setting > Compiler > tab Linker Settings, lalu tambahkan library bernama "libglu32.a" pada folder directory instalasi Codeblocks > MinGW > x86_64-w64-mingw32 > Lib
-
Salin dan jalankan kode program C++ dibawah
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <cstdio>
void controls(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if(action == GLFW_PRESS)
if(key == GLFW_KEY_ESCAPE)
glfwSetWindowShouldClose(window, GL_TRUE);
}
GLFWwindow* initWindow(const int resX, const int resY)
{
if(!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
return NULL;
}
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
// Open a window and create its OpenGL context
GLFWwindow* window = glfwCreateWindow(resX, resY, "TEST", NULL, NULL);
if(window == NULL)
{
fprintf(stderr, "Failed to open GLFW window.\n");
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
glfwSetKeyCallback(window, controls);
// Get info of GPU and supported OpenGL version
printf("Renderer: %s\n", glGetString(GL_RENDERER));
printf("OpenGL version supported %s\n", glGetString(GL_VERSION));
glEnable(GL_DEPTH_TEST); // Depth Testing
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
return window;
}
void drawCube()
{
GLfloat vertices[] =
{
-1, -1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1,
1, -1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, -1, 1, 1, -1, 1, 1, -1, -1,
-1, 1, -1, -1, 1, 1, 1, 1, 1, 1, 1, -1,
-1, -1, -1, -1, 1, -1, 1, 1, -1, 1, -1, -1,
-1, -1, 1, -1, 1, 1, 1, 1, 1, 1, -1, 1
};
GLfloat colors[] =
{
0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0,
1, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 0,
0, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0,
0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1
};
static float alpha = 0;
//attempt to rotate cube
glRotatef(alpha, 0, 1, 0);
/* We have a color array and a vertex array */
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, vertices);
glColorPointer(3, GL_FLOAT, 0, colors);
/* Send data : 24 vertices */
glDrawArrays(GL_QUADS, 0, 24);
/* Cleanup states */
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
alpha += 1;
}
void display( GLFWwindow* window )
{
while(!glfwWindowShouldClose(window))
{
// Scale to window size
GLint windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
// Draw stuff
glClearColor(0.0, 0.8, 0.3, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION_MATRIX);
glLoadIdentity();
gluPerspective( 60, (double)windowWidth / (double)windowHeight, 0.1, 100 );
glMatrixMode(GL_MODELVIEW_MATRIX);
glTranslatef(0,0,-5);
drawCube();
// Update Screen
glfwSwapBuffers(window);
// Check for any input, or window movement
glfwPollEvents();
}
}
int main(int argc, char** argv)
{
GLFWwindow* window = initWindow(1024, 620);
if( NULL != window )
{
display( window );
}
glfwDestroyWindow(window);
glfwTerminate();
return 0;
}
Tugas : Membuat Kincir dalam format .cpp