init rework (2)
This commit is contained in:
parent
86968e2ba7
commit
15a74fccaf
17
include/program.h
Normal file
17
include/program.h
Normal file
@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <vertex-buffer.h>
|
||||
|
||||
struct program_t
|
||||
{
|
||||
unsigned shader;
|
||||
vertex_buffer_t *buffer;
|
||||
};
|
||||
|
||||
struct program_t program;
|
||||
|
||||
void
|
||||
program_init();
|
||||
|
||||
void
|
||||
program_draw();
|
86
src/program.c
Normal file
86
src/program.c
Normal file
@ -0,0 +1,86 @@
|
||||
GLuint compile_shader(const char *source, GLenum type)
|
||||
{
|
||||
GLint status;
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &source, 0);
|
||||
glCompileShader(shader);
|
||||
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_FALSE)
|
||||
{
|
||||
char error_log[256];
|
||||
glGetShaderInfoLog(shader, sizeof(error_log), NULL, error_log);
|
||||
log_write("GL Shader compilation error:\n%s\n", error_log);
|
||||
exit(1);
|
||||
}
|
||||
return shader;
|
||||
}
|
||||
|
||||
void
|
||||
setup_matrices(GLuint shader, int textures)
|
||||
{
|
||||
glUseProgram(shader);
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader, "model"), 1, 0, ds.model.data);
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader, "view"), 1, 0, ds.view.data);
|
||||
glUniformMatrix4fv(glGetUniformLocation(shader, "projection"), 1, 0, ds.projection.data);
|
||||
glUniform1i(glGetUniformLocation(shader, "texture"), 0);
|
||||
glUseProgram(0);
|
||||
}
|
||||
|
||||
const char *shader_ultimate_vert =
|
||||
"uniform mat4 model;\n"
|
||||
"uniform mat4 view;\n"
|
||||
"uniform mat4 projection;\n"
|
||||
"attribute vec2 vertex;\n"
|
||||
"attribute vec2 tex_coord;\n"
|
||||
"attribute vec4 color;\n"
|
||||
"attribute uint draw_mode;\n"
|
||||
"flat out uint frag_DrawMode;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_TexCoord[0].xy = tex_coord.xy;\n"
|
||||
" gl_FrontColor = color;\n"
|
||||
" gl_Position = projection*(view*(model*vec4(vertex,0.0,1.0)));\n"
|
||||
" frag_DrawMode = draw_mode;\n"
|
||||
"}";
|
||||
const char *shader_ultimate_frag =
|
||||
"uniform sampler2D texture;\n"
|
||||
"flat in uint frag_DrawMode\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" if (frag_DrawMode == 1)\n"
|
||||
" gl_FragColor = gl_Color;\n"
|
||||
" else\n"
|
||||
" {\n"
|
||||
" vec4 tex = texture2D(texture, gl_TexCoord[0].xy);\n"
|
||||
" if (frag_DrawMode == 2)\n"
|
||||
" gl_FragColor = gl_Color * tex\n"
|
||||
" else if (frag_DrawMode == 3)\n"
|
||||
" gl_FragColor = vec4(gl_Color.rgb, gl_Color.a * texture2D(texture, gl_TexCoord[0].xy).r);\n"
|
||||
" else\n"
|
||||
" gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
|
||||
"}";
|
||||
|
||||
void
|
||||
program_init()
|
||||
{
|
||||
program.buffer = vertex_buffer_new("vertex:2f,tex_coord:2f,color:4f,draw_mode:1I");
|
||||
program.shader = glCreateProgram();
|
||||
GLint status;
|
||||
GLuint sh_frag = compile_shader(shader_ultimate_frag, GL_FRAGMENT_SHADER);
|
||||
glAttachShader(program->shader, sh_frag);
|
||||
glDeleteShader(sh_frag);
|
||||
|
||||
GLuint sh_vert = compile_shader(shader_ultimate_vert, GL_VERTEX_SHADER);
|
||||
glAttachShader(program->shader, sh_vert);
|
||||
glDeleteShader(sh_vert);
|
||||
|
||||
glLinkProgram(program->shader);
|
||||
glGetProgramiv(program->shader, GL_LINK_STATUS, &status);
|
||||
if (status == GL_FALSE)
|
||||
{
|
||||
char error_log[256];
|
||||
glGetShaderInfoLog(program->shader, sizeof(error_log), NULL, error_log);
|
||||
log_write("GL Shader linking error:\n%s\n", error_log);
|
||||
exit(1);
|
||||
}
|
||||
}
|
@ -95,28 +95,9 @@ const char *shader_v2ft2fc4f_vert =
|
||||
"uniform mat4 model;\n"
|
||||
"uniform mat4 view;\n"
|
||||
"uniform mat4 projection;\n"
|
||||
"attribute vec2 vertex;\n"
|
||||
"attribute vec2 tex_coord;\n"
|
||||
"attribute vec4 color;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_TexCoord[0].xy = tex_coord.xy;\n"
|
||||
" gl_FrontColor = color;\n"
|
||||
" gl_Position = projection*(view*(model*vec4(vertex,0.0,1.0)));\n"
|
||||
"}";
|
||||
const char *shader_v2ft2fc4f_frag =
|
||||
"uniform sampler2D texture;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" vec4 tex = texture2D(texture, gl_TexCoord[0].xy);\n"
|
||||
" gl_FragColor = vec4(gl_Color.rgb * tex.rgb, gl_Color.a*tex.a);\n"
|
||||
"}";
|
||||
const char *shader_v2ft2fc4f_freetype_frag =
|
||||
"uniform sampler2D texture;\n"
|
||||
"void main()\n"
|
||||
"{\n"
|
||||
" gl_FragColor = vec4(gl_Color.rgb, gl_Color.a * texture2D(texture, gl_TexCoord[0].xy).r);\n"
|
||||
"}";
|
||||
|
||||
|
||||
|
||||
|
||||
void
|
||||
program_init_everything()
|
||||
|
Loading…
x
Reference in New Issue
Block a user