Exemplo n.º 1
0
	void updateImageRect()
	{
		GLfloat x1, x2, y1, y2;

		const CATextField::TextFieldAlign& align = m_pTextFieldX->getTextFieldAlign();
		if (align == CATextField::Right)
		{
			x1 = m_iLabelWidth - m_obRect.size.width;
		}
		else if (align == CATextField::Center)
		{
			x1 = (m_iLabelWidth - m_obRect.size.width) / 2;
		}
		else
		{
			x1 = 0;
		}

		y1 = (m_obContentSize.height - m_obRect.size.height) / 2;
		x2 = x1 + m_obRect.size.width;
		y2 = y1 + m_obRect.size.height;
		m_sQuad.bl.vertices = vertex3(x1, y1, m_fVertexZ);
		m_sQuad.br.vertices = vertex3(x2, y1, m_fVertexZ);
		m_sQuad.tl.vertices = vertex3(x1, y2, m_fVertexZ);
		m_sQuad.tr.vertices = vertex3(x2, y2, m_fVertexZ);
	}
Exemplo n.º 2
0
void SuperAnimSprite::SetTexture(CCTexture2D *theTexture, CCRect theTextureRect)
{
	if (theTexture == NULL)
	{
		return;
	}
	
	if (mTexture != NULL)
	{
		mTexture->release();
		mTexture = NULL;
	}
	
	// retain this texture in case removed by removeUnusedTextures();
	theTexture->retain();
	mTexture = theTexture;
	
	// Set Texture coordinates
	CCRect theTexturePixelRect = CC_RECT_POINTS_TO_PIXELS(theTextureRect);
	float aTextureWidth = (float)mTexture->getPixelsWide();
	float aTextureHeight = (float)mTexture->getPixelsHigh();
	
	float aLeft, aRight, aTop, aBottom;
	aLeft = theTexturePixelRect.origin.x / aTextureWidth;
	aRight = (theTexturePixelRect.origin.x + theTexturePixelRect.size.width) / aTextureWidth;
	aTop = theTexturePixelRect.origin.y / aTextureHeight;
	aBottom = (theTexturePixelRect.origin.y + theTexturePixelRect.size.height) / aTextureHeight;
	
	mQuad.bl.texCoords.u = aLeft;
	mQuad.bl.texCoords.v = aBottom;
	mQuad.br.texCoords.u = aRight;
	mQuad.br.texCoords.v = aBottom;
	mQuad.tl.texCoords.u = aLeft;
	mQuad.tl.texCoords.v = aTop;
	mQuad.tr.texCoords.u = aRight;
	mQuad.tr.texCoords.v = aTop;
	
	// Set position
	//float x1 = 0;
	//float y1 = 0;
	//float x2 = x1 + theTextureRect.size.width;
	//float y2 = y1 + theTextureRect.size.height;
	
	float x1 = theTexturePixelRect.size.width * -0.5f;
	float y1 = theTexturePixelRect.size.height * -0.5f;
	float x2 = theTexturePixelRect.size.width * 0.5f;
	float y2 = theTexturePixelRect.size.height * 0.5f;
	
	mQuad.bl.vertices = vertex3(x1, y1, 0);
	mQuad.br.vertices = vertex3(x2, y1, 0);
	mQuad.tl.vertices = vertex3(x1, y2, 0);
	mQuad.tr.vertices = vertex3(x2, y2, 0);
	
	// Set color
	ccColor4B aDefaultColor = {255, 255, 255, 255};
	mQuad.bl.colors = aDefaultColor;
	mQuad.br.colors = aDefaultColor;
	mQuad.tl.colors = aDefaultColor;
	mQuad.tr.colors = aDefaultColor;
}
Exemplo n.º 3
0
void CAGifView::updateImageRect()
{
    m_sQuad.bl.vertices = vertex3(  m_fLeft, m_fBottom, m_fVertexZ);
    m_sQuad.br.vertices = vertex3( m_fRight, m_fBottom, m_fVertexZ);
    m_sQuad.tl.vertices = vertex3(  m_fLeft, m_fTop,    m_fVertexZ);
    m_sQuad.tr.vertices = vertex3( m_fRight, m_fTop,    m_fVertexZ);
}
Exemplo n.º 4
0
void CATextField::updateImageRect()
{
    GLfloat x1,x2,y1,y2;

	if (m_eTextEditAlign == eTextEditAlignRight)
	{
		x1 = m_iHoriMargins + m_iLabelWidth - m_obRect.size.width;
	}
	else if (m_eTextEditAlign == eTextEditAlignCenter)
	{
		x1 = m_iHoriMargins + (m_iLabelWidth - m_obRect.size.width) / 2;
	}
	else
	{
		x1 = m_iHoriMargins;
	}
	
	y1 = m_iVertMargins;
    x2 = x1 + m_obRect.size.width;
    y2 = y1 + m_obRect.size.height;
    m_sQuad.bl.vertices = vertex3(x1, y1, m_fVertexZ);
    m_sQuad.br.vertices = vertex3(x2, y1, m_fVertexZ);
    m_sQuad.tl.vertices = vertex3(x1, y2, m_fVertexZ);
    m_sQuad.tr.vertices = vertex3(x2, y2, m_fVertexZ);
}
Exemplo n.º 5
0
void CAImageView::updateImageRect()
{
    // Don't update Z.
    m_sQuad.bl.vertices = vertex3(m_fLeft, m_fTop, 0);
    m_sQuad.br.vertices = vertex3(m_fRight, m_fTop, 0);
    m_sQuad.tl.vertices = vertex3(m_fLeft, m_fBottom, 0);
    m_sQuad.tr.vertices = vertex3(m_fRight, m_fBottom, 0);
}
Exemplo n.º 6
0
Joint::Joint(){
	inboard_displacement = vertex3();
	outboard_displacement = vertex3();
	angle = 0.0f;
	child = NULL;
	parent = NULL;
	angularVelocity = 0.00001f;
	path = new Trajectory();
}
Exemplo n.º 7
0
Joint::Joint(const Pose& localPose){
	inboard_displacement = vertex3();
	outboard_displacement = vertex3();
	angle = 0.0f;
	child = NULL;
	parent = NULL;
	this->local_pose = localPose;
	this->local_transformation = local_pose.getRotation() * local_pose.translate_object();
	angularVelocity = 0.00001f;
	path = new Trajectory();
}
void CC3DSprite::updateVertexRect()
{
	float x1 = -m_obContentSize.width * this->m_obAnchorPoint.x;
	float y1 = -m_obContentSize.height * this->m_obAnchorPoint.y;
	float x2 = -x1 + m_obContentSize.width;
	float y2 = -y1 + m_obContentSize.height;

	m_sQuad.bl.vertices = vertex3(x1, y1, 0);
	m_sQuad.br.vertices = vertex3(x2, y1, 0);
	m_sQuad.tl.vertices = vertex3(x1, y2, 0);
	m_sQuad.tr.vertices = vertex3(x2, y2, 0);
}
Exemplo n.º 9
0
	void Skin::draw()
	{
		// If it is not visible, or one of its ancestors is not visible, then do nothing:
		if( !m_bVisible)
		{
			m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0,0,0);
		}
		else 
		{
			//
			// calculate the Quad based on the Affine Matrix
			//

			CCSize size = m_obRect.size;

			float x1 = m_obOffsetPosition.x;
			float y1 = m_obOffsetPosition.y;

			float x2 = x1 + size.width;
			float y2 = y1 + size.height;

			float x = m_sTransform.tx;
			float y = m_sTransform.ty;

			float cr = m_sTransform.a;
			float sr = m_sTransform.b;
			float cr2 = m_sTransform.d;
			float sr2 = -m_sTransform.c;
			float ax = x1 * cr - y1 * sr2 + x;
			float ay = x1 * sr + y1 * cr2 + y;

			float bx = x2 * cr - y1 * sr2 + x;
			float by = x2 * sr + y1 * cr2 + y;

			float cx = x2 * cr - y2 * sr2 + x;
			float cy = x2 * sr + y2 * cr2 + y;

			float dx = x1 * cr - y2 * sr2 + x;
			float dy = x1 * sr + y2 * cr2 + y;

			m_sQuad.bl.vertices = vertex3( RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), m_fVertexZ );
			m_sQuad.br.vertices = vertex3( RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), m_fVertexZ );
			m_sQuad.tl.vertices = vertex3( RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), m_fVertexZ );
			m_sQuad.tr.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ );
		}

		// MARMALADE CHANGE: ADDED CHECK FOR NULL, TO PERMIT SPRITES WITH NO BATCH NODE / TEXTURE ATLAS
		if (m_pobTextureAtlas)
		{
			m_pobTextureAtlas->updateQuad(&m_sQuad, m_pobTextureAtlas->getTotalQuads());
		}
	}
