Exemple #1
0
void triebWerk::CEntity::SetDrawable(IDrawable * a_pDrawable)
{
	RemoveDrawable();
	m_pDrawable = a_pDrawable;
}
	void GraphicContext::EndDraw()
	{
		//wglMakeCurrent(GetDC(_rcontext->getWindow()->CMWindowHandle()), _rcontext->getRenderingContext());
		glFlush();

		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		
		//actual draw

		CMShape *_drawableShape = *_drawables.begin();
			
		glUseProgram(_rcontext->getProgramIndex());


		for (int i = 0; i < _drawables.size(); i++)
		{
			std::cout << _cameraX << std::endl;
			_drawableShape = _drawables.at(i);
			//elegant way of transforming primitives

			if (_drawableShape->GetIndices().size() == 0)
			{
				RemoveDrawable(_drawableShape);
				i--;
				continue;
			}
			//first projection, as it should be
			_projection = _defaultProjection;

				_view= glm::translate(glm::vec3(_cameraX, _cameraY, 0.0f));
			//then translate the primitive where it should be 
			_projection = glm::translate(_projection, glm::vec3(_drawableShape->GetX(), _drawableShape->GetY(), 0.0f));


			//then scale the primitive as it should be 
			if (_drawableShape->GetSize().getX() != 1 || _drawableShape->GetSize().getY() != 1)
				_projection = glm::scale(_projection, glm::vec3(_drawableShape->GetSize().getX(), _drawableShape->GetSize().getY(), 0.0f));


			//then rotate the primitive as it should be
			if (_drawableShape->GetRotation() != 0 || _drawableShape->GetOrigon().getX() != 0 || _drawableShape->GetOrigon().getY() != 0)
			{
				_projection	= glm::rotate(_projection, (float)_drawableShape->GetRotation(), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::translate(glm::vec3(-_drawableShape->GetOrigon().getX(), -_drawableShape->GetOrigon().getY(), 0.0f));
			}

			//finish with a touch of mint and glUniformMatrix4fv, and voilá!
			glUniformMatrix4fv(_projectionLocation, 1, GL_FALSE, reinterpret_cast<const float*>(&_projection));
			glUniformMatrix4fv(_viewLocation, 1, GL_FALSE, reinterpret_cast<const float*>(&_view));
			//you got yourself a handy way of dumping your workload to the GPU!

			glUniform3f(_colorIndex, _drawableShape->GetColorR(), _drawableShape->GetColorG(), _drawableShape->GetColorB());
			glUniform1f(_alphaChannel, _drawableShape->GetColorA());
			

			//set vertex data
			glBindBuffer(GL_ARRAY_BUFFER, buffers[0]);
			glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* _drawableShape->GetVertices().size(), &_drawableShape->GetVertices()[0], GL_STATIC_DRAW);

			glBindBuffer(GL_ARRAY_BUFFER, 0);
			//set fragment data


			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
			glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLfloat)*_drawableShape->GetIndices().size(), &_drawableShape->GetIndices()[0], GL_STATIC_DRAW);

			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

			
		
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]);
			
			glBindTexture(GL_TEXTURE_2D, _drawableShape->_hasImage ?_drawableShape->GetImage()->getTextureId() : 0);

			glDrawElements(GL_TRIANGLES, _drawableShape->GetIndices().size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid*>(0));

		
			
			

		
			
		}
			glBindTexture(GL_TEXTURE_2D, 0u);

			glBindBuffer(GL_ARRAY_BUFFER, 0);
			glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
			glUseProgram(0);

			SwapBuffers(_rcontext->getHDC());//Bring back buffer to foreground

			

	}