gl module

pull/362/head
Alexander Medvednikov 2019-06-22 21:24:49 +02:00
parent bed53f444d
commit f831fd62f2
5 changed files with 430 additions and 0 deletions

175
gl/1shader.v 100644
View File

@ -0,0 +1,175 @@
module gl
import os
import gx
import glm
// import darwin
import const (
GL_VERTEX_SHADER
GL_FRAGMENT_SHADER
GL_ARRAY_BUFFER
GL_TRIANGLES
GL_CULL_FACE
GL_BLEND
GL_LINES
)
struct Shader {
program_id int
}
const (
TEXT_VERT = '#version 330 core
layout (location = 0) in vec4 vertex; // <vec2 pos, vec2 tex>
out vec2 TexCoords;
uniform mat4 projection;
void main()
{
gl_Position = projection * vec4(vertex.xy, 0.0, 1.0);
TexCoords = vertex.zw;
} '
TEXT_FRAG = '#version 330 core
in vec2 TexCoords;
out vec4 color;
uniform sampler2D text;
uniform vec3 textColor;
void main()
{
vec4 sampled = vec4(1.0, 1.0, 1.0, texture(text, TexCoords).r);
color = vec4(textColor, 1.0) * sampled;
} '
SIMPLE_VERT = ' #version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 TexCoord;
uniform mat4 projection;
void main() {
gl_Position = projection * vec4(aPos, 1.0);
// gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
//TexCoord = vec2(aTexCoord.x, aTexCoord.y);
TexCoord = aTexCoord;
}
'
SIMPLE_FRAG = '#version 330 core
out vec4 FragColor;
uniform vec3 color;
uniform bool has_texture;
in vec3 ourColor;
in vec2 TexCoord;
uniform sampler2D ourTexture;
void main() {
// FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);
// FragColor = vec4(1.0f, 0.0f, 0.0f, 1.0f);
if (has_texture) {
FragColor = texture(ourTexture, TexCoord);
} else {
FragColor = vec4(color, 1.0f);
}
}
'
)
pub fn new_shader(name string) Shader {
// TODO This is not used, remove
mut dir := ''
// Already have absolute path
if name.starts_with('/') {
dir = ''
}
vertex_path := '${dir}${name}.vert'
fragment_path := '${dir}${name}.frag'
println('shader path=$vertex_path,\n fpath="$fragment_path"')
// vertex_src := os.read_file(vertex_path.trim_space())
mut vertex_src = ''
mut fragment_src = ''
if name == 'text' {
vertex_src = TEXT_VERT
fragment_src = TEXT_FRAG
}
else if name == 'simple' {
// println('new shader simple!!!')
// println(SIMPLE_VERT)
vertex_src = SIMPLE_VERT
fragment_src = SIMPLE_FRAG
}
// ////////////////////////////////////////
vertex_shader := gl.create_shader(GL_VERTEX_SHADER)
gl.shader_source(vertex_shader, 1, vertex_src, 0)
gl.compile_shader(vertex_shader)
if gl.shader_compile_status(vertex_shader) == 0 {
log := gl.shader_info_log(vertex_shader)
println('shader $vertex_shader compilation failed')
println('shader source = $vertex_src')
os.exit('shader')
}
// fragment shader
// fragment_src := os.read_file(fragment_path.trim_space())
fragment_shader := gl.create_shader(GL_FRAGMENT_SHADER)
gl.shader_source(fragment_shader, 1, fragment_src, 0)
gl.compile_shader(fragment_shader)
if gl.shader_compile_status(fragment_shader) == 0 {
println('fragment $fragment_shader shader compilation failed')
os.exit('shader')
}
// link shaders
shader_program := gl.create_program()
gl.attach_shader(shader_program, vertex_shader)
gl.attach_shader(shader_program, fragment_shader)
gl.link_program(shader_program)
// check for linking errors
success := gl.get_program_link_status(shader_program)
if success == 0 {
println('shader compilation failed')
println('vertex source = $vertex_src')
println('fragment source = $fragment_src')
os.exit('shader')
}
shader := Shader {
program_id: shader_program,
}
return shader
}
pub fn (s Shader) use() {
gl.use_program(s.program_id)
}
pub fn (s Shader) uni_location(key string) int {
return C.glGetUniformLocation(s.program_id, key.str)
}
// fn (s Shader) set_mat4(str string, f *float) {
pub fn (s Shader) set_mat4(str string, m glm.Mat4) {
// TODO cache uniform location
C.glUniformMatrix4fv(s.uni_location(str), 1, false, m.data)
}
pub fn (s Shader) set_int(str string, n int) {
C.glUniform1i(s.uni_location(str), n)
}
pub fn (s Shader) set_color(str string, c gx.Color) {
C.glUniform3f(s.uni_location(str), float(c.r) / 255.0, float(c.g) / 255.0, float(c.b) / 255.0)
}

