init
This commit is contained in:
108
plugin/examples/hello_triangle/triangle.c
Normal file
108
plugin/examples/hello_triangle/triangle.c
Normal file
@ -0,0 +1,108 @@
|
||||
#include <gsr/plugin.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <GL/gl.h>
|
||||
|
||||
const char vertex_shader_source[] =
|
||||
"attribute vec4 vertex_pos; \n"
|
||||
"void main() { \n"
|
||||
" gl_Position = vertex_pos; \n"
|
||||
"}";
|
||||
|
||||
const char fragment_shader_source[] =
|
||||
"precision mediump float; \n"
|
||||
"uniform vec3 color; \n"
|
||||
"void main() { \n"
|
||||
" gl_FragColor = vec4(color, 1.0); \n"
|
||||
"}";
|
||||
|
||||
typedef struct {
|
||||
GLuint shader_program;
|
||||
GLuint vao;
|
||||
GLuint vbo;
|
||||
GLint color_uniform;
|
||||
unsigned int counter;
|
||||
} Triangle;
|
||||
|
||||
static GLuint load_shader(const char *shaderSrc, GLenum type) {
|
||||
GLuint shader = glCreateShader(type);
|
||||
glShaderSource(shader, 1, &shaderSrc, NULL);
|
||||
glCompileShader(shader);
|
||||
return shader;
|
||||
}
|
||||
|
||||
static void draw(const gsr_plugin_draw_params *params, void *userdata) {
|
||||
Triangle *triangle = userdata;
|
||||
|
||||
GLfloat glverts[6];
|
||||
|
||||
glverts[0] = -0.5f;
|
||||
glverts[1] = -0.5f;
|
||||
|
||||
glverts[2] = 0.5f;
|
||||
glverts[3] = -0.5f;
|
||||
|
||||
glverts[4] = 0.0f;
|
||||
glverts[5] = 0.5f;
|
||||
|
||||
glBindVertexArray(triangle->vao);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, triangle->vbo);
|
||||
glBufferSubData(GL_ARRAY_BUFFER, 0, 6 * sizeof(float), glverts);
|
||||
|
||||
glUseProgram(triangle->shader_program);
|
||||
const double pp = triangle->counter * 0.05;
|
||||
glUniform3f(triangle->color_uniform, 0.5 + sin(pp)*0.5, 0.5 + cos(pp)*0.5, 0.5 + sin(0.2 + pp)*0.5);
|
||||
glDrawArrays(GL_TRIANGLES, 0, 3);
|
||||
|
||||
glBindVertexArray(0);
|
||||
glUseProgram(0);
|
||||
|
||||
++triangle->counter;
|
||||
}
|
||||
|
||||
bool gsr_plugin_init(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret) {
|
||||
Triangle *triangle = calloc(1, sizeof(Triangle));
|
||||
if(!triangle)
|
||||
return false;
|
||||
|
||||
triangle->shader_program = glCreateProgram();
|
||||
|
||||
const GLuint vertex_shader = load_shader(vertex_shader_source, GL_VERTEX_SHADER);
|
||||
const GLuint fragment_shader = load_shader(fragment_shader_source, GL_FRAGMENT_SHADER);
|
||||
|
||||
glAttachShader(triangle->shader_program, vertex_shader);
|
||||
glAttachShader(triangle->shader_program, fragment_shader);
|
||||
glBindAttribLocation(triangle->shader_program, 0, "vertex_pos");
|
||||
glLinkProgram(triangle->shader_program);
|
||||
|
||||
glGenVertexArrays(1, &triangle->vao);
|
||||
glBindVertexArray(triangle->vao);
|
||||
|
||||
glGenBuffers(1, &triangle->vbo);
|
||||
glBindBuffer(GL_ARRAY_BUFFER, triangle->vbo);
|
||||
glBufferData(GL_ARRAY_BUFFER, 6 * sizeof(float), NULL, GL_DYNAMIC_DRAW);
|
||||
|
||||
glEnableVertexAttribArray(0);
|
||||
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), (void *)0);
|
||||
|
||||
glBindVertexArray(0);
|
||||
|
||||
glDeleteShader(vertex_shader);
|
||||
glDeleteShader(fragment_shader);
|
||||
|
||||
triangle->color_uniform = glGetUniformLocation(triangle->shader_program, "color");
|
||||
|
||||
ret->name = "hello_triangle";
|
||||
ret->version = 1;
|
||||
ret->userdata = triangle;
|
||||
ret->draw = draw;
|
||||
return true;
|
||||
}
|
||||
|
||||
void gsr_plugin_deinit(void *userdata) {
|
||||
Triangle *triangle = userdata;
|
||||
glDeleteProgram(triangle->shader_program);
|
||||
free(triangle);
|
||||
}
|
||||
56
plugin/plugin.h
Normal file
56
plugin/plugin.h
Normal file
@ -0,0 +1,56 @@
|
||||
#ifndef GSR_PLUGIN_H
|
||||
#define GSR_PLUGIN_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define GSR_PLUGIN_INTERFACE_MAJOR_VERSION 0
|
||||
#define GSR_PLUGIN_INTERFACE_MINOR_VERSION 1
|
||||
|
||||
#define GSR_PLUGIN_INTERFACE_MAKE_VERSION(major, minor) (((major) << 16) | (minor))
|
||||
#define GSR_PLUGIN_INTERFACE_VERSION GSR_PLUGIN_INTERFACE_MAKE_VERSION(GSR_PLUGIN_INTERFACE_MAJOR_VERSION, GSR_PLUGIN_INTERFACE_MINOR_VERSION)
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
GSR_PLUGIN_GRAPHICS_API_EGL_ES,
|
||||
GSR_PLUGIN_GRAPHICS_API_GLX,
|
||||
} gsr_plugin_graphics_api;
|
||||
|
||||
typedef enum {
|
||||
GSR_PLUGIN_COLOR_DEPTH_8_BITS,
|
||||
GSR_PLUGIN_COLOR_DEPTH_10_BITS,
|
||||
} gsr_plugin_color_depth;
|
||||
|
||||
typedef struct {
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
} gsr_plugin_draw_params;
|
||||
|
||||
typedef struct {
|
||||
unsigned int width;
|
||||
unsigned int height;
|
||||
unsigned int fps;
|
||||
gsr_plugin_color_depth color_depth;
|
||||
gsr_plugin_graphics_api graphics_api;
|
||||
} gsr_plugin_init_params;
|
||||
|
||||
typedef struct {
|
||||
const char *name; /* Mandatory */
|
||||
unsigned int version; /* Mandatory, can't be 0 */
|
||||
void *userdata; /* Optional */
|
||||
|
||||
/* Optional, called when the plugin is expected to draw something to the current framebuffer */
|
||||
void (*draw)(const gsr_plugin_draw_params *params, void *userdata);
|
||||
} gsr_plugin_init_return;
|
||||
|
||||
/* The plugin is expected to implement these functions and export them: */
|
||||
bool gsr_plugin_init(const gsr_plugin_init_params *params, gsr_plugin_init_return *ret);
|
||||
void gsr_plugin_deinit(void *userdata);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* GSR_PLUGIN_H */
|
||||
Reference in New Issue
Block a user