[osg-users] OSG 3.5.0 (130) GLES2 Shaders Example
Alex Cham
cau.mbox at gmail.com
Mon Jul 20 17:30:18 PDT 2015
Just OSG 3.5.0 (130) GLES2 Shaders Example V2
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2015 Alex Cham
*
* This example is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This example 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
* OpenSceneGraph Public License for more details.
#define OPENSCENEGRAPH_MAJOR_VERSION 3
#define OPENSCENEGRAPH_MINOR_VERSION 5
#define OPENSCENEGRAPH_PATCH_VERSION 0
#define OPENSCENEGRAPH_SOVERSION 130
$ git log -1
commit bc3a77cb1561ed2e47e88ab507180bcf15309f31
Author: Robert OSFIELD <robert.osfield at gmail.com>
Date: Fri Jul 17 18:31:22 2015 +0000
*/
#include <osgViewer/Viewer>
#include <osg/ShapeDrawable>
#include <osg/Geode>
#include <osg/Vec3>
#include <osg/Program>
#include <osg/Shader>
#include <osg/Uniform>
using namespace osg;
static const char *vertexShaderSource = {
"#version 100\n"
// "uniform mat4 mvp_matrix;// model-view-projection matrix\n"
"uniform mat3 normal_matrix;// normal matrix\n"
"uniform vec3 ec_light_dir;// light direction in eye coords\n"
"attribute vec4 a_vertex;// vertex position\n"
"attribute vec3 a_normal;// vertex normal\n"
// "attribute vec2 a_texcoord;// texture coordinates\n"
// "varying float v_diffuse;\n"
// "varying vec2 v_texcoord;\n"
"void main(void)\n"
"{\n"
"// put vertex normal into eye coords\n"
"vec3 ec_normal = normalize(normal_matrix * a_normal);\n"
// "// emit diffuse scale factor, texcoord, and position\n"
// "v_diffuse = max(dot(ec_light_dir, ec_normal), 0.0);\n"
// "v_texcoord= a_texcoord;\n"
"gl_Position = gl_ModelViewProjectionMatrix * a_vertex;\n"
"}\n"};
static const char *fragmentShaderSource = {
"#version 100\n"
"precision mediump float;\n"
"void main()\n"
"{\n"
"gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n"
"}\n"};
int main(int, char **)
{
osg::setNotifyLevel(osg::INFO);
// construct the viewer.
osgViewer::Viewer viewer;
// use a geode with a Box ShapeDrawable
osg::Geode* geode = new osg::Geode();
geode->addDrawable(new osg::ShapeDrawable(new osg::Box(osg::Vec3(0.0f,0.0f,0.0f),1.0f)));
osg::StateSet *ss = geode->getOrCreateStateSet();
osg::Program* program = new osg::Program;
program->addShader( new osg::Shader( osg::Shader::VERTEX, vertexShaderSource ) );
program->addShader( new osg::Shader( osg::Shader::FRAGMENT, fragmentShaderSource ) );
ss->setAttributeAndModes(program, osg::StateAttribute::ON);
viewer.setSceneData(geode);
return viewer.run();
}
/*
mkdir ./GLES2ContextBuild
cd ./GLES2ContextBuild
cmake \
-DCMAKE_C_COMPILER="/usr/bin/clang" \
-DCMAKE_CXX_COMPILER="/usr/bin/clang++" \
-DCMAKE_CXX_FLAGS="-std=c++11 -v -Wall" \
-DBUILD_OSG_EXAMPLES=1 \
-DOSG_GL1_AVAILABLE=OFF \
-DOSG_GL2_AVAILABLE=OFF \
-DOSG_GL3_AVAILABLE=OFF \
-DOSG_GLES1_AVAILABLE=OFF \
-DOSG_GLES2_AVAILABLE=ON \
-DOPENGL_INCLUDE_DIR=/usr/include/ \
-DOPENGL_LIBRARY=/usr/lib/x86_64-linux-gnu/libGLESv2.so \
-DOPENGL_egl_LIBRARY=/usr/lib/x86_64-linux-gnu/libEGL.so \
-DOSG_GL_DISPLAYLISTS_AVAILABLE=OFF \
-DSG_GL_MATRICES_AVAILABLE=OFF \
-DOSG_GL_VERTEX_FUNCS_AVAILABLE=OFF \
-DOSG_GL_VERTEX_ARRAY_FUNCS_AVAILABLE=OFF \
-DOSG_GL_FIXED_FUNCTION_AVAILABLE=OFF \
-DOSG_CPP_EXCEPTIONS_AVAILABLE=OFF \
../GLES2ContextSource
http://trac.openscenegraph.org/projects/osg/wiki/Community/OpenGL-ES
Based on OSG osgsimpleshaders.cpp
http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk/examples/osgsimpleshaders/osgsimpleshaders.cpp
See Also Hello Triangle: An OpenGL ES 2.0 Example
https://www.khronos.org/assets/uploads/books/openglr_es_20_programming_guide_sample.pdf
OpenGL ES 2.0 API & OpenGL ES Shading Language 1.0 Quick Reference Card
https://www.khronos.org/files/opengles20-reference-card.pdf
OpenGL ES Shading Language Version 1.00
https://www.khronos.org/files/opengles_shading_language.pdf
*/
Thank you!
Cheers,
Alex
------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=64385#64385
More information about the osg-users
mailing list