This repository has been archived by the owner on May 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* See Copyright Notice in LICENSE.txt */ | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <unistd.h> | ||
|
||
#include <GL/glew.h> | ||
#include <GL/gl.h> | ||
#include <GL/glfw.h> | ||
#include <FTGL/ftgl.h> | ||
#include <lauxlib.h> | ||
#include <lualib.h> | ||
|
||
#include "misc.h" | ||
|
||
typedef struct { | ||
GLuint fs; | ||
GLuint vs; | ||
GLuint po; | ||
} shader_t; | ||
|
||
LUA_TYPE_DECL(shader); | ||
|
||
/* Instance methods */ | ||
|
||
static int shader_use(lua_State *L) { | ||
shader_t *shader = checked_shader(L, 1); | ||
glUseProgram(shader->po); | ||
lua_pushnumber(L, 1); | ||
return 1; | ||
} | ||
|
||
static const luaL_reg shader_methods[] = { | ||
{"use", shader_use}, | ||
{0,0} | ||
}; | ||
|
||
/* Lifecycle */ | ||
|
||
int shader_new(lua_State *L, const char *vertex, const char *fragment) { | ||
char log[1024]; | ||
GLsizei log_len; | ||
GLuint fs = 0, vs = 0, po = 0; | ||
|
||
// Pixel | ||
vs = glCreateShader(GL_VERTEX_SHADER); | ||
glShaderSource(vs, 1, &vertex, NULL); | ||
glCompileShader(vs); | ||
|
||
glGetShaderInfoLog(vs, sizeof(log), &log_len, log); | ||
if (log_len > 0) | ||
goto error; | ||
|
||
// Fragment | ||
fs = glCreateShader(GL_FRAGMENT_SHADER); | ||
glShaderSource(fs, 1, &fragment, NULL); | ||
glCompileShader(fs); | ||
|
||
glGetShaderInfoLog(fs, sizeof(log), &log_len, log); | ||
if (log_len > 0) | ||
goto error; | ||
|
||
// Program Object | ||
po = glCreateProgram(); | ||
glAttachShader(po, vs); | ||
glAttachShader(po, fs); | ||
glLinkProgram(po); | ||
|
||
glGetProgramInfoLog(fs, sizeof(log), &log_len, log); | ||
if (log_len > 0) | ||
goto error; | ||
|
||
// GLint compiled; | ||
// glGetObjectParameteriv(ShaderObject, GL_COMPILE_STATUS, &compiled); | ||
// if (!compiled) | ||
// luaL_error(L, "%s", log); | ||
// | ||
// fprintf(stderr, "SHADER: %d %d %d\n", fs, vs, po); | ||
|
||
shader_t *shader = push_shader(L); | ||
shader->fs = fs; | ||
shader->vs = vs; | ||
shader->po = po; | ||
return 1; | ||
|
||
error: | ||
if (po) | ||
glDeleteProgram(po); | ||
if (vs) | ||
glDeleteShader(vs); | ||
if (fs) | ||
glDeleteShader(fs); | ||
return luaL_error(L, "%s", log); | ||
} | ||
|
||
static int shader_gc(lua_State *L) { | ||
shader_t *shader = to_shader(L, 1); | ||
glDeleteProgram(shader->po); | ||
glDeleteShader(shader->vs); | ||
glDeleteShader(shader->fs); | ||
return 0; | ||
} | ||
|
||
LUA_TYPE_IMPL(shader); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* See Copyright Notice in LICENSE.txt */ | ||
|
||
#ifndef SHADER_H | ||
#define SHADER_H | ||
|
||
int shader_register(lua_State *L); | ||
int shader_new(lua_State *L, const char *vertex, const char *fragment); | ||
|
||
#endif |