void RenderScene()
{
	// Model matrix : being updated by idle
	// Our ModelViewProjection : multiplication of our 3 matrices
	MVP = Projection * View * Model; // Remember, matrix multiplication is the other way around

	// Use our shader
	glUseProgram(ShaderMainProgramId);

	// Send our transformation to the currently bound shader, in the "MVP" uniform

	glUniformMatrix4fv(MainShaderMvpId, 1, GL_FALSE, &MVP[0][0]);

	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

	// Bind texture to texture unit
	glActiveTexture(GL_TEXTURE0); // change active texture unit to number 0
	glBindTexture(GL_TEXTURE_2D, TextureId); // bind the texture to the active texture unit (which is now 0)

	// Bind shader variable "Texture" to texture unit
	glUniform1iARB(MainShaderTextureId, 0); // Notice the 0 to indicate you want to sample from the texture bound to Texture Unit GL_TEXTURE0.

	float imageRatio = (float)theTexMap.GetNumCols() / theTexMap.GetNumRows();
	if (imageRatio > 1)
	{
		imageRatio = 1.0 / imageRatio;
	}

	glBegin(GL_QUADS);

	glTexCoord2f(0.0, 1.0);
	glVertex3f(-1.0, 0.0, -1.0*imageRatio);

	glTexCoord2f(0.0, 0.0);
	glVertex3f(-1.0, 0.0, 1.0*imageRatio);

	glTexCoord2f(1.0, 0.0);
	glVertex3f(1.0, 0.0, 1.0*imageRatio);

	glTexCoord2f(1.0, 1.0);
	glVertex3f(1.0, 0.0, -1.0*imageRatio);

	glEnd();


	// Draw groups
	glUseProgram(ShaderModelProgramId);
	glUniformMatrix4fv(ModelShaderMvpId, 1, GL_FALSE, &MVP[0][0]);

	int modelNum = 0;
	for (std::vector<Group>::iterator it = (*Data).begin(); it != (*Data).end(); ++it)
	{
		RenderGroup(it->items, it->color, modelNum++);
		RenderGroupCount(it->items.size(), 0);
	}
	glutWarpPointer(g_iWindowWidth/2, g_iWindowHeight/2);
}
Beispiel #2
0
void Eigenbackground::UpdateHistory(int frame_num, const RgbImage& new_frame)
{
	if(frame_num < m_params.HistorySize())
	{
		CvMat src_row;
		cvGetRow(m_pcaData, &src_row, frame_num);
		cvReshape(&src_row, &src_row, 3, new_frame.Ptr()->height);
		cvCopy(new_frame.Ptr(), &src_row);
	}
}
Beispiel #3
0
 void createTexture(){
     glGenTextures(1, &textureMap[0]);
     glBindTexture(GL_TEXTURE_2D, textureMap[0]);
     imagMap.LoadBmpFile(groundTexture); /* Passar a string para const char em C */
     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
     glTexImage2D(GL_TEXTURE_2D, 0, 3,
         imagMap.GetNumCols(),
         imagMap.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE,
         imagMap.ImageData());
 }
