Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Gotcha, thanks. No need for apologies, I never would have got as far as I did to begin with if it weren't for that post. Thanks again everyone for the assistance on this one, I have the LUT method working perfectly now with floating point values. Jean-Sébastien Guay wrote: > Hi Ethan, > > > > Thanks Sergey, that clears it up for me. > > > > Yes, Sergey has it right, that post was written from memory and > obviously I got it backwards. I always cast the variable to int, or > explicitly create a uniform of type SAMPLER_2D which avoids this issue > altogether but is more lines of code. > > Sorry for the confusion, > > J-S > -- > __ > Jean-Sebastien Guay > http://whitestar02.dyndns-web.com/ > ___ > osg-users mailing list > > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org > > -- > Post generated by Mail2Forum -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45663#45663 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Hi Ethan, Thanks Sergey, that clears it up for me. Yes, Sergey has it right, that post was written from memory and obviously I got it backwards. I always cast the variable to int, or explicitly create a uniform of type SAMPLER_2D which avoids this issue altogether but is more lines of code. Sorry for the confusion, J-S -- __ Jean-Sebastien Guay jean_...@videotron.ca http://whitestar02.dyndns-web.com/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Thanks Sergey, that clears it up for me. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45643#45643 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
From opengl docs: glUniform1i and glUniform1iv are the only two functions that may be used to load uniform variables defined as sampler types. Loading samplers with any other function will result in a GL_INVALID_OPERATION error. And osg uses this calls only for int uniforms or sampler uniforms, for unsigned it calls glUniformui* funcitons. Cheers. 21.02.2012, 17:45, "Ethan Fahy" : > By the way I had originally put the TEXTURE_UNIT as a const unsigned int > because of the 10th post in this thread: > http://forum.openscenegraph.org/viewtopic.php?t=8273 > as it was suggested by Jean-Sebastien. Not sure if there is some disconnect > that I am still not getting but changing it to a regular int seems to have > solved my problems so I will just move forward using a regular int. > > -- > Read this topic online here: > http://forum.openscenegraph.org/viewtopic.php?p=45637#45637 > > ___ > osg-users mailing list > osg-users@lists.openscenegraph.org > http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
By the way I had originally put the TEXTURE_UNIT as a const unsigned int because of the 10th post in this thread: http://forum.openscenegraph.org/viewtopic.php?t=8273 as it was suggested by Jean-Sebastien. Not sure if there is some disconnect that I am still not getting but changing it to a regular int seems to have solved my problems so I will just move forward using a regular int. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45637#45637 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Jean-Sebastien and Sergey, Thanks very much to you both. Sergey, you were spot-on about the unsigned int vs int thing causing my OpenGL error and Jean-Sebastien you were correct about the texture clamping default. -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45635#45635 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Hi there I dont read all messages here, but if problem still there - your uniform for sampler initialized wrong You should use state->addUniform(new osg::Uniform("lut", int(TEXTURE_UNIT))); notice int cast there or you should create uniform with explicit type specification of osg::Uniform::INT, or use int TEXTURE_UNIT declaration. in your case osg uniform detect uniform type from constructor which is unsigned and opengl use INT uniforms for samplers, so osg trying to set INT uniform with opengl call which works only with unsigned uniforms so here goes your error. Cheers, Sergey. 17.02.2012, 18:30, "Ethan Fahy" : > Thanks for the reply Alex. My setup is exactly as you describe but I'm still > having troubles. I've taken the essential bits of my code to create a sample > main() to illustrate exactly what I'm doing: > > Code: > > int main(int argc, char* argv[]) > { > //INITIAL SETUP > //create root > osg::ref_ptr root = new osg::Group(); > //load osg-dem generated ive terrain complex > osg::ref_ptr terrainNode = > osgDB::readNodeFile("terrain.ive"); > //attach terrain node to root > root->addChild(terrainNode); > //create stateSet from terrainNode > osg::StateSet *state = terrainNode->getOrCreateStateSet(); > //create program > osg::Program *program = new osg::Program; > //create shaders > osg::Shader *vertObj = new osg::Shader( osg::Shader::VERTEX ); > osg::Shader *fragObj = new osg::Shader( osg::Shader::FRAGMENT ); > //add shaders to program > program->addShader( fragObj ); > program->addShader( vertObj ); > //load shader src files into shaders > vertObj->loadShaderSourceFromFile( "shader.vert" ); > fragObj->loadShaderSourceFromFile( "shader.frag" ); > > //CREATE LOOKUP TABLE AND IMAGE > //allocate memory for 4 by 4 lookup table > int height=4; > int width=4; > const int size = width*height*4;//*4 for rgba channels > unsigned char* data = (unsigned char*)calloc(size, sizeof(unsigned > char)); > //Store arbitrary value of unsigned char 101 in each rgba channel in > a flattened 1D data array > int dataIndex; > for( int i=0 ; i < height ; i++ ){ > for( int j=0 ; j < width ; j++ ){ > dataIndex = i*width*4 + j*4; > data[dataIndex] = 101;//red > data[dataIndex+1] = 101;//green > data[dataIndex+2] = 101;//blue > data[dataIndex+3] = 101;//alpha > } > } > //create image > osg::ref_ptr image = new osg::Image; > image->setOrigin(osg::Image::BOTTOM_LEFT); > image->setImage(width, height, 1 ,GL_RGBA,GL_RGBA,GL_UNSIGNED_BYTE, > (unsigned char*)data,osg::Image::NO_DELETE); > > //create texture2D and add image to it > osg::ref_ptr lutTexture = new osg::Texture2D; > lutTexture->setTextureSize(width, height);//unsure if this is needed > or if it's inherited from the setImage function below > lutTexture->setInternalFormat(GL_RGBA);//unsure if this is needed or > if it's inherited from the setImage function below > lutTexture->setFilter(osg::Texture::MIN_FILTER, > osg::Texture::NEAREST); > lutTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); > lutTexture->setImage(image); > > //assign texture to hardcoded texture unit 1 so that it can be > accessed in the shader > const unsigned int TEXTURE_UNIT = 1; > state->setTextureAttributeAndModes(TEXTURE_UNIT, lutTexture, > osg::StateAttribute::ON); > state->addUniform(new osg::Uniform("lut", TEXTURE_UNIT)); > > //attach the program to the stateSet > state->setAttributeAndModes(program, osg::StateAttribute::ON); > > //open an osgviewer to see the root > osgViewer::Viewer viewer; > viewer.setSceneData(root); > viewer.setLightingMode(osg::View::NO_LIGHT); > viewer.setCameraManipulator(new osgGA::TrackballManipulator); > viewer.home(); > return viewer.run(); > } > > and here are the vert and frag shaders: > shader.vert > > Code: > > void main(void) > { > gl_TexCoord[0] = gl_MultiTexCoord0; > gl_TexCoord[1] = gl_MultiTexCoord1; //unsure if this is needed? > gl_FrontColor = gl_Color; > gl_Position = ftransform(); > } > > shader.frag > > Code: > > uniform sampler2D lut; > uniform sampler2D baseTexture; > void main(void) > { > //get color of the terrain's texture > vec4 color = texture2D(baseTexture, gl_TexCoord[0].st); > //find index value from data in the rgb channels of the terrain's > texture. (left out complicated equations since they are relevant, but I've > tested that their values are between 0-1 > float index1 = some indexing lo
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Hello Ethan, Is a border being added to my texture somehow? If so then this could explain why my shader is pulling non-uniform values from my uniform lookup table texture... Set the wrap mode to CLAMP_TO_EDGE instead of CLAMP_TO_BORDER (which is the default, and the default border color is black). It always seemed counterintuitive to me that OpenGL's default wrap mode is clamp to border, as this is a relic of times past and no one sets an appropriate border color anyways... Most of the time, you want either clamp to edge or repeat mode... Hope this helps, J-S -- __ Jean-Sebastien Guay jean_...@videotron.ca http://whitestar02.dyndns-web.com/ ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
I tried setting the texture that I created to texture unit = 0 and found that the resultant object in osgviewer was unchanged from its original texture. I'm not if this means that I have created my texture incorrectly or if the texture attachment process is not correct. apecoraro wrote: > I honestly don't know why it doesn't work. Your code looks correct to me. You > might try removing the shader and setting your lut to use texture unit 0 on > your terrain node. Then you can at least verify that the texture is created > correct by viewing it on the terrain using the fixed function pipeline. > > Alex -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45548#45548 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
I honestly don't know why it doesn't work. Your code looks correct to me. You might try removing the shader and setting your lut to use texture unit 0 on your terrain node. Then you can at least verify that the texture is created correct by viewing it on the terrain using the fixed function pipeline. Alex -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45547#45547 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Thanks for the reply Alex. I tried those changes (I think I've tried just about every pixel/internal format that I could think of) and the behavior of my program hasn't changed unfortunately. apecoraro wrote: > I think the internal format of your osg::Image should be set to GL_RGBA8 not > GL_RGBA, which is a pixel format not an internal format. I find the OpenGL > 2.1 documentation very confusing on the use of internal format and pixel > format, but I think the 3.3 and 4.2 documentation is much easier to > understand. So check it out to verify what I'm saying. > > http://www.opengl.org/sdk/docs/ > > Also, insetad of setting the osg::Texture2D internal format to GL_RGBA you > should set its internal format mode to osg::Texture::USE_IMAGE_DATA_FORMAT > (actually that is the default, but when you call setInternalFormat it changes > the mode to osg::Texture::USE_USER_DEFINED_FORMAT). So I think just removing > the call to setInternalFormat on the texture will suffice. > > Change this: > > Code: > > image->setImage(width, height, 1 ,GL_RGBA,GL_RGBA,GL_UNSIGNED_BYTE, (unsigned > char*)data,osg::Image::NO_DELETE); > > > > > to this: > > Code: > > image->setImage(width, height, 1 ,GL_RGBA8,GL_RGBA,GL_UNSIGNED_BYTE, > (unsigned char*)data,osg::Image::NO_DELETE); > > > > > and this: > > Code: > > lutTexture->setInternalFormat(GL_RGBA); > > > > > to this: > > Code: > > lutTexture->setInternalFormatMode(osg::Texture::USE_IMAGE_DATA_FORMAT); > > > > > Alex -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45543#45543 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
I think the internal format of your osg::Image should be set to GL_RGBA8 not GL_RGBA, which is a pixel format not an internal format. I find the OpenGL 2.1 documentation very confusing on the use of internal format and pixel format, but I think the 3.3 and 4.2 documentation is much easier to understand. So check it out to verify what I'm saying. http://www.opengl.org/sdk/docs/ Also, I insetad of setting the think you osg::Texture2D lut should set its internal format mode to osg::Texture::USE_IMAGE_DATA_FORMAT (actually that is the default, but when you call setInternalFormat it changes the mode to osg::Texture::USE_USER_DEFINED_FORMAT). So I think just removing the call to setInternalFormat on the texture will suffice. Change this: Code: image->setImage(width, height, 1 ,GL_RGBA,GL_RGBA,GL_UNSIGNED_BYTE, (unsigned char*)data,osg::Image::NO_DELETE); to this: Code: image->setImage(width, height, 1 ,GL_RGBA8,GL_RGBA,GL_UNSIGNED_BYTE, (unsigned char*)data,osg::Image::NO_DELETE); and this: Code: lutTexture->setInternalFormat(GL_RGBA); to this: Code: lutTexture->setInternalFormatMode(osg::Texture::USE_IMAGE_DATA_FORMAT); Alex -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45542#45542 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Thanks for the reply Alex. My setup is exactly as you describe but I'm still having troubles. I've taken the essential bits of my code to create a sample main() to illustrate exactly what I'm doing: Code: int main(int argc, char* argv[]) { //INITIAL SETUP //create root osg::ref_ptr root = new osg::Group(); //load osg-dem generated ive terrain complex osg::ref_ptr terrainNode = osgDB::readNodeFile("terrain.ive"); //attach terrain node to root root->addChild(terrainNode); //create stateSet from terrainNode osg::StateSet *state = terrainNode->getOrCreateStateSet(); //create program osg::Program *program = new osg::Program; //create shaders osg::Shader *vertObj = new osg::Shader( osg::Shader::VERTEX ); osg::Shader *fragObj = new osg::Shader( osg::Shader::FRAGMENT ); //add shaders to program program->addShader( fragObj ); program->addShader( vertObj ); //load shader src files into shaders vertObj->loadShaderSourceFromFile( "shader.vert" ); fragObj->loadShaderSourceFromFile( "shader.frag" ); //CREATE LOOKUP TABLE AND IMAGE //allocate memory for 4 by 4 lookup table int height=4; int width=4; const int size = width*height*4;//*4 for rgba channels unsigned char* data = (unsigned char*)calloc(size, sizeof(unsigned char)); //Store arbitrary value of unsigned char 101 in each rgba channel in a flattened 1D data array int dataIndex; for( int i=0 ; i < height ; i++ ){ for( int j=0 ; j < width ; j++ ){ dataIndex = i*width*4 + j*4; data[dataIndex] = 101;//red data[dataIndex+1] = 101;//green data[dataIndex+2] = 101;//blue data[dataIndex+3] = 101;//alpha } } //create image osg::ref_ptr image = new osg::Image; image->setOrigin(osg::Image::BOTTOM_LEFT); image->setImage(width, height, 1 ,GL_RGBA,GL_RGBA,GL_UNSIGNED_BYTE, (unsigned char*)data,osg::Image::NO_DELETE); //create texture2D and add image to it osg::ref_ptr lutTexture = new osg::Texture2D; lutTexture->setTextureSize(width, height);//unsure if this is needed or if it's inherited from the setImage function below lutTexture->setInternalFormat(GL_RGBA);//unsure if this is needed or if it's inherited from the setImage function below lutTexture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::NEAREST); lutTexture->setFilter(osg::Texture::MAG_FILTER, osg::Texture::NEAREST); lutTexture->setImage(image); //assign texture to hardcoded texture unit 1 so that it can be accessed in the shader const unsigned int TEXTURE_UNIT = 1; state->setTextureAttributeAndModes(TEXTURE_UNIT, lutTexture, osg::StateAttribute::ON); state->addUniform(new osg::Uniform("lut", TEXTURE_UNIT)); //attach the program to the stateSet state->setAttributeAndModes(program, osg::StateAttribute::ON); //open an osgviewer to see the root osgViewer::Viewer viewer; viewer.setSceneData(root); viewer.setLightingMode(osg::View::NO_LIGHT); viewer.setCameraManipulator(new osgGA::TrackballManipulator); viewer.home(); return viewer.run(); } and here are the vert and frag shaders: shader.vert Code: void main(void) { gl_TexCoord[0] = gl_MultiTexCoord0; gl_TexCoord[1] = gl_MultiTexCoord1; //unsure if this is needed? gl_FrontColor = gl_Color; gl_Position = ftransform(); } shader.frag Code: uniform sampler2D lut; uniform sampler2D baseTexture; void main(void) { //get color of the terrain's texture vec4 color = texture2D(baseTexture, gl_TexCoord[0].st); //find index value from data in the rgb channels of the terrain's texture. (left out complicated equations since they are relevant, but I've tested that their values are between 0-1 float index1 = some indexing logic here; float index2 = some more index logic here; //Look up new color values from lookup texture based on indices vec2 lutCoord = vec2(index1, index2); gl_FragColor = texture2D(lut, lutCoord); } So, if I create an image that has all values of rgba set to unsigned char=101, I would expect that if I lookup values in that texture that they should always be equal to rgba=(101,101,101,101) as long as my lutCoords are both between 0.0-1.0. However when I actually run this code the terrain texture is not all the same color and I get this error on the command line: "Warning: detected OpenGL error 'invalid operation' at After Renderer:compile" By adding/removing things from my frag shade
Re: [osg-users] Shader lookup table method: does lookup texture have to be bound to a separate geometry?
Hi Ethan, All you should need to do is attach your osg::Texture to the osg::Stateset that you are attaching your osg::Program to and then you also need to add an osg::Uniform to the Stateset for the texture sampler that you use in your shader program to access the texture: Code: osg::ref_ptr spGeometry = new osg::Geometry(); osg::ref_ptr spTexture = new osg::Texture2D(); osg::StateSet* pStateSet = sp->getOrCreateStateSet(); pStateSet->setTextureAttributeAndModes(0, spTexture.get(), osg::StateAttribute::ON); //bind the sampler to texture unit zero osg::Uniform* pSampler = new osg::Uniform("TextureSampler", 0); pStateSet->addUniform(pSampler); Thank you! Cheers, Alex -- Read this topic online here: http://forum.openscenegraph.org/viewtopic.php?p=45521#45521 ___ osg-users mailing list osg-users@lists.openscenegraph.org http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org