C++-创建几何图形
C++ - Creating Geometry
我在大学被指派编译给我们的代码并识别:
- 已使用的OpenGL基元的类型
- 已经创建了多少个基元
- 已经创建了多少个顶点
- 网格的属性
给我们的代码应该创建一个多维数据集,但它没有。我已经看了好几个小时了,不知道为什么。
我想这可能是因为">createCube()"在主体中根本没有提到,所以我添加了">场景=createCube然而,一开始这给了我一个错误,createCube()尚未定义。经过几个小时的摆弄,我不再得到这个错误,尽管仍然没有立方体。我只看到一扇空窗户。
它不起作用也没关系,因为我可以在没有看到立方体的情况下回答问题,但是任务的下一部分是创建一个四面体而不是立方体,为此我需要代码来工作。
如果有人能看到我缺少的东西,我们将不胜感激。
// Geometry
//
// This tutorial supports learning
// about assembling a scene in a scene graph
// using transformation cores
// headers for OpenSG configuration and GLUT
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGSceneFileHandler.h>
#include <OpenSG/OSGGeoProperties.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
// Simple Scene manager for accesing cameras and geometry
OSG::SimpleSceneManagerRefPtr mgr;
int setupGLUT(int *argc, char *argv[]);
int main(int argc, char **argv)
{
// initialise OpenSG
OSG::osgInit(argc, argv);
// initialise GLUT
int winid = setupGLUT(&argc, argv);
{
// create a OSGGLUT window
OSG::GLUTWindowRefPtr gwin = OSG::GLUTWindow::create();
gwin->setGlutId(winid);
gwin->init();
//read the file which will be passed as an argument
OSG::NodeRefPtr scene = OSG::Node::create();
OSG::NodeRefPtr createCube = OSG::Node::create();
scene = createCube;
//commit all changes to OpenSG
OSG::commitChanges();
// create the SimpleSceneManager helper
mgr = OSG::SimpleSceneManager::create();
// tell the manager what to manage
mgr->setWindow(gwin);
mgr->setRoot(scene);
// show the whole scene
mgr->showAll();
}
// GLUT main loop
glutMainLoop();
return 0;
}
//
// GLUT callback functions
//
// redraw the window
void display(void)
{
mgr->redraw();
}
// react to size changes
void reshape(int w, int h)
{
mgr->resize(w, h);
glutPostRedisplay();
}
// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
if (!state)
mgr->mouseButtonPress(button, x, y);
glutPostRedisplay();
}
// react to mouse motions with pressed buttons
void motion(int x, int y)
{
mgr->mouseMove(x, y);
glutPostRedisplay();
}
// react to keys
void keyboard(unsigned char k, int x, int y)
{
switch (k)
{
case 'e':
{
// clean up global variables
mgr = NULL;
OSG::osgExit();
exit(0);
}
break;
case 's':
{
mgr->setStatistics(!mgr->getStatistics());
}
break;
}
}
// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
glutInit(argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
int winid = glutCreateWindow("05 Transformations Tutorial");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
return winid;
}
OSG::NodeRefPtr createCube()
{
//create a node to hold the geometry
OSG::NodeRefPtr geonode = OSG::Node::create();
//create a geometry
OSG::GeometryRefPtr geo = OSG::Geometry::create();
//The primitive types.
//OpenGL provides us with several different types of shapes that we can draw
//(e.g. GL_LINES, GL_POLYGON, GL_QUADS, GL_TRIANGLES)
//we need to specify the type of geometry we want to use
//lets start by using only triangles (although different types can be freely mixed)
OSG::GeoUInt8PropertyRefPtr type = OSG::GeoUInt8Property::create();
//MODIFY HERE
type->addValue(GL_TRIANGLES);
//The primitive lengths.
//These define the number of vertices to be passed to OpenGL for each primitive.
//Thus there have to be at least as many entries as in the types property.
//in the case of the cube we are using 12 triangles which each have 3 vertices (12 X 3 = 36)
OSG::GeoUInt32PropertyRefPtr lens = OSG::GeoUInt32Property::create();
//MODIFY HERE
lens->addValue(36);
// The vertices.
OSG::GeoPnt3fPropertyRefPtr pnts = OSG::GeoPnt3fProperty::create();
//MODIFY HERE with positions of your geometry
pnts->addValue(OSG::Pnt3f(-0.5, 0.5, 0.5));
pnts->addValue(OSG::Pnt3f(0.5, 0.5, 0.5));
pnts->addValue(OSG::Pnt3f(0.5, -0.5, 0.5));
pnts->addValue(OSG::Pnt3f(-0.5, -0.5, 0.5));
pnts->addValue(OSG::Pnt3f(-0.5, 0.5, -0.5));
pnts->addValue(OSG::Pnt3f(0.5, 0.5, -0.5));
pnts->addValue(OSG::Pnt3f(0.5, -0.5, -0.5));
pnts->addValue(OSG::Pnt3f(-0.5, -0.5, -0.5));
// The normals.
//These are used for lighting calculations and have to point away from the
//surface. Normals are standard vectors.
OSG::GeoVec3fPropertyRefPtr norms = OSG::GeoVec3fProperty::create();
norms->push_back(OSG::Vec3f(0, 0, 1));
norms->push_back(OSG::Vec3f(1, 0, 0));
norms->push_back(OSG::Vec3f(0, 0, -1));
norms->push_back(OSG::Vec3f(-1, 0, 0));
norms->push_back(OSG::Vec3f(0, 1, 0));
norms->push_back(OSG::Vec3f(0, -1, 0));
// The colours.
// GeoColor3fProperty stores all color values that will be used
OSG::GeoColor3fPropertyRecPtr colors = OSG::GeoColor3fProperty::create();
colors->addValue(OSG::Color3f(0, 0, 1));
colors->addValue(OSG::Color3f(0, 0, 1));
colors->addValue(OSG::Color3f(0, 0, 1));
colors->addValue(OSG::Color3f(0, 0, 1));
colors->addValue(OSG::Color3f(0, 0, 1));
colors->addValue(OSG::Color3f(0, 0, 1));
// The indices.
// in order not to replicate the same positions all the time,
// use index number of the position
OSG::GeoUInt32PropertyRefPtr indices = OSG::GeoUInt32Property::create();
//face 1: front
//face 1 - triangle 1
indices->addValue(0);
indices->addValue(2);
indices->addValue(1);
//face 1 - triangle 2
indices->addValue(0);
indices->addValue(3);
indices->addValue(2);
//face 2: right
//face 2 - triangle 1
indices->addValue(1);
indices->addValue(2);
indices->addValue(6);
//face 3 - triangle 2
indices->addValue(1);
indices->addValue(6);
indices->addValue(5);
//face 3: back
//face 3 - triangle 1
indices->addValue(5);
indices->addValue(6);
indices->addValue(7);
//face 3 - triangle 2
indices->addValue(5);
indices->addValue(7);
indices->addValue(4);
//face 4: left
//face 4 - triangle 1
indices->addValue(4);
indices->addValue(7);
indices->addValue(3);
//face 4 - triangle 2
indices->addValue(4);
indices->addValue(3);
indices->addValue(0);
//face 5: top
//face 5 - triangle 1
indices->addValue(4);
indices->addValue(1);
indices->addValue(5);
//face 5 - triangle 2
indices->addValue(4);
indices->addValue(0);
indices->addValue(1);
//face 6: bottom
//face 6 - triangle 1
indices->addValue(2);
indices->addValue(3);
indices->addValue(7);
//face 6 - triangle 2
indices->addValue(2);
indices->addValue(7);
indices->addValue(6);
// The indices for colours and normals
// as normals are different for each side of the cube, we use a special index for this property
OSG::GeoUInt32PropertyRefPtr indicesnormpos = OSG::GeoUInt32Property::create();
//face 1: front
//face 1 - triangle 1
indicesnormpos->addValue(0);
indicesnormpos->addValue(0);
indicesnormpos->addValue(0);
//face 1 - triangle 2
indicesnormpos->addValue(0);
indicesnormpos->addValue(0);
indicesnormpos->addValue(0);
//face 2: right
//face 2 - triangle 1
indicesnormpos->addValue(1);
indicesnormpos->addValue(1);
indicesnormpos->addValue(1);
//face 3 - triangle 2
indicesnormpos->addValue(1);
indicesnormpos->addValue(1);
indicesnormpos->addValue(1);
//face 3: back
//face 3 - triangle 1
indicesnormpos->addValue(2);
indicesnormpos->addValue(2);
indicesnormpos->addValue(2);
//face 3 - triangle 2
indicesnormpos->addValue(2);
indicesnormpos->addValue(2);
indicesnormpos->addValue(2);
//face 4: left
//face 4 - triangle 1
indicesnormpos->addValue(3);
indicesnormpos->addValue(3);
indicesnormpos->addValue(3);
//face 4 - triangle 2
indicesnormpos->addValue(3);
indicesnormpos->addValue(3);
indicesnormpos->addValue(3);
//face 5: top
//face 5 - triangle 1
indicesnormpos->addValue(4);
indicesnormpos->addValue(4);
indicesnormpos->addValue(4);
//face 5 - triangle 2
indicesnormpos->addValue(4);
indicesnormpos->addValue(4);
indicesnormpos->addValue(4);
//face 6: bottom
//face 6 - triangle 1
indicesnormpos->addValue(5);
indicesnormpos->addValue(5);
indicesnormpos->addValue(5);
//face 6 - triangle 2
indicesnormpos->addValue(5);
indicesnormpos->addValue(5);
indicesnormpos->addValue(5);
// Put it all together into a Geometry NodeCore.
geo->setTypes(type);
geo->setLengths(lens);
geo->setProperty(pnts, OSG::Geometry::PositionsIndex);
geo->setIndex(indices, OSG::Geometry::PositionsIndex);
geo->setProperty(norms, OSG::Geometry::NormalsIndex);
geo->setIndex(indicesnormpos, OSG::Geometry::NormalsIndex);
geo->setProperty(colors, OSG::Geometry::ColorsIndex);
geo->setIndex(indicesnormpos, OSG::Geometry::ColorsIndex);
// if you were not using any indexing you will simply use:
//geo->setTypes (type);
//geo->setLengths (lens);
//geo->setPositions (pnts);
//geo->setNormals (norms);
//geo->setColors (colors);
geonode->setCore(geo);
return geonode;
}
当您在main中调用OSG::NodeRefPtr createCube()
时,它不是由声明的。所以,只需声明:
OSG::NodeRefPtr createCube();
int setupGLUT(int *argc, char *argv[]);
int main(int argc, char **argv) {
// Rest of code
而且,总的来说:
//read the file which will be passed as an argument
OSG::NodeRefPtr scene = createCube();
您可以删除以下语句:
OSG::NodeRefPtr createCube = OSG::Node::create();
scene = createCube;
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 使用std::multimap迭代器创建std::list
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 使用CMake创建QML插件
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 试图在visual studio上用C++创建一个桌面应用程序
- std::threads可以从Windows DLL中的全局变量创建/销毁吗?
- 如何在C++20中创建模板别名的推导指南
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 链接到自行创建的dll失败
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++类内存结构中创建"spacer"?
- 如何使用变体创建几何图形
- C++-创建几何图形