0% found this document useful (0 votes)
53 views

Praktikum OpenGL Primitive Drawing

The document describes source code for a triangle rendering program using OpenGL. It initializes GLFW and GLEW, creates a window, defines vertex and fragment shaders, links the shaders into a program, and renders a triangle by drawing arrays. It provides the triangle's vertex coordinates and renders using shaders that color the triangle purple. The program outputs the graphics card renderer and supported OpenGL version.

Uploaded by

Lusiana Diyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Praktikum OpenGL Primitive Drawing

The document describes source code for a triangle rendering program using OpenGL. It initializes GLFW and GLEW, creates a window, defines vertex and fragment shaders, links the shaders into a program, and renders a triangle by drawing arrays. It provides the triangle's vertex coordinates and renders using shaders that color the triangle purple. The program outputs the graphics card renderer and supported OpenGL version.

Uploaded by

Lusiana Diyan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Nama : LUSIANA DIYAN NINGRUM (2210181051)

Kelas : 2 D4 Teknik Komputer B


PRAKTIKUM 1 WORKSHOP GRAFIKA KOMPUTER

SOURCE CODE TRIANGLE PROGRAM


#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <stdio.h>

int main(void) {

if (!glfwInit())

fprintf(stderr, "ERROR : Could not start GLFW3\n");

return 1;

GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);

if (!window)

fprintf(stderr, "ERROR : could open windows with GLFW3\n");

glfwTerminate();

return 1;

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;

glewInit();

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LESS);

float points[] = {

0.0f, 0.5f, 0.0f,

0.5f, -0.5f, 0.0f,

-0.5f, -0.5f, 0.0f,

};

GLuint vbo;

glGenBuffers(1, &vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

GLuint vao = 0;

glGenVertexArrays(1, &vao);

glBindVertexArray(vao);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

const char* vertex_shader =

"#version 400\n"

"in vec3 vp;"

"void main() {"

" gl_Position = vec4(vp, 1.0);"

"}";

const char* fragment_shader =

"#version 400\n"

"out vec4 frag_colour;"

"void main() {"

" frag_colour = vec4(0.9, 0.2, 0.6, 1.0);"

"}";

GLuint vs = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vs, 1, &vertex_shader, NULL);

glCompileShader(vs);

GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fs, 1, &fragment_shader, NULL);

glCompileShader(fs);

GLuint shader_programme = glCreateProgram();

glAttachShader(shader_programme, fs);

glAttachShader(shader_programme, vs);

glLinkProgram(shader_programme);

while (!glfwWindowShouldClose(window)) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(shader_programme);

glBindVertexArray(vao);

glDrawArrays(GL_TRIANGLES, 0, 3);

glPointSize(15.0);
glfwPollEvents();

glfwSwapBuffers(window);

glfwTerminate();

HASIL PERCOBAAN
0, 0.1, 0.5, 1.0

0.9, 0.2, 0.6, 1.0


ketika program di atas ditambahkan dengan kode berikut :
const GLubyte* renderer = glGetString(GL_RENDERER);

const GLubyte* version = glGetString(GL_VERSION);

printf("Renderer : %s\n", renderer);

printf("OpenGL version supported %s\n", version);

Hasil Percobaan

0.9, 0.7, 0.1, 1.0


TRIANGLE POINTS
#include <GL/glew.h>

#include <GLFW/glfw3.h>

#include <stdio.h>

int main(void) {

GLFWwindow* window;

if (!glfwInit())

fprintf(stderr, "ERROR : could not start GLFW3\n");

return -1;

window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);

if (!window)

fprintf(stderr, "ERROR : could not open window with GLFW3\n");

glfwTerminate();

return -1;

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;

glewInit();

const GLubyte* renderer = glGetString(GL_RENDER);

const GLubyte* version = glGetString(GL_VERSION);

printf("Renderer : %s\n", renderer);

printf("OpenGL Version supported %s\n", version);

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LESS);

float points[] = {

0.0f, 0.5f, 0.0f,

0.5f, -0.5f, 0.0f,

-0.5f, -0.5f, 0.0f

};
GLuint vbo;

glGenBuffers(1, &vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

GLuint vao = 0;

glGenVertexArrays(1, &vao);

glBindVertexArray(vao);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

const char* vertex_shader =

"#version 400\n"

"in vec3 vp;"

"void main() {"

"gl_Position = vec4(vp, 1.0);"

"}";

const char* fragment_shader =

"#version 400\n"

"out vec4 frag_colour;"

"void main(){"

"frag_colour = vec4(0.2, 0.8, 0.5, 1.0);"

"}";

GLuint vs = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vs, 1, &vertex_shader, NULL);

glCompileShader(vs);

GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fs, 1, &fragment_shader, NULL);

glCompileShader(fs);

GLuint shader_programme = glCreateProgram();

glAttachShader(shader_programme, fs);

glAttachShader(shader_programme, vs);

glLinkProgram(shader_programme);

while (!glfwWindowShouldClose(window))

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_programme);

glBindVertexArray(vao);

glDrawArrays(GL_POINTS, 0, 3);

glPointSize(20.0);

glfwPollEvents();

glfwSwapBuffers(window);

glfwTerminate();

HASIL PERCOBAAN
ASSIGMENT 1(1)

if (!glfwInit())

fprintf(stderr, "ERROR : Could not start GLFW3\n");

return 1;

GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Triangle", NULL, NULL);

if (!window)

fprintf(stderr, "ERROR : could open windows with GLFW3\n");

glfwTerminate();

return 1;

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;

glewInit();

const GLubyte* renderer = glGetString(GL_RENDERER);

const GLubyte* version = glGetString(GL_VERSION);

printf("Renderer : %s\n", renderer);

printf("OpenGL version supported %s\n", version);

glEnable(GL_DEPTH_TEST);

glDepthFunc(GL_LESS);

float points[] = {

0.5f, 0.5f, 0.0f,

0.5f, -0.5f, 0.0f,

-0.5f, -0.5f, 0.0f,

-0.5f, 0.5f, 0.0f,

0.5f, 0.5f, 0.0f,

-0.5f, 0.5f, 0.0f,

0.5f, -0.5f, 0.0f,

-0.5f, -0.5f, 0.0f

};

GLuint vbo;

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);

glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

GLuint vao = 0;

glGenVertexArrays(1, &vao);

glBindVertexArray(vao);

glEnableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, vbo);

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

const char* vertex_shader =

"#version 400\n"

"in vec3 vp;"

"void main() {"

" gl_Position = vec4(vp, 1.0);"

"}";

const char* fragment_shader =

"#version 400\n"

"out vec4 frag_colour;"

"void main() {"

" frag_colour = vec4(0.9, 0.7, 0.1, 1.0);"

"}";

GLuint vs = glCreateShader(GL_VERTEX_SHADER);

glShaderSource(vs, 1, &vertex_shader, NULL);

glCompileShader(vs);

GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);

glShaderSource(fs, 1, &fragment_shader, NULL);

glCompileShader(fs);

GLuint shader_programme = glCreateProgram();

glAttachShader(shader_programme, fs);

glAttachShader(shader_programme, vs);

glLinkProgram(shader_programme);

while (!glfwWindowShouldClose(window)) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glUseProgram(shader_programme);

glBindVertexArray(vao);

glDrawArrays(GL_LINES, 0, 8);
glPointSize(30.0);

glfwPollEvents();

glfwSwapBuffers(window);

glfwTerminate();

HASIL PERCOBAAN
ASSIGNMENT 1(2)
if (!glfwInit())

{
fprintf(stderr, "ERROR : Could not start GLFW3\n");
return 1;
}

GLFWwindow* window = glfwCreateWindow(640, 480, "Hello Result", NULL, NULL);


if (!window)
{
fprintf(stderr, "ERROR : could open windows with GLFW3\n");
glfwTerminate();
return 1;
}

glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
glewInit();

const GLubyte* renderer = glGetString(GL_RENDERER);


const GLubyte* version = glGetString(GL_VERSION);
printf("Renderer : %s\n", renderer);
printf("OpenGL version supported %s\n", version);

glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);

float points[] = {
0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, 0.5f, 0.0f,
-0.5f, 0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
-0.5f, -0.5f, 0.0f
};

GLuint vbo;
glGenBuffers(1, &vbo);

glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(points), points, GL_STATIC_DRAW);

GLuint vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);

const char* vertex_shader =


"#version 400\n"
"in vec3 vp;"
"void main() {"
" gl_Position = vec4(vp, 1.0);"
"}";

const char* fragment_shader =


"#version 400\n"
"out vec4 frag_colour;"
"void main() {"
" frag_colour = vec4(0.5, 0.7, 0.1, 1.0);"
"}";

GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);

GLuint shader_programme = glCreateProgram();


glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
while (!glfwWindowShouldClose(window)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(shader_programme);
glBindVertexArray(vao);

glDrawArrays(GL_LINE_LOOP, 0, 8);
//glPointSize(30.0);

glfwPollEvents();

glfwSwapBuffers(window);
}

glfwTerminate();
}

HASIL PERCOBAAN
glDrawArrays(GL_LINE_LOOP, 0, 3);

glDrawArrays(GL_LINE_LOOP, 0, 4);

You might also like