Exemplo n.º 10
0
void CATextField::updateImageRect()
{
    GLfloat x1,x2,y1,y2;

	x1 = m_iHoriMargins;
	y1 = m_iVertMargins;
    x2=x1+m_obRect.size.width;
    y2=y1+m_obRect.size.height;
    m_sQuad.bl.vertices = vertex3(x1, y1, m_fVertexZ);
    m_sQuad.br.vertices = vertex3(x2, y1, m_fVertexZ);
    m_sQuad.tl.vertices = vertex3(x1, y2, m_fVertexZ);
    m_sQuad.tr.vertices = vertex3(x2, y2, m_fVertexZ);
}
Exemplo n.º 11
0
void CALabel::updateImageRect()
{
    GLfloat x1,x2,y1,y2;
    x1=0;
    y1=0;
    y1=m_obContentSize.height - m_obRect.size.height - y1;
    y1=y1-pTextHeight;
    x2=x1+m_obRect.size.width;
    y2=y1+m_obRect.size.height;
    m_sQuad.bl.vertices = vertex3(x1, y1, 0);
    m_sQuad.br.vertices = vertex3(x2, y1, 0);
    m_sQuad.tl.vertices = vertex3(x1, y2, 0);
    m_sQuad.tr.vertices = vertex3(x2, y2, 0);
}
Exemplo n.º 12
0
void CAView::updateImageRect()
{
    // Don't update Z.
    
    GLfloat x1,x2,y1,y2;
    x1 = 0;
    y1 = 0;
    x2 = m_obContentSize.width;
    y2 = m_obContentSize.height;
    
    m_sQuad.bl.vertices = vertex3(x1, y1, m_fVertexZ);
    m_sQuad.br.vertices = vertex3(x2, y1, m_fVertexZ);
    m_sQuad.tl.vertices = vertex3(x1, y2, m_fVertexZ);
    m_sQuad.tr.vertices = vertex3(x2, y2, m_fVertexZ);
}
Exemplo n.º 13
0
// Operator between matrix & vertex
inline ccVertex3F operator*(const SuperAnimMatrix3 &theMatrix3, const ccVertex3F &theVec)
{
	return vertex3(
				   theMatrix3.m00*theVec.x + theMatrix3.m01*theVec.y + theMatrix3.m02,
				   theMatrix3.m10*theVec.x + theMatrix3.m11*theVec.y + theMatrix3.m12,
				   theVec.z);
}
Exemplo n.º 14
0
void Joint::rotate(){
	//not sure if this is how it works...
	//local_pose.orientation = local_pose.orientation + rotate;
	//PoseEuler temp = PoseEuler(vertex3(), vertex3(0.0, 0.0, angle));
//	local_transformation = local_transformation * ( temp.getRotation()* angularVelocity);
	
	Pose currentPose = path->update();

	float x, y;
	x = currentPose.position.getx();
	y = currentPose.position.gety();
	angle = -PI/2.0 + (float) (std::atan2(y, x) - std::atan2(1.0, 0.0));


	//REALLY MEANS: angle = angularVelocity * dt + angle0;
	//angle += angularVelocity;
	//std::cout << "x:" << x << ", y:" << y << ", theta:" << angle << std::endl;
	Pose temp = Pose(vertex3(), quaternion(angle, 0.0, 0.0));
	//Pose temp = Pose(vertex3(), quaternion(0.0, angle, 0.0));
	local_transformation = temp.getRotation();

	//local_transformation = ( temp.getRotation() * angularVelocity);

	//if(!isNull()){
	//	for(unsigned int i=0; i<children.size(); i++){
	//		this->children.at(i)->child->rotate(rotate);
	//	}
	//}
}
Exemplo n.º 15
0
void CCSprite::setTextureRectInPixels(CGRect rect, bool rotated, CGSize size)
{
	m_obRectInPixels = rect;
	m_obRect = CC_RECT_PIXELS_TO_POINTS(rect);
	m_bRectRotated = rotated;

	setContentSizeInPixels(size);
	updateTextureCoords(m_obRectInPixels);

	CGPoint relativeOffsetInPixels = m_obUnflippedOffsetPositionFromCenter;

	// issue #732
	if (m_bFlipX)
	{
		relativeOffsetInPixels.x = -relativeOffsetInPixels.x;
	}
	if (m_bFlipY)
	{
		relativeOffsetInPixels.y = -relativeOffsetInPixels.y;
	}

	m_obOffsetPositionInPixels.x = relativeOffsetInPixels.x + (m_tContentSizeInPixels.width - m_obRectInPixels.size.width) / 2;
	m_obOffsetPositionInPixels.y = relativeOffsetInPixels.y + (m_tContentSizeInPixels.height - m_obRectInPixels.size.height) / 2;

	// rendering using SpriteSheet
	if (m_bUsesBatchNode)
	{
		// update dirty_, don't update recursiveDirty_
		m_bDirty = true;
	}
	else
	{
		// self rendering
		
		// Atlas: Vertex
		float x1 = 0 + m_obOffsetPositionInPixels.x;
		float y1 = 0 + m_obOffsetPositionInPixels.y;
		float x2 = x1 + m_obRectInPixels.size.width;
		float y2 = y1 + m_obRectInPixels.size.height;

		// Don't update Z.
		m_sQuad.bl.vertices = vertex3(x1, y1, 0);
		m_sQuad.br.vertices = vertex3(x2, y1, 0);
		m_sQuad.tl.vertices = vertex3(x1, y2, 0);
		m_sQuad.tr.vertices = vertex3(x2, y2, 0);
	}
}
Exemplo n.º 16
0
void CCSprite::useSelfRender(void)
{
    m_uAtlasIndex = CCSpriteIndexNotInitialized;
	m_bUsesBatchNode = false;
	m_pobTextureAtlas = NULL;
    m_pobBatchNode = NULL;
	m_bDirty = m_bRecursiveDirty = false;

	float x1 = 0 + m_obOffsetPositionInPixels.x;
	float y1 = 0 + m_obOffsetPositionInPixels.y;
	float x2 = x1 + m_obRectInPixels.size.width;
	float y2 = y1 + m_obRectInPixels.size.height;
	m_sQuad.bl.vertices = vertex3(x1, y1, 0);
	m_sQuad.br.vertices = vertex3(x2, y1, 0);
	m_sQuad.tl.vertices = vertex3(x1, y2, 0);
	m_sQuad.tr.vertices = vertex3(x2, y2, 0);
}
Exemplo n.º 17
0
void Joint::translate(const vertex3& movement){
	//how much to move, or where to move to?
	//simply add how much it moves to it's pose

	Pose temp = Pose(movement, vertex3());
	local_transformation = local_transformation * temp.translate_object();
	//local_pose.position = local_pose.position + movement;
}
Exemplo n.º 18
0
void TextureSprite::addPolygon(const CCPointVector &points)
{
    /// copy vertices positions and texture coordinates
    for (const CCPoint &p : points) {
        /// The vertices position in pixel
        mVertexPos.push_back(vertex3(p.x, p.y, .0f));   /// x y z
        /// The texture is loaded upside down by OpenGL. (1 - Y coord) will swap the texture Y coordinate.
        mTexCoords.push_back(tex2(p.x / mTexSize.width, 1 - (p.y / mTexSize.height)));
    }
}
Exemplo n.º 19
0
void 
CPrimitiveScene::draw()
{    
    CCCamera* pCamera = CCDirector::sharedDirector()->getRunningScene()->getCamera();
    pCamera->setEyeXYZ(0,0,10);
    pCamera->setCenterXYZ(-50.0f, -50.0f, 0);

    // open yellow poly
    ccDrawColor4B(255, 255, 0, 255);
    glLineWidth(10);
    ccVertex3F vertices[] = { vertex3(0,0,0), vertex3(50,50,0), vertex3(100,50,0), vertex3(100,100,0), vertex3(50,100,0.1) };
        
    s_pShader->use();
    s_pShader->setUniformForModelViewProjectionMatrix();
    s_pShader->setUniformLocationWith4fv(s_nColorLocation, (GLfloat*) &s_tColor.r, 1);

    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position );


    int numberOfPoints = 5;
    // XXX: Mac OpenGL error. arrays can't go out of scope before draw is executed
    ccVertex3F* newPoli = new ccVertex3F[numberOfPoints];

    // iPhone and 32-bit machines optimization
    if( sizeof(CCPoint) == sizeof(ccVertex3F) )
    {
        glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, vertices);
    }
    else
    {
        // Mac on 64-bit
        for( unsigned int i=0; i<numberOfPoints;i++)
        {
            newPoli[i] = vertex3( vertices[i].x, vertices[i].y, vertices[i].z );
        }
        glVertexAttribPointer(kCCVertexAttrib_Position, 3, GL_FLOAT, GL_FALSE, 0, newPoli);
    }    

    glDrawArrays(GL_TRIANGLE_FAN, 0, (GLsizei) numberOfPoints);

    CC_SAFE_DELETE_ARRAY(newPoli);
    CC_INCREMENT_GL_DRAWS(1);
}
Exemplo n.º 20
0
void CCSquirmAnimNode::rebuildVertices()
{
	if(m_vertices_dirty)
	{
		memset(&m_sQuad, 0, sizeof(m_sQuad));
		int w = m_obContentSize.width;
		int h = m_obContentSize.height;
		int texture_w = m_pobTexture->getPixelsWide();
		int texture_h = m_pobTexture->getPixelsHigh();
		float max_u0 = (float)w / (float)texture_w / m_texture_horz_scale;
		float max_u1 = max_u0 / m_mask_horz_scale;

		ccColor4B clr = ccc4(m_color.r, m_color.g, m_color.b, m_opacity);

		m_sQuad.bl.vertices = vertex3(0.0f, 0.0f, 0.0f);
		m_sQuad.bl.colors = clr;
		m_sQuad.bl.texCoords = tex2(0.0f, 1.0f);
		m_sQuad.bl.texCoords1 = tex2(m_mask_left_edge, 1.0f);

		m_sQuad.br.vertices = vertex3(m_obContentSize.width, 0.0f, 0.0f);
		m_sQuad.br.colors = clr;
		m_sQuad.br.texCoords = tex2(max_u0, 1.0f);
		m_sQuad.br.texCoords1 = tex2(m_mask_left_edge + max_u1, 1.0f);

		m_sQuad.tl.vertices = vertex3(0.0f, m_obContentSize.height, 0.0f);
		m_sQuad.tl.colors = clr;
		m_sQuad.tl.texCoords = tex2(0.0f, 0.0f);
		m_sQuad.tl.texCoords1 = tex2(m_mask_left_edge, 0.0f);

		m_sQuad.tr.vertices = vertex3(m_obContentSize.width, m_obContentSize.height, 0.0f);
		m_sQuad.tr.colors = clr;
		m_sQuad.tr.texCoords = tex2(max_u0, 0.0f);
		m_sQuad.tr.texCoords1 = tex2(m_mask_left_edge + max_u1, 0.0f);

		m_vertices_dirty = false;
	}
}
Exemplo n.º 21
0
/**
 *  init opens and parses the raw file "filename".
 * .raw files consist of lines as such:
 * f1 f2 f3 f4 f5 f6 f7 f8 f9
 *  These are all floats, where each triplet specifies one vertice of the triangle (to be read as:
 * v1: (f1, f2, f3); v2: (f4, f5, f6); v3: (f7, f8, f9)
 *  init populates class variable vertices with this information
 */
