2008-09-06 23:23:19 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2008 Cyril Comparon <cyril.comparon@gmail.com>
|
|
|
|
* Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
2008-09-29 21:45:10 +00:00
|
|
|
/**
|
|
|
|
* SECTION:element-glbumper
|
|
|
|
*
|
|
|
|
* Bump mapping using the normal method.
|
|
|
|
*
|
|
|
|
* <refsect2>
|
|
|
|
* <title>Examples</title>
|
|
|
|
* |[
|
|
|
|
* gst-launch -v videotestsrc ! glupload ! glbumper location=normalmap.bmp ! glimagesink
|
|
|
|
* ]| A pipeline to test normal mapping.
|
2009-01-15 17:39:48 +00:00
|
|
|
* FBO (Frame Buffer Object) and GLSL (OpenGL Shading Language) are required.
|
2008-09-29 21:45:10 +00:00
|
|
|
* </refsect2>
|
|
|
|
*/
|
|
|
|
|
2008-09-06 23:23:19 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2009-02-23 11:35:48 +00:00
|
|
|
#include <stdlib.h>
|
2009-01-22 00:16:38 +00:00
|
|
|
#include <png.h>
|
2009-01-22 01:19:31 +00:00
|
|
|
#include "gstglbumper.h"
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT gst_gl_bumper_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
|
|
|
static const GstElementDetails element_details =
|
2009-02-11 06:39:14 +00:00
|
|
|
GST_ELEMENT_DETAILS ("OpenGL bumper filter",
|
2008-09-06 23:23:19 +00:00
|
|
|
"Filter/Effect",
|
|
|
|
"Bump mapping filter",
|
|
|
|
"Cyril Comparon <cyril.comparon@gmail.com>, Julien Isorce <julien.isorce@gmail.com>");
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
PROP_0,
|
|
|
|
PROP_LOCATION
|
|
|
|
};
|
|
|
|
|
|
|
|
#define DEBUG_INIT(bla) \
|
|
|
|
GST_DEBUG_CATEGORY_INIT (gst_gl_bumper_debug, "glbumper", 0, "glbumper element");
|
|
|
|
|
|
|
|
GST_BOILERPLATE_FULL (GstGLBumper, gst_gl_bumper, GstGLFilter,
|
2009-02-11 06:39:14 +00:00
|
|
|
GST_TYPE_GL_FILTER, DEBUG_INIT);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
static void gst_gl_bumper_set_property (GObject * object, guint prop_id,
|
2009-02-11 06:39:14 +00:00
|
|
|
const GValue * value, GParamSpec * pspec);
|
2008-09-06 23:23:19 +00:00
|
|
|
static void gst_gl_bumper_get_property (GObject * object, guint prop_id,
|
2009-02-11 06:39:14 +00:00
|
|
|
GValue * value, GParamSpec * pspec);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
static void gst_gl_bumper_reset (GstGLFilter * filter);
|
|
|
|
static void gst_gl_bumper_init_shader (GstGLFilter * filter);
|
2008-09-06 23:23:19 +00:00
|
|
|
static gboolean gst_gl_bumper_filter (GstGLFilter * filter,
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLBuffer * inbuf, GstGLBuffer * outbuf);
|
|
|
|
static void gst_gl_bumper_callback (gint width, gint height, guint texture,
|
|
|
|
gpointer stuff);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//vertex source
|
|
|
|
static const gchar *bumper_v_src =
|
2009-02-11 06:39:14 +00:00
|
|
|
"attribute vec3 aTangent;\n"
|
|
|
|
"\n"
|
|
|
|
"varying vec3 vNormal;\n"
|
|
|
|
"varying vec3 vTangent;\n"
|
|
|
|
"varying vec3 vVertexToLight0;\n"
|
|
|
|
"varying vec3 vVertexToLight1;\n"
|
|
|
|
"\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" // transform the vertex\n"
|
|
|
|
" gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;\n"
|
|
|
|
"\n"
|
|
|
|
" // transform the normal and the tangent to scene coords\n"
|
|
|
|
" vNormal = normalize(gl_NormalMatrix * gl_Normal);\n"
|
|
|
|
" vTangent = normalize(gl_NormalMatrix * aTangent);\n"
|
|
|
|
"\n"
|
|
|
|
" // transforming the vertex position to modelview-space\n"
|
|
|
|
" //const vec4 vertexInSceneCoords = gl_ModelViewMatrix * gl_Vertex;\n"
|
|
|
|
"\n"
|
|
|
|
" // calculate the vector from the vertex position to the light position\n"
|
|
|
|
" vVertexToLight0 = normalize(gl_LightSource[0].position).xyz;\n"
|
|
|
|
" vVertexToLight1 = normalize(gl_LightSource[1].position).xyz;\n"
|
|
|
|
"\n"
|
|
|
|
" // transit vertex color\n"
|
|
|
|
" gl_FrontColor = gl_BackColor = gl_Color;\n"
|
|
|
|
"\n"
|
|
|
|
" // use the two first sets of texture coordinates in the fragment shader\n"
|
|
|
|
" gl_TexCoord[0] = gl_MultiTexCoord0;\n"
|
|
|
|
" gl_TexCoord[1] = gl_MultiTexCoord1;\n" "}\n";
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//fragment source
|
|
|
|
static const gchar *bumper_f_src =
|
2009-02-11 06:39:14 +00:00
|
|
|
"#extension GL_ARB_texture_rectangle : enable\n"
|
|
|
|
"uniform sampler2DRect texture0;\n"
|
|
|
|
"uniform sampler2DRect texture1;\n"
|
|
|
|
"\n"
|
|
|
|
"varying vec3 vNormal;\n"
|
|
|
|
"varying vec3 vTangent;\n"
|
|
|
|
"varying vec3 vVertexToLight0;\n"
|
|
|
|
"varying vec3 vVertexToLight1;\n"
|
|
|
|
"\n"
|
|
|
|
"void main()\n"
|
|
|
|
"{\n"
|
|
|
|
" // get the color of the textures\n"
|
|
|
|
" vec4 textureColor = texture2DRect(texture0, gl_TexCoord[0].st);\n"
|
|
|
|
" vec3 normalmapItem = texture2DRect(texture1, gl_TexCoord[1].st).xyz * 2.0 - 1.0;\n"
|
|
|
|
"\n"
|
|
|
|
" // calculate matrix that transform from tangent space to normalmap space (contrary of intuition)\n"
|
|
|
|
" vec3 binormal = cross(vNormal, vTangent);\n"
|
|
|
|
" mat3 tangentSpace2normalmapSpaceMat = mat3(vTangent, binormal, vNormal);\n"
|
|
|
|
"\n"
|
|
|
|
" // disturb the normal\n"
|
|
|
|
" vec3 disturbedNormal = tangentSpace2normalmapSpaceMat * normalmapItem;\n"
|
|
|
|
"\n"
|
|
|
|
" // calculate the diffuse term and clamping it to [0;1]\n"
|
|
|
|
" float diffuseTerm0 = clamp(dot(disturbedNormal, vVertexToLight0), 0.0, 1.0);\n"
|
|
|
|
" float diffuseTerm1 = clamp(dot(disturbedNormal, vVertexToLight1), 0.0, 1.0);\n"
|
|
|
|
"\n"
|
|
|
|
" vec3 irradiance = (diffuseTerm0 * gl_LightSource[0].diffuse.rgb + diffuseTerm1 * gl_LightSource[1].diffuse.rgb);\n"
|
|
|
|
"\n"
|
|
|
|
" // calculate the final color\n"
|
|
|
|
" gl_FragColor = vec4(irradiance * textureColor.rgb, textureColor.w);\n"
|
|
|
|
"}\n";
|
2008-09-06 23:23:19 +00:00
|
|
|
|
2009-01-22 00:16:38 +00:00
|
|
|
#define LOAD_ERROR(msg) { GST_WARNING ("unable to load %s: %s", bumper->location, msg); display->isAlive = FALSE; return; }
|
|
|
|
|
|
|
|
//png reading error handler
|
2009-02-11 06:39:14 +00:00
|
|
|
static void
|
|
|
|
user_warning_fn (png_structp png_ptr, png_const_charp warning_msg)
|
2009-01-22 00:16:38 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
g_warning ("%s\n", warning_msg);
|
2009-01-22 00:16:38 +00:00
|
|
|
}
|
|
|
|
|
2008-09-06 23:23:19 +00:00
|
|
|
//Called in the gl thread
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_init_resources (GstGLFilter * filter)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
|
|
|
GstGLBumper *bumper = GST_GL_BUMPER (filter);
|
2009-01-22 00:16:38 +00:00
|
|
|
GstGLDisplay *display = filter->display;
|
2008-09-06 23:23:19 +00:00
|
|
|
|
2009-01-22 00:16:38 +00:00
|
|
|
png_structp png_ptr;
|
|
|
|
png_infop info_ptr;
|
2009-01-22 01:19:31 +00:00
|
|
|
guint sig_read = 0;
|
2009-01-22 00:16:38 +00:00
|
|
|
png_uint_32 width = 0;
|
|
|
|
png_uint_32 height = 0;
|
2009-01-22 01:19:31 +00:00
|
|
|
gint bit_depth = 0;
|
|
|
|
gint color_type = 0;
|
|
|
|
gint interlace_type = 0;
|
2009-01-22 00:16:38 +00:00
|
|
|
png_FILE_p fp = NULL;
|
|
|
|
guint y = 0;
|
|
|
|
guchar *raw_data = NULL;
|
|
|
|
guchar **rows = NULL;
|
|
|
|
|
|
|
|
if (!filter->display)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* BEGIN load png image file */
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
if ((fp = fopen (bumper->location, "rb")) == NULL)
|
2009-01-22 00:16:38 +00:00
|
|
|
LOAD_ERROR ("file not found");
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
if (png_ptr == NULL) {
|
|
|
|
fclose (fp);
|
2009-01-22 00:16:38 +00:00
|
|
|
LOAD_ERROR ("failed to initialize the png_struct");
|
|
|
|
}
|
|
|
|
|
|
|
|
png_set_error_fn (png_ptr, NULL, NULL, user_warning_fn);
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
info_ptr = png_create_info_struct (png_ptr);
|
|
|
|
if (info_ptr == NULL) {
|
|
|
|
fclose (fp);
|
|
|
|
png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
|
2009-01-22 00:16:38 +00:00
|
|
|
LOAD_ERROR ("failed to initialize the memory for image information");
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_init_io (png_ptr, fp);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_set_sig_bytes (png_ptr, sig_read);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_read_info (png_ptr, info_ptr);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
|
|
|
|
&interlace_type, int_p_NULL, int_p_NULL);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
if (color_type != PNG_COLOR_TYPE_RGB) {
|
|
|
|
fclose (fp);
|
|
|
|
png_destroy_read_struct (&png_ptr, png_infopp_NULL, png_infopp_NULL);
|
2009-01-22 00:16:38 +00:00
|
|
|
LOAD_ERROR ("color type is not rgb");
|
|
|
|
}
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
raw_data = (guchar *) malloc (sizeof (guchar) * width * height * 3);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
rows = (guchar **) malloc (sizeof (guchar *) * height);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
for (y = 0; y < height; ++y)
|
|
|
|
rows[y] = (guchar *) (raw_data + y * width * 3);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_read_image (png_ptr, rows);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
free (rows);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
png_read_end (png_ptr, info_ptr);
|
|
|
|
png_destroy_read_struct (&png_ptr, &info_ptr, png_infopp_NULL);
|
|
|
|
fclose (fp);
|
2009-01-22 00:16:38 +00:00
|
|
|
|
|
|
|
/* END load png image file */
|
|
|
|
|
|
|
|
bumper->bumpmap_width = width;
|
|
|
|
bumper->bumpmap_height = height;
|
2009-02-11 06:39:14 +00:00
|
|
|
|
2008-09-06 23:23:19 +00:00
|
|
|
glGenTextures (1, &bumper->bumpmap);
|
2009-02-11 06:39:14 +00:00
|
|
|
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, bumper->bumpmap);
|
2008-09-06 23:23:19 +00:00
|
|
|
glTexImage2D (GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA,
|
2009-02-11 06:39:14 +00:00
|
|
|
bumper->bumpmap_width, bumper->bumpmap_height, 0,
|
|
|
|
GL_RGB, GL_UNSIGNED_BYTE, raw_data);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
2009-01-22 00:16:38 +00:00
|
|
|
free (raw_data);
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//Called in the gl thread
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_reset_resources (GstGLFilter * filter)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
|
|
|
GstGLBumper *bumper = GST_GL_BUMPER (filter);
|
2009-02-11 06:39:14 +00:00
|
|
|
|
|
|
|
if (bumper->bumpmap) {
|
2008-09-06 23:23:19 +00:00
|
|
|
glDeleteTextures (1, &bumper->bumpmap);
|
|
|
|
bumper->bumpmap = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gst_gl_bumper_base_init (gpointer klass)
|
|
|
|
{
|
|
|
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
|
|
|
|
|
|
|
gst_element_class_set_details (element_class, &element_details);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_class_init (GstGLBumperClass * klass)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GObjectClass *gobject_class;
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
gobject_class = (GObjectClass *) klass;
|
|
|
|
gobject_class->set_property = gst_gl_bumper_set_property;
|
|
|
|
gobject_class->get_property = gst_gl_bumper_get_property;
|
|
|
|
|
|
|
|
GST_GL_FILTER_CLASS (klass)->filter = gst_gl_bumper_filter;
|
|
|
|
GST_GL_FILTER_CLASS (klass)->display_init_cb = gst_gl_bumper_init_resources;
|
|
|
|
GST_GL_FILTER_CLASS (klass)->display_reset_cb = gst_gl_bumper_reset_resources;
|
|
|
|
GST_GL_FILTER_CLASS (klass)->onInitFBO = gst_gl_bumper_init_shader;
|
|
|
|
GST_GL_FILTER_CLASS (klass)->onReset = gst_gl_bumper_reset;
|
|
|
|
|
|
|
|
g_object_class_install_property (gobject_class,
|
2009-02-11 06:39:14 +00:00
|
|
|
PROP_LOCATION, g_param_spec_string ("location",
|
|
|
|
"Normal map location",
|
|
|
|
"Normal map location", NULL, G_PARAM_READWRITE));
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_init (GstGLBumper * bumper, GstGLBumperClass * klass)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
bumper->shader = NULL;
|
|
|
|
bumper->bumpmap = 0;
|
|
|
|
bumper->bumpmap_width = 0;
|
|
|
|
bumper->bumpmap_height = 0;
|
|
|
|
bumper->location = NULL;
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_reset (GstGLFilter * filter)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
GstGLBumper *bumper_filter = GST_GL_BUMPER (filter);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//blocking call, wait the opengl thread has destroyed the shader
|
|
|
|
gst_gl_display_del_shader (filter->display, bumper_filter->shader);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_set_property (GObject * object, guint prop_id,
|
|
|
|
const GValue * value, GParamSpec * pspec)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
|
|
|
GstGLBumper *bumper = GST_GL_BUMPER (object);
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_LOCATION:
|
|
|
|
if (bumper->location != NULL)
|
|
|
|
g_free (bumper->location);
|
|
|
|
bumper->location = g_value_dup_string (value);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_get_property (GObject * object, guint prop_id,
|
|
|
|
GValue * value, GParamSpec * pspec)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
|
|
|
GstGLBumper *bumper = GST_GL_BUMPER (object);
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
switch (prop_id) {
|
|
|
|
case PROP_LOCATION:
|
|
|
|
g_value_set_string (value, bumper->location);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
|
|
|
|
break;
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_init_shader (GstGLFilter * filter)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
|
|
|
GstGLBumper *bumper = GST_GL_BUMPER (filter);
|
|
|
|
|
|
|
|
//blocking call, wait the opengl thread has compiled the shader
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_display_gen_shader (filter->display, bumper_v_src, bumper_f_src,
|
|
|
|
&bumper->shader);
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2009-02-11 06:39:14 +00:00
|
|
|
gst_gl_bumper_filter (GstGLFilter * filter, GstGLBuffer * inbuf,
|
|
|
|
GstGLBuffer * outbuf)
|
2008-09-06 23:23:19 +00:00
|
|
|
{
|
|
|
|
gpointer bumper_filter = GST_GL_BUMPER (filter);
|
|
|
|
|
|
|
|
//blocking call, use a FBO
|
|
|
|
gst_gl_display_use_fbo (filter->display, filter->width, filter->height,
|
2009-02-11 06:39:14 +00:00
|
|
|
filter->fbo, filter->depthbuffer, outbuf->texture, gst_gl_bumper_callback,
|
|
|
|
inbuf->width, inbuf->height, inbuf->texture,
|
|
|
|
//bumper_filter->fovy, bumper_filter->aspect, bumper_filter->znear, bumper_filter->zfar,
|
|
|
|
45, (gdouble) filter->width / (gdouble) filter->height, 0.1, 50,
|
|
|
|
GST_GL_DISPLAY_PROJECTION_PERSPECIVE, bumper_filter);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
//opengl scene, params: input texture (not the output filter->texture)
|
|
|
|
static void
|
|
|
|
gst_gl_bumper_callback (gint width, gint height, guint texture, gpointer stuff)
|
|
|
|
{
|
2009-02-11 06:39:14 +00:00
|
|
|
static GLfloat xrot = 0;
|
|
|
|
static GLfloat yrot = 0;
|
|
|
|
static GLfloat zrot = 0;
|
|
|
|
|
|
|
|
GstGLBumper *bumper = GST_GL_BUMPER (stuff);
|
2008-09-06 23:23:19 +00:00
|
|
|
GLint locTangent = 0;
|
|
|
|
|
|
|
|
//choose the lights
|
|
|
|
GLfloat light_direction0[] = { 1.0, 0.0, -1.0, 0.0 }; // light goes along -x
|
2009-02-11 06:39:14 +00:00
|
|
|
GLfloat light_direction1[] = { -1.0, 0.0, -1.0, 0.0 }; // light goes along x
|
2008-09-06 23:23:19 +00:00
|
|
|
GLfloat light_diffuse0[] = { 1.0, 1.0, 1.0, 1.0 };
|
|
|
|
GLfloat light_diffuse1[] = { 1.0, 1.0, 1.0, 1.0 };
|
|
|
|
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
|
|
|
|
|
|
|
|
//eye point
|
2009-02-11 06:39:14 +00:00
|
|
|
glMatrixMode (GL_PROJECTION);
|
|
|
|
gluLookAt (0.0, 0.0, -6.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
|
|
|
|
glMatrixMode (GL_MODELVIEW);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//scene conf
|
2009-02-11 06:39:14 +00:00
|
|
|
glEnable (GL_DEPTH_TEST);
|
|
|
|
glDepthFunc (GL_LEQUAL);
|
|
|
|
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
|
|
|
|
glShadeModel (GL_SMOOTH);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//set the lights
|
2009-02-11 06:39:14 +00:00
|
|
|
glLightfv (GL_LIGHT0, GL_POSITION, light_direction0);
|
|
|
|
glLightfv (GL_LIGHT0, GL_DIFFUSE, light_diffuse0);
|
|
|
|
glLightfv (GL_LIGHT1, GL_POSITION, light_direction1);
|
|
|
|
glLightfv (GL_LIGHT1, GL_DIFFUSE, light_diffuse1);
|
|
|
|
glMaterialfv (GL_FRONT, GL_DIFFUSE, mat_diffuse);
|
|
|
|
glColorMaterial (GL_FRONT_AND_BACK, GL_DIFFUSE);
|
|
|
|
glEnable (GL_COLOR_MATERIAL);
|
|
|
|
glEnable (GL_LIGHTING);
|
|
|
|
glEnable (GL_LIGHT0);
|
|
|
|
glEnable (GL_LIGHT1);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//configure shader
|
|
|
|
gst_gl_shader_use (bumper->shader);
|
2009-02-11 06:39:14 +00:00
|
|
|
locTangent =
|
|
|
|
gst_gl_shader_get_attribute_location (bumper->shader, "aTangent");
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//set the normal map
|
|
|
|
glActiveTextureARB (GL_TEXTURE1_ARB);
|
|
|
|
gst_gl_shader_set_uniform_1i (bumper->shader, "texture1", 1);
|
|
|
|
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, bumper->bumpmap);
|
|
|
|
|
|
|
|
//set the video texture
|
|
|
|
glActiveTextureARB (GL_TEXTURE0_ARB);
|
|
|
|
gst_gl_shader_set_uniform_1i (bumper->shader, "texture0", 0);
|
|
|
|
glBindTexture (GL_TEXTURE_RECTANGLE_ARB, texture);
|
|
|
|
|
|
|
|
//glTranslatef(2.0f, 2.0f, 5.0f);
|
|
|
|
|
2009-02-11 06:39:14 +00:00
|
|
|
glRotatef (xrot, 1.0f, 0.0f, 0.0f);
|
|
|
|
glRotatef (yrot, 0.0f, 1.0f, 0.0f);
|
|
|
|
glRotatef (zrot, 0.0f, 0.0f, 1.0f);
|
2008-09-06 23:23:19 +00:00
|
|
|
|
|
|
|
//Cube
|
2009-02-11 06:39:14 +00:00
|
|
|
glBegin (GL_QUADS);
|
|
|
|
|
|
|
|
// front face
|
|
|
|
glNormal3d (0.0, 0.0, -1.0);
|
|
|
|
glVertexAttrib3dARB (locTangent, 0.0, 1.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, 0.0);
|
|
|
|
glVertex3d (1.0, 1.0, -1.0); // B
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, bumper->bumpmap_height);
|
|
|
|
glVertex3d (1.0, -1.0, -1.0); // A
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width,
|
|
|
|
bumper->bumpmap_height);
|
|
|
|
glVertex3d (-1.0, -1.0, -1.0); // D
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width, 0.0);
|
|
|
|
glVertex3d (-1.0, 1.0, -1.0); // C
|
|
|
|
|
|
|
|
// right face
|
|
|
|
glNormal3d (-1.0, 0.0, 0.0);
|
|
|
|
glVertexAttrib3dARB (locTangent, 0.0, 1.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, 0.0);
|
|
|
|
glVertex3d (-1.0, 1.0, -1.0); // C
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, bumper->bumpmap_height);
|
|
|
|
glVertex3d (-1.0, -1.0, -1.0); // D
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width,
|
|
|
|
bumper->bumpmap_height);
|
|
|
|
glVertex3d (-1.0, -1.0, 1.0); // H
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width, 0.0);
|
|
|
|
glVertex3d (-1.0, 1.0, 1.0); // G
|
|
|
|
|
|
|
|
// back face
|
|
|
|
glNormal3d (0.0, 0.0, 1.0);
|
|
|
|
glVertexAttrib3dARB (locTangent, 0.0, 1.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, 0.0);
|
|
|
|
glVertex3d (-1.0, 1.0, 1.0); // G
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, bumper->bumpmap_height);
|
|
|
|
glVertex3d (-1.0, -1.0, 1.0); // H
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width,
|
|
|
|
bumper->bumpmap_height);
|
|
|
|
glVertex3d (1.0, -1.0, 1.0); // E
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width, 0.0);
|
|
|
|
glVertex3d (1.0, 1.0, 1.0); // F
|
|
|
|
|
|
|
|
// left face
|
|
|
|
glNormal3d (1.0, 0.0, 0.0);
|
|
|
|
glVertexAttrib3dARB (locTangent, 0.0, 1.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, 0.0);
|
|
|
|
glVertex3d (1.0, 1.0, 1.0); // F
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, bumper->bumpmap_height);
|
|
|
|
glVertex3d (1.0, -1.0, 1.0); // E
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width,
|
|
|
|
bumper->bumpmap_height);
|
|
|
|
glVertex3d (1.0, -1.0, -1.0); // A
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width, 0.0);
|
|
|
|
glVertex3d (1.0, 1.0, -1.0); // B
|
|
|
|
|
|
|
|
// top face
|
|
|
|
glNormal3d (0.0, 1.0, 0.0);
|
|
|
|
glVertexAttrib3dARB (locTangent, 0.0, 0.0, 1.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, 0.0);
|
|
|
|
glVertex3d (1.0, 1.0, 1.0); // F
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, bumper->bumpmap_height);
|
|
|
|
glVertex3d (1.0, 1.0, -1.0); // B
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width,
|
|
|
|
bumper->bumpmap_height);
|
|
|
|
glVertex3d (-1.0, 1.0, -1.0); // C
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width, 0.0);
|
|
|
|
glVertex3d (-1.0, 1.0, 1.0); // G
|
|
|
|
|
|
|
|
// bottom face
|
|
|
|
glNormal3d (0.0, -1.0, 0.0);
|
|
|
|
glVertexAttrib3dARB (locTangent, 0.0, 0.0, -1.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, 0.0);
|
|
|
|
glVertex3d (1.0, -1.0, -1.0); // A
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, 0.0, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, 0.0, bumper->bumpmap_height);
|
|
|
|
glVertex3d (1.0, -1.0, 1.0); // E
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, height);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width,
|
|
|
|
bumper->bumpmap_height);
|
|
|
|
glVertex3d (-1.0, -1.0, 1.0); // H
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE0_ARB, width, 0.0);
|
|
|
|
glMultiTexCoord2dARB (GL_TEXTURE1_ARB, bumper->bumpmap_width, 0.0);
|
|
|
|
glVertex3d (-1.0, -1.0, -1.0); // D
|
|
|
|
glEnd ();
|
|
|
|
|
|
|
|
glUseProgram (0);
|
|
|
|
glDisable (GL_LIGHT0);
|
|
|
|
glDisable (GL_LIGHT1);
|
|
|
|
glDisable (GL_LIGHTING);
|
|
|
|
glDisable (GL_COLOR_MATERIAL);
|
|
|
|
|
|
|
|
xrot += 1.0f;
|
|
|
|
yrot += 0.9f;
|
|
|
|
zrot += 1.1f;
|
2008-09-06 23:23:19 +00:00
|
|
|
}
|