convert GLShader to a Ruby class
This commit is contained in:
parent
c3b4c33304
commit
bf15a702ab
@ -1,3 +1,4 @@
|
||||
#if 0
|
||||
#include "GLProgram.h"
|
||||
#include <stdint.h>
|
||||
#include <iostream>
|
||||
@ -80,3 +81,4 @@ GLint GLProgram::get_uniform(const std::string & uniform_name)
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,4 @@
|
||||
#if 0
|
||||
#ifndef GLPROGRAM_H
|
||||
#define GLPROGRAM_H
|
||||
|
||||
@ -25,3 +26,4 @@ protected:
|
||||
typedef Ref<GLProgram> GLProgramRef;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
114
src/GLShader.cc
114
src/GLShader.cc
@ -1,69 +1,99 @@
|
||||
#include "GLShader.h"
|
||||
#include <iostream>
|
||||
#include "ruby.h"
|
||||
#include "gl3w.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
GLShader::~GLShader()
|
||||
typedef struct
|
||||
{
|
||||
if (m_id > 0)
|
||||
GLuint id;
|
||||
} GLShader;
|
||||
|
||||
static VALUE ruby_class;
|
||||
|
||||
static const char * get_shader_type_name(GLenum shader_type)
|
||||
{
|
||||
switch (shader_type)
|
||||
{
|
||||
glDeleteShader(m_id);
|
||||
case GL_VERTEX_SHADER:
|
||||
return "vertex";
|
||||
case GL_FRAGMENT_SHADER:
|
||||
return "fragment";
|
||||
default:
|
||||
return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
bool GLShader::create(GLenum shader_type, const char *source, size_t size)
|
||||
static GLuint create_shader(GLenum shader_type, const char *source, size_t size)
|
||||
{
|
||||
GLint status;
|
||||
|
||||
m_id = glCreateShader(shader_type);
|
||||
if (m_id > 0)
|
||||
GLuint id = glCreateShader(shader_type);
|
||||
if (id > 0)
|
||||
{
|
||||
GLint status;
|
||||
|
||||
GLint length = size;
|
||||
glShaderSource(m_id, 1, &source, &length);
|
||||
glShaderSource(id, 1, &source, &length);
|
||||
|
||||
glCompileShader(m_id);
|
||||
glCompileShader(id);
|
||||
|
||||
glGetShaderiv(m_id, GL_COMPILE_STATUS, &status);
|
||||
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
|
||||
if (status == GL_TRUE)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
GLint log_length;
|
||||
cerr << "Error compiling ";
|
||||
switch (shader_type)
|
||||
{
|
||||
case GL_VERTEX_SHADER:
|
||||
cerr << "vertex";
|
||||
break;
|
||||
case GL_FRAGMENT_SHADER:
|
||||
cerr << "fragment";
|
||||
break;
|
||||
}
|
||||
cerr << " shader" << endl;
|
||||
glGetShaderiv(m_id, GL_INFO_LOG_LENGTH, &log_length);
|
||||
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &log_length);
|
||||
if (log_length > 0)
|
||||
{
|
||||
char * log = new char[log_length];
|
||||
glGetShaderInfoLog(m_id, log_length, &log_length, log);
|
||||
cerr << "Shader Log:" << endl << log << endl;
|
||||
delete[] log;
|
||||
char log[log_length + 1];
|
||||
glGetShaderInfoLog(id, log_length, &log_length, log);
|
||||
log[log_length] = '\0';
|
||||
glDeleteShader(id);
|
||||
rb_raise(rb_eRuntimeError,
|
||||
"Error compiling %s shader:\n%s\n%s\n%s\n",
|
||||
get_shader_type_name(shader_type),
|
||||
"--------------------",
|
||||
log,
|
||||
"--------------------");
|
||||
}
|
||||
else
|
||||
{
|
||||
glDeleteShader(id);
|
||||
rb_raise(rb_eRuntimeError,
|
||||
"Error compiling %s shader",
|
||||
get_shader_type_name(shader_type));
|
||||
}
|
||||
glDeleteShader(m_id);
|
||||
}
|
||||
return false;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
bool GLShader::create(GLenum shader_type, PathRef path)
|
||||
static void GLShader_free(void * ptr)
|
||||
{
|
||||
uint8_t * file;
|
||||
size_t size;
|
||||
if (!path->read(&file, &size))
|
||||
return false;
|
||||
if (size == 0u)
|
||||
return false;
|
||||
GLShader * glshader = (GLShader *)ptr;
|
||||
|
||||
bool result = create(shader_type, (const char *)file, size);
|
||||
delete[] file;
|
||||
return result;
|
||||
if (glshader->id > 0)
|
||||
{
|
||||
glDeleteShader(glshader->id);
|
||||
}
|
||||
|
||||
delete glshader;
|
||||
}
|
||||
|
||||
static VALUE GLShader_new(VALUE klass, VALUE type, VALUE source)
|
||||
{
|
||||
GLShader * glshader = new GLShader();
|
||||
|
||||
#if 0
|
||||
/* TODO */
|
||||
glshader->id = create_shader();
|
||||
#endif
|
||||
|
||||
return Data_Wrap_Struct(klass, NULL, GLShader_free, glshader);
|
||||
}
|
||||
|
||||
void GLShader_Init()
|
||||
{
|
||||
ruby_class = rb_define_class("GLShader", rb_cObject);
|
||||
rb_define_singleton_method(ruby_class, "new", (VALUE(*)(...))GLShader_new, 2);
|
||||
rb_define_attr(ruby_class, "id", 0, 1);
|
||||
}
|
||||
|
@ -1,22 +1,6 @@
|
||||
#ifndef GLSHADER_H
|
||||
#define GLSHADER_H
|
||||
|
||||
#include "Ref.h"
|
||||
#include "Path.h"
|
||||
#include "gl3w.h"
|
||||
|
||||
class GLShader
|
||||
{
|
||||
public:
|
||||
GLShader() { m_id = 0u; }
|
||||
~GLShader();
|
||||
bool create(GLenum shader_type, const char *source, size_t size);
|
||||
bool create(GLenum shader_type, PathRef path);
|
||||
GLuint get_id() { return m_id; }
|
||||
bool valid() { return m_id > 0; }
|
||||
protected:
|
||||
GLuint m_id;
|
||||
};
|
||||
typedef Ref<GLShader> GLShaderRef;
|
||||
void GLShader_Init();
|
||||
|
||||
#endif
|
||||
|
@ -1,10 +1,8 @@
|
||||
#if 0
|
||||
#include "GUI.h"
|
||||
#include "gl3w.h"
|
||||
#include <iostream>
|
||||
|
||||
#define WIDTH 500
|
||||
#define HEIGHT 500
|
||||
|
||||
bool GUI::load()
|
||||
{
|
||||
glClearColor (0.0, 0.0, 0.0, 0.0);
|
||||
@ -146,3 +144,4 @@ void GUI::draw_rect(int x, int y, int width, int height, float r, float g, float
|
||||
glVertexAttribIPointer(0, 2, GL_INT, 0, 0);
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
|
||||
}
|
||||
#endif
|
||||
|
@ -1,3 +1,4 @@
|
||||
#if 0
|
||||
#ifndef JES_GUI_H
|
||||
#define JES_GUI_H
|
||||
|
||||
@ -35,3 +36,4 @@ protected:
|
||||
typedef Ref<GUI> GUIRef;
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
@ -59,7 +59,7 @@ static void Window_free(void * ptr)
|
||||
if (window->sdl_window)
|
||||
SDL_DestroyWindow(window->sdl_window);
|
||||
|
||||
free(window);
|
||||
delete window;
|
||||
}
|
||||
|
||||
static VALUE Window_new(VALUE klass)
|
||||
@ -72,7 +72,7 @@ static VALUE Window_new(VALUE klass)
|
||||
}
|
||||
|
||||
Init_SDL();
|
||||
Window * window = (Window *)malloc(sizeof(Window));
|
||||
Window * window = new Window();
|
||||
window->sdl_window = SDL_CreateWindow("jes",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
|
Loading…
x
Reference in New Issue
Block a user