int
rawReader::init(const char* filename){
	std::string str = "" ; 
	
	plint = true;

	// open .raw file
	std::cout << "Reading .raw file " << filename << std::endl;
    std::ifstream in ; 
    in.open(filename) ; 
    if (!in.is_open()) {
		std::cerr << "Unable to open file " << filename << "\n" ; 
		return 1 ;
	}

	// parse .raw file
	while(in){
		getline(in, str);

		if(str.empty()){
			// do nothing
		} // read and store vertices
		else{
			float x1,y1,z1;
			float x2,y2,z2;
			float x3,y3,z3;
			sscanf(str.c_str(), "%f %f %f %f %f %f %f %f %f", &x1, &y1, &z1, &x2, &y2, &z2, &x3, &y3, &z3);
			vec3 vertex1(x1,y1,z1);
			vec3 vertex2(x2,y2,z2);
			vec3 vertex3(x3,y3,z3);
			vertices.push_back(vertex1);
			vertices.push_back(vertex2);
			vertices.push_back(vertex3);
		} 
	}
	
	calculateNormals();

	std::cout << ".raw file read successful" << std::endl;
	return 0;
}
Exemplo n.º 22
0
std::shared_ptr<GenericDataArray<float> > MeshManager::generateNormals(
	std::shared_ptr<GenericDataArray<float> > vertices,
	std::shared_ptr<GenericDataArray<unsigned int> > indices)
{
	std::vector<glm::vec3> normalsVec(vertices->length());
	std::shared_ptr<GenericDataArray<float> > normals = std::make_shared<GenericDataArray<float> >(vertices->length());

	for (unsigned int i = 0; i < indices->length(); ++i) {
		unsigned int vertexIndex = indices->at(i, 0);
		glm::vec3 vertex1(vertices->at(vertexIndex, 0), vertices->at(vertexIndex, 1), vertices->at(vertexIndex, 2));

		vertexIndex = indices->at(i, 1);
		glm::vec3 vertex2(vertices->at(vertexIndex, 0), vertices->at(vertexIndex, 1), vertices->at(vertexIndex, 2));

		vertexIndex = indices->at(i, 2);
		glm::vec3 vertex3(vertices->at(vertexIndex, 0), vertices->at(vertexIndex, 1), vertices->at(vertexIndex, 2));

		glm::vec3 edge1 = vertex2 - vertex1;
		glm::vec3 edge2 = vertex3 - vertex1;

		glm::vec3 normal = glm::normalize(glm::cross(edge1, edge2));

		for (unsigned int j = 0; j < 3; ++j) {
			if (normalsVec.at(indices->at(i, j)) != glm::vec3()) {
				normalsVec.at(indices->at(i, j)) = glm::normalize(normalsVec.at(indices->at(i, j)) + normal);
			}
			else {
				normalsVec.at(indices->at(i, j)) = normal;
			}
		}
	}

	for (unsigned int i = 0; i < normalsVec.size(); ++i) {
		normals->at(i) = GenericDataArray<float>::value_type(&normalsVec[i][0]);
	}

	return normals;
}
Exemplo n.º 23
0
void Skeleton2D_BoneDisplay::updateTransform(void)
{
	CCAssert(m_pobBatchNode, "updateTransform is only valid when CCSprite is being rendered using an CCSpriteBatchNode");

	// recalculate matrix only if it is dirty
	if( isDirty() ) {

		// If it is not visible, or one of its ancestors is not visible, then do nothing:
		//if( !IsVisable() || ( m_pParent && m_pParent != m_pobBatchNode /*&& ((CCSprite*)m_pParent)->m_bShouldBeHidden*/) )
		if (false)
		{
			m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0,0,0);
			m_bShouldBeHidden = true;
		}
		else 
		{//
			CCAffineTransform tm;
			m_bShouldBeHidden = false;

			tm = nodeToParentTransform();
			//
			// calculate the Quad based on the Affine Matrix
			//

			CCSize size = m_obRect.size;

			float x1 = m_obOffsetPosition.x;
			float y1 = m_obOffsetPosition.y;

			float x2 = x1 + size.width;
			float y2 = y1 + size.height;
			float x = tm.tx;
			float y = tm.ty;

			float cr = tm.a;
			float sr = tm.b;
			float cr2 = tm.d;
			float sr2 = -tm.c;
			float ax = x1 * cr - y1 * sr2 + x;
			float ay = x1 * sr + y1 * cr2 + y;

			float bx = x2 * cr - y1 * sr2 + x;
			float by = x2 * sr + y1 * cr2 + y;

			float cx = x2 * cr - y2 * sr2 + x;
			float cy = x2 * sr + y2 * cr2 + y;

			float dx = x1 * cr - y2 * sr2 + x;
			float dy = x1 * sr + y2 * cr2 + y;

			ax*=-1.0f;ay*=-1.0f;bx*=-1.0f;by*=-1.0f;cx*=-1.0f;cy*=-1.0f;dx*=-1.0f;dy*=-1.0f;

			m_sQuad.bl.vertices = vertex3( RENDER_IN_SUBPIXEL(ax), RENDER_IN_SUBPIXEL(ay), m_fVertexZ );
			m_sQuad.br.vertices = vertex3( RENDER_IN_SUBPIXEL(bx), RENDER_IN_SUBPIXEL(by), m_fVertexZ );
			m_sQuad.tl.vertices = vertex3( RENDER_IN_SUBPIXEL(dx), RENDER_IN_SUBPIXEL(dy), m_fVertexZ );
			m_sQuad.tr.vertices = vertex3( RENDER_IN_SUBPIXEL(cx), RENDER_IN_SUBPIXEL(cy), m_fVertexZ );
		}

		m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex);
		m_bRecursiveDirty = false;
		setDirty(false);
	}

	// recursively iterate over children
	if( m_bHasChildren ) 
	{
		arrayMakeObjectsPerformSelector(m_pChildren, updateTransform, CCSprite*);
	}
