[osg-users] Generating a cube using geometry shaders

2014-09-14 Thread Ahmed Hesham
Hi,

I'm trying to generate a cube using GLSL shaders. The basic idea is to pass a 
single vertex that represents the center of the cube, and let the geometry 
shader do the calculations. No luck so far, the only output I get is a single 
white pixel (point) in the center of the screen. C++/GLSL code below, any ideas 
are appreciated. 

Thank you!

Cheers,
Ahmed

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61028#61028





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] Generating a cube using geometry shaders

2014-09-14 Thread Ahmed Hesham
CPP file:

Code:
#include osgDB/ReadFile
#include osgViewer/Viewer

struct ModelViewProjectionMatrixCallback : public osg::Uniform::Callback {
ModelViewProjectionMatrixCallback(osg::Camera *fCamera) :
_camera(fCamera) {
}

virtual void operator()(osg::Uniform *uniform, osg::NodeVisitor *nv) {
osg::Matrixd viewMatrix = _camera-getViewMatrix();
osg::Matrixd modelMatrix = osg::computeLocalToWorld(nv-getNodePath());
osg::Matrixd modelViewProjectionMatrix = modelMatrix * viewMatrix * 
_camera-getProjectionMatrix();
uniform-set(modelViewProjectionMatrix);
}

osg::Camera *_camera;
};

osg::ref_ptrosg::Program createProgram(std::string fProgramName, std::string 
fVertexShader, std::string fGeometryShader, std::string fFragmentShader)
{
osg::ref_ptrosg::Shader vertexShader = 
osgDB::readShaderFile(fVertexShader);
osg::ref_ptrosg::Shader geometryShader = 
osgDB::readShaderFile(fGeometryShader);
osg::ref_ptrosg::Shader fragmentShader = 
osgDB::readShaderFile(fFragmentShader);

osg::ref_ptrosg::Program program = new osg::Program;
program-setName(fProgramName);
program-addShader(vertexShader);
program-addShader(geometryShader);
program-addShader(fragmentShader);

return program.release();
}

int main(int argc, char **argv)
{
// Program
osg::ref_ptrosg::Program program = createProgram(SinglePass, 
singlepass.vert, singlepass.geom, singlepass.frag);

// Vertices
osg::ref_ptrosg::Vec3Array vertices = new osg::Vec3Array;
vertices-push_back(osg::Vec3(0.5f, 0.0f, 0.5f));

// Geometry
osg::ref_ptrosg::Geometry geom = new osg::Geometry;
geom-setUseDisplayList(false);
geom-setVertexArray(vertices);
geom-setVertexAttribArray(0, vertices);
geom-addPrimitiveSet(new osg::DrawArrays(GL_POINTS, 0, vertices-size()));

// Geode
osg::ref_ptrosg::Geode screenGeode = new osg::Geode;
screenGeode-addDrawable(geom);

// State set
osg::StateSet *geomStateSet = geom-getOrCreateStateSet();
geomStateSet-setAttribute(program);
osg::ref_ptrosg::Point point = new osg::Point;
point-setSize(5.0f);
geomStateSet-setAttribute(point);

// Viewer
osgViewer::Viewer viewer;
viewer.setSceneData(screenGeode.get());
viewer.setUpViewInWindow(50, 50, 800, 600, 0);
viewer.getCamera()-setViewport(0, 0, 800, 600);
viewer.getCamera()-setClearMask(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
viewer.getCamera()-setClearColor(osg::Vec4(0.2f, 0.2f, 0.2f, 1.0f));

// MVP
osg::ref_ptrosg::Uniform MVP = new osg::Uniform(osg::Uniform::FLOAT_MAT4, 
ModelviewProjection);
MVP-setUpdateCallback(new 
ModelViewProjectionMatrixCallback(viewer.getCamera()));
geomStateSet-addUniform(MVP);

return viewer.run();
}



Vertex shader:

Code:
#version 330

in vec4 Position;
out vec4 vPosition;
uniform mat4 ModelviewProjection;

void main()
{
gl_Position = ModelviewProjection * Position;
vPosition = Position;
}



Geometry shader:

Code:
#version 330

layout(points) in;
layout(triangle_strip, max_vertices = 24) out;

in vec4 vPosition[1];

uniform mat4 ModelviewProjection;
uniform mat4 ProjectionMatrix;
uniform mat4 ViewMatrix;
uniform mat4 Modelview;

vec4 objCube[8]; // Object space coordinate of cube corner
vec4 ndcCube[8]; // Normalized device coordinate of cube corner
ivec4 faces[6]; // Vertex indices of the cube faces

void emit_vert(int vert)
{
gl_Position = ndcCube[vert];
EmitVertex();
}

void emit_face(int face)
{
emit_vert(faces[face][1]); emit_vert(faces[face][0]);
emit_vert(faces[face][3]); emit_vert(faces[face][2]);
EndPrimitive();
}

void main()
{
faces[0] = ivec4(0,1,3,2); faces[1] = ivec4(5,4,6,7);
faces[2] = ivec4(4,5,0,1); faces[3] = ivec4(3,2,7,6);
faces[4] = ivec4(0,3,4,7); faces[5] = ivec4(2,1,6,5);

vec4 P = vPosition[0];
vec4 I = vec4(0.25,0,0,0);
vec4 J = vec4(0,0.25,0,0);
vec4 K = vec4(0,0,0.25,0);

objCube[0] = P+K+I+J; objCube[1] = P+K+I-J;
objCube[2] = P+K-I-J; objCube[3] = P+K-I+J;
objCube[4] = P-K+I+J; objCube[5] = P-K+I-J;
objCube[6] = P-K-I-J; objCube[7] = P-K-I+J;

// Transform the corners of the box:
for (int vert = 0; vert  8; vert++)
ndcCube[vert] = ModelviewProjection * objCube[vert];

// Emit the six faces:
for (int face = 0; face  6; face++)
emit_face(face);
}



Fragment shader:

Code:
#version 330

out vec4 FragColor;

void main()
{
FragColor = vec4(1.0, 0.0, 0.0, 1.0);
}



--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61030#61030





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] About HUD' projection

2014-09-14 Thread Bram Vaessen
For a normal 2D HUD I suggest orthogonal.

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61031#61031





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


[osg-users] order of MatrixTransform

2014-09-14 Thread Bram Vaessen
Hi,

I'm a little confused about something simple... the order in which to add 
MatrixTransform.

For example I want to rotate something around x,y,z using 3 matrixtransforms.

as I understand I can do this by:
-translate(-x,-y,-z)
-rotate
-translate(x,y,z)

when I add them, which is the correct order?

-root- translate(-x,-y,-z) - rotate - translate(x,y,z) - geode

or...

-root- translate(x,y,z) - rotate - translate(-x,-y,-z) - geode

thanks!

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61032#61032





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] order of MatrixTransform

2014-09-14 Thread Robert Osfield
HI Bram,


On 14 September 2014 13:23, Bram Vaessen bram.vaes...@gmail.com wrote:

 Hi,

 I'm a little confused about something simple... the order in which to add
 MatrixTransform.

 For example I want to rotate something around x,y,z using 3
 matrixtransforms.

 as I understand I can do this by:
 -translate(-x,-y,-z)
 -rotate
 -translate(x,y,z)

 when I add them, which is the correct order?

 -root- translate(-x,-y,-z) - rotate - translate(x,y,z) - geode

 or...

 -root- translate(x,y,z) - rotate - translate(-x,-y,-z) - geode


The second version is the correct one.

You can of course do this all with a single PositionAttitudeTransform or a
single Transform node, this will be neater and lead to better performance.

Robert.
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


[osg-users] OpenGL error 'invalid operation' when adding 'unsigned int' Uniform

2014-09-14 Thread Selmar Kok
Hi,

I encountered something peculiar. Everything seems to work fine, though.

I get the following warning message:


 
 Warning: detected OpenGL error 'invalid operation' at After Renderer::compile
 


Investigating the issue (and stripping it down somewhat), I found that the 
error message disappears when I change 'unsigned int' in the following piece of 
code to 'int':


Code:

unsigned int texUnit = 0;
ref_ptrUniform uniform = new Uniform(sampler, texUnit);
stateSet-addUniform(uniform);




I'm curious if this is a bug or something I'm forgetting?

For completeness, here's the code that replicates the 'issue' for me:


Code:

#include osgViewer/Viewer

using namespace osg;

int main(int argc, char** argv)
{
osgViewer::Viewer viewer;

viewer.setUpViewInWindow(50, 50, 640, 480);

ref_ptrGroup root = new Group;
ref_ptrGeode geode = new Geode;
ref_ptrGeometry geometry = new Geometry;

root-addChild(geode);
geode-addChild(geometry);

geometry-setUseDisplayList(false);
geometry-setUseVertexBufferObjects(true);

ref_ptrStateSet stateSet = geometry-getOrCreateStateSet();

ref_ptrProgram program = new Program();
program-setName(TestProgram);
stateSet-setAttributeAndModes( program, StateAttribute::ON );

ref_ptrShader vertShader = new Shader(osg::Shader::Type::VERTEX);
ref_ptrShader fragShader = new Shader(osg::Shader::Type::FRAGMENT);
program-addShader(vertShader);
program-addShader(fragShader);

vertShader-setShaderSource(
void main(void){\n
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex;\n
}\n
);
fragShader-setShaderSource(
uniform sampler2D sampler;\n
void main (void){\n
gl_FragColor = vec4(1, 1, 1, 1) + texture2D(sampler, vec2(0, 
0)).rgba;\n
}\n
);

osg::Vec3 quadVerts[] = {
osg::Vec3(-1, -1, 1),
osg::Vec3(1, -1, 1),
osg::Vec3(1, 1, -1),
osg::Vec3(-1, 1, -1)};

geometry-setVertexArray( new Vec3Array(4, quadVerts) );
geometry-addPrimitiveSet( new DrawArrays(PrimitiveSet::QUADS, 0, 1) );

unsigned int texUnit = 0;
ref_ptrUniform uniform = new Uniform(sampler, texUnit);
stateSet-addUniform(uniform);

viewer.setSceneData(root);

viewer.run();
}




Thanks,

Selmar[/code]

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61034#61034





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] order of MatrixTransform

2014-09-14 Thread Bram Vaessen
Thanks.

Is there a way to make it a single node, but still being able to easily change 
the rotation around the point (the point on which to rotate doesn't change) ?

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61035#61035





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] order of MatrixTransform

2014-09-14 Thread Trajce Nikolov NICK
Hi Bram,

If you use single MatrixTransform you can achieve the same effect in the
way you are constructing the scenegraph above by multiplication of
different matrices. Something like

osg::MatrixTransform *mxt = new osg::MatrixTransform;
mxt-setMatrix( osg::Matrix::translate(-myPoint) *
osg::Matrix::rotate(myRotation) * osg::Matrix::translate(myPoint) );

And you keep track and change myRotation, which is osg::Quat.

Nick

On Sun, Sep 14, 2014 at 5:29 PM, Bram Vaessen bram.vaes...@gmail.com
wrote:

 Thanks.

 Is there a way to make it a single node, but still being able to easily
 change the rotation around the point (the point on which to rotate doesn't
 change) ?

 --
 Read this topic online here:
 http://forum.openscenegraph.org/viewtopic.php?p=61035#61035





 ___
 osg-users mailing list
 osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




-- 
trajce nikolov nick
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] order of MatrixTransform

2014-09-14 Thread Bram Vaessen
Ok thanks, I would have to recalculate the matrix every time the rotation 
changes I assume, but in the end that is probably more efficient than having 3 
matrixtransforms in the scenegraph... right?

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61037#61037





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


Re: [osg-users] order of MatrixTransform

2014-09-14 Thread Trajce Nikolov NICK
yes. This is what I think Robert had in mind when he mentioned performance
efficiency

Nick

On Sun, Sep 14, 2014 at 10:42 PM, Bram Vaessen bram.vaes...@gmail.com
wrote:

 Ok thanks, I would have to recalculate the matrix every time the rotation
 changes I assume, but in the end that is probably more efficient than
 having 3 matrixtransforms in the scenegraph... right?

 --
 Read this topic online here:
 http://forum.openscenegraph.org/viewtopic.php?p=61037#61037





 ___
 osg-users mailing list
 osg-users@lists.openscenegraph.org
 http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




-- 
trajce nikolov nick
___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org


[osg-users] OSG Android and Vuforia

2014-09-14 Thread David
Hi,

Any one knows how to render an osg model in OpenGL ES 2.0? I am working on an 
Android project, and had been using the osgAndroid 
(https://gitorious.org/osgandroid) open source project, which supports OpenGL 
ES 1.x only, but Vuforia (QCAR) supports only ES 2.0.

I have been trying to migrate the osg rendering with OpenGL ES 2.0, but there 
are lots of errors in the rendering calls.

A quick search in this forum shows that I need to write some shader files for 
these osg models.  Anyone knows how to write such shader files?


Thank you!
David

--
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=61039#61039





___
osg-users mailing list
osg-users@lists.openscenegraph.org
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org