Ejemplo n.º 1
0
void CGLWidget::initializeGL()
{
    qDebug() << "init gl...";
    this->initializeOpenGLFunctions();
    this->glClearColor(0, 0, 0, 1);

    m_vao.create();
    QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

    m_program = new QOpenGLShaderProgram;
    m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/glsl/basic.vert");
    m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/glsl/basic.frag");
    m_program->link();

    m_program->bind();
    m_mvpLoc = m_program->uniformLocation("mvp");

    m_vbo.create();
    m_vbo.bind();
    m_vbo.setUsagePattern(QOpenGLBuffer::UsagePattern::StaticDraw);

    m_uvBuff.create();
    m_uvBuff.bind();
    setupVertexAttribs();
    loadGLTexture();
}
Ejemplo n.º 2
0
void CGLWidget::initializeGL()
{
    qDebug() << "init gl...";
    this->initializeOpenGLFunctions();
    this->glClearColor(0, 0, 0, 1);

    m_vao.create();
    QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

    m_program = new QOpenGLShaderProgram;
    m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/glsl/basic.vert");
    m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/glsl/basic.frag");
    m_program->link();

    m_program->bind();
    m_mvpLoc = m_program->uniformLocation("mvp");
    m_modelMatLoc = m_program->uniformLocation("m");
    m_viewMatLoc = m_program->uniformLocation("v");
    m_lightPosLoc = m_program->uniformLocation("lightPosition_worldspace");

    m_vbo.create();
    m_vbo.bind();

    m_uvBuff.create();
    m_uvBuff.bind();

    m_normalBuff.create();
    m_normalBuff.bind();

    m_indexBuff.create();
    m_indexBuff.bind();

    setupVertexAttribs();
    loadGLTexture();
}
Ejemplo n.º 3
0
void GLRasterTexture::initializeGL(bool coreProfile) {
    if(m_data.size() == 0) return;
    m_program = new QOpenGLShaderProgram;
    m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, coreProfile ? vertexShaderSourceCore : vertexShaderSource);
    m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, coreProfile ? fragmentShaderSourceCore : fragmentShaderSource);
    m_program->bindAttributeLocation("vertex", 0);
    m_program->bindAttributeLocation("texCoord", 1);
    m_program->link();

    m_program->bind();
    m_projMatrixLoc = m_program->uniformLocation("projMatrix");
    m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
    m_textureSamplerLoc = m_program->uniformLocation("texture");

    m_vao.create();
    QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

    // Setup our vertex buffer object.
    m_vbo.create();
    m_vbo.bind();
    m_vbo.allocate(constData(), m_count * sizeof(GLfloat));

    // Store the vertex attribute bindings for the program.
    setupVertexAttribs();

    m_program->setUniformValue(m_textureSamplerLoc, 0);

    m_program->release();
    m_built = true;
}
Ejemplo n.º 4
0
void GLWidget::initializeGL()
{
    // In this example the widget's corresponding top-level window can change
    // several times during the widget's lifetime. Whenever this happens, the
    // QOpenGLWidget's associated context is destroyed and a new one is created.
    // Therefore we have to be prepared to clean up the resources on the
    // aboutToBeDestroyed() signal, instead of the destructor. The emission of
    // the signal will be followed by an invocation of initializeGL() where we
    // can recreate all resources.
    connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &GLWidget::cleanup);

    initializeOpenGLFunctions();
    glClearColor(0, 0, 0, m_transparent ? 0 : 1);

    m_program = new QOpenGLShaderProgram;
    m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_core ? vertexShaderSourceCore : vertexShaderSource);
    m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_core ? fragmentShaderSourceCore : fragmentShaderSource);
    m_program->bindAttributeLocation("vertex", 0);
    m_program->bindAttributeLocation("normal", 1);
    m_program->link();

    m_program->bind();
    m_projMatrixLoc = m_program->uniformLocation("projMatrix");
    m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
    m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
    m_lightPosLoc = m_program->uniformLocation("lightPos");

    // Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x
    // implementations this is optional and support may not be present
    // at all. Nonetheless the below code works in all cases and makes
    // sure there is a VAO when one is needed.
    m_vao.create();
    QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

    // Setup our vertex buffer object.
    m_logoVbo.create();
    m_logoVbo.bind();
    m_logoVbo.allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat));

    // Store the vertex attribute bindings for the program.
    setupVertexAttribs();

    // Our camera never changes in this example.
    m_camera.setToIdentity();
    m_camera.translate(0, 0, -1);

    // Light position is fixed.
    m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, -1));

    m_program->release();

    std::cout<<" gl version : "<<glGetString( GL_VERSION )<<" glsl version: "<<glGetString( GL_SHADING_LANGUAGE_VERSION )<<std::endl;
}
Ejemplo n.º 5
0
void view::initializeGL()
{
    connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &view::cleanup);

    initializeOpenGLFunctions();
    glClearColor(0, 0, 0, m_transparent ? 0 : 1);

    m_program = new QOpenGLShaderProgram;
    m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_core ? vertexShaderSourceCore : vertexShaderSource);
    m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_core ? fragmentShaderSourceCore : fragmentShaderSource);
    m_program->bindAttributeLocation("vertex", 0);
    m_program->bindAttributeLocation("normal", 1);
    m_program->link();

    m_program->bind();
    m_projMatrixLoc = m_program->uniformLocation("projMatrix");
    m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
    m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
    m_lightPosLoc = m_program->uniformLocation("lightPos");

    m_vao.create();
    QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

    // Setup our vertex buffer object.
    m_logoVbo.create();
    m_logoVbo.bind();
    m_logoVbo.allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat));

    // Store the vertex attribute bindings for the program.
    setupVertexAttribs();

    // Our camera never changes in this example.
    m_camera.setToIdentity();
    m_camera.translate(0, 0, -1);

    // Light position is fixed.
    m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));

    m_program->release();
}
Ejemplo n.º 6
0
void CGLWidget::initializeGL()
{
    std::cout << "init gl..." << std::endl;
    this->initializeOpenGLFunctions();
    this->glClearColor(0, 0, 1, 1);

    m_vao.create();
    QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

    m_program = new QOpenGLShaderProgram;
    m_program->addShaderFromSourceFile(QOpenGLShader::Vertex, ":/glsl/basic.vert");
    m_program->addShaderFromSourceFile(QOpenGLShader::Fragment, ":/glsl/basic.frag");
    m_program->link();

    m_program->bind();

    m_vbo.create();
    m_vbo.bind();
    m_vbo.allocate(vertexData, sizeof(vertexData));

    setupVertexAttribs();
}
Ejemplo n.º 7
0
void CompasWidget::initializeGL(){
	// In this example the widget's corresponding top-level window can change
	// several times during the widget's lifetime. Whenever this happens, the
	// QOpenGLWidget's associated context is destroyed and a new one is created.
	// Therefore we have to be prepared to clean up the resources on the
	// aboutToBeDestroyed() signal, instead of the destructor. The emission of
	// the signal will be followed by an invocation of initializeGL() where we
	// can recreate all resources.
	connect(context(), &QOpenGLContext::aboutToBeDestroyed, this, &CompasWidget::cleanup);

	initializeOpenGLFunctions();
	glClearColor(0, 0, 0, m_transparent ? 0 : 1);

	m_program = new QOpenGLShaderProgram;
	m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, m_core ? vertexShaderSourceCore : vertexShaderSource);
	m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, m_core ? fragmentShaderSourceCore : fragmentShaderSource);
	m_program->bindAttributeLocation("vertex", 0);
	m_program->bindAttributeLocation("normal", 1);
	m_program->link();

	m_program->bind();
	m_projMatrixLoc = m_program->uniformLocation("projMatrix");
	m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
	m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
	m_lightPosLoc = m_program->uniformLocation("lightPos");

	// Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x
	// implementations this is optional and support may not be present
	// at all. Nonetheless the below code works in all cases and makes
	// sure there is a VAO when one is needed.
	m_vao.create();
	QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);

	// Setup our vertex buffer object.
	m_logoVbo.create();
	m_logoVbo.bind();
	m_logoVbo.allocate(m_logo.constData(), m_logo.count() * sizeof(GLfloat));

	// Store the vertex attribute bindings for the program.
	setupVertexAttribs();

	// Our camera never changes in this example.
	m_camera.setToIdentity();
	m_camera.translate(0, 0, -1);

	// Light position is fixed.
	m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));

	m_program->release();



	QSize sz=size();
	m_fbo2 = new QOpenGLFramebufferObject(sz, QOpenGLFramebufferObject::CombinedDepthStencil);



	m_program2 = new QOpenGLShaderProgram;
	m_program2->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderVideoStreamSource);
	m_program2->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderVideoStreamSource);
	m_program2->bindAttributeLocation("vertex", 0);
	m_program2->bindAttributeLocation("coord", 1);
	m_program2->link();
	m_matrixLoc = m_program->uniformLocation("matrix");

	m_vao2 = new QOpenGLVertexArrayObject;
	m_vao2->create();
	m_vao2->bind();

	m_vbo2 = new QOpenGLBuffer;
	m_vbo2->create();
	m_vbo2->bind();

	GLfloat v[] = {
		-0.5, 0.5, 0.5, 0.5,-0.5,0.5,-0.5,-0.5,0.5,
		0.5, -0.5, 0.5, -0.5,0.5,0.5,0.5,0.5,0.5,
		-0.5, -0.5, -0.5, 0.5,-0.5,-0.5,-0.5,0.5,-0.5,
		0.5, 0.5, -0.5, -0.5,0.5,-0.5,0.5,-0.5,-0.5,

		0.5, -0.5, -0.5, 0.5,-0.5,0.5,0.5,0.5,-0.5,
		0.5, 0.5, 0.5, 0.5,0.5,-0.5,0.5,-0.5,0.5,
		-0.5, 0.5, -0.5, -0.5,-0.5,0.5,-0.5,-0.5,-0.5,
		-0.5, -0.5, 0.5, -0.5,0.5,-0.5,-0.5,0.5,0.5,

		0.5, 0.5,  -0.5, -0.5, 0.5,  0.5,  -0.5,  0.5,  -0.5,
		-0.5,  0.5,  0.5,  0.5,  0.5,  -0.5, 0.5, 0.5,  0.5,
		-0.5,  -0.5, -0.5, -0.5, -0.5, 0.5,  0.5, -0.5, -0.5,
		0.5, -0.5, 0.5,  0.5,  -0.5, -0.5, -0.5,  -0.5, 0.5
	};
	GLfloat texCoords[] = {
		0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
		1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,
		1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
		0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,

		1.0f,1.0f, 1.0f,0.0f, 0.0f,1.0f,
		0.0f,0.0f, 0.0f,1.0f, 1.0f,0.0f,
		0.0f,0.0f, 1.0f,1.0f, 1.0f,0.0f,
		1.0f,1.0f, 0.0f,0.0f, 0.0f,1.0f,

		0.0f,1.0f, 1.0f,0.0f, 1.0f,1.0f,
		1.0f,0.0f, 0.0f,1.0f, 0.0f,0.0f,
		1.0f,0.0f, 1.0f,1.0f, 0.0f,0.0f,
		0.0f,1.0f, 0.0f,0.0f, 1.0f,1.0f
	};

	const int vertexCount = 36;
	m_vbo2->allocate(sizeof(GLfloat) * vertexCount * 5);
	m_vbo2->write(0, v, sizeof(GLfloat) * vertexCount * 3);
	m_vbo2->write(sizeof(GLfloat) * vertexCount * 3, texCoords, sizeof(GLfloat) * vertexCount * 2);
	m_vbo2->release();

	if (m_vao2->isCreated()){
		setupVertexAttribs2();
	}
	m_vao2->release();
}
Ejemplo n.º 8
0
void MassPoint::setup(const vec3f& lo, const vec3f& hi, const vec4f& color)
{
	float l = lo.x;
	float r = hi.x;
	float b = lo.y;
	float t = hi.y;
	float n = lo.z;
	float f = hi.z;

	float vertices[][3] = { { l, b, f }, { l, t, f }, { r, t, f },
							{ r, b, f }, { l, b, n }, { l, t, n }, { r, t, n }, { r, b, n } };

	float normals[][3] = { { -1, -1, 1 }, { -1, 1, 1 }, { 1, 1, 1 }, { 1, -1, 1 },
							{ -l, -1, -1 }, { -1, 1, -1 }, { 1, 1, -1 }, { 1, -1, -1 } };


	const int ctVertices = 8;
	vector<float> arrVertices;
	arrVertices.resize(24);
	for(int i=0; i < ctVertices; i++)
	{
		arrVertices[i*3 + 0] = vertices[i][0];
		arrVertices[i*3 + 1] = vertices[i][1];
		arrVertices[i*3 + 2] = vertices[i][2];
	}

	vector<float> arrNormals;
	arrNormals.resize(24);
	for(int i=0; i < ctVertices; i++)
	{
		vec3f n(&normals[i][0]);
		n.normalize();
		arrNormals[i*3 + 0] = n.x;
		arrNormals[i*3 + 1] = n.y;
		arrNormals[i*3 + 2] = n.z;
	}

	vector<U32> arrIndices;
	arrIndices.resize(24);
	arrIndices[0] = 0;
	arrIndices[1] = 3;
	arrIndices[2] = 2;
	arrIndices[3] = 1;

	arrIndices[4] = 4;
	arrIndices[5] = 5;
	arrIndices[6] = 6;
	arrIndices[7] = 7;

	arrIndices[8] = 3;
	arrIndices[9] = 0;
	arrIndices[10] = 4;
	arrIndices[11] = 7;

	arrIndices[12] = 1;
	arrIndices[13] = 2;
	arrIndices[14] = 6;
	arrIndices[15] = 5;

	arrIndices[16] = 2;
	arrIndices[17] = 3;
	arrIndices[18] = 7;
	arrIndices[19] = 6;

	arrIndices[20] = 5;
	arrIndices[21] = 4;
	arrIndices[22] = 0;
	arrIndices[23] = 1;

	//Setup Buffers
	setupVertexAttribs(arrVertices, 3, mbtPosition);
	setupVertexAttribs(arrNormals, 3, mbtNormal);
	setupPerVertexColor(color, ctVertices, 4);
	setupIndexBufferObject(arrIndices, ftQuads);

}
Ejemplo n.º 9
0
void SceneBox::setup(const vec3f& lo, const vec3f& hi) {
    m_lo = lo;
    m_hi = hi;
	float l = lo.x; float r = hi.x;
	float b = lo.y; float t = hi.y;
	float n = lo.z; float f = hi.z;

	float vertices [][3] = {{l, b, f}, {l, t, f}, {r, t, f},
							  {r, b, f}, {l, b, n}, {l, t, n},
							  {r, t, n}, {r, b, n}};

	vector<float> arrVertices;
	arrVertices.resize(24 * 3);
	vector<float> arrNormals;
	arrNormals.resize(24 * 3);
	/*
	U32 indices[24] = {0, 3, 2, 1, 4, 5, 6, 7,
					   3, 0, 4, 7, 1, 2, 6, 5,
					   2, 3, 7, 6, 5, 4, 0, 1};

	*/

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

	vec3f center = (lo + hi) * 0.5f;
	for(int i=0; i<6; i++) {
		for(int j=0; j<4; j++)
		{
			int idxVertex = i*4 + j;

			vec3f v(vertices[indices[idxVertex]]);
			arrVertices[idxVertex * 3] = v.x;
			arrVertices[idxVertex * 3 + 1] = v.y;
			arrVertices[idxVertex * 3 + 2] = v.z;

			vec3f n = (center - v);
			n.normalize();
			arrNormals[idxVertex * 3] = n.x;
			arrNormals[idxVertex * 3 + 1] = n.y;
			arrNormals[idxVertex * 3 + 2] = n.z;
		}
	}

	vec4f red(1,0,0,1);
	vec4f green(0,1,0,1);
	vec4f white(0.89, 0.88, 0.85, 1);
	vec4f gray(0.5, 0.5, 0.5, 1);
	vec4f colorSet[6];
	colorSet[0] = white;
	colorSet[1] = gray;
	colorSet[2] = gray;
	colorSet[3] = white;
	colorSet[4] = green;
	colorSet[5] = red;

	//Setup colors buffer
	vector<float> arrColors;
	arrColors.resize(24 * 4);

	//Face
	for(int i=0; i<6; i++) {
		//Vertex
		for(int j=0; j<4; j++)
		{
			int idxVertex = i*4 + j;
			vec4f color = colorSet[i];
			arrColors[idxVertex * 4] = color.x;
			arrColors[idxVertex * 4 + 1] = color.y;
			arrColors[idxVertex * 4 + 2] = color.z;
			arrColors[idxVertex * 4 + 3] = color.w;
		}
	}

	//Setup Mesh
	setupVertexAttribs(arrVertices, 3, vatPosition);
	setupVertexAttribs(arrNormals, 3, vatNormal);
	setupVertexAttribs(arrColors, 4, vatColor);
	//setupPerVertexColor(vec4f(1,0,0,1), m_ctVertices, 4);
	m_ctFaceElements = m_ctVertices;
	m_faceMode = ftQuads;
}