Triangle
Goals
- to make using graphic card
- manipulate buffer in GPU
Boilerplate to create triangle in opengl 📐🔺
Floder structure
app.jl
using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
include("Frame02.jl")
window = GLFW.CreateWindow(600,600,"App")
GLFW.MakeContextCurrent(window)
# get version info
renderer = unsafe_string(glGetString(GL_RENDERER))
version = unsafe_string(glGetString(GL_VERSION))
@info "Renderder: $renderer"
@info "OpenGL version supported: $version"
# enable depth test
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LESS)
ctx = CImGui.CreateContext()
ImGui_ImplGlfw_InitForOpenGL(window,true)
ImGui_ImplOpenGL3_Init(130)
while !GLFW.WindowShouldClose(window)
glClearColor(0.2,0.2,0.2,1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
GLFW.PollEvents()
ImGui_ImplOpenGL3_NewFrame()
ImGui_ImplGlfw_NewFrame()
CImGui.NewFrame()
Frame02.Frame()
CImGui.Render()
ImGui_ImplOpenGL3_RenderDrawData(CImGui.GetDrawData())
GLFW.MakeContextCurrent(window)
GLFW.SwapBuffers(window)
end
CImGui.DestroyContext(ctx)
GLFW.DestroyWindow(window)
Frame02
module Frame02
using CImGui
using CImGui.CSyntax
using CImGui.CSyntax.CStatic
using CImGui.GLFWBackend
using CImGui.OpenGLBackend
using CImGui.GLFWBackend.GLFW
using CImGui.OpenGLBackend.ModernGL
# print errors in shader compilation
function shader_info_log(shader::GLuint)
max_length = GLint(0)
@c glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &max_length)
actual_length = GLsizei(0)
log = Vector{GLchar}(undef, max_length)
@c glGetShaderInfoLog(shader, max_length, &actual_length, log)
@info "shader info log for GL index $shader: $(String(log))"
end
const vert_source = """
#version 410 core
in vec3 vp;
void main(void){
gl_Position = vec4(vp,1.0);
}
"""
const frag_source = """
#version 410 core
out vec4 frag_colour;
void main(void){
frag_colour = vec4(0.5,0.0,0.5,1.0);
}
"""
# vertex data
const points = GLfloat[ 0.0, 0.5, 0.0,
0.5, -0.5, 0.0,
-0.5, -0.5, 0.0]
function CompileShader(type,source)
vert_shader = glCreateShader(type)
glShaderSource(vert_shader,1,Ptr{GLchar}[pointer(source)],C_NULL)
glCompileShader(vert_shader)
shader_info_log(vert_shader)
return vert_shader
end
function CreateShader(vertex_shader,fragment_shader)
program = glCreateProgram()
vs = CompileShader(GL_VERTEX_SHADER,vertex_shader)
fs = CompileShader(GL_FRAGMENT_SHADER,fragment_shader)
glAttachShader(program,vs)
glAttachShader(program,fs)
glLinkProgram(program)
# create buffer located in the memory of graphic card
vbo = GLuint(0)
@c glGenBuffers(1,&vbo)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glBufferData(GL_ARRAY_BUFFER,sizeof(points),points,GL_DYNAMIC_DRAW)
#crate array
global vao = GLuint(0)
@c glGenBuffers(1,&vao)
glBindVertexArray(vao)
glBindBuffer(GL_ARRAY_BUFFER,vbo)
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,0,C_NULL)
glEnableVertexAttribArray(0)
return program
end
function Frame()
shader = CreateShader(vert_source,frag_source)
glUseProgram(shader)
glBindVertexArray(vao)
glDrawArrays(GL_TRIANGLES, 0, 3)
end
end