#if CC_SPRITE_DEBUG_DRAW
	// draw bounding box
	CCPoint vertices[4] = {
		ccp( m_sQuad.bl.vertices.x, m_sQuad.bl.vertices.y ),
		ccp( m_sQuad.br.vertices.x, m_sQuad.br.vertices.y ),
		ccp( m_sQuad.tr.vertices.x, m_sQuad.tr.vertices.y ),
		ccp( m_sQuad.tl.vertices.x, m_sQuad.tl.vertices.y ),
	};
	ccDrawPoly(vertices, 4, true);
#endif // CC_SPRITE_DEBUG_DRAW
}
Exemplo n.º 24
0
void CCSprite::updateTransform(void)
{
	assert(m_bUsesBatchNode);

	// optimization. Quick return if not dirty
	if (! m_bDirty)
	{
		return;
	}

	CGAffineTransform matrix;

	// Optimization: if it is not visible, then do nothing
	if (! m_bIsVisible)
	{
		m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0,0,0);
		m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex);
		m_bDirty = m_bRecursiveDirty = false;
		return;
	}

	// Optimization: If parent is batchnode, or parent is nil
	// build Affine transform manually
	if (! m_pParent || m_pParent == m_pobBatchNode)
	{
		float radians = -CC_DEGREES_TO_RADIANS(m_fRotation);
		float c = cosf(radians);
		float s = sinf(radians);

        matrix = CGAffineTransformMake(c * m_fScaleX, s * m_fScaleX,
			-s * m_fScaleY, c * m_fScaleY,
			m_tPositionInPixels.x, m_tPositionInPixels.y);
		matrix = CGAffineTransformTranslate(matrix, -m_tAnchorPointInPixels.x, -m_tAnchorPointInPixels.y);
	} else // parent_ != batchNode_ 
	{
		// else do affine transformation according to the HonorParentTransform
		matrix = CGAffineTransformIdentity;
		ccHonorParentTransform prevHonor = CC_HONOR_PARENT_TRANSFORM_ALL;

		for (CCNode *p = this; p && p != m_pobBatchNode; p = p->getParent())
		{
			// Might happen. Issue #1053
			// how to implement, we can not use dynamic
			// NSAssert( [p isKindOfClass:[CCSprite class]], @"CCSprite should be a CCSprite subclass. Probably you initialized an sprite with a batchnode, but you didn't add it to the batch node." );
			struct transformValues_ tv;
			((CCSprite*)p)->getTransformValues(&tv);

			// If any of the parents are not visible, then don't draw this node
			if (! tv.visible)
			{
				m_sQuad.br.vertices = m_sQuad.tl.vertices = m_sQuad.tr.vertices = m_sQuad.bl.vertices = vertex3(0,0,0);
				m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex);
				m_bDirty = m_bRecursiveDirty = false;

				return;
			}

			CGAffineTransform newMatrix = CGAffineTransformIdentity;

			// 2nd: Translate, Rotate, Scale
			if( prevHonor & CC_HONOR_PARENT_TRANSFORM_TRANSLATE )
			{
				newMatrix = CGAffineTransformTranslate(newMatrix, tv.pos.x, tv.pos.y);
			}

			if( prevHonor & CC_HONOR_PARENT_TRANSFORM_ROTATE )
			{
				newMatrix = CGAffineTransformRotate(newMatrix, -CC_DEGREES_TO_RADIANS(tv.rotation));
			}

			if( prevHonor & CC_HONOR_PARENT_TRANSFORM_SCALE ) 
			{
				newMatrix = CGAffineTransformScale(newMatrix, tv.scale.x, tv.scale.y);
			}

			// 3rd: Translate anchor point
			newMatrix = CGAffineTransformTranslate(newMatrix, -tv.ap.x, -tv.ap.y);

			// 4th: Matrix multiplication
			matrix = CGAffineTransformConcat( matrix, newMatrix);

			prevHonor = ((CCSprite*)p)->getHornorParentTransform();
		}
	}

	//
	// calculate the Quad based on the Affine Matrix
	//
	CGSize size = m_obRectInPixels.size;

	float x1 = m_obOffsetPositionInPixels.x;
	float y1 = m_obOffsetPositionInPixels.y;

	float x2 = x1 + size.width;
	float y2 = y1 + size.height;
    float x = matrix.tx;
	float y = matrix.ty;
	
	float cr = matrix.a;
	float sr = matrix.b;
	float cr2 = matrix.d;
	float sr2 = -matrix.c;
	float ax = x1 * cr - y1 * sr2 + x;
	float ay = x1 * sr + y1 * cr2 + y;
	
	float bx = x2 * cr - y1 * sr2 + x;
	float by = x2 * sr + y1 * cr2 + y;
	
	float cx = x2 * cr - y2 * sr2 + x;
	float cy = x2 * sr + y2 * cr2 + y;
	
	float dx = x1 * cr - y2 * sr2 + x;
	float dy = x1 * sr + y2 * cr2 + y;

	m_sQuad.bl.vertices = vertex3((float)RENDER_IN_SUBPIXEL(ax), (float)RENDER_IN_SUBPIXEL(ay), m_fVertexZ);
	m_sQuad.br.vertices = vertex3((float)RENDER_IN_SUBPIXEL(bx), (float)RENDER_IN_SUBPIXEL(by), m_fVertexZ);
	m_sQuad.tl.vertices = vertex3((float)RENDER_IN_SUBPIXEL(dx), (float)RENDER_IN_SUBPIXEL(dy), m_fVertexZ);
	m_sQuad.tr.vertices = vertex3((float)RENDER_IN_SUBPIXEL(cx), (float)RENDER_IN_SUBPIXEL(cy), m_fVertexZ);

	m_pobTextureAtlas->updateQuad(&m_sQuad, m_uAtlasIndex);
	m_bDirty = m_bRecursiveDirty = false;
}
Exemplo n.º 25
0
void PointSet::AddControlVolume ( std::vector<glm::vec3> *allvertices )
{
    Vector3DF pos;
	Point* p;	
	int dimx=5;
	int dimy=5;
	float dimz=10;
	int ccnt=0;
	//vec3 a = myvertices. ; 
	glm::vec3 startpoint(0,0.0,-5.0);
	glm::vec3 direction(0,0,1.0);
	glm::vec3 vertex1(0,1.0,2.0);
	glm::vec3 vertex2(-1.0,-1.0,0);
	glm::vec3 vertex3(1.0,-1.0,0);
	glm::vec3 v1,v2,v3;
	int a = allvertices->size();
	double value=0;
	value=PointSet::Test_RayPolyIntersect(startpoint,direction,vertex1,vertex2,vertex3);
	int count=0;
	float var=1.0;
	//glm::vec3 *a = myvertices;
	for (float x = -5; x < 18; x += 1.0)
	{
		for (float y = -5; y <= 14; y += 1.0)
		{	
			 for (float z = -5; z <= 25; z += var )
			 {
				 /*if(z>=-5.0 && z <2.0)
					 var =1.0;
				  else if(z>=2.0 && z < 8.0)
					 var =0.70;
				   else if(z>=8.0 && z < 15.0)
					 var =0.6;
				    else if(z >= 15.0 && z <=25.0)
					 var =0.3;*/

				 count = 0 ;
				 startpoint=glm::vec3((float)x,(float)y,(float)z);

				 direction=glm::vec3(1.0,0.1,0);
				 for(int v=0 ;v < a ; v+=3)
				 {
					 //checking each vertex in the grid with all the 192 triangles of mesh
					 v1=(*allvertices)[v];
					 v2=(*allvertices)[v+1];
					 v3=(*allvertices)[v+2];
					 vertex1= vec3(v1[0],v1[1],v1[2]);
					 vertex2= vec3(v2[0],v2[1],v2[2]);
					 vertex3= vec3(v3[0],v3[1],v3[2]);
					 value=PointSet::Test_RayPolyIntersect(startpoint,direction,vertex1,vertex2,vertex3);
						if(value != -1.0)
						{
							 count++;
						}

				 }

				 //odd
				 if( count % 2 == 1)
				 {
							 //inside the mesh
						p = GetPoint ( AddPointReuse () );
						pos.Set ( x,y,z+10.0f);
						p->pos = pos;	
						p->type = 1;
						p->clr = COLORA( 1.0,0.3,0.3, 0);  ///   0.1,0.3,1.0,1.0 
						
						ccnt++;
				 }
				 //var=var-0.1;
			}
			// var = 1.0;
			 
		}

	}	

}
Exemplo n.º 26
0
void TriangleWindow::generateTerrain()
{
    float scale = .2f;

    GLfloat y1;
    GLfloat y2;
    GLfloat y3;
    GLfloat y4;

    if (QFile::exists(":/heightmap-2.png")) {
        if(!m_image.load(":/heightmap-2.png"))
        {
            std::cout << "image non chargé ";
            exit(0);
        }
    }
    else
    {
        std::cout << "image not found ";
    }

    for(int x = 0; x < m_image.width() - 1; x++)
    {
        for(int z = 0; z < m_image.height() - 1; z++)
        {
            unsigned char* line = m_image.scanLine(z);
            unsigned char* line2 = m_image.scanLine(z+1);
            y1 = (((GLfloat)line[x*4])/255)*20;
            y2 = (((GLfloat)line[(x*4)+4])/255)*20;
            y3 = (((GLfloat)line2[(x*4)])/255)*20;
            y4 = (((GLfloat)line2[(x*4)+4])/255)*20;

            _texture.push_back(x/(m_image.width()*1.0)); _texture.push_back(z/(m_image.height()*1.0));
            _texture.push_back((x+1)/(m_image.width()*1.0)); _texture.push_back(z/(m_image.height()*1.0));
            _texture.push_back(x/(m_image.width()*1.0)); _texture.push_back((z+1)/(m_image.height()*1.0));

            _texture.push_back((x+1)/(m_image.width()*1.0)); _texture.push_back(z/(m_image.height()*1.0));
            _texture.push_back(x/(m_image.width()*1.0)); _texture.push_back((z+1)/(m_image.height()*1.0));
            _texture.push_back((x+1)/(m_image.width()*1.0)); _texture.push_back((z+1)/(m_image.height()*1.0));

            QVector3D vertex1(x*scale, y1, z*scale);
            _map.push_back(vertex1);
            _color.push_back(displayColor(y1));

            QVector3D vertex2((x+1)*scale, y2, z*scale);
            _map.push_back(vertex2);
            _color.push_back(displayColor(y2));

            QVector3D vertex3(x*scale, y3, (z+1)*scale);
            _map.push_back(vertex3);
            _color.push_back(displayColor(y3));

            QVector3D normal = QVector3D::normal(vertex1, vertex2, vertex3);
            _normal.push_back(normal);

            normal = QVector3D::normal(vertex2, vertex3, vertex1);
            _normal.push_back(normal);

            normal = QVector3D::normal(vertex3, vertex1, vertex2);
            _normal.push_back(normal);

            _map.push_back(vertex2);
            _color.push_back(displayColor(y2));

            QVector3D vertex4((x+1)*scale, y4, (z+1)*scale);
            _map.push_back(vertex4);
            _color.push_back(displayColor(y4));

            _map.push_back(vertex3);
            _color.push_back(displayColor(y3));

            normal = QVector3D::normal(vertex2, vertex4, vertex3);
            _normal.push_back(normal);

            normal = QVector3D::normal(vertex4, vertex3, vertex2);
            _normal.push_back(normal);

            normal = QVector3D::normal(vertex3, vertex2, vertex4);
            _normal.push_back(normal);
        }
    }
}
Exemplo n.º 27
0
void PrimitiveMeshHelper::SolidCube(float width, float height, float depth) {
  SR_ASSERT(width > 0 && height > 0 && depth > 0);
  width = width/2;
  height = height/2;
  depth = depth/2;

  DrawCmdData<Vertex> cmd;
  cmd.draw_mode = kDrawTriangles;
  VertexList &vert_list = cmd.vertex_list;
  std::vector<unsigned short> &index_list = cmd.index_list;

  //normal
  {
    // Front Face
    int baseIndex = vert_list.size();
    vec3 normal(0, 0, +1);

    vec2 texCoord1(0, 0);	vec3 vertex1(-width, -height, depth);
    vec2 texCoord2(1, 0);	vec3 vertex2( width, -height, depth);
    vec2 texCoord3(1, 1);	vec3 vertex3( width,  height, depth);
    vec2 texCoord4(0, 1);	vec3 vertex4(-width,  height, depth);

    //add vertex
    Vertex v1;  v1.pos = vertex1; v1.texcoord = texCoord1;  v1.normal = normal;
    Vertex v2;  v2.pos = vertex2; v2.texcoord = texCoord2;  v2.normal = normal;
    Vertex v3;  v3.pos = vertex3; v3.texcoord = texCoord3;  v3.normal = normal;
    Vertex v4;  v4.pos = vertex4; v4.texcoord = texCoord4;  v4.normal = normal;

    vert_list.push_back(v1);
    vert_list.push_back(v2);
    vert_list.push_back(v3);
    vert_list.push_back(v4);

    //add index
    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 1);
    index_list.push_back(baseIndex + 2);

    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 2);
    index_list.push_back(baseIndex + 3);
  }
  {
    // Back Face
    int baseIndex = vert_list.size();
    vec3 normal(0, 0, -1);

    vec2 texCoord1(1, 0);	vec3 vertex1(-width, -height, -depth);
    vec2 texCoord2(1, 1);	vec3 vertex2(-width,  height, -depth);
    vec2 texCoord3(0, 1);	vec3 vertex3( width,  height, -depth);
    vec2 texCoord4(0, 0);	vec3 vertex4( width, -height, -depth);

    //add vertex
    Vertex v1;  v1.pos = vertex1; v1.texcoord = texCoord1;  v1.normal = normal;
    Vertex v2;  v2.pos = vertex2; v2.texcoord = texCoord2;  v2.normal = normal;
    Vertex v3;  v3.pos = vertex3; v3.texcoord = texCoord3;  v3.normal = normal;
    Vertex v4;  v4.pos = vertex4; v4.texcoord = texCoord4;  v4.normal = normal;

    vert_list.push_back(v1);
    vert_list.push_back(v2);
    vert_list.push_back(v3);
    vert_list.push_back(v4);

    //add index
    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 1);
    index_list.push_back(baseIndex + 2);

    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 2);
    index_list.push_back(baseIndex + 3);
  }

  {
    // Top Face
    int baseIndex = vert_list.size();
    vec3 normal(0, 1, 0);

    vec2 texCoord1(0, 1);	vec3 vertex1(-width, height, -depth);
    vec2 texCoord2(0, 0);	vec3 vertex2(-width, height,  depth);
    vec2 texCoord3(1, 0);	vec3 vertex3( width, height,  depth);
    vec2 texCoord4(1, 1);	vec3 vertex4( width, height, -depth);

    //add vertex
    Vertex v1;  v1.pos = vertex1; v1.texcoord = texCoord1;  v1.normal = normal;
    Vertex v2;  v2.pos = vertex2; v2.texcoord = texCoord2;  v2.normal = normal;
    Vertex v3;  v3.pos = vertex3; v3.texcoord = texCoord3;  v3.normal = normal;
    Vertex v4;  v4.pos = vertex4; v4.texcoord = texCoord4;  v4.normal = normal;

    vert_list.push_back(v1);
    vert_list.push_back(v2);
    vert_list.push_back(v3);
    vert_list.push_back(v4);

    //add index
    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 1);
    index_list.push_back(baseIndex + 2);

    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 2);
    index_list.push_back(baseIndex + 3);
  }

  {
    // Bottom Face
    int baseIndex = vert_list.size();
    vec3 normal(0, -1, 0);

    vec2 texCoord1(1, 1);	vec3 vertex1(-width, -height, -depth);
    vec2 texCoord2(0, 1);	vec3 vertex2( width, -height, -depth);
    vec2 texCoord3(0, 0);	vec3 vertex3( width, -height,  depth);
    vec2 texCoord4(1, 0);	vec3 vertex4(-width, -height,  depth);

    //add vertex
    Vertex v1;  v1.pos = vertex1; v1.texcoord = texCoord1;  v1.normal = normal;
    Vertex v2;  v2.pos = vertex2; v2.texcoord = texCoord2;  v2.normal = normal;
    Vertex v3;  v3.pos = vertex3; v3.texcoord = texCoord3;  v3.normal = normal;
    Vertex v4;  v4.pos = vertex4; v4.texcoord = texCoord4;  v4.normal = normal;

    vert_list.push_back(v1);
    vert_list.push_back(v2);
    vert_list.push_back(v3);
    vert_list.push_back(v4);

    //add index
    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 1);
    index_list.push_back(baseIndex + 2);

    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 2);
    index_list.push_back(baseIndex + 3);
  }

  {
    // Right face
    int baseIndex = vert_list.size();
    vec3 normal(1, 0, 0);

    vec2 texCoord1(1, 0);	vec3 vertex1(width, -height, -depth);
    vec2 texCoord2(1, 1);	vec3 vertex2(width,  height, -depth);
    vec2 texCoord3(0, 1);	vec3 vertex3(width,  height,  depth);
    vec2 texCoord4(0, 0);	vec3 vertex4(width, -height,  depth);

    //add vertex
    Vertex v1;  v1.pos = vertex1; v1.texcoord = texCoord1;  v1.normal = normal;
    Vertex v2;  v2.pos = vertex2; v2.texcoord = texCoord2;  v2.normal = normal;
    Vertex v3;  v3.pos = vertex3; v3.texcoord = texCoord3;  v3.normal = normal;
    Vertex v4;  v4.pos = vertex4; v4.texcoord = texCoord4;  v4.normal = normal;

    vert_list.push_back(v1);
    vert_list.push_back(v2);
    vert_list.push_back(v3);
    vert_list.push_back(v4);

    //add index
    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 1);
    index_list.push_back(baseIndex + 2);

    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 2);
    index_list.push_back(baseIndex + 3);
  }

  {
    // Left Face
    int baseIndex = vert_list.size();
    vec3 normal(0, -1, 0);

    vec2 texCoord1(0, 0);	vec3 vertex1(-width, -height, -depth);
    vec2 texCoord2(1, 0);	vec3 vertex2(-width, -height,  depth);
    vec2 texCoord3(1, 1);	vec3 vertex3(-width,  height,  depth);
    vec2 texCoord4(0, 1);	vec3 vertex4(-width,  height, -depth);

    //add vertex
    Vertex v1;  v1.pos = vertex1; v1.texcoord = texCoord1;  v1.normal = normal;
    Vertex v2;  v2.pos = vertex2; v2.texcoord = texCoord2;  v2.normal = normal;
    Vertex v3;  v3.pos = vertex3; v3.texcoord = texCoord3;  v3.normal = normal;
    Vertex v4;  v4.pos = vertex4; v4.texcoord = texCoord4;  v4.normal = normal;

    vert_list.push_back(v1);
    vert_list.push_back(v2);
    vert_list.push_back(v3);
    vert_list.push_back(v4);

    //add index
    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 1);
    index_list.push_back(baseIndex + 2);

    index_list.push_back(baseIndex + 0);
    index_list.push_back(baseIndex + 2);
    index_list.push_back(baseIndex + 3);
  }
  this->cmd_list_->push_back(cmd);
}
Exemplo n.º 28
0
void vertex(const Point &p)
{
    vertex3(p);
}
Exemplo n.º 29
0
std::vector<DrawCmdData<Vertex_1P1N1UV>> SolidCubeFactory::CreateNormalMesh()
{
	float width = width_/2;
	float height = height_/2;
	float depth = depth_/2;

	DrawCmdData<Vertex_1P1N1UV> cmd;
	cmd.draw_mode = kDrawTriangles;
	DrawCmdData<Vertex_1P1N1UV>::VertexList &vert_list = cmd.vertex_list;
	std::vector<unsigned short> &index_list = cmd.index_list;

	//normal
	{
		// Front Face
		int baseIndex = vert_list.size();
		Vec3 normal(0, 0, +1);

		Vec2 texCoord1(0, 0);	Vec3 vertex1(-width, -height, depth);
		Vec2 texCoord2(1, 0);	Vec3 vertex2( width, -height, depth);
		Vec2 texCoord3(1, 1);	Vec3 vertex3( width,  height, depth);
		Vec2 texCoord4(0, 1);	Vec3 vertex4(-width,  height, depth);

		//add vertex
		Vertex_1P1N1UV v1;  v1.p = vertex1; v1.uv = texCoord1;  v1.n = normal;
		Vertex_1P1N1UV v2;  v2.p = vertex2; v2.uv = texCoord2;  v2.n = normal;
		Vertex_1P1N1UV v3;  v3.p = vertex3; v3.uv = texCoord3;  v3.n = normal;
		Vertex_1P1N1UV v4;  v4.p = vertex4; v4.uv = texCoord4;  v4.n = normal;

		vert_list.push_back(v1);
		vert_list.push_back(v2);
		vert_list.push_back(v3);
		vert_list.push_back(v4);

		//add index
		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 1);
		index_list.push_back(baseIndex + 2);

		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 2);
		index_list.push_back(baseIndex + 3);
	}
	{
		// Back Face
		int baseIndex = vert_list.size();
		Vec3 normal(0, 0, -1);

		Vec2 texCoord1(1, 0);	Vec3 vertex1(-width, -height, -depth);
		Vec2 texCoord2(1, 1);	Vec3 vertex2(-width,  height, -depth);
		Vec2 texCoord3(0, 1);	Vec3 vertex3( width,  height, -depth);
		Vec2 texCoord4(0, 0);	Vec3 vertex4( width, -height, -depth);

		//add vertex
		Vertex_1P1N1UV v1;  v1.p = vertex1; v1.uv = texCoord1;  v1.n = normal;
		Vertex_1P1N1UV v2;  v2.p = vertex2; v2.uv = texCoord2;  v2.n = normal;
		Vertex_1P1N1UV v3;  v3.p = vertex3; v3.uv = texCoord3;  v3.n = normal;
		Vertex_1P1N1UV v4;  v4.p = vertex4; v4.uv = texCoord4;  v4.n = normal;

		vert_list.push_back(v1);
		vert_list.push_back(v2);
		vert_list.push_back(v3);
		vert_list.push_back(v4);

		//add index
		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 1);
		index_list.push_back(baseIndex + 2);

		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 2);
		index_list.push_back(baseIndex + 3);
	}

	{
		// Top Face
		int baseIndex = vert_list.size();
		Vec3 normal(0, 1, 0);

		Vec2 texCoord1(0, 1);	Vec3 vertex1(-width, height, -depth);
		Vec2 texCoord2(0, 0);	Vec3 vertex2(-width, height,  depth);
		Vec2 texCoord3(1, 0);	Vec3 vertex3( width, height,  depth);
		Vec2 texCoord4(1, 1);	Vec3 vertex4( width, height, -depth);

		//add vertex
		Vertex_1P1N1UV v1;  v1.p = vertex1; v1.uv = texCoord1;  v1.n = normal;
		Vertex_1P1N1UV v2;  v2.p = vertex2; v2.uv = texCoord2;  v2.n = normal;
		Vertex_1P1N1UV v3;  v3.p = vertex3; v3.uv = texCoord3;  v3.n = normal;
		Vertex_1P1N1UV v4;  v4.p = vertex4; v4.uv = texCoord4;  v4.n = normal;

		vert_list.push_back(v1);
		vert_list.push_back(v2);
		vert_list.push_back(v3);
		vert_list.push_back(v4);

		//add index
		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 1);
		index_list.push_back(baseIndex + 2);

		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 2);
		index_list.push_back(baseIndex + 3);
	}

	{
		// Bottom Face
		int baseIndex = vert_list.size();
		Vec3 normal(0, -1, 0);

		Vec2 texCoord1(1, 1);	Vec3 vertex1(-width, -height, -depth);
		Vec2 texCoord2(0, 1);	Vec3 vertex2( width, -height, -depth);
		Vec2 texCoord3(0, 0);	Vec3 vertex3( width, -height,  depth);
		Vec2 texCoord4(1, 0);	Vec3 vertex4(-width, -height,  depth);

		//add vertex
		Vertex_1P1N1UV v1;  v1.p = vertex1; v1.uv = texCoord1;  v1.n = normal;
		Vertex_1P1N1UV v2;  v2.p = vertex2; v2.uv = texCoord2;  v2.n = normal;
		Vertex_1P1N1UV v3;  v3.p = vertex3; v3.uv = texCoord3;  v3.n = normal;
		Vertex_1P1N1UV v4;  v4.p = vertex4; v4.uv = texCoord4;  v4.n = normal;

		vert_list.push_back(v1);
		vert_list.push_back(v2);
		vert_list.push_back(v3);
		vert_list.push_back(v4);

		//add index
		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 1);
		index_list.push_back(baseIndex + 2);

		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 2);
		index_list.push_back(baseIndex + 3);
	}

	{
		// Right face
		int baseIndex = vert_list.size();
		Vec3 normal(1, 0, 0);

		Vec2 texCoord1(1, 0);	Vec3 vertex1(width, -height, -depth);
		Vec2 texCoord2(1, 1);	Vec3 vertex2(width,  height, -depth);
		Vec2 texCoord3(0, 1);	Vec3 vertex3(width,  height,  depth);
		Vec2 texCoord4(0, 0);	Vec3 vertex4(width, -height,  depth);

		//add vertex
		Vertex_1P1N1UV v1;  v1.p = vertex1; v1.uv = texCoord1;  v1.n = normal;
		Vertex_1P1N1UV v2;  v2.p = vertex2; v2.uv = texCoord2;  v2.n = normal;
		Vertex_1P1N1UV v3;  v3.p = vertex3; v3.uv = texCoord3;  v3.n = normal;
		Vertex_1P1N1UV v4;  v4.p = vertex4; v4.uv = texCoord4;  v4.n = normal;

		vert_list.push_back(v1);
		vert_list.push_back(v2);
		vert_list.push_back(v3);
		vert_list.push_back(v4);

		//add index
		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 1);
		index_list.push_back(baseIndex + 2);

		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 2);
		index_list.push_back(baseIndex + 3);
	}

	{
		// Left Face
		int baseIndex = vert_list.size();
		Vec3 normal(-1, 0, 0);

		Vec2 texCoord1(0, 0);	Vec3 vertex1(-width, -height, -depth);
		Vec2 texCoord2(1, 0);	Vec3 vertex2(-width, -height,  depth);
		Vec2 texCoord3(1, 1);	Vec3 vertex3(-width,  height,  depth);
		Vec2 texCoord4(0, 1);	Vec3 vertex4(-width,  height, -depth);

		//add vertex
		Vertex_1P1N1UV v1;  v1.p = vertex1; v1.uv = texCoord1;  v1.n = normal;
		Vertex_1P1N1UV v2;  v2.p = vertex2; v2.uv = texCoord2;  v2.n = normal;
		Vertex_1P1N1UV v3;  v3.p = vertex3; v3.uv = texCoord3;  v3.n = normal;
		Vertex_1P1N1UV v4;  v4.p = vertex4; v4.uv = texCoord4;  v4.n = normal;

		vert_list.push_back(v1);
		vert_list.push_back(v2);
		vert_list.push_back(v3);
		vert_list.push_back(v4);

		//add index
		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 1);
		index_list.push_back(baseIndex + 2);

		index_list.push_back(baseIndex + 0);
		index_list.push_back(baseIndex + 2);
		index_list.push_back(baseIndex + 3);
	}

	std::vector<DrawCmdData<Vertex_1P1N1UV>> data_list;
	data_list.push_back(cmd);
	return data_list;
}
Exemplo n.º 30
0
void compare_wls1(TString filename="../p15m_nwls/wcsim.root",TString histoname="p15m_nwls", Int_t *flag, TString rootfile = "temp.root") {

    TFile *file1;
    if (*flag!=0) {
        //file1 = new TFile(rootfile,"Update");
        file1 = new TFile(rootfile,"RECREATE");
    } else {
        file1 = new TFile(rootfile,"RECREATE");
    }
    TString filename1;
    filename1 = histoname + "_digi";
    TTree *T = new TTree(filename1,filename1);
    T->SetDirectory(file1);
    Double_t diginpe,digitime,cor_digitime,digitheta,dis_digihit;
    Int_t neve;

    Double_t mom;
    Double_t pos_x,pos_y,pos_z;
    Double_t dir_x,dir_y,dir_z;
    Double_t tube_x,tube_y,tube_z;
    Double_t totankdis;
    Double_t vertex[3],dir[3];

    Double_t tube_pos[3];

    T->Branch("digi_eve",&neve,"data/I");
    T->Branch("diginpe",&diginpe,"data/D");
    T->Branch("digitime",&digitime,"data/D");
    T->Branch("cor_digitime",&cor_digitime,"data/D");
    T->Branch("digitheta",&digitheta,"data/D");
    T->Branch("dis_dighit",&dis_digihit,"data/D");

    T->Branch("mom",&mom,"data/D");
    T->Branch("totankdis",&totankdis,"data/D");
    T->Branch("pos_x",&vertex[0],"data/D");
    T->Branch("pos_y",&vertex[1],"data/D");
    T->Branch("pos_z",&vertex[2],"data/D");

    T->Branch("dir_x",&dir[0],"data/D");
    T->Branch("dir_y",&dir[1],"data/D");
    T->Branch("dir_z",&dir[2],"data/D");

    T->Branch("tube_x",&tube_pos[0],"data/D");
    T->Branch("tube_y",&tube_pos[1],"data/D");
    T->Branch("tube_z",&tube_pos[2],"data/D");

    filename1 = histoname + "_hit";
    TTree *t1 = new TTree(filename1,filename1);
    t1->SetDirectory(file1);

    Double_t wavelength, truetime, corr_time,theta,distance,index;
    Int_t qe_flag,parentid,tubeid,totalpe;

    Int_t ntracks;

    t1->Branch("ntracks",&ntracks,"data/I");
    t1->Branch("neve",&neve,"data/I");
    t1->Branch("wavelength",&wavelength,"data/D");
    t1->Branch("truetime",&truetime,"data/D");
    t1->Branch("corr_time",&corr_time,"data/D");
    t1->Branch("theta",&theta,"data/D");
    t1->Branch("distance",&distance,"data/D");
    t1->Branch("index",&index,"data/D");

    t1->Branch("mom",&mom,"data/D");
    t1->Branch("totankdis",&totankdis,"data/D");
    t1->Branch("pos_x",&vertex[0],"data/D");
    t1->Branch("pos_y",&vertex[1],"data/D");
    t1->Branch("pos_z",&vertex[2],"data/D");

    t1->Branch("dir_x",&dir[0],"data/D");
    t1->Branch("dir_y",&dir[1],"data/D");
    t1->Branch("dir_z",&dir[2],"data/D");

    t1->Branch("tube_x",&tube_pos[0],"data/D");
    t1->Branch("tube_y",&tube_pos[1],"data/D");
    t1->Branch("tube_z",&tube_pos[2],"data/D");

    // t1->Branch("pos_x",&pos_x,"data/D");
//   t1->Branch("pos_y",&pos_y,"data/D");
//   t1->Branch("pos_z",&pos_z,"data/D");

//   t1->Branch("dir_x",&dir_x,"data/D");
//   t1->Branch("dir_y",&dir_y,"data/D");
//   t1->Branch("dir_z",&dir_z,"data/D");

//   t1->Branch("tube_x",&tube_x,"data/D");
//   t1->Branch("tube_y",&tube_y,"data/D");
//   t1->Branch("tube_z",&tube_z,"data/D");

    t1->Branch("qe_flag",&qe_flag,"data/I");
    t1->Branch("parentid",&parentid,"data/I");
    t1->Branch("tubeid",&tubeid,"data/I");
    t1->Branch("totalpe",&totalpe,"data/I");


    TFile *file = new TFile(filename);
    TTree  *wcsimT = file->Get("wcsimT");
    WCSimRootEvent *wcsimrootsuperevent = new WCSimRootEvent();
    wcsimT->SetBranchAddress("wcsimrootevent",&wcsimrootsuperevent);
    wcsimT->GetBranch("wcsimrootevent")->SetAutoDelete(kTRUE);



    TTree *gtree = file->Get("wcsimGeoT");
    WCSimRootGeom *wcsimrootgeom = new WCSimRootGeom();
    gbranch = gtree->GetBranch("wcsimrootgeom");
    gbranch->SetAddress(&wcsimrootgeom);
    gtree->GetEntry(0);

    WCSimRootPMT *pmt;

    Double_t pmt_pos[500000][3];

    for (Int_t i=0; i!=wcsimrootgeom->GetWCNumPMT(); i++) {
        pmt_pos[i][0] = (wcsimrootgeom->GetPMT(i)).GetPosition(0);
        pmt_pos[i][1] = (wcsimrootgeom->GetPMT(i)).GetPosition(1);
        pmt_pos[i][2] = (wcsimrootgeom->GetPMT(i)).GetPosition(2);
    }

    //in terms of wavelength (total NPE) real hit
    filename1 = histoname + "_total_wl";
    TH1F *hqx = new TH1F(filename1,filename1,600,200,800);

    //NPE in each event sum over digi hit
    filename1 = histoname + "_total_npe";
    TH1F *hqx2 = new TH1F(filename1,filename1,1000,0.,10000);

    //digitized hit time
    filename1 = histoname + "_digitime";
    TH1F *hqx1 = new TH1F(filename1,filename1,500,900,1400);

    //corrected digitized hit time
    filename1 = histoname + "_cor_digitime";
    TH1F *hqx4 = new TH1F(filename1,filename1,1000,400,1400);

    //digitized hit angle
    filename1 = histoname + "_digitheta";
    TH1F *hqx5 = new TH1F(filename1,filename1,180,0,180);



    //TH2F *h1 = new TH2F("h1","h1",100,1000,20000,100,90000,140000);

    Double_t index = 1.333;



    neve = *flag;

    cout << histoname << "\t" << wcsimT->GetEntries() << endl;
    for (Int_t j=0; j!=wcsimT->GetEntries(); j++) {
        //for (Int_t j=0;j!=90;j++){
        // cout << j << endl;
        wcsimT->GetEvent(j);
        neve ++;

        WCSimRootTrigger *wcsimrootevent = wcsimrootsuperevent->GetTrigger(0);
        temp = (TClonesArray*)wcsimrootevent->GetTracks();

        Int_t ntrack =  wcsimrootevent->GetNtrack();
        //cout << ntrack << endl;
        ntracks = ntrack;

        mom = ((WCSimRootTrack*)temp->At(ntrack-1))->GetP();
        //get the vertex information
        vertex[0] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetStart(0);
        vertex[1] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetStart(1);
        vertex[2] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetStart(2);

        //get position information
        dir[0] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetDir(0);
        dir[1] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetDir(1);
        dir[2] = ((WCSimRootTrack*)temp->At(ntrack-1))->GetDir(2);

        totankdis=ToTankDistance(vertex,dir);

        TVector3 vertex3(vertex[0],vertex[1],vertex[2]);
        TVector3 dir3(dir[0],dir[1],dir[2]);


        //loop through digi hit
        int max = wcsimrootevent->GetNcherenkovdigihits();

        double sum = 0;

        for (int i=0; i<max; i++) {
            // cout << max << "\t" << i << endl;
            WCSimRootCherenkovDigiHit *cDigiHit = ((WCSimRootCherenkovDigiHit*)wcsimrootevent->GetCherenkovDigiHits()->At(i));
            hqx1->Fill(cDigiHit->GetT());
            tube_pos[0] =  pmt_pos[(cDigiHit->GetTubeId()-1)][0];
            tube_pos[1] =  pmt_pos[(cDigiHit->GetTubeId()-1)][1];
            tube_pos[2] =  pmt_pos[(cDigiHit->GetTubeId()-1)][2];

            TVector3 hit3(tube_pos[0],tube_pos[1],tube_pos[2]);
            TVector3 dis = hit3-vertex3;

            diginpe = cDigiHit->GetQ();
            digitime = cDigiHit->GetT();
            cor_digitime = digitime-dis.Mag()/299792458.*1.333*1.e7;
            digitheta = dis.Angle(dir3)/3.1415926*180.;
            dis_digihit = dis.Mag();


            hqx4->Fill(cor_digitime,diginpe);
            hqx5->Fill(digitheta,diginpe);
            sum += diginpe;

            T->Fill();
        }
        hqx2->Fill(sum);


        //loop through real hit



        //loop through PMT hit first
        max = wcsimrootevent-> GetNcherenkovhits();
        //cout << max << endl;
        if (max ==0) {
            t1->Fill();
        }
        for (int i=0; i<max; i++) {
            WCSimRootCherenkovHit* wcsimrootcherenkovhit =
                dynamic_cast<WCSimRootCherenkovHit*>((wcsimrootevent->GetCherenkovHits())->At(i));

            totalpe = wcsimrootcherenkovhit->GetTotalPe(1);
            tubeid = wcsimrootcherenkovhit->GetTubeID() ;

            //loop through hit time etc
            for (int k=0; k<totalpe; k++) {
                TObject *element2 = (wcsimrootevent->GetCherenkovHitTimes())->
                                    At(wcsimrootcherenkovhit->GetTotalPe(0)+k);
                WCSimRootCherenkovHitTime *wcsimrootcherenkovhittime
                    = dynamic_cast<WCSimRootCherenkovHitTime*>(element2);

                wavelength =wcsimrootcherenkovhittime->GetWavelength();
                qe_flag = wcsimrootcherenkovhittime->GetQe_flag();
                truetime = wcsimrootcherenkovhittime->GetTruetime();
                parentid = wcsimrootcherenkovhittime->GetParentID();

                pos_x = wcsimrootcherenkovhittime->GetPosX() ;
                pos_y = wcsimrootcherenkovhittime->GetPosY() ;
                pos_z = wcsimrootcherenkovhittime->GetPosZ() ;
                dir_x = wcsimrootcherenkovhittime->GetDirX() ;
                dir_y = wcsimrootcherenkovhittime->GetDirY() ;
                dir_z = wcsimrootcherenkovhittime->GetDirZ() ;

                tube_pos[0] =  pmt_pos[tubeid-1][0];
                tube_pos[1] =  pmt_pos[tubeid-1][1];
                tube_pos[2] =  pmt_pos[tubeid-1][2];

                tube_x = tube_pos[0];
                tube_y = tube_pos[1];
                tube_z = tube_pos[2];



                TVector3 hit3(tube_pos[0],tube_pos[1],tube_pos[2]);
                TVector3 dis = hit3-vertex3;

                distance = dis.Mag();
                theta = dis.Angle(dir3)/3.1415926*180.;
                //index = index(wavelength);
                index = 1.34;
                corr_time = truetime - distance/299792458.*1e7*index;

                if (qe_flag==1) {
                    hqx->Fill(wavelength);
                }

                t1->Fill();
            }
        }



    }

    if (flag==1) {
        hqx->SetDirectory(file1);
        hqx2->SetDirectory(file1);
        hqx1->SetDirectory(file1);
        hqx4->SetDirectory(file1);
        hqx5->SetDirectory(file1);
        file1->Write();
        file1->Close();
    } else {
        hqx->SetDirectory(file1);
        hqx2->SetDirectory(file1);
        hqx1->SetDirectory(file1);
        hqx4->SetDirectory(file1);
        hqx5->SetDirectory(file1);
        file1->Write();
        file1->Close();
    }

    *flag = neve;

}