drawer.cpp 4.41 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps  *
* version 0.1                                                                  *
* Copyright (C) 2009, IGG Team, LSIIT, University of Strasbourg                *
*                                                                              *
* This library is free software; you can redistribute it and/or modify it      *
* under the terms of the GNU Lesser General Public License as published by the *
* Free Software Foundation; either version 2.1 of the License, or (at your     *
* option) any later version.                                                   *
*                                                                              *
* This library is distributed in the hope that it will be useful, but WITHOUT  *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License  *
* for more details.                                                            *
*                                                                              *
* You should have received a copy of the GNU Lesser General Public License     *
* along with this library; if not, write to the Free Software Foundation,      *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.           *
*                                                                              *
* Web site: https://iggservis.u-strasbg.fr/CGoGN/                              *
* Contact information: cgogn@unistra.fr                                        *
*                                                                              *
*******************************************************************************/

#include "Utils/drawer.h"
26 27
#include "Utils/Shaders/shaderColorPerVertex.h"

28 29 30 31
#include "Utils/vbo.h"

namespace CGoGN
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
32

33 34 35
namespace Utils
{

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
36
Drawer::Drawer() : m_currentWidth(1.0f)
37 38 39 40 41 42 43 44 45 46 47 48
{
	m_vboPos = new Utils::VBO();
	m_vboPos->setDataSize(3);

	m_vboCol = new Utils::VBO();
	m_vboCol->setDataSize(3);

	m_shader = new Utils::ShaderColorPerVertex();

	m_shader->setAttributePosition(m_vboPos);
	m_shader->setAttributeColor(m_vboCol);

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
49
	Utils::GLSLShader::registerShader(NULL, m_shader);
50 51 52 53 54 55 56 57 58

	m_dataPos.reserve(128);
	m_dataCol.reserve(128);
	m_begins.reserve(16);
//	m_modes.reserve(16);
}

Drawer::~Drawer()
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
59
	Utils::GLSLShader::unregisterShader(NULL, m_shader);
60 61 62 63 64 65 66
	delete m_shader;
	delete m_vboPos;
	delete m_vboCol;
}

void Drawer::lineWidth(float lw)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
67
	m_currentWidth = lw;
68 69 70 71 72 73 74 75 76
}

void Drawer::pointSize(float ps)
{
	m_currentWidth = ps;
}

void Drawer::begin(GLenum mode)
{
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
77
	m_begins.push_back(PrimParam(m_dataPos.size(), mode, m_currentWidth));
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
}

void Drawer::end()
{
	m_begins.back().nb = m_dataPos.size() - m_begins.back().begin;
}

void Drawer::color(const Geom::Vec3f& col)
{
	if (m_dataPos.size() == m_dataCol.size())
		m_dataCol.push_back(col);
	else
		m_dataCol.back() = col;
}

void Drawer::color3f(float r, float g, float b)
{
	color(Geom::Vec3f(r,g,b));
}

void Drawer::vertex(const Geom::Vec3f& v)
{
	if (m_dataPos.size() == m_dataCol.size())
	{
		if (m_dataCol.empty())
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
103
			m_dataCol.push_back(Geom::Vec3f(1.0f, 1.0f, 1.0f));
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
		else
			m_dataCol.push_back( m_dataCol.back());
	}

	m_dataPos.push_back(v);
}

void Drawer::vertex3f(float r, float g, float b)
{
	vertex(Geom::Vec3f(r,g,b));
}

void Drawer::newList(GLenum comp)
{
	m_compile = comp;
	m_dataPos.clear();
	m_dataCol.clear();
	m_begins.clear();
}

void Drawer::endList()
{
	unsigned int nbElts = m_dataPos.size();

	m_vboPos->bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
129
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataPos[0]), GL_STREAM_DRAW);
130 131

	m_vboCol->bind();
Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
132
	glBufferData(GL_ARRAY_BUFFER, nbElts * sizeof(Geom::Vec3f), &(m_dataCol[0]), GL_STREAM_DRAW);
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

	// free memory
	std::vector<Geom::Vec3f> tempo;
	tempo.swap(m_dataPos);
	std::vector<Geom::Vec3f> tempo2;
	tempo2.swap(m_dataCol);

	if (m_compile != GL_COMPILE)
		callList();
}

void Drawer::callList()
{
	if (m_begins.empty())
		return;

	m_shader->enableVertexAttribs();
	for (std::vector<PrimParam>::iterator pp = m_begins.begin(); pp != m_begins.end(); ++pp)
	{
		if (pp->mode == GL_POINTS)
			glPointSize(pp->width);
		if ((pp->mode == GL_LINES) || (pp->mode == GL_LINE_LOOP))
			glLineWidth(pp->width);
		glDrawArrays(pp->mode, pp->begin, pp->nb);
	}
 	m_shader->disableVertexAttribs();
}

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
161
} // namespace Utils
162

Pierre Kraemer's avatar
merge..  
Pierre Kraemer committed
163
} // namespace CGoGN