-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Open
Description
Running a simple program to render a sphere in full screen causes a set fault when
using the setUpViewerAsEmbeddedInWindow or the setUpViewInWindow method.
Commenting out these calls fixes the issue and the Sphere is rendered but using only using a small portion of the available window space.
Running this on an M1 MacBook.
Can you please share an alternate way of rendering the scene in full screen?
Thanks
#include <osgViewer/Viewer>
#include <osg/Geode>
#include <osg/ShapeDrawable>
#include <osg/Material>
#include <osg/LightModel>
#include <osg/Vec4>
#include <osg/Vec3>
#include <iostream>
int main(int argc, char** argv) {
// 1. Create the root of the scene graph
osg::ref_ptr<osg::Group> root = new osg::Group();
// 2. Create a Geode node to hold the geometry
osg::ref_ptr<osg::Geode> geode = new osg::Geode();
// 3. Create a sphere geometry drawable
// Parameters: center (Vec3), radius (float)
osg::ref_ptr<osg::ShapeDrawable> sphereDrawable =
new osg::ShapeDrawable(new osg::Sphere(osg::Vec3(0.0f, 0.0f, 0.0f), 1.0f)); // Sphere at origin with radius 1.0
// Set a color for the sphere
sphereDrawable->setColor(osg::Vec4(0.0f, 0.5f, 1.0f, 1.0f)); // Blue color
// Add the drawable to the Geode
geode->addDrawable(sphereDrawable.get());
// 4. Attach basic lighting and material for better visual appearance
// This makes the sphere look 3D by reacting to light
osg::ref_ptr<osg::StateSet> stateSet = geode->getOrCreateStateSet();
osg::ref_ptr<osg::Material> material = new osg::Material();
material->setDiffuse(osg::Material::FRONT_AND_BACK, osg::Vec4(0.0f, 0.5f, 1.0f, 1.0f)); // Diffuse color
material->setSpecular(osg::Material::FRONT_AND_BACK, osg::Vec4(1.0f, 1.0f, 1.0f, 1.0f)); // Specular highlight color
material->setShininess(osg::Material::FRONT_AND_BACK, 64.0f); // Shininess value
stateSet->setAttributeAndModes(material.get(), osg::StateAttribute::ON);
stateSet->setMode(GL_LIGHTING, osg::StateAttribute::ON); // Enable lighting for this node
// Add a basic light model to the root of the scene (affects all children)
osg::ref_ptr<osg::LightModel> lightModel = new osg::LightModel();
lightModel->setTwoSided(true); // Lighting on both sides of surfaces
root->getOrCreateStateSet()->setAttributeAndModes(lightModel.get(), osg::StateAttribute::ON);
root->getOrCreateStateSet()->setMode(GL_LIGHTING, osg::StateAttribute::ON);
// 5. Add the Geode to the root of the scene graph
root->addChild(geode.get());
// 6. Set up the OSG Viewer
osgViewer::Viewer viewer;
viewer.setSceneData(root.get());
// 7. Use setUpViewerAsEmbeddedInWindow to specify window position and size
// Parameters: x (left), y (top), width, height
// This will create a window of 800x600 pixels at position (50, 50) on the screen.
viewer.setUpViewInWindow(0, 0, 800, 800);
// Optional: Adjust the camera position to get a good view of the sphere
viewer.getCamera()->setViewMatrixAsLookAt(
osg::Vec3(0.0f, -5.0f, 2.0f), // Eye position
osg::Vec3(0.0f, 0.0f, 0.0f), // Look-at point (center of the sphere)
osg::Vec3(0.0f, 0.0f, 1.0f) // Up vector (Z-axis up)
);
// 8. Run the viewer loop
// This will handle rendering, event processing, and scene graph updates
return viewer.run();
}
Metadata
Metadata
Assignees
Labels
No labels