Skip to content

Commit

Permalink
Added functions to load shaders from files and added a texture
Browse files Browse the repository at this point in the history
test/example
  • Loading branch information
philogb committed Jul 27, 2009
1 parent ebb9941 commit 88b085b
Show file tree
Hide file tree
Showing 8 changed files with 826 additions and 69 deletions.
675 changes: 675 additions & 0 deletions examples/gles/ch9-simple-texture-cubemap.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions examples/gles/shaders/ch9-fshader.sl
@@ -0,0 +1,7 @@
/*precision mediump float;*/
varying vec3 v_normal;
uniform samplerCube s_texture;
void main()
{
gl_FragColor = textureCube(s_texture, v_normal);
}
8 changes: 8 additions & 0 deletions examples/gles/shaders/ch9-vshader.sl
@@ -0,0 +1,8 @@
attribute vec4 a_position;
attribute vec3 a_normal;
varying vec3 v_normal;
void main()
{
gl_Position = a_position;
v_normal = a_normal;
}
91 changes: 57 additions & 34 deletions glesbindings/glesbind.cpp
Expand Up @@ -6,6 +6,7 @@
#include "glew_desktop_shim.h"
#elif defined(__APPLE__)
#include <OpenGL/OpenGL.h>
#include "gles_desktop_shim.h"
#else
#define GL_GLEXT_PROTOTYPES
#include <GL/gl.h>
Expand All @@ -14,11 +15,14 @@
#include "gles_desktop_shim.h"
#endif

#include <fstream>
#include <string>
using namespace v8;


Persistent<Object> GlesFactory::self_;
Persistent<Context> GlesFactory::gles_persistent_context;
char* GlesFactory::root_path;
// glGenBuffers uses an output parameter to return an array of ints.
Handle<Value> GLESglGenBuffersCallback(const Arguments& args) {
if (args.Length() != 1)
Expand Down Expand Up @@ -1035,9 +1039,7 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
if(args[8]->IsArray()) {
Handle<Array> arr_handle = Handle<Array>::Cast(args[8]);

switch(type) {
case GL_UNSIGNED_BYTE:
{
if( type == GL_UNSIGNED_BYTE ) {
GLubyte* pixels = new GLubyte[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1055,11 +1057,7 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_BYTE:
{
} else if( type == GL_BYTE ) {
GLbyte* pixels = new GLbyte[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1077,11 +1075,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_BITMAP:
{
} else if( type == GL_BITMAP ) {
GLbitfield* pixels = new GLbitfield[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1099,11 +1094,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_UNSIGNED_SHORT:
{
} else if( type == GL_UNSIGNED_SHORT ) {
GLushort* pixels = new GLushort[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1121,11 +1113,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_SHORT:
{
} else if( type == GL_SHORT ) {
GLshort* pixels = new GLshort[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1143,11 +1132,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_UNSIGNED_INT:
{
} else if( type == GL_UNSIGNED_INT ) {
GLuint* pixels = new GLuint[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1165,11 +1151,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_INT:
{
} else if( type == GL_INT ) {
GLint* pixels = new GLint[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1187,11 +1170,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_FLOAT:
{
} else if( type == GL_FLOAT ) {
GLfloat* pixels = new GLfloat[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1210,8 +1190,6 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {

delete[] pixels;
}
break;
}
}

return v8::Undefined();
Expand Down Expand Up @@ -1515,6 +1493,49 @@ Handle<Value> GLESglGetShaderSourceCallback(const Arguments& args) {
return handle_scope.Close(String::New(log));
}

// We expect to be called with a shader id and a single string.
Handle<Value> GLESglShaderSourceFileCallback(const Arguments& args) {
if (args.Length() != 2)
return v8::Undefined();

HandleScope handle_scope;
GLuint shader_id = args[0]->Uint32Value();
// GLSL source is defined as an ASCII subset.
v8::String::AsciiValue filepath_ascii(args[1]);
if (!*filepath_ascii)
return v8::Undefined();

char* filepath_str = *filepath_ascii;

//read the file source
char* filename = new char[strlen(GlesFactory::root_path) + strlen(filepath_str) +1];
strcpy(filename, GlesFactory::root_path);
strcat(filename, filepath_str);

std::ifstream in_file(filename);

if(!in_file.is_open()) return v8::Undefined();

std::string line, full_text = "";
while (! in_file.eof() ) {
std::getline (in_file, line);
full_text += line + "\n";
}

char* ans = new char[full_text.length() +1];
strcpy(ans, full_text.c_str());

GLsizei code_len = full_text.length();
glShaderSource(shader_id, 1, (const GLchar**) &ans, &code_len);

delete[] ans;
delete[] filename;

Handle<Object> res(GlesFactory::self_);
return res;
}



Handle<Value> GLESglActiveTextureCallback(const Arguments& args) {
//if less that nbr of formal parameters then do nothing
Expand Down Expand Up @@ -4583,6 +4604,8 @@ Handle<ObjectTemplate> GlesFactory::createGles(void) {

Gles->Set(String::NewSymbol("GetRenderbufferParameter"), FunctionTemplate::New(GLESglGetRenderbufferParameterCallback));

Gles->Set(String::NewSymbol("ShaderSourceFile"), FunctionTemplate::New(GLESglShaderSourceFileCallback));


// Again, return the result through the current handle scope.
return handle_scope.Close(Gles);
Expand Down
1 change: 1 addition & 0 deletions glesbindings/glesbind.h
Expand Up @@ -14,6 +14,7 @@ class GlesFactory {

static v8::Persistent<v8::Object> self_;
static v8::Persistent<v8::Context> gles_persistent_context;
static char* root_path;
};

#endif /* GLESBIND_H_ */
5 changes: 4 additions & 1 deletion glesbindings/glesbind.py
Expand Up @@ -82,7 +82,8 @@
accessor_extras = ['glGetParameter',
'glGetTexParameter',
'glGetVertexAttrib',
'glGetRenderbufferParameter']
'glGetRenderbufferParameter',
'glShaderSourceFile']

EXCLUDE = re.compile(exclude, re.VERBOSE)
TEMPLATE = re.compile(template, re.VERBOSE)
Expand Down Expand Up @@ -141,6 +142,8 @@ def main():
#include "gles_desktop_shim.h"
#endif
#include <fstream>
#include <string>
using namespace v8;
Expand Down
85 changes: 51 additions & 34 deletions glesbindings/glescustom.cpp
Expand Up @@ -1014,9 +1014,7 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
if(args[8]->IsArray()) {
Handle<Array> arr_handle = Handle<Array>::Cast(args[8]);

switch(type) {
case GL_UNSIGNED_BYTE:
{
if( type == GL_UNSIGNED_BYTE ) {
GLubyte* pixels = new GLubyte[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1034,11 +1032,7 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_BYTE:
{
} else if( type == GL_BYTE ) {
GLbyte* pixels = new GLbyte[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1056,11 +1050,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_BITMAP:
{
} else if( type == GL_BITMAP ) {
GLbitfield* pixels = new GLbitfield[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1078,11 +1069,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_UNSIGNED_SHORT:
{
} else if( type == GL_UNSIGNED_SHORT ) {
GLushort* pixels = new GLushort[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1100,11 +1088,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_SHORT:
{
} else if( type == GL_SHORT ) {
GLshort* pixels = new GLshort[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1122,11 +1107,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_UNSIGNED_INT:
{
} else if( type == GL_UNSIGNED_INT ) {
GLuint* pixels = new GLuint[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1144,11 +1126,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_INT:
{
} else if( type == GL_INT ) {
GLint* pixels = new GLint[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1166,11 +1145,8 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {
(const void*)pixels);

delete[] pixels;
}
break;

case GL_FLOAT:
{
} else if( type == GL_FLOAT ) {
GLfloat* pixels = new GLfloat[arr_handle->Length()];
for (unsigned j = 0; j < arr_handle->Length(); j++) {
Handle<Value> arg(arr_handle->Get(Integer::New(j)));
Expand All @@ -1189,8 +1165,6 @@ Handle<Value> GLESglTexImage2DCallback(const Arguments& args) {

delete[] pixels;
}
break;
}
}

return v8::Undefined();
Expand Down Expand Up @@ -1493,3 +1467,46 @@ Handle<Value> GLESglGetShaderSourceCallback(const Arguments& args) {

return handle_scope.Close(String::New(log));
}

// We expect to be called with a shader id and a single string.
Handle<Value> GLESglShaderSourceFileCallback(const Arguments& args) {
if (args.Length() != 2)
return v8::Undefined();

HandleScope handle_scope;
GLuint shader_id = args[0]->Uint32Value();
// GLSL source is defined as an ASCII subset.
v8::String::AsciiValue filepath_ascii(args[1]);
if (!*filepath_ascii)
return v8::Undefined();

char* filepath_str = *filepath_ascii;

//read the file source
char* filename = new char[strlen(GlesFactory::root_path) + strlen(filepath_str) +1];
strcpy(filename, GlesFactory::root_path);
strcat(filename, filepath_str);

std::ifstream in_file(filename);

if(!in_file.is_open()) return v8::Undefined();

std::string line, full_text = "";
while (! in_file.eof() ) {
std::getline (in_file, line);
full_text += line + "\n";
}

char* ans = new char[full_text.length() +1];
strcpy(ans, full_text.c_str());

GLsizei code_len = full_text.length();
glShaderSource(shader_id, 1, (const GLchar**) &ans, &code_len);

delete[] ans;
delete[] filename;

Handle<Object> res(GlesFactory::self_);
return res;
}

0 comments on commit 88b085b

Please sign in to comment.