Пример #1
0
void save_rgb_float_image_to_file(const char* file_name, float*& rgb_image, int width, int height)
{
	Magick::Image image;
	image.read(width, height, "RGB", Magick::FloatPixel, rgb_image);
	image.flip();
	image.write(file_name);
}
Пример #2
0
//----------------------------------------------------------------------------------------------------------------------
// Image Magick Image loading routines
//----------------------------------------------------------------------------------------------------------------------
bool Image::load( const std::string &_fname  ) noexcept
{
  #ifdef IMAGE_DEBUG_ON
  std::cerr<<"loading with ImageMagick"<<std::endl;
  #endif
  Magick::Image image;
  Magick::Blob blob;

  try
  {
    image.read(_fname);
    // need to flip image as OpenGL uses textures starting the other way round.
    image.flip();
    image.write(&blob, "RGBA");
  }
  catch (Magick::Error& Error)
  {
  std::cout << "Error loading texture '" << _fname << "': " << Error.what() << std::endl;
  return false;
  }
  m_width=image.columns();
  m_height=image.rows();
  m_channels=4;
  m_format=GL_RGBA;
  m_data.reset(new unsigned char[ m_width*m_height*m_channels]);
  // simple memcpy of the blob data to our internal data, not worrying about RGB/RGBA
  // here (as OpenGL doesn't really either).
  memcpy(m_data.get(),blob.data(),blob.length());
  return true;
}
Пример #3
0
void texture_t::load_cube_map( vector<string> const & files_names )
{
   Magick::Image img;

   for (int i = 0; i < files_names.size(); ++i)
   {
      img.read(files_names[i]);

      int res_x = img.rows()
         ,res_y = img.columns();

      scoped_array<unsigned char> data(new unsigned char[res_x * res_y * 3]);

      if (i == 2 || i == 3)
         img.flip();
      else
         img.flop();

      img.write(0, 0, res_x, res_y, "RGB", CharPixel, data.get());

      glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

      glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB16, res_x, res_y, 0, GL_RGB
                   ,GL_UNSIGNED_BYTE, data.get());
   }

   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
   glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
}
Пример #4
0
// Orient an image's pixels as EXIF instructs
int base_orient(Exiv2::ExifData & exif, Magick::Image & img) {
	int orient = -1;
	try {
		orient = exif["Exif.Image.Orientation"].toLong();
		if (-1 == orient) {
			orient = exif["Exif.Panasonic.Rotation"].toLong();
		}
		if (-1 == orient) {
			orient = exif["Exif.MinoltaCs5D.Rotation"].toLong();
		}
	} catch (Exiv2::Error &) {}
	if (1 > orient || 8 < orient) {
		orient = 1;
	}
	switch (orient) {
		case 2:
			img.flop();
		break;
		case 3:
			img.rotate(180.0);
		break;
		case 4:
			img.flip();
		break;
		case 5:
			img.rotate(90.0);
			img.flip();
		break;
		case 6:
			img.rotate(90.0);
		break;
		case 7:
			img.rotate(270.0);
			img.flip();
		break;
		case 8:
			img.rotate(270.0);
		break;
		default:
		break;
	}
	return orient;
}
Пример #5
0
void drawScene() {
	// get the axises
	Vector3d ux = (camera->v % camera->vup).normalize();
	Vector3d uy = camera->vup.normalize();
	Vector3d uz = -camera->v.normalize();
	Vector3d origin = camera->vp - (camera->focal * uz);
	
	// loop over every pixel
	for(int row = 0; row < image.rows(); row++) {
		for(int col = 0; col < image.columns(); col++) {
			Vector3d p = origin + camera->p(row, col, ux, uy);
			Vector3d ur = perspective ? (p - camera->vp).normalize() : -uz;

			double distance, min = -1;
			Sphere* sphere;
			
			
			// loop over the spheres
			for(int i = 0; i < nspheres; i++) {
				distance = spheres[i]->intersection(p, ur);
				if(distance > 0 && (min < 0 || distance < min)) {
					min = distance;
					sphere = spheres[i];
				}
			}

			Color color(0, 0, 0);
			  
			// if a sphere is hit loop over the lights
			if(min > 0) {
				Color ambient = sphere->material.color * sphere->material.ambient;
				Color diffuse(0, 0, 0);
				Color specular(0, 0, 0);
				Vector3d hit = p + (ur * min);				
				for(int i = 0; i < 3; i++) {
					diffuse = diffuse + lights[i]->diffuse(ur, hit, *sphere);
					specular = specular + lights[i]->specular(ur, hit, *sphere);
				}
				
        Color reflection = Light::reflection(*sphere, ur, hit, spheres, nspheres, lights, nlights, 0);
				
				// total up the color
				color = ambient + diffuse + specular + reflection;
			}
			
			image.pixelColor(col, row, color.ColorRGB());
		}
	}
	
	image.flip();
	imageToPixmap();
}
Пример #6
0
void ObjectData::loadTexture(string objID, string textureFileName)
{

    list<singleObject>::iterator iter;
    for (iter = listOfObjects.begin() ; iter != listOfObjects.end(); iter++)
        {
            if ( iter->id == objID )
            {
                // load texture image
                Magick::InitializeMagick("");
                Magick::Image image;
                Magick::Blob m_blob;
                try 
                    { 
                     // Read a file into image object 
                     if ( textureFileName != "")
                        {
                         image.read( textureFileName );
                         image.flip();
                         image.write(&m_blob, "RGBA");
                        }
                     else
                        {
                         throw std::invalid_argument("No texture file found");
                        }

                    } 
                catch(exception& tmp) 
                    { 
                     cout << "Error while reading in texture image, texture file not found"  << endl; 
                    } 

                int imageWidth = image.columns();
                int imageHeight = image.rows();


                // setup texture
                glGenTextures(1, &(iter->bTexture)); 
                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D,  iter->bTexture);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_blob.data());

                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
                glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
                }
        }

}
Пример #7
0
//------------------------------------------------------------------------------
void Image::ReadFile(const std::string& _fileName)
{
    //get data from file to local array
    Magick::Image imageFile;
    imageFile.read(_fileName);
    imageFile.flip();
    m_width = imageFile.columns();
    m_height = imageFile.rows();
    uint8_t* data = new uint8_t[ m_width * m_height * 4 ];
    imageFile.write( 0, 0, m_width, m_height, "RGBA", Magick::CharPixel, data );
	uint32_t size = m_width * m_height * 4;
    m_pixels = std::vector<Color>(0);
	for( uint32_t i=0; i< size; i+=4 )
	{
		uint32_t index = i/4.0;
		if( index < m_width * m_height )
		m_pixels.push_back ( Color ( data[i]/255.0, data[i+1]/255.0, data[i+2]/255.0, data[i+3]/255.0 ) );
	}
	delete [] data;
}
Пример #8
0
void writeImage() {
	if(filename != "") {
		image.flip();
		image.write(filename);
	}
}
Пример #9
0
void pixmapToImage() {
	Magick::PixelPacket* pixels = image.setPixels(0, 0, image.columns(), image.rows());
	*pixels = *pixmap;
	image.syncPixels();
	image.flip();
}
Пример #10
0
void imageToPixmap() {
	image.flip();
	pixmap = image.getPixels(0, 0, image.columns(), image.rows());
}
Пример #11
0
void Magick::flipImage::operator()( Magick::Image &image_ ) const
{
  image_.flip( );
}
Пример #12
0
bool initialize()    
{
    //cout << "Makes it to initalize!" << endl;
    bool loadedSuccess = true;
    char defualtOBJName[] = "iceRink.obj"; //Change to change the default loaded object.
    btCollisionShape *tempShape = NULL;
    btDefaultMotionState *tempMotionState = NULL;
    btScalar tempMass;
    btVector3 tempInertia;
    btRigidBody *tempRigidBody = NULL;
    
    //Collision Masks
    int shapeColidesWith = COL_WALL | COL_SHAPE;
    int wallColidesWith = COL_SHAPE;    
    

//TABLE
    globalObjCount++;
    Vertex *geometry;
    btVector3 tempVect = btVector3(0.0f, 1.0f, 0.0f);
    btScalar planeScaler = 3;
    objTriMesh = new btTriangleMesh();
    loadedSuccess = loadOBJ(defualtOBJName, &geometry);
    if ( !loadedSuccess )
    {
        cout << "OBJ file not found or invalid format" << endl;
        return false;
    }

    glGenBuffers(1, &vbo_geometry);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_geometry);
    glBufferData(GL_ARRAY_BUFFER, sizeof(geometry)*numberTriangles*3, geometry, GL_STATIC_DRAW);

    //Create collision Objects
    //Initalize the Hockey Table.
    tempShape = new btBvhTriangleMeshShape(objTriMesh, true); //Hockey Table
    //tempShape = new btStaticPlaneShape(tempVect, planeScaler);
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0,-15,0)));
    tempMass = 0;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    tempShape->calculateLocalInertia(tempMass, tempInertia);
    btRigidBody::btRigidBodyConstructionInfo shapeRigidBodyCI(tempMass, tempMotionState, tempShape, tempInertia);
    tempRigidBody = new btRigidBody(shapeRigidBodyCI);
    objectsDataList.addObject(0, tempShape, tempMotionState, tempMass, tempInertia, vbo_geometry, numberTriangles, 1, tempRigidBody);
    dynamicsWorld->addRigidBody(tempRigidBody, COL_WALL, wallColidesWith);

    tempRigidBody = NULL;
    delete geometry;
    numberTriangles = 0;
    delete objTriMesh;
    objTriMesh = new btTriangleMesh();