Beispiel #4
0
void Surface::loadTexture(string im_path) {
    RgbImage image;

    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    image.LoadBmpFile(im_path.c_str());
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 3,
                 image.GetNumCols(),
                 image.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE,
                 image.ImageData() );
    glBindTexture(GL_TEXTURE_2D, 0); // deactivate
}
Beispiel #5
0
void Eigenbackground::Subtract(int frame_num, const RgbImage& data,  
																BwImage& low_threshold_mask, BwImage& high_threshold_mask)
{
	// create eigenbackground
	if(frame_num == m_params.HistorySize())
	{
		// create the eigenspace
		m_pcaAvg = cvCreateMat( 1, m_pcaData->cols, CV_32F );
		m_eigenValues = cvCreateMat( m_pcaData->rows, 1, CV_32F ); 
		m_eigenVectors = cvCreateMat( m_pcaData->rows, m_pcaData->cols, CV_32F );
		cvCalcPCA(m_pcaData, m_pcaAvg, m_eigenValues, m_eigenVectors, CV_PCA_DATA_AS_ROW);

		int index = 0;
		for(unsigned int r = 0; r < m_params.Height(); ++r)
		{
			for(unsigned int c = 0; c < m_params.Width(); ++c)
			{
				for(int ch = 0; ch < m_background.Ptr()->nChannels; ++ch)
				{
					m_background(r,c,0) = static_cast<unsigned char>(cvmGet(m_pcaAvg,0,index)+0.5);
					index++;
				}
			}
		}
	}

	if(frame_num >= m_params.HistorySize())
	{
		// project new image into the eigenspace
		int w = data.Ptr()->width;
		int h = data.Ptr()->height;
		int ch = data.Ptr()->nChannels;
		CvMat* dataPt = cvCreateMat(1, w*h*ch, CV_8UC1); 
		CvMat data_row;
		cvGetRow(dataPt, &data_row, 0);
		cvReshape(&data_row, &data_row, 3, data.Ptr()->height); 
		cvCopy(data.Ptr(), &data_row); 

		CvMat* proj = cvCreateMat(1, m_params.EmbeddedDim(), CV_32F);
		cvProjectPCA(dataPt, m_pcaAvg, m_eigenVectors, proj);

		// reconstruct point
		CvMat* result = cvCreateMat(1, m_pcaData->cols, CV_32F);
		cvBackProjectPCA(proj, m_pcaAvg, m_eigenVectors, result);

		// calculate Euclidean distance between new image and its eigenspace projection
		int index = 0;
		for(unsigned int r = 0; r < m_params.Height(); ++r)
		{
			for(unsigned int c = 0; c < m_params.Width(); ++c)
			{
				double dist = 0;
				bool bgLow = true;
				bool bgHigh = true;
				for(int ch = 0; ch < 3; ++ch)
				{
					dist = (data(r,c,ch) - cvmGet(result,0,index))*(data(r,c,ch) - cvmGet(result,0,index));
					if(dist > m_params.LowThreshold())
						bgLow = false;
					if(dist > m_params.HighThreshold())
						bgHigh = false;
					index++;
				}
				
				if(!bgLow)
				{
					low_threshold_mask(r,c) = FOREGROUND;
				}
				else
				{
					low_threshold_mask(r,c) = BACKGROUND;
				}

				if(!bgHigh)
				{
					high_threshold_mask(r,c) = FOREGROUND;
				}
				else
				{
					high_threshold_mask(r,c) = BACKGROUND;
				}
			}
		}
		
		cvReleaseMat(&result);
		cvReleaseMat(&proj);		
		cvReleaseMat(&dataPt);
	}
	else 
	{
		// set entire image to background since there is not enough information yet
		// to start performing background subtraction
		for(unsigned int r = 0; r < m_params.Height(); ++r)
		{
			for(unsigned int c = 0; c < m_params.Width(); ++c)
			{
				low_threshold_mask(r,c) = BACKGROUND;
				high_threshold_mask(r,c) = BACKGROUND;
			}
		}
	}

	UpdateHistory(frame_num, data);
}
void LoadResources()
{
	// Load file
	Data = ParseFile("A01.txt");




	// Load texture
	// Let OpenGL create us a new texture ID to use
	glGenTextures(1, &TextureId);

	// Bind against the texture ID
	glBindTexture(GL_TEXTURE_2D, TextureId);

	// Set properties on current bound texture
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

	// Load texture data
	auto h = gluBuild2DMipmaps(GL_TEXTURE_2D, 3, theTexMap.GetNumCols(), theTexMap.GetNumRows(), GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData());
	//glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, theTexMap.GetNumCols(), theTexMap.GetNumRows(), 0, GL_RGB, GL_UNSIGNED_BYTE, theTexMap.ImageData());





	// Create and compile our GLSL program from the shaders
	ShaderMainProgramId = LoadShaders("Main.vertexshader", "Main.fragmentshader");
	ShaderModelProgramId = LoadShaders("Model.vertexshader", "Model.fragmentshader");

	MainShaderMvpId = glGetUniformLocation(ShaderMainProgramId, "MVP");
	MainShaderTextureId = glGetUniformLocation(ShaderMainProgramId, "Texture");

	ModelShaderMvpId = glGetUniformLocation(ShaderModelProgramId, "MVP");
	ModelShaderColorId = glGetUniformLocation(ShaderModelProgramId, "Color");





	// Upload and bind vertexes/indices
	Vertex vertices[8] = {
		{ 0.0f*0.05, 1.0f*0.05, 0.0f*0.05 },
		{ 0.0f*0.05, 1.0f*0.05, 1.0f*0.05 },
		{ 1.0f*0.05, 1.0f*0.05, 1.0f*0.05 },
		{ 1.0f*0.05, 1.0f*0.05, 0.0f*0.05 },
		{ 0.0f*0.05, 0.0f*0.05, 0.0f*0.05 },
		{ 0.0f*0.05, 0.0f*0.05, 1.0f*0.05 },
		{ 1.0f*0.05, 0.0f*0.05, 1.0f*0.05 },
		{ 1.0f*0.05, 0.0f*0.05, 0.0f*0.05 }
	};

	GLubyte indices[36] = {
		0, 3, 7,
		5, 7, 0,
		4, 0, 7,
		0, 3, 7,
		7, 3, 6,
		3, 2, 6,
		6, 2, 5,
		2, 1, 5,
		5, 1, 4,
		1, 0, 4,
		5, 4, 6,
		4, 7, 6
	};

	// Vertex buffer
	glGenBuffers(1, &VertexBufferId);
	glBindBuffer(GL_ARRAY_BUFFER, VertexBufferId);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	// Index buffer
	glGenBuffers(1, &IndexBufferId);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IndexBufferId);
	glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
}