209
gl/gl.v 100644
View File

@ -0,0 +1,209 @@
module gl
// #define STB_IMAGE_IMPLEMENTATION
// #include <stb_image.h>
// ///////
import const (
GL_TEXTURE_2D
GL_TEXTURE0
GL_FLOAT
GL_VERTEX_SHADER
GL_ELEMENT_ARRAY_BUFFER
GL_DEPTH_TEST
)
/*
type Vec3 {
x float
y float
z float
}
type Vec2 {
x int
y int
}
*/
pub fn viewport(a int, b int, c int, d int) {
C.glViewport(a, b, c, d)
}
pub fn clear_color(r, g, b, a int) {
# glClearColor(((float)r)/255.0,((float)g)/255.0,b/255.0, a/255.0);
// println('CLEAR COLOR')
// #glClearColor(0.3f, 0.9f, 0.32f, 1.0f);
// C.glClearColor(r, g, b, a)
}
pub fn clear() {
# glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT|GL_STENCIL_BUFFER_BIT);
}
pub fn create_shader(typ int) int {
return C.glCreateShader(typ)
}
pub fn create_program() int {
return C.glCreateProgram()
}
pub fn shader_source(shader int, a int, source string, b int) {
C.glShaderSource(shader, a, &source.str, b)
}
pub fn compile_shader(shader int) {
C.glCompileShader(shader)
}
pub fn shader_compile_status(shader int) int {
success := 0
# glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
return success
}
pub fn attach_shader(program int, shader int) {
// fn (s Shader) attach(program int) {
C.glAttachShader(program, shader)
}
pub fn link_program(program int) {
C.glLinkProgram(program)
}
pub fn get_program_link_status(program int) int {
success := 0
# glGetProgramiv(program, GL_LINK_STATUS, &success);
return success
}
pub fn delete_shader(shader int) {
C.glDeleteShader(shader)
}
pub fn shader_info_log(shader int) string {
# printf("GET info log\n");
# char infoLog[512];
# glGetShaderInfoLog(shader, 512, NULL, infoLog);
# printf("log=%s\n", infoLog);
# return tos_no_len(infoLog);
return ''
}
pub fn get_program_info_log(program int) string {
# char infoLog[512];
# glGetProgramInfoLog(program, 1024, NULL, infoLog);
# return tos_no_len(infoLog);
return ''
}
pub fn bind_vao(vao u32) {
C.glBindVertexArray(vao)
}
pub fn bind_buffer(typ int, vbo u32) {
C.glBindBuffer(typ, vbo)
}
pub fn gen_texture() u32 {
res := u32(0)
C.glGenTextures(1, &res)
return res
}
pub fn active_texture(t int) {
C.glActiveTexture(t)
}
pub fn bind_2d_texture(texture u32) {
C.glBindTexture(GL_TEXTURE_2D, texture)
}
pub fn delete_texture(texture u32) {
C.glDeleteTextures(1, &texture)
}
pub fn buffer_data(typ int, size int, arr voidptr, draw_typ int) {
C.glBufferData(typ, size, arr, draw_typ)
}
pub fn buffer_data_int(typ int, vertices[]int, draw_typ int) {
size := sizeof(int) * vertices.len
C.glBufferData(typ, size, vertices.data, draw_typ)
}
pub fn buffer_data_float(typ int, vertices[]float, draw_typ int) {
size := sizeof(float) * vertices.len
C.glBufferData(typ, size, vertices.data, draw_typ)
}
pub fn set_vbo(vbo u32, vertices[]float, draw_typ int) {
gl.bind_buffer(GL_ARRAY_BUFFER, vbo)
gl.buffer_data_float(GL_ARRAY_BUFFER, vertices, draw_typ)
}
pub fn set_ebo(ebo u32, indices[]int, draw_typ int) {
gl.bind_buffer(GL_ELEMENT_ARRAY_BUFFER, ebo)
// gl.buffer_data_int(GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
gl.buffer_data_int(GL_ELEMENT_ARRAY_BUFFER, indices, draw_typ)
}
// /////////////////////
// fn gen_vertex_arrays(a int, vao uint) {
// # glGenVertexArrays(a, &VAO);
// }
pub fn draw_arrays(typ, start, len int) {
C.glDrawArrays(typ, start, len)
}
pub fn draw_elements(mode, count, typ, indices int) {
C.glDrawElements(mode, count, typ, indices)
}
pub fn use_program(program int) {
C.glUseProgram(program)
}
pub fn gen_vertex_array() u32 {
VAO := u32(0)
# glGenVertexArrays(1, &VAO);
return VAO
}
pub fn enable_vertex_attrib_array(n int) {
C.glEnableVertexAttribArray(n)
}
pub fn gen_buffer() u32 {
VBO := u32(0)
# glGenBuffers(1, &VBO);
return VBO
}
pub fn vertex_attrib_pointer(index, size int, typ int, normalized bool, stride int, ptr int) {
if typ == GL_FLOAT {
stride *= sizeof(float)
ptr *= sizeof(float)
}
C.glVertexAttribPointer(index, size, typ, normalized, stride, ptr)
}
pub fn tex_param(key, val int) {
C.glTexParameteri(GL_TEXTURE_2D, key, val)
}
pub fn enable(val int) {
C.glEnable(val)
}
pub fn disable(val int) {
C.glDisable(val)
}
pub fn scissor(a, b, c, d int) {
C.glScissor(a, b, c, d)
}
pub fn generate_mipmap(typ int) {
C.glGenerateMipmap(typ)
}