//cout << "Makes it past loading the table!" << endl;

/*
//CUBE
    globalObjCount++;
    Vertex *Cube;
    objTriMesh = new btTriangleMesh();
    btVector3 squareVect = btVector3(0.6f, 0.6f, 0.6f);
    loadedSuccess = loadOBJ("Cube.obj", &Cube);
    if ( !loadedSuccess )
    {
        cout << "OBJ file not found or invalid format" << endl;
        return false;
    }
 
    // Create a Vertex Buffer object to store this vertex info on the GPU
    glGenBuffers(1, &vbo_cube);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_cube);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Cube)*numberTriangles*3, Cube, GL_STATIC_DRAW);

    //Initalize the Cube.
    //tempShape = new btBvhTriangleMeshShape(objTriMesh, true);//Cube

    tempShape = new btBoxShape(squareVect);
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0.5f,4.0f,0.0f)));
    tempMass = 1;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(1, tempShape, tempMotionState, tempMass, tempInertia, vbo_cube, numberTriangles, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(1), COL_SHAPE, shapeColidesWith);

    delete Cube;
    numberTriangles = 0;
    delete objTriMesh;
    objTriMesh = new btTriangleMesh();
 
*/

//CYLINDER
    //Paddle 1
    globalObjCount++;
    Vertex *cylinder;
    btVector3 cylinderVect = btVector3(0.6f, 0.6f, 0.6f);
    objTriMesh = new btTriangleMesh();
    loadedSuccess = loadOBJ("Paddle.obj", &cylinder);
    if ( !loadedSuccess )
    {
        cout << "OBJ file not found or invalid format" << endl;
        return false;
    }

    // Create a Vertex Buffer object to store this vertex info on the GPU
    glGenBuffers(1, &vbo_cylinder);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_cylinder);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cylinder)*numberTriangles*3, cylinder, GL_STATIC_DRAW);

    //Initalize the Cylinder
    //tempShape = new btBvhTriangleMeshShape(objTriMesh, true); //cylinder
    tempShape = new btCylinderShape(cylinderVect);    
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(-2.0f,1.0f,2.0f)));
    tempMass = 1;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    tempShape->calculateLocalInertia(tempMass, tempInertia);
    btRigidBody::btRigidBodyConstructionInfo paddleOneRigidBodyCI(tempMass, tempMotionState, tempShape, tempInertia);
    tempRigidBody = new btRigidBody(paddleOneRigidBodyCI);
    objectsDataList.addObject(1, tempShape, tempMotionState, tempMass, tempInertia, vbo_cylinder, numberTriangles, .5f, tempRigidBody);
    dynamicsWorld->addRigidBody(tempRigidBody, COL_SHAPE, shapeColidesWith);

    //delete cylinder;
    tempRigidBody = NULL;
    numberTriangles = 0;
    delete objTriMesh;
    objTriMesh = new btTriangleMesh();

    cout << "Loaded Paddle 1" << endl;
