Skip to content
Snippets Groups Projects
Commit d1c7942c authored by dg's avatar dg
Browse files

preliminary shaders

git-svn-id: http://svn.net-core.org/repos/t-engine4@733 51575b47-30f0-44d4-a5cc-537603b46e54
parent 42e8102e
No related branches found
No related tags found
No related merge requests found
......@@ -305,6 +305,14 @@ function _M:cleanFOV()
self._map:cleanSeen()
end
local shad = core.shader.newProgram(nil, [[
void main()
{
// Sampling The Texture And Passing It To The Frame Buffer
gl_FragColor = vec4(0.0,1.0,0.0,1.0);
}
]])
--- Updates the map on the given spot
-- This updates many things, from the C map object, the FOV caches, the minimap if it exists, ...
function _M:updateMap(x, y)
......@@ -361,10 +369,10 @@ function _M:updateMap(x, y)
-- Cache the textures in the C map object
self._map:setGrid(x, y,
g, g_r, g_g, g_b,
t, t_r, t_g, t_b,
o, o_r, o_g, o_b,
a, a_r, a_g, a_b,
g, shad, g_r, g_g, g_b,
t, nil, t_r, t_g, t_b,
o, nil, o_r, o_g, o_b,
a, nil, a_r, a_g, a_b,
mm
)
......
No preview for this file type
......@@ -61,6 +61,7 @@ int luaopen_map(lua_State *L);
int luaopen_particles(lua_State *L);
int luaopen_sound(lua_State *L);
int luaopen_lanes(lua_State *L);
int luaopen_shaders(lua_State *L);
int luaopen_noise(lua_State *L);
static int traceback (lua_State *L) {
......@@ -464,6 +465,7 @@ void boot_lua(int state, bool rebooting, int argc, char *argv[])
luaopen_particles(L);
luaopen_sound(L);
luaopen_noise(L);
luaopen_shaders(L);
// Make the uids repository
lua_newtable(L);
......
......@@ -205,30 +205,35 @@ static int map_set_grid(lua_State *L)
int y = luaL_checknumber(L, 3);
GLuint *g = lua_isnil(L, 4) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 4);
float g_r = lua_tonumber(L, 5);
float g_g = lua_tonumber(L, 6);
float g_b = lua_tonumber(L, 7);
GLuint *t = lua_isnil(L, 8) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 8);
float t_r = lua_tonumber(L, 9);
float t_g = lua_tonumber(L, 10);
float t_b = lua_tonumber(L, 11);
GLuint *o = lua_isnil(L, 12) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 12);
float o_r = lua_tonumber(L, 13);
float o_g = lua_tonumber(L, 14);
float o_b = lua_tonumber(L, 15);
GLuint *a = lua_isnil(L, 16) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 16);
float a_r = lua_tonumber(L, 17);
float a_g = lua_tonumber(L, 18);
float a_b = lua_tonumber(L, 19);
unsigned char mm = lua_tonumber(L, 20);
GLuint *shad_g = lua_isnil(L, 5) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{shader}", 5);
float g_r = lua_tonumber(L, 6);
float g_g = lua_tonumber(L, 7);
float g_b = lua_tonumber(L, 8);
GLuint *t = lua_isnil(L, 9) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 9);
GLuint *shad_t = lua_isnil(L, 10) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{shader}", 10);
float t_r = lua_tonumber(L, 11);
float t_g = lua_tonumber(L, 12);
float t_b = lua_tonumber(L, 13);
GLuint *o = lua_isnil(L, 14) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 14);
GLuint *shad_o = lua_isnil(L, 15) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{shader}", 15);
float o_r = lua_tonumber(L, 16);
float o_g = lua_tonumber(L, 17);
float o_b = lua_tonumber(L, 18);
GLuint *a = lua_isnil(L, 19) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{texture}", 19);
GLuint *shad_a = lua_isnil(L, 20) ? NULL : (GLuint*)auxiliar_checkclass(L, "gl{shader}", 20);
float a_r = lua_tonumber(L, 21);
float a_g = lua_tonumber(L, 22);
float a_b = lua_tonumber(L, 23);
unsigned char mm = lua_tonumber(L, 24);
if (x < 0 || y < 0 || x >= map->w || y >= map->h) return 0;
map->grids_terrain[x][y].texture = g ? *g : 0;
map->grids_terrain[x][y].shader = shad_g ? *shad_g : 0;
map->grids_terrain[x][y].tint_r = g_r;
map->grids_terrain[x][y].tint_g = g_g;
map->grids_terrain[x][y].tint_b = g_b;
......@@ -471,6 +476,7 @@ static int map_to_screen(lua_State *L)
glColor4f((map->shown_r + m->tint_r)/2, (map->shown_g + m->tint_g)/2, (map->shown_b + m->tint_b)/2, a);
else
glColor4f(map->shown_r, map->shown_g, map->shown_b, a);
if (m->shader) glUseProgramObjectARB(m->shader);
glBindTexture(GL_TEXTURE_2D, map->grids_terrain[i][j].texture);
glBegin(GL_QUADS);
glTexCoord2f(0,0); glVertex3f(0 +dx, 0 +dy,-99);
......@@ -478,6 +484,7 @@ static int map_to_screen(lua_State *L)
glTexCoord2f(1,1); glVertex3f(map->tile_w +dx, map->tile_h +dy,-99);
glTexCoord2f(0,1); glVertex3f(0 +dx, map->tile_h +dy,-99);
glEnd();
if (m->shader) glUseProgramObjectARB(0);
}
}
}
......
......@@ -25,6 +25,7 @@
typedef struct {
GLuint texture;
GLuint shader;
float tint_r;
float tint_g;
float tint_b;
......
/*
TE4 - T-Engine 4
Copyright (C) 2009, 2010 Nicolas Casalini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Nicolas Casalini "DarkGod"
darkgod@te4.org
*/
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#include "auxiliar.h"
#include "types.h"
#include "music.h"
#include "script.h"
#include "tSDL.h"
#include "tgl.h"
#include "shaders.h"
bool shaders_active = TRUE;
static GLuint loadShader(const char* code, GLuint type)
{
GLuint v = glCreateShaderObjectARB(type);
glShaderSourceARB(v, 1, &code, 0);
glCompileShaderARB(v);
CHECKGLSLCOMPILE(v, "inline");
printf("New GL Shader %d of type %d\n", v, type);
return v;
}
static int program_new(lua_State *L)
{
if (!shaders_active) return 0;
const char *vert = lua_isstring(L, 1) ? lua_tostring(L, 1) : NULL;
const char *frag = lua_isstring(L, 2) ? lua_tostring(L, 2) : NULL;
GLuint *s = (GLuint*)lua_newuserdata(L, sizeof(GLuint));
auxiliar_setclass(L, "gl{shader}", -1);
*s = glCreateProgramObjectARB();
if (vert) glAttachObjectARB(*s, loadShader(vert, GL_VERTEX_SHADER));
if (frag) glAttachObjectARB(*s, loadShader(frag, GL_FRAGMENT_SHADER));
glLinkProgramARB(*s);
CHECKGLSLLINK(*s);
printf("New GL Shader program %d\n", *s);
return 1;
}
static int program_free(lua_State *L)
{
GLuint *s = (GLuint*)auxiliar_checkclass(L, "gl{shader}", 1);
/*
if(VertShader)
{
glDetachObjectARB(Program,VertShader);
glDeleteObjectARB(VertShader);
}
if(FragShader)
{
glDetachObjectARB(Program,FragShader);
glDeleteObjectARB(FragShader);
}
*/
glDeleteObjectARB(*s);
lua_pushnumber(L, 1);
return 1;
}
static const struct luaL_reg shaderlib[] =
{
{"newProgram", program_new},
{NULL, NULL},
};
static const struct luaL_reg shader_reg[] =
{
{"__gc", program_free},
{NULL, NULL},
};
int luaopen_shaders(lua_State *L)
{
auxiliar_newclass(L, "gl{shader}", shader_reg);
luaL_openlib(L, "core.shader", shaderlib, 0);
return 1;
}
#ifndef __SHADERS_H
#define __SHADERS_H
inline bool _CheckGLSLShaderCompile(GLuint shader, const char* file)
{
int success;
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetObjectParameterivARB(shader, GL_COMPILE_STATUS, &success);
glGetObjectParameterivARB(shader, GL_INFO_LOG_LENGTH,&infologLength);
if(infologLength>0)
{
infoLog = (char *)malloc(infologLength);
glGetInfoLogARB(shader, infologLength, &charsWritten, infoLog);
}
if(success!=GL_TRUE)
{
// something went wrong
printf("GLSL ERROR: Compile error in shader %s\n", file);
printf("%s\n",infoLog);
free(infoLog);
return FALSE;
}
#ifdef _SHADERVERBOSE
if(infologLength>1)
{
// nothing went wrong, just warnings or messages
printf("GLSL WARNING: Compile log for shader %s\n", file);
printf("%s\n",infoLog);
}
#endif
if(infologLength>0)
{
free(infoLog);
}
return TRUE;
}
inline bool _CheckGLSLProgramLink(GLuint program)
{
int success;
glGetObjectParameterivARB(program, GL_LINK_STATUS, &success);
if(success!=GL_TRUE)
{
// Something went Wrong
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetObjectParameterivARB(program, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
printf("OPENGL ERROR: Program link Error");
printf("%s\n",infoLog);
free(infoLog);
}
return FALSE;
}
return TRUE;
}
inline bool _CheckGLSLProgramValid(GLuint program)
{
int success;
glGetObjectParameterivARB(program, GL_VALIDATE_STATUS, &success);
if(success!=GL_TRUE)
{
// Something went Wrong
int infologLength = 0;
int charsWritten = 0;
char *infoLog;
glGetObjectParameterivARB(program, GL_INFO_LOG_LENGTH,&infologLength);
if (infologLength > 0)
{
infoLog = (char *)malloc(infologLength);
glGetInfoLogARB(program, infologLength, &charsWritten, infoLog);
printf("OPENGL ERROR: Program Validation Failure");
printf("%s\n",infoLog);
free(infoLog);
}
return FALSE;
}
return TRUE;
}
#ifdef _DEBUG
#define CHECKGL( GLcall ) \
GLcall; \
if(!_CheckGL_Error( #GLcall, __FILE__, __LINE__)) \
exit(-1);
#define CHECKGLSLCOMPILE( Shader, file ) \
if(!_CheckGLSLShaderCompile( Shader , file)) \
exit(-1);
#define CHECKGLSLLINK( Program ) \
if(!_CheckGLSLProgramLink( Program )) \
exit(-1);
#define CHECKGLSLVALID( Program ) \
glValidateProgramARB( Program ); \
if(!_CheckGLSLProgramValid( Program )) \
exit(-1);
#else
#define CHECKGL( GLcall) \
GLcall;
#define CHECKGLSLCOMPILE( Shader, file )
#define CHECKGLSLLINK( Program )
#define CHECKGLSLVALID( Program )
#endif
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment