Exemple #1
6
TriangleSceneNode::TriangleSceneNode()
    : m_verts_buffer(QOpenGLBuffer(QOpenGLBuffer::VertexBuffer))
    , m_element_buffer(QOpenGLBuffer(QOpenGLBuffer::IndexBuffer))
{
    GLfloat verts[] = {-.5f, -.5f, 0.f,
                       .5f, -.5f, 0.f,
                        0.f, .5f, 0.f};
    m_verts_count = sizeof(verts)/sizeof(verts[0]);

    GLuint elems[] = {0,1,2};
    m_element_count = sizeof(elems)/sizeof(elems[0]);

    m_vao.create();
    m_vao.bind();

    m_verts_buffer.create();
    m_verts_buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_verts_buffer.bind();
    m_verts_buffer.allocate(verts, sizeof(verts));
    m_verts_buffer.release();

    m_element_buffer.create();
    m_element_buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_element_buffer.bind();
    m_element_buffer.allocate(elems, sizeof(elems));
    m_element_buffer.release();

    m_vao.release();

    m_verts = {-.5, -.5, 0,
               0, .5, 0,
               .5, -.5, 0};

    m_program = std::unique_ptr<QOpenGLShaderProgram>(new QOpenGLShaderProgram());

    m_program->addShaderFromSourceCode(QOpenGLShader::Vertex,
                                     "attribute highp vec3 verts;\n"
                                     "varying highp vec4 color;\n"
                                     "uniform highp mat4 mvp;\n"
                                     "void main() {"
                                     "color = vec4(verts + vec3(.5,.5,1.0), 1.0);"
                                     "gl_Position = mvp * vec4(verts, 1.0);"
                                     "}\n");
    m_program->addShaderFromSourceCode(QOpenGLShader::Fragment,
                                     "varying highp vec4 color;\n"
                                     "void main() {"
                                     "gl_FragColor = vec4(1.0,1.0,1.0,1.0);"
                                     "}\n");
    m_program->link();
}
void PolygonalDrawable::createVAO()
{
    m_vao.create();
    m_vao.bind();

    VertexReuse::optimize(
        m_vertices,
        m_normals,
        m_texcs,
        m_indices);

    t_vec3s vertices;
    t_vec3s normals;

    delete m_he;
    m_he = new HalfEdgeStructure(*this);
    m_he->calculatePerVertexNormals(180.0f);

    HalfEdgeStructure::t_faces::const_iterator i = m_he->faces().begin();
    const HalfEdgeStructure::t_faces::const_iterator iEnd = m_he->faces().end();
    for(; i != iEnd; ++i)
    {
        const HalfEdgeStructure::HalfEdge * const he0 = i->he->prev;
        const HalfEdgeStructure::HalfEdge * const he1 = i->he;
        const HalfEdgeStructure::HalfEdge * const he2 = i->he->next;

        normals.push_back(he0->normal);
        vertices.push_back(*(he0->vertex));
        normals.push_back(he1->normal);
        vertices.push_back(*(he1->vertex));
        normals.push_back(he2->normal);
        vertices.push_back(*(he2->vertex));
    }

    m_vertices = vertices;
    m_normals = normals;

    m_vertexBuffer = QOpenGLBuffer();
    m_vertexBuffer.create();
    m_vertexBuffer.bind();
    m_vertexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_vertexBuffer.allocate(m_vertices.constData(), m_vertices.size() * sizeof(QVector3D));

    m_normalBuffer = QOpenGLBuffer();
    m_normalBuffer.create();
    m_normalBuffer.bind();
    m_normalBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_normalBuffer.allocate(m_normals.constData(), m_normals.size() * sizeof(QVector3D));

    m_texcoordBuffer.create();
    m_texcoordBuffer.bind();
    m_texcoordBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_texcoordBuffer.allocate(m_texcs.constData(), m_texcs.size() * sizeof(QVector2D));

    m_indexBuffer.create();
    m_indexBuffer.bind();
    m_indexBuffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_indexBuffer.allocate(m_indices.constData(), m_indices.size() * sizeof(unsigned int));
}
Exemple #3
0
/**
  \brief Called when the opengl context is good
  */
void cwTile::initialize() {
    TriangleIndexBuffer = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
    TriangleVertexBuffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);

    TriangleIndexBuffer.create();
    TriangleVertexBuffer.create();

    vVertex = Program->attributeLocation("vVertex");
}
Exemple #4
0
//===================================================================
Mesh::Mesh(QObject *parent) : QObject(parent),
  mDebugView(false), mMode(GL_TRIANGLES), mTexture(0), mTextureImage(0), mOpacity(1)
{
    // ctor

    mShaderProgram = new QOpenGLShaderProgram(this);

    mVertexBuffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
    mIndexBuffer  = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
    mTexture      = new QOpenGLTexture(QOpenGLTexture::Target2D);

    resetTransform();


    addMaterial(Material());
}
void PolygonalDrawable::createVAO(QOpenGLShaderProgram * program)
{
    m_vao.create();
    m_vao.bind();

    m_vertexBuffer = QOpenGLBuffer();
    m_vertexBuffer.create();
    m_vertexBuffer.bind();
    m_vertexBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
    m_vertexBuffer.allocate(m_vertices.constData(), m_vertices.size() * sizeof(QVector3D));

    program->setAttributeBuffer("in_vertex", GL_FLOAT, 0, 3, 3 * sizeof(float));
    if (m_vertices.size())
    {
        program->enableAttributeArray("in_vertex");
    }

    m_normalBuffer = QOpenGLBuffer();
    m_normalBuffer.create();
    m_normalBuffer.bind();
    m_normalBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
    m_normalBuffer.allocate(m_normals.constData(), m_normals.size() * sizeof(QVector3D));


    program->setAttributeBuffer("in_normal", GL_FLOAT, 0, 3, 3 * sizeof(float));
    if (m_normals.size())
    {
        program->enableAttributeArray("in_normal");
    }

    m_texcoordBuffer.create();
    m_texcoordBuffer.bind();
    m_texcoordBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
    m_texcoordBuffer.allocate(m_texcs.constData(), m_texcs.size() * sizeof(QVector2D));

    program->setAttributeBuffer("in_texcoord", GL_FLOAT, 0, 2, 2 * sizeof(float));
    if (m_texcs.size())
    {
        program->enableAttributeArray("in_texcoord");
    }

    m_indexBuffer.create();
    m_indexBuffer.bind();
    m_indexBuffer.setUsagePattern( QOpenGLBuffer::StaticDraw );
    m_indexBuffer.allocate(m_indices.constData(), m_indices.size() * sizeof(unsigned int));
}
Exemple #6
0
void Grid::initBuffer()
{
    makeGrid(10, 1);
	vbo = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
	vbo.create();
	vbo.bind();
	vbo.allocate(&VertexData[0], 84 * sizeof(QVector3D));
    vbo.release();
}
Exemple #7
0
//===================================================================
Mesh::Mesh(const Mesh &mesh)
{
    // copy ctor
    mShaderProgram = new QOpenGLShaderProgram(this);

    mDebugView = mesh.mDebugView;
    mIndexBuffer = QOpenGLBuffer(mesh.mIndexBuffer);
    mMode = mesh.mMode;
    mTextureImage = mesh.mTextureImage;
    setTextureImage(mTextureImage);

    for (int index = 0; index < mesh.mVertices.count(); index++)
          mVertices.append(mesh.vertices().at(index)) ;

    addMaterial(Material());
    mOpacity = mesh.mOpacity;
}
Exemple #8
0
void Window::initializeGL()
{
    initializeOpenGLFunctions();
    connect(context(), SIGNAL(aboutToBeDestroyed()), this, SLOT(teardownGL()), Qt::DirectConnection);
    printVersionInformation();

    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    //Loading, compiling and linking shaders.
	shaderProgram = new QOpenGLShaderProgram();
	shaderProgram->addShaderFromSourceFile(QOpenGLShader::Vertex, ".\\simple.vert");
	shaderProgram->addShaderFromSourceFile(QOpenGLShader::Fragment, ".\\simple.frag");
	shaderProgram->link();
	shaderProgram->bind();

	//Creating the VAO
	vertexArrayObject.create();
	vertexArrayObject.bind();

	//We now need to combine the data of out mesh and our interator mesh
	vector<Vertex> combinedVertexBuffer;
	combinedVertexBuffer.reserve(mesh->getVertexVector().size() +
		interatorMesh->getVertexVector().size());
	combinedVertexBuffer.insert(combinedVertexBuffer.end(), mesh->
		getVertexVector().begin(), mesh->getVertexVector().end());
	combinedVertexBuffer.insert(combinedVertexBuffer.end(), interatorMesh->
		getVertexVector().begin(), interatorMesh->getVertexVector().end());


	//Index buffer
	vector<unsigned int> combinedIndexBuffer;
	combinedIndexBuffer.reserve(mesh->getIndexVector().size() + interatorMesh->
		getIndexVector().size());
	combinedIndexBuffer.insert(combinedIndexBuffer.end(), mesh->
		getIndexVector().begin(), mesh->getIndexVector().end());

	unsigned int vertexVectorSize = static_cast<unsigned int>(mesh->
		getVertexVector().size());
	for (auto index : interatorMesh->getIndexVector()) {
		combinedIndexBuffer.push_back(index + vertexVectorSize);
	}

	//Creating the VBO for the mesh
    vertexBuffer.create();
	vertexBuffer.bind();
	vertexBuffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
	vertexBuffer.allocate(combinedVertexBuffer.data(), 
		static_cast<int>(combinedVertexBuffer.size() * sizeof(Vertex)));

	//Creating IBO for the mesh
	indexBuffer = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
	indexBuffer.create();
	indexBuffer.bind();
	indexBuffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
	indexBuffer.allocate(combinedIndexBuffer.data(),
		static_cast<int>(combinedIndexBuffer.size() * sizeof(unsigned int)));

	//Enabling attribute arrays for vertex, normal and color data.
	shaderProgram->enableAttributeArray(0);
	shaderProgram->enableAttributeArray(1);
	shaderProgram->enableAttributeArray(2);
	shaderProgram->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
	shaderProgram->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());
	shaderProgram->setAttributeBuffer(2, GL_FLOAT, Vertex::normalOffset(), Vertex::NormalTupleSize, Vertex::stride());

	vertexArrayObject.release();
	vertexBuffer.release();
	indexBuffer.release();
	shaderProgram->release();

	//Setting up model, view and projection matrices.
	mvpSetup();

	//Creating interator ray
	interator = new Ray(vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 0.0f, 1.0f));

	//Setting up the collision detector
	collisionDetector.setMesh(mesh);
	collisionDetector.setRay(interator);

	//Initializing haptic device
	if (hapticsEnabled) {
		haptic.initializeHL();
		haptic.updateHapticWorkspace();
		haptic.setInterator(interator);
		haptic.setCollisionDetector(&collisionDetector);
		haptic.setCollisionPath(&path);
		haptic.setMesh(mesh);
		haptic.setShaderProgram(shaderProgram);
		haptic.setVAO(&vertexArrayObject);
		handle = hdScheduleAsynchronous(HapticInterface::mainHapticCallback,
			&haptic, HD_MAX_SCHEDULER_PRIORITY);
		hdStartScheduler();
	}
}
ParticleSceneNode::ParticleSceneNode()
    : m_program(new QOpenGLShaderProgram)
    , m_square_buffer(QOpenGLBuffer(QOpenGLBuffer::VertexBuffer))
    , m_particle_verts_buffer(QOpenGLBuffer(QOpenGLBuffer::VertexBuffer))
    , m_tex_uv_buffer(QOpenGLBuffer(QOpenGLBuffer::VertexBuffer))
    , m_particle_system(new ParticleSystem)
{
    const float psize = .03f;
    //vertex data
    GLfloat square_verts[] = {-psize, -psize, 0.0,
                              -psize,  psize, 0.0,
                               psize, -psize, 0.0,
                               psize,  psize, 0.0};

    GLfloat particle_verts[] = {-.5f, -.5f,  0.0f,
                                 .5f, -.5f,  0.0f,
                                 .5f,  .5f,  0.0f,
                                -.5f,  .5f,  0.0f,
                                -.5f, -.5f,  0.5f,
                                 .5f, -.5f,  0.5f,
                                 .5f,  .5f,  0.5f,
                                -.5f,  .5f,  0.5f,};

    GLfloat tex_uv[] = {0.0f, 1.0f,
                        0.0f, 0.0f,
                        1.0f, 1.0f,
                        1.0f, 0.0f};

    m_particle_verts_count = sizeof(particle_verts)/sizeof(particle_verts[0])/3;
    m_billboard_verts_count = sizeof(square_verts)/sizeof(square_verts[0])/3;
    m_tex_uv_count = sizeof(tex_uv)/sizeof(tex_uv)/3;


    //Texture
    std::vector<uint8_t> &particle_texture = tex_gen::particle_texture_1();
    int tex_size = tex_gen::particle_tex_1_size();

    qDebug() << "tx" << particle_texture[100];

    //glActiveTexture(GL_TEXTURE0);

    glGenTextures(1, &m_texture_id);
    glBindTexture(GL_TEXTURE_2D, m_texture_id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glGenerateMipmap(GL_TEXTURE_2D);
    glTexImage2D(GL_TEXTURE_2D,
                 0,
                 GL_RGB,
                 tex_size, //+2
                 tex_size, //+2
                 0,
                 GL_RGB,
                 GL_UNSIGNED_BYTE,
                 particle_texture.data());

    //Initialize buffers

    m_vao.create();
    m_vao.bind();

    m_square_buffer.create();
    m_square_buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_square_buffer.bind();
    m_square_buffer.allocate(square_verts, sizeof(square_verts));
    m_square_buffer.release();

    m_particle_verts_buffer.create();
    m_particle_verts_buffer.setUsagePattern(QOpenGLBuffer::DynamicDraw);
    m_particle_verts_buffer.bind();
    m_particle_verts_buffer.allocate(particle_verts, sizeof(particle_verts));
    m_particle_verts_buffer.release();

    m_tex_uv_buffer.create();
    m_tex_uv_buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
    m_tex_uv_buffer.bind();
    m_tex_uv_buffer.allocate(tex_uv, sizeof(tex_uv));
    m_tex_uv_buffer.release();

    m_vao.release();

    //initialize shader program

    m_program->addShaderFromSourceFile(QOpenGLShader::Vertex,
                                       ":/shaders/particle1.vsh");
    m_program->addShaderFromSourceFile(QOpenGLShader::Fragment,
                                       ":/shaders/particle1.fsh");
    m_program->link();

    m_loc_stride = 0;
    m_loc_offset = 0;
    m_loc_count = 3;
}
void Window::initializeGL() {
	// Initialize OpenGL Backend
	initializeOpenGLFunctions();
	//This is equivalent to register an IDLE function callback for animation
	//We don't control the frame rate is as fast as the system is available
	//Maybe using the vsync
	connect(this, SIGNAL(frameSwapped()), this, SLOT(update()));
	printVersionInformation();
	
	//Global GL configurations
	glEnable(GL_CULL_FACE);
	glCullFace(GL_BACK);
	glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
	// Application-specific initialization
	{
		// Create Shader (Do not release until VAO is created)
		m_program_ptr = new QOpenGLShaderProgram();
		m_program_ptr->addShaderFromSourceFile(QOpenGLShader::Vertex, "Resources/shaders/simpleVertex.glsl");
		m_program_ptr->addShaderFromSourceFile(QOpenGLShader::Fragment, "Resources/shaders/simpleFragment.glsl");
		m_program_ptr->link();
		m_program_ptr->bind();

		//Fill the arrays with date in CPU side
		createGeomerty();
		// Create Vertex Array Object (Remember this needs to be done BEFORE binding the vertex)
		m_vao.create();
		m_vao.bind();

		// Create Buffers (Do not release until VAO is created)
		m_vertex_buffer = QOpenGLBuffer(QOpenGLBuffer::VertexBuffer);
		m_vertex_buffer.create();
		m_vertex_buffer.bind();
		m_vertex_buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
		m_vertex_buffer.allocate(m_vertices.constData(), m_vertices.size() * sizeof(Vertex));

		m_index_buffer = QOpenGLBuffer(QOpenGLBuffer::IndexBuffer);
		m_index_buffer.create();
		m_index_buffer.bind();
		m_index_buffer.setUsagePattern(QOpenGLBuffer::StaticDraw);
		m_index_buffer.allocate(m_indexes.constData(), m_indexes.size() * sizeof(unsigned int));
		
		
		m_program_ptr->enableAttributeArray(0);
		m_program_ptr->enableAttributeArray(1);
		m_program_ptr->setAttributeBuffer(0, GL_FLOAT, Vertex::positionOffset(), Vertex::PositionTupleSize, Vertex::stride());
		m_program_ptr->setAttributeBuffer(1, GL_FLOAT, Vertex::colorOffset(), Vertex::ColorTupleSize, Vertex::stride());

		// Release (unbind) all
		m_vao.release();
		m_index_buffer.release();
		m_index_buffer.release();
		m_program_ptr->release();

		//Once we have a copy in the GPU there is no need to keep a CPU copy (unless yopu want to)
		m_indexes.clear();
		m_vertices.clear();
	}

	//Program main logic
	//Camera start with no rotation
	m_new_rotation = m_base_rotation = QQuaternion(1.0f, QVector3D(0.0f, 0.0f, 0.0f));
	m_mouse_draging = false;
	m_fovy_y = 45.0f;
}