/*
    //Paddle 2
    globalObjCount++;
    Vertex *cylinder;
    objTriMesh = new btTriangleMesh();
    loadedSuccess = loadOBJ("Paddle.obj", &cylinder);
    if ( !loadedSuccess )
    {
        cout << "OBJ file not found or invalid format" << endl;
        return false;
    }

     Create a Vertex Buffer object to store this vertex info on the GPU
    glGenBuffers(1, &vbo_cylinder);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_cylinder);
    glBufferData(GL_ARRAY_BUFFER, sizeof(cylinder)*numberTriangles*3, cylinder, GL_STATIC_DRAW);

    Initalize the Cylinder
    tempShape = new btBvhTriangleMeshShape(objTriMesh, true); //cylinder
    tempShape = new btCylinderShape(cylinderVect);    
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(2.0f,1.0f,2.0f)));
    tempMass = 1;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(2, tempShape, tempMotionState, tempMass, tempInertia, vbo_cylinder, numberTriangles, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(2), COL_SHAPE, shapeColidesWith);

    delete cylinder;
    numberTriangles = 0;
    delete objTriMesh;
    objTriMesh = new btTriangleMesh();

//SPHERE
    globalObjCount++;
    Vertex *sphere;
    objTriMesh = new btTriangleMesh();
    btScalar sphereScaler = 1;
    loadedSuccess = loadOBJ("Earth.obj", &sphere);
    if ( !loadedSuccess )
    {
        cout << "OBJ file not found or invalid format" << endl;
        return false;
    }

    // Create a Vertex Buffer object to store this vertex info on the GPU
    glGenBuffers(1, &vbo_sphere);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_sphere);
    glBufferData(GL_ARRAY_BUFFER, sizeof(sphere)*numberTriangles*3, sphere, GL_STATIC_DRAW);

    //Initalize the Sphere
    //tempShape = new btBvhTriangleMeshShape(objTriMesh, true); //Sphere
    tempShape = new btSphereShape(sphereScaler); 
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(4.0f,1.0f,1.0f)));
    tempMass = 1;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(1, tempShape, tempMotionState, tempMass, tempInertia, vbo_sphere, numberTriangles, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(1), COL_SHAPE, shapeColidesWith);
 
    delete sphere;
    numberTriangles = 0;
*/

///Walls
/*
    globalObjCount++;
    btVector3 left_wall = btVector3(4.f, 4.f, 4.f);
    tempShape = new btBoxShape( left_wall);
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(11, 0, 0.0f)));
    tempMass = 100;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(4, tempShape, tempMotionState, tempMass, tempInertia, 0, 1, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(4), COL_SHAPE, shapeColidesWith);

    globalObjCount++;
    btVector3 top_wall = btVector3(6.f, 6.f, 6.f);
    tempShape = new btBoxShape( top_wall);
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0, 0, 9.5f)));
    tempMass = 100;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(5, tempShape, tempMotionState, tempMass, tempInertia, 0, 1, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(5), COL_SHAPE, shapeColidesWith);

    globalObjCount++;
    btVector3 bottom_wall = btVector3(6.f, 6.f, 6.f);
    tempShape = new btBoxShape( bottom_wall);
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(0, 0, -9.f)));
    tempMass = 100;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(6, tempShape, tempMotionState, tempMass, tempInertia, 0, 1, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(6), COL_SHAPE, shapeColidesWith);

    globalObjCount++;
    btVector3 right_wall = btVector3(4.f, 4.f, 4.f);
    tempShape = new btBoxShape( right_wall);
    tempMotionState = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1), btVector3(-7, 0, 0.0f)));
    tempMass = 100;
    tempInertia = btVector3(0.0f, 0.0f, 0.0f);
    objectsDataList.addObject(7, tempShape, tempMotionState, tempMass, tempInertia, 0, 1, .5f);
    dynamicsWorld->addRigidBody(objectsDataList.getRigidBody(7), COL_SHAPE, shapeColidesWith);
*/

 


    //Clean Up
    //tempShape = NULL;
    //delete objTriMesh;
    //objTriMesh = NULL;
    tempShape = NULL;
    tempMotionState = NULL;

    //--Load vertex shader and fragment shader from 2 text files
    ShaderLoader loader("vertexShader.txt", "fragmentShader.txt");
    program = loader.LoadShader();
    
    //Now we set the locations of the attributes and uniforms
    //this allows us to access them easily while rendering
    loc_position = glGetAttribLocation(program,
                    const_cast<const char*>("v_position"));
    if(loc_position == -1)
    {
        std::cerr << "[F] POSITION NOT FOUND" << std::endl;
        return false;
    }

    loc_uv = glGetAttribLocation(program,
                    const_cast<const char*>("v_uv"));
    if(loc_uv == -1)
    {
        std::cerr << "[F] V_UV NOT FOUND" << std::endl;
        return false;
    }

    loc_mvpmat = glGetUniformLocation(program,
                    const_cast<const char*>("mvpMatrix"));
    if(loc_mvpmat == -1)
    {
        std::cerr << "[F] MVPMATRIX NOT FOUND" << std::endl;
        return false;
    }
    
    //--Init the view and projection matrices
    //  if you will be having a moving camera the view matrix will need to more dynamic
    //  ...Like you should update it before you render more dynamic 
    //  for this project having them static will be fine
    view = glm::lookAt( glm::vec3(.5, 7.0, 0), //Eye Position
                        glm::vec3(.5, 0.0, 0.0), //Focus point
                        glm::vec3(0.0, 0.0, 1.0)); //Positive Y is up

    projection = glm::perspective( 45.0f, //the FoV typically 90 degrees is good which is what this is set to
                                   float(w)/float(h), //Aspect Ratio, so Circles stay Circular
                                   0.01f, //Distance to the near plane, normally a small value like this
                                   100.0f); //Distance to the far plane, 


    // load texture image
    Magick::InitializeMagick("");
    Magick::Image image;
    Magick::Blob m_blob;
    try
        { 
         // Read a file into image object 
         if ( textureFileName != "")
            {
             image.read( textureFileName );
             image.flip();
             image.write(&m_blob, "RGBA");
            }
         else
            {
             throw std::invalid_argument("No texture file found");
            }

        } 
    catch(exception& tmp) 
        { 
         cout << "Error while reading in texture image, texture file not found"  << endl; 
        } 

    int imageWidth = image.columns();
    int imageHeight = image.rows();


    // setup texture
    glGenTextures(1, &aTexture); 
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, aTexture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, imageWidth, imageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, m_blob.data());

    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

    //enable depth testing
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);

    //and its done
    return true;
}