示例#1
0
GL_Window::GL_Window(int x, int y, int w, int h, const char *label):
    Fl_Gl_Window(x, y, w, h, label)
{
    _gw = new osgViewer::GraphicsWindowEmbedded(x,y,w,h);

    root = new osg::Group;
    geode = new osg::Geode;

    osg::ref_ptr<osg::Capsule> myCapsule (new osg::Capsule(osg::Vec3f(),1,2));
    osg::ref_ptr<osg::Box> myBox (new osg::Box(osg::Vec3f(),2,2,2));
    osg::ref_ptr<osg::Cylinder> myCylinder (new osg::Cylinder(osg::Vec3f(),.75,2.5));
    osg::ref_ptr<osg::Sphere> mySphere (new osg::Sphere(osg::Vec3f(),1.5));
    osg::ref_ptr<osg::Cone> myCone (new osg::Cone(osg::Vec3f(),1,2));

    shape_vec.push_back(new osg::ShapeDrawable(myBox.get()));
    shape_vec.push_back(new osg::ShapeDrawable(myCapsule.get()));
    shape_vec.push_back(new osg::ShapeDrawable(myCone.get()));
    shape_vec.push_back(new osg::ShapeDrawable(myCylinder.get()));
    shape_vec.push_back(new osg::ShapeDrawable(mySphere.get()));

    geode->addDrawable(shape_vec[1].get());     // default shape to pick from

    root->addChild(geode.get());

    stateset = geode->getOrCreateStateSet();

    // initial material properties
    diffuse_color.set(1, 1, 1, 1.0);
    specular_color.set(1,1,1,1);
    ambient_color.set(1,1,1,1);
    shininess = 10;
    alpha = 1;

    // apply the properties to the material
    material = new osg::Material;
    material->setDiffuse(osg::Material::FRONT, diffuse_color);
    material->setSpecular(osg::Material::FRONT, specular_color);
    material->setShininess(osg::Material::FRONT, shininess);
    material->setAmbient(osg::Material::FRONT, ambient_color);

    stateset->setAttribute(material.get());
    //stateset->setMode(GL_LIGHTING, osg::StateAttribute::OFF);

    texture = new osg::Texture2D;

    getCamera()->setViewport(new osg::Viewport(0,0,w,h));
    getCamera()->setProjectionMatrixAsPerspective(30.0f, static_cast<double>(w)/static_cast<double>(h), 1.0f, 10000.0f);
    getCamera()->setGraphicsContext(_gw.get());

    setSceneData(root.get());
    setCameraManipulator(new osgGA::TrackballManipulator);
    addEventHandler(new osgViewer::StatsHandler);
    setThreadingModel(osgViewer::Viewer::SingleThreaded);
}
示例#2
0
static void
generate_system (ModeInfo * mi)
{
	pipesstruct *pp = &pipes[MI_SCREEN(mi)];
    Bool        wire = MI_IS_WIREFRAME(mi);

	int         newdir;
	int         OPX, OPY, OPZ;

    Bool reset_p = False;

    pp->system_index = 0;
    pp->system_size = 0;
    pinit (mi, 1);

    while (1) {
      glNewList (get_dlist (mi, pp->system_size++), GL_COMPILE);
      mi->polygon_count = 0;

	glPushMatrix();

	FindNeighbors(mi);

    if (wire)
      glColor4fv (pp->system_color);
    else
      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pp->system_color);

	/* If it's the begining of a system, draw a sphere */
	if (pp->olddir == dirNone) {
		glPushMatrix();
		glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
		mySphere(0.6, wire);
		glPopMatrix();
	}
	/* Check for stop conditions */
	if (pp->ndirections == 0 || pp->counter > pp->system_length) {
		glPushMatrix();
		glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
		/* Finish the system with another sphere */
		mySphere(0.6, wire);

		glPopMatrix();

		/* If the maximum number of system was drawn, restart (clearing the screen), */
		/* else start a new system. */
		if (++pp->system_number > pp->number_of_systems) {
          reset_p = True;
		} else {
			pinit(mi, 0);
		}

        goto NEXT;
	}
	pp->counter++;
	pp->turncounter++;

	/* Do will the direction change? if so, determine the new one */
	newdir = pp->nowdir;
	if (!pp->directions[newdir]) {	/* cannot proceed in the current direction */
		newdir = SelectNeighbor(mi);
	} else {
		if (tightturns) {
			/* random change (20% chance) */
			if ((pp->counter > 1) && (NRAND(100) < 20)) {
				newdir = SelectNeighbor(mi);
			}
		} else {
			/* Chance to turn increases after each length of pipe drawn */
			if ((pp->counter > 1) && NRAND(50) < NRAND(pp->turncounter + 1)) {
				newdir = SelectNeighbor(mi);
				pp->turncounter = 0;
			}
		}
	}

	/* Has the direction changed? */
	if (newdir == pp->nowdir) {
		/* If not, draw the cell's center pipe */
		glPushMatrix();
		glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
		/* Chance of factory shape here, if enabled. */
		if ((pp->counter > 1) && (NRAND(100) < factory)) {
			MakeShape(mi, newdir);
		} else {
			MakeTube(mi, newdir);
		}
		glPopMatrix();
	} else {
		/* If so, draw the cell's center elbow/sphere */
		int         sysT = pp->system_type;

		if (sysT == NofSysTypes + 1) {
			sysT = ((pp->system_number - 1) % NofSysTypes) + 1;
		}
		glPushMatrix();

		switch (sysT) {
			case 1:
				glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0);
				mySphere(elbowradius, wire);
				break;
			case 2:
			case 3:
				switch (pp->nowdir) {
					case dirUP:
						switch (newdir) {
							case dirLEFT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								glRotatef(180.0, 1.0, 0.0, 0.0);
								break;
							case dirRIGHT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								glRotatef(180.0, 1.0, 0.0, 0.0);
								glRotatef(180.0, 0.0, 1.0, 0.0);
								break;
							case dirFAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(90.0, 0.0, 1.0, 0.0);
								glRotatef(180.0, 0.0, 0.0, 1.0);
								break;
							case dirNEAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(90.0, 0.0, 1.0, 0.0);
								glRotatef(180.0, 1.0, 0.0, 0.0);
								break;
						}
						break;
					case dirDOWN:
						switch (newdir) {
							case dirLEFT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								break;
							case dirRIGHT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								glRotatef(180.0, 0.0, 1.0, 0.0);
								break;
							case dirFAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(270.0, 0.0, 1.0, 0.0);
								break;
							case dirNEAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(90.0, 0.0, 1.0, 0.0);
								break;
						}
						break;
					case dirLEFT:
						switch (newdir) {
							case dirUP:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								glRotatef(180.0, 0.0, 1.0, 0.0);
								break;
							case dirDOWN:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								glRotatef(180.0, 1.0, 0.0, 0.0);
								glRotatef(180.0, 0.0, 1.0, 0.0);
								break;
							case dirFAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(270.0, 1.0, 0.0, 0.0);
								glRotatef(180.0, 0.0, 1.0, 0.0);
								break;
							case dirNEAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(270.0, 1.0, 0.0, 0.0);
								glRotatef(180.0, 0.0, 0.0, 1.0);
								break;
						}
						break;
					case dirRIGHT:
						switch (newdir) {
							case dirUP:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								break;
							case dirDOWN:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0);
								glRotatef(180.0, 1.0, 0.0, 0.0);
								break;
							case dirFAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(270.0, 1.0, 0.0, 0.0);
								break;
							case dirNEAR:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(90.0, 1.0, 0.0, 0.0);
								break;
						}
						break;
					case dirNEAR:
						switch (newdir) {
							case dirLEFT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(270.0, 1.0, 0.0, 0.0);
								break;
							case dirRIGHT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(270.0, 1.0, 0.0, 0.0);
								glRotatef(180.0, 0.0, 1.0, 0.0);
								break;
							case dirUP:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(270.0, 0.0, 1.0, 0.0);
								break;
							case dirDOWN:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 - (one_third));
								glRotatef(90.0, 0.0, 1.0, 0.0);
								glRotatef(180.0, 0.0, 0.0, 1.0);
								break;
						}
						break;
					case dirFAR:
						switch (newdir) {
							case dirUP:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 + (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(90.0, 0.0, 1.0, 0.0);
								break;
							case dirDOWN:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0, (pp->PY - 12) / 3.0 * 4.0 - (one_third), (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(90.0, 0.0, 1.0, 0.0);
								glRotatef(180.0, 1.0, 0.0, 0.0);
								break;
							case dirLEFT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 - (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(90.0, 1.0, 0.0, 0.0);
								break;
							case dirRIGHT:
								glTranslatef((pp->PX - 16) / 3.0 * 4.0 + (one_third), (pp->PY - 12) / 3.0 * 4.0, (pp->PZ - 16) / 3.0 * 4.0 + (one_third));
								glRotatef(270.0, 1.0, 0.0, 0.0);
								glRotatef(180.0, 0.0, 0.0, 1.0);
								break;
						}
						break;
				}
				myElbow(mi, (sysT == 2));
				break;
		}
		glPopMatrix();
	}

	OPX = pp->PX;
	OPY = pp->PY;
	OPZ = pp->PZ;
	pp->olddir = pp->nowdir;
	pp->nowdir = newdir;
	switch (pp->nowdir) {
		case dirUP:
			pp->PY++;
			break;
		case dirDOWN:
			pp->PY--;
			break;
		case dirLEFT:
			pp->PX--;
			break;
		case dirRIGHT:
			pp->PX++;
			break;
		case dirNEAR:
			pp->PZ++;
			break;
		case dirFAR:
			pp->PZ--;
			break;
	}
	pp->Cells[pp->PX][pp->PY][pp->PZ] = 1;

	/* Cells'face pipe */
	glTranslatef(((pp->PX + OPX) / 2.0 - 16) / 3.0 * 4.0, ((pp->PY + OPY) / 2.0 - 12) / 3.0 * 4.0, ((pp->PZ + OPZ) / 2.0 - 16) / 3.0 * 4.0);
	MakeTube(mi, newdir);

    NEXT:
	glPopMatrix();
    glEndList();
    pp->poly_counts [pp->system_size-1] = mi->polygon_count;

    if (reset_p)
      break;
    }
}
示例#3
0
/* draw the scene */
static Bool draw_antinspect_strip(ModeInfo * mi) 
{
  antinspectstruct *mp = &antinspect[MI_SCREEN(mi)];
  int         i, j;
  int         mono = MI_IS_MONO(mi);

  int ro = (((int)antposition[1])/(360/(2*ANTCOUNT))) % (2*ANTCOUNT);

  glEnable(GL_TEXTURE_2D);
  position0[1] = 9.6;
  glLightfv(GL_LIGHT0, GL_POSITION, position0);

  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
  glRotatef(-30.0, 0.0, 1.0, 0.0);

  glDisable(GL_TEXTURE_2D);
  glDisable(GL_BLEND);

  /* render ground plane */
  glBegin(GL_TRIANGLES);
  glColor4fv(MaterialShadow);
  glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlack);
  glNormal3f(0.0, 1.0, 0.0);

  /* middle tri */
  glVertex3f(0.0, 0.0, -1.0);
  glVertex3f(-sqrt(3.0)/2.0, 0.0, 0.5);
  glVertex3f(sqrt(3.0)/2.0, 0.0, 0.5);
  mi->polygon_count++;
  glEnd();

  /* rotate */
  for(i = 0; i < 3; ++i) {
    glRotatef(120.0, 0.0, 1.0, 0.0);
    glBegin(GL_TRIANGLES);
    glVertex3f(0.0, 0.0, 1.0 + 3.0);
    glVertex3f(sqrt(3.0)/2.0, 0.0, -0.5 + 3.0);
    glVertex3f(-sqrt(3.0)/2.0, 0.0, -0.5 + 3.0);
    mi->polygon_count++;
    glEnd();
  }

  /* first render shadows -- no depth required */
  if(shadows) {
    GLfloat m[4][4];
    shadowmatrix(m, ground, position0);
    
    glColor4fv(MaterialShadow);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
    
    glDisable(GL_BLEND);
    glDisable(GL_LIGHTING);
    
    /* display ant shadow */
    glPushMatrix();
    glTranslatef(0.0, 0.001, 0.0);
    glMultMatrixf(m[0]);

    for(i = 0; i < ANTCOUNT; ++i) {

      /* draw ant */
      glPushMatrix();

      /* center */
      glRotatef(antposition[i], 0.0, 1.0, 0.0);
      glTranslatef(2.4, 0.0, 0.0);
      glTranslatef(0.0, antsphere[i], 0.0);
      glRotatef(90.0, 0.0, 1.0, 0.0);

      /* orient ant */
      glRotatef(10.0, 0.0, 1.0, 0.0);
      glRotatef(40.0, 0.0, 0.0, 1.0);
      glTranslatef(0.0, -0.8, 0.0);
      glRotatef(180.0, 0.0, 1.0, 0.0);
      glRotatef(90.0, 0.0, 0.0, 1.0);

      /* set colour */
      glColor4fv(MaterialShadow);
      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);

      if(antposition[i] > 360.0)
	antposition[i] = 0.0;
      draw_antinspect_ant(mi, mp, MaterialShadow, mono, mySphere2, myCone2);

      glDisable(GL_BLEND);
      glDisable(GL_LIGHTING);

      /* draw sphere */
      glRotatef(-20.0, 1.0, 0.0, 0.0);
      glRotatef(-mp->ant_step*2, 0.0, 0.0, 1.0);
      glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialShadow);
      mySphere2(1.2);

      glPopMatrix();
    }
  
    glPopMatrix();
  }

  glEnable(GL_LIGHTING);

  /* truants */
  for(j = 0; j < ANTCOUNT; ++j) {
    /* determine rendering order */
    i = antorder[ro][j];

    glPushMatrix();
    
    /* center */
    glRotatef(antposition[i], 0.0, 1.0, 0.0);
    glTranslatef(2.4, 0.0, 0.0);
    glTranslatef(0.0, antsphere[i], 0.0);    
    glRotatef(90.0, 0.0, 1.0, 0.0);

    /* draw ant */
    glPushMatrix();
    glRotatef(10.0, 0.0, 1.0, 0.0);
    glRotatef(40.0, 0.0, 0.0, 1.0);
    glTranslatef(0.0, -0.8, 0.0);
    glRotatef(180.0, 0.0, 1.0, 0.0);
    glRotatef(90.0, 0.0, 0.0, 1.0);
    if(antposition[i] > 360.0)
      antposition[i] = 0.0;
    glEnable(GL_BLEND);
    draw_antinspect_ant(mi, mp, antmaterial[i], mono, mySphere2, myCone2);
    glDisable(GL_BLEND);
    glPopMatrix();

    /* draw sphere */
    glRotatef(-20.0, 1.0, 0.0, 0.0);
    glRotatef(-mp->ant_step*2, 0.0, 0.0, 1.0);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mono ? MaterialGray5 : antmaterial[i]);
    mySphere2(1.2);
    glEnable(GL_BLEND);
    glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialBlack);
    mySphere(1.16);
    glDisable(GL_BLEND);
        
    glPopMatrix();

    /* finally, evolve */
    antposition[i] += antvelocity[i];
  }

  /* but the step size is the same! */
  mp->ant_step += 0.2;
  
  mp->ant_position += 1;
  return True;
}
示例#4
0
// The MAIN function, from here we start the application and run the main loop
int main()
{
    ///*************************************************************************************///
    ///******************************** PCL LOADING PART ***********************************///
    ///*************************************************************************************///

    // (This part will be separated later!)

    // Later, this will be according to the command line argument
    // Now I test it, with "fovam2a_bin_compressed.pcd"
    if (pcl::io::loadPCDFile<pcl::PointXYZRGB> ("fovam2a_bin_compressed.pcd", *cloud) == -1) // load the file
    {
        PCL_ERROR ("Couldn't read file test_pcd.pcd \n");
        return (-1);
    }
    std::cout << "Loaded " << cloud->width * cloud->height << " data points from test_pcd.pcd with the following fields: " << std::endl;


    std::cout << cloud->width << std::endl;
    std::cout << cloud->height << std::endl;

    // Set the initial minimum value of each coordinate:
    min_x = cloud->points[0].x;
    min_y = cloud->points[0].y;
    min_z = cloud->points[0].z;

    // Calculate the minimum value of each coordinate:
    for (size_t i = 0; i < cloud->points.size (); ++i)
    {
        if(cloud->points[i].x < min_x) min_x = cloud->points[i].x;
        if(cloud->points[i].y < min_y) min_y = cloud->points[i].y;
        if(cloud->points[i].z < min_z) min_z = cloud->points[i].z;
    }

    // Transform the cloud to the origin of its coordinate system, for easier handling of the cloud data.
    // This part of the code should be removed later. (Just helped me at the beginning)
    for(size_t i = 0; i < cloud->points.size (); ++i)
    {
        cloud->points[i].x -= min_x;
        cloud->points[i].y -= min_y;
        cloud->points[i].z -= min_z;
    }

    ///*************************************************************************************///
    ///******************************** GLFW INIT PART ***********************************///
    ///*************************************************************************************///

    // Init GLFW
    glfwInit();

    // Set all the required options for GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    // Create a GLFWwindow object that we can use for GLFW's functions
    GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "PCL with OpenGL", nullptr, nullptr);
    glfwMakeContextCurrent(window);

    // Set the required callback functions
    glfwSetKeyCallback(window, key_callback);
    glfwSetCursorPosCallback(window, mouse_callback);
    glfwSetScrollCallback(window, scroll_callback);

    // Options
    glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    // This is how GLEW knows that it should use a modern approach for retrieving function pointers and extensions
    glewExperimental = GL_TRUE;

    // Initialize GLEW to setup the OpenGL Function pointers
    glewInit();

    // Define the viewport dimensions
    glViewport(0, 0, WIDTH, HEIGHT);

    glEnable(GL_DEPTH_TEST);
    glPointSize(1.3f);

    ///*************************************************************************************///
    ///******************************** VBO LOADING PART ***********************************///
    ///*************************************************************************************///

    Shader ourShader("shader.vs", "shader.frag"); // load the shaders

    std::size_t v_size = cloud->points.size() * 6; // size of my points
    // (all of them contains 6 member)

    // I will fill up this vector with all the data from cloud->points
    std::vector<GLfloat> vertices(v_size);

    // for efficient data reading, I start a new thread for reading the half of my data.
    std::future<void> result( std::async([&]()
    {
        for(size_t i = 1; i < cloud->points.size (); i+=2)
        {
            size_t num = (i * 6);

            vertices[num + 0] = cloud->points[i].x;
            vertices[num + 1] = cloud->points[i].y;
            vertices[num + 2] = cloud->points[i].z;

            vertices[num + 3] = (float)cloud->points[i].r / 256.f;
            vertices[num + 4] = (float)cloud->points[i].g / 256.f;
            vertices[num + 5] = (float)cloud->points[i].b / 256.f;
        }

    }));

    // another half of my points
    for(size_t i = 0; i < cloud->points.size (); i+=2)
    {
        size_t num = (i * 6);

        vertices[num + 0] = cloud->points[i].x;
        vertices[num + 1] = cloud->points[i].y;
        vertices[num + 2] = cloud->points[i].z;

        vertices[num + 3] = (float)cloud->points[i].r / 256.f;
        vertices[num + 4] = (float)cloud->points[i].g / 256.f;
        vertices[num + 5] = (float)cloud->points[i].b / 256.f;
    }

    result.get(); // wait for the other thread
    // Now, I've filled up my vector!

    std::cout << "*****!!!DONE! (read)!!!*****" << std::endl;

    /// Create the VBO from the data:
    GLuint VBO, VAO;
    glGenVertexArrays(1, &VAO);
    glGenBuffers(1, &VBO);

    // Bind the Vertex Array Object first, then bind and set the vertex buffer(s) and the attribute pointer(s).
    glBindVertexArray(VAO);

    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * vertices.size(), vertices.data(), GL_STATIC_DRAW);

    // Position attribute
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)0);
    glEnableVertexAttribArray(0);

    // Color attribute
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
    glEnableVertexAttribArray(1);

    glBindVertexArray(0); // Unbind VAO

    std::cout << "*****!!!DONE! (VBO load)!!!*****" << std::endl;

    ourShader.Use();

    /// Load transformation matrices
    GLuint MatrixID_modelview = glGetUniformLocation(ourShader.Program, "modelview");
    GLuint MatrixID_proj = glGetUniformLocation(ourShader.Program, "projection");

    // Send our transformations to the currently bound shader
    glUniformMatrix4fv(MatrixID_modelview, 1, GL_FALSE, &ModelView[0][0]);
    glUniformMatrix4fv(MatrixID_proj, 1, GL_FALSE, &Proj[0][0]);

    vertices.clear();
    //cloud.reset();

    VeryNaiveSphere mySphere(500, 500, glm::vec3(10000.f, 10000.f, 10000.f));
    mySphere.init_sphere();

    float resolution = 128.0f; // for the leaf level of the octree

    pcl::octree::OctreePointCloudSearch<pcl::PointXYZRGB> octree (resolution);

    // Fill up my tree:
    octree.setInputCloud (cloud);
    octree.addPointsFromInputCloud();

    ///*************************************************************************************///
    ///******************************** MAIN LOOP PART ***********************************///
    ///*************************************************************************************///

    std::cout << "*****!!!LOOP!!!*****" << std::endl;
    // Main loop
    while (!glfwWindowShouldClose(window))
    {
        // Set frame time
        GLfloat currentFrame = glfwGetTime();
        deltaTime = currentFrame - lastFrame;
        lastFrame = currentFrame;

        // Check if any events have been activiated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();
        Do_Camera_movement();
        Do_Sphere_movement(mySphere, octree);

        // Render
        // Clear the colorbuffer and the depthbuffer
        glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Draw
        ourShader.Use();

        Proj = glm::perspective(camera.Zoom, (float)WIDTH / (float)HEIGHT, cam_near, cam_far);

        ModelView = camera.GetViewMatrix();

        glUniformMatrix4fv(MatrixID_proj, 1, GL_FALSE, &Proj[0][0]);
        glUniformMatrix4fv(MatrixID_modelview, 1, GL_FALSE, &ModelView[0][0]);

        glBindVertexArray(VAO);
        glDrawArrays(GL_POINTS, 0, v_size / 6);
        glBindVertexArray(0);

        mySphere.draw(MatrixID_modelview, ModelView);

        // Swap the screen buffers
        glfwSwapBuffers(window);
    }
    // Properly de-allocate all resources once they've outlived their purpose
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);

    // Terminate GLFW, clearing any resources allocated by GLFW.
    glfwTerminate();
    return 0;
}
示例#5
0
static Bool
draw_moebius_ant(moebiusstruct * mp, const float *Material, int mono)
{
	float       cos1 = cos(mp->ant_step);
	float       cos2 = cos(mp->ant_step + 2 * Pi / 3);
	float       cos3 = cos(mp->ant_step + 4 * Pi / 3);
	float       sin1 = sin(mp->ant_step);
	float       sin2 = sin(mp->ant_step + 2 * Pi / 3);
	float       sin3 = sin(mp->ant_step + 4 * Pi / 3);

	if (mono)
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, MaterialGray5);
	else
		glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, Material);
	glEnable(GL_CULL_FACE);
	glPushMatrix();
	glScalef(1, 1.3, 1);
	if (!mySphere(0.18))
		return False;
	glScalef(1, 1 / 1.3, 1);
	glTranslatef(0.00, 0.30, 0.00);
	if (!mySphere(0.2))
		return False;

	glTranslatef(-0.05, 0.17, 0.05);
	glRotatef(-90, 1, 0, 0);
	glRotatef(-25, 0, 1, 0);
	if (!myCone(0.05))
		return False;
	glTranslatef(0.00, 0.10, 0.00);
	if (!myCone(0.05))
		return False;
	glRotatef(25, 0, 1, 0);
	glRotatef(90, 1, 0, 0);

	glScalef(1, 1.3, 1);
	glTranslatef(0.15, -0.65, 0.05);
	if (!mySphere(0.25))
		return False;
	glScalef(1, 1 / 1.3, 1);
	glPopMatrix();
	glDisable(GL_CULL_FACE);

	glDisable(GL_LIGHTING);
	/* ANTENNAS */
	glBegin(GL_LINES);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, 0.30, 0.00);
	glColor3fv(MaterialGray);
	glVertex3f(0.40, 0.70, 0.40);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, 0.30, 0.00);
	glColor3fv(MaterialGray);
	glVertex3f(0.40, 0.70, -0.40);
	glEnd();
	glBegin(GL_POINTS);
	if (mono)
		glColor3fv(MaterialGray6);
	else
		glColor3fv(MaterialRed);
	glVertex3f(0.40, 0.70, 0.40);
	glVertex3f(0.40, 0.70, -0.40);
	glEnd();

	/* LEFT-FRONT ARM */
	glBegin(GL_LINE_STRIP);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, 0.05, 0.18);
	glVertex3f(0.35 + 0.05 * cos1, 0.15, 0.25);
	glColor3fv(MaterialGray);
	glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
	glEnd();

	/* LEFT-CENTER ARM */
	glBegin(GL_LINE_STRIP);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, 0.00, 0.18);
	glVertex3f(0.35 + 0.05 * cos2, 0.00, 0.25);
	glColor3fv(MaterialGray);
	glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
	glEnd();

	/* LEFT-BACK ARM */
	glBegin(GL_LINE_STRIP);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, -0.05, 0.18);
	glVertex3f(0.35 + 0.05 * cos3, -0.15, 0.25);
	glColor3fv(MaterialGray);
	glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
	glEnd();

	/* RIGHT-FRONT ARM */
	glBegin(GL_LINE_STRIP);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, 0.05, -0.18);
	glVertex3f(0.35 - 0.05 * sin1, 0.15, -0.25);
	glColor3fv(MaterialGray);
	glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
	glEnd();

	/* RIGHT-CENTER ARM */
	glBegin(GL_LINE_STRIP);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, 0.00, -0.18);
	glVertex3f(0.35 - 0.05 * sin2, 0.00, -0.25);
	glColor3fv(MaterialGray);
	glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
	glEnd();

	/* RIGHT-BACK ARM */
	glBegin(GL_LINE_STRIP);
	if (mono)
		glColor3fv(MaterialGray5);
	else
		glColor3fv(Material);
	glVertex3f(0.00, -0.05, -0.18);
	glVertex3f(0.35 - 0.05 * sin3, -0.15, -0.25);
	glColor3fv(MaterialGray);
	glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
	glEnd();

	glBegin(GL_POINTS);
	if (mono)
		glColor3fv(MaterialGray8);
	else
		glColor3fv(MaterialMagenta);
	glVertex3f(-0.20 + 0.05 * cos1, 0.25 + 0.1 * sin1, 0.45);
	glVertex3f(-0.20 + 0.05 * cos2, 0.00 + 0.1 * sin2, 0.45);
	glVertex3f(-0.20 + 0.05 * cos3, -0.25 + 0.1 * sin3, 0.45);
	glVertex3f(-0.20 - 0.05 * sin1, 0.25 + 0.1 * cos1, -0.45);
	glVertex3f(-0.20 - 0.05 * sin2, 0.00 + 0.1 * cos2, -0.45);
	glVertex3f(-0.20 - 0.05 * sin3, -0.25 + 0.1 * cos3, -0.45);
	glEnd();

	glEnable(GL_LIGHTING);

	mp->ant_step += 0.3;
	return True;
}