16
gl/gl_lin.v 100644
View File

@ -0,0 +1,16 @@
module gl
// #include <OpenGL/gl.h>
// #flag -framework OpenGL
#include "glad.h"
#include "glad.c"
fn init_glad() {
println('init_glad() win')
// # ok= gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
ok := C.gladLoadGL()
if !ok {
os.exit('Failed to initialize glad OpenGL context')
}
// C.printf("initglad test: glClear ADDR=%p\n', glClear);
}

18
gl/gl_mac.v 100644
View File

@ -0,0 +1,18 @@
module gl
#include <Cocoa/Cocoa.h>
#include <CoreGraphics/CoreGraphics.h>
// #include <OpenGL/gl.h>
// #flag -framework OpenGL
#include "/Users/alex/code/lang/gl/glad.h"
#include "/Users/alex/code/lang/gl/glad.c"
fn init_glad() {
println('init_glad() win')
ok := C.gladLoadGL()
// # ok= gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
if !ok {
os.exit('Failed to initialize glad OpenGL context')
}
// # printf("glClear ADDR=%p\n", glClear);
}

12
gl/gl_win.v 100644
View File

@ -0,0 +1,12 @@
module gl
fn init_glad() {
println('init_glad() win')
ok := false
# ok= gladLoadGLLoader((GLADloadproc) glfwGetProcAddress);
if !ok {
os.exit('Failed to initialize glad OpenGL context')
}
# printf("glClear ADDR=%p\n", glClear);
}