Exemplo n.º 1
2
    VertexArrayObject() {
        glCreateVertexArrays(1,&vertexArrayObject);
        glCreateBuffers(3,buffer);
        const static constexpr float data_0[]{
            -0.5f,-0.5f,0.0f,1,
             0.5f,-0.5f,0.0f,1,
             0.0f, 0.0f,0.0f,1,
        };
        const static constexpr float data_1[]{
            -0.5f,-0.5f,0.0f,1,
            0.5f,-0.5f,0.0f,1,
            0.0f, .25f,0.0f,1,
        };
        const static constexpr float data_2[]{
            -0.5f,-0.5f,0.0f,1,
            0.5f,-0.5f,0.0f,1,
            0.0f, 0.5f,0.0f,1,
        };

        glNamedBufferData(buffer[0],sizeof(data_0),data_0,GL_STATIC_DRAW);
        glNamedBufferData(buffer[1],sizeof(data_1),data_1,GL_STATIC_DRAW);
        glNamedBufferData(buffer[2],sizeof(data_2),data_2,GL_STATIC_DRAW);

        glVertexArrayVertexBuffer(vertexArrayObject,0,buffer[0],0,sizeof(float[4]));
        glVertexArrayVertexBuffer(vertexArrayObject,1,buffer[1],0,sizeof(float[4]));
        glVertexArrayVertexBuffer(vertexArrayObject,2,buffer[2],0,sizeof(float[4]));

        glEnableVertexArrayAttrib(vertexArrayObject,0);
        glVertexArrayAttribFormat(vertexArrayObject,0,4,GL_FLOAT,false,0);
        glVertexArrayAttribBinding(vertexArrayObject,0,rand()%3);

    }
Exemplo n.º 2
1
void SetupModel(HWND hwnd)
{
    RECT rect;
    GetClientRect(hwnd, &rect);

    // Vertex Buffer (position)
    GLuint positionLocation = 0;
    GLuint positionBindindex = 0;

    glCreateBuffers(1, &positionBuffer);

    glNamedBufferData(positionBuffer,
        sizeof(positions), positions, GL_STATIC_DRAW);

    // Vertex Buffer (uv)
    GLuint uvLocation = 1;
    GLuint uvBindindex = 1;

    glCreateBuffers(1, &uvBuffer);

    glNamedBufferData(uvBuffer,
        sizeof(uvs), uvs, GL_STATIC_DRAW);

    // Index Buffer

    glCreateBuffers(1, &indexBuffer);

    glNamedBufferData(indexBuffer,
        sizeof(indices), indices, GL_STATIC_DRAW);

    // Vertex Array
    glCreateVertexArrays(1, &vertexArray);

    glEnableVertexArrayAttrib(vertexArray, positionLocation);
    glVertexArrayAttribFormat(vertexArray, positionLocation,
        2, GL_FLOAT, GL_FALSE, 0);

    glVertexArrayAttribBinding(vertexArray, positionLocation,
        positionBindindex);
    glVertexArrayVertexBuffer(vertexArray, positionBindindex,
        positionBuffer, static_cast<GLintptr>(0), sizeof(GLfloat) * 2);

    glEnableVertexArrayAttrib(vertexArray, uvLocation);
    glVertexArrayAttribFormat(vertexArray, uvLocation,
        2, GL_FLOAT, GL_FALSE, 0);

    glVertexArrayAttribBinding(vertexArray, uvLocation,
        uvBindindex);
    glVertexArrayVertexBuffer(vertexArray, uvBindindex,
        uvBuffer, static_cast<GLintptr>(0), sizeof(GLfloat) * 2);

    glVertexArrayElementBuffer(vertexArray, indexBuffer);
}
Exemplo n.º 3
1
void MainComponent::start() {
  if (isRunning) {
    return;
  }

  static const float vertices[] =
  {
       0.25, -0.25, 0.5, 1.0,
      -0.25, -0.25, 0.5, 1.0,
       0.25,  0.25, 0.5, 1.0
  };

  Shader shader = Shader();
  glUseProgram(shader._program);

  GLuint vbo;

  glCreateBuffers(1, &vbo);
  glCreateVertexArrays(1, &_vao);

  glBindVertexArray(_vao);

  glNamedBufferStorage(vbo, sizeof(vertices), vertices, 0);
  glBindBuffer(GL_ARRAY_BUFFER, vbo);

  glVertexArrayVertexBuffer(_vao, 0, vbo, 0, 16);
  glVertexArrayAttribFormat(_vao, 0, 4, GL_FLOAT, GL_FALSE, 0);
  glVertexArrayAttribBinding(_vao, 0, 0);
  glEnableVertexArrayAttrib(_vao, 0);
  glEnableVertexAttribArray(0);

  run();
}
Exemplo n.º 4
0
	//??glVertexArrayVertexBuffers
	void _bindBuffer(
		GLuint bindingindex,
		GLuint buffer,
		GLintptr offset       /*包的起始偏置*/,
		GLsizei stride        /*包的大小*/,
		GLuint relativeoffset /*解包后的起始偏置*/,
		Size size             /*读取数据的大小*/,
		GLenum type           /*读取数据的类型*/,
		GLboolean normalized = false/*是否强制归一化*/
		) {
		//本函数假定glsl attribindex 与 buffer bindingindex 一致
		const GLuint & attribindex = bindingindex;
		//将buffer绑定
		this->enableVertexArrayAttrib(bindingindex);
		this->vertexArrayVertexBuffer(
			bindingindex, 
			buffer, 
			offset, 
			stride
			);
		//设定glsl如何从buffer中取数据
		this->vertexArrayAttribFormat(
			attribindex, 
			size, 
			type, 
			normalized, 
			relativeoffset
			);
		//bind glsl to loacal
		glVertexArrayAttribBinding(value_, attribindex, bindingindex);
	}
Exemplo n.º 5
0
    void startup()
    {
        rendering_program = compile_shaders();

        GLuint buffer[2];
        GLuint vao;

        static const GLfloat positions[] = { 0.25f, -0.25f, 0.5f, 1.0f,
                                            -0.25f, -0.25f, 0.5f, 1.0f,
                                             0.25f,  0.25f, 0.5f, 1.0f };

        static const GLfloat colors[] = { 1.0, 0.0, 0.0, 1.0,
                                          1.0, 0.0, 0.0, 1.0,
                                          1.0, 0.0, 0.0, 1.0 };
        
        // Create the vertex array object.
        glCreateVertexArrays(1, &vao);

        // Create two buffers.
        glCreateBuffers(2, &buffer[0]);

        // Initialize the first buffer.
        glNamedBufferStorage(buffer[0], sizeof(positions), positions, 0);

        // Bind it to the vertex array - offset zero, stride = sizeof(vec4)
        glVertexArrayVertexBuffer(vao, 0, buffer[0], 0, sizeof(vmath::vec4));

        // Tell OpenGL what the format of the attribute is.
        glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0);

        // Tell OpenGL which vertex buffer binding to use for this attribute.
        glVertexArrayAttribBinding(vao, 0, 0);

        // Enable the attribute.
        glEnableVertexArrayAttrib(vao, 0);

        // Perform similar initialization for the second buffer.
        glNamedBufferStorage(buffer[1], sizeof(colors), colors, 0);
        glVertexArrayVertexBuffer(vao, 1, buffer[1], 0, sizeof(vmath::vec4));
        glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, 0);
        glVertexArrayAttribBinding(vao, 1, 1);
        glEnableVertexArrayAttrib(vao, 1);

        glEnableVertexAttribArray(1);
        glBindVertexArray(vao);
    }
	bool initVertexArray()
	{
		glCreateVertexArrays(1, &VertexArrayName);

		glVertexArrayAttribBinding(VertexArrayName, semantic::attr::POSITION, 0);
		glVertexArrayAttribFormat(VertexArrayName, semantic::attr::POSITION, 2, GL_FLOAT, GL_FALSE, 0);
		glEnableVertexArrayAttrib(VertexArrayName, semantic::attr::POSITION);

		glVertexArrayAttribBinding(VertexArrayName, semantic::attr::TEXCOORD, 0);
		glVertexArrayAttribFormat(VertexArrayName, semantic::attr::TEXCOORD, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec2));
		glEnableVertexArrayAttrib(VertexArrayName, semantic::attr::TEXCOORD);

		glVertexArrayElementBuffer(VertexArrayName, BufferName[buffer::ELEMENT]);
		glVertexArrayVertexBuffer(VertexArrayName, 0, BufferName[buffer::VERTEX], 0, sizeof(glf::vertex_v2fv2f));

		return true;
	}
Exemplo n.º 7
0
bool SimpleUniformGL::InitScene()
{
	glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a);

	std::vector<GLuint> shaders;
	shaders.push_back(GLUtil::CreateShader(GL_VERTEX_SHADER, "SimpleUniformVert.glsl"));
	shaders.push_back(GLUtil::CreateShader(GL_FRAGMENT_SHADER, "SimpleUniformFrag.glsl"));
	shaderProgram = GLUtil::CreateProgram(shaders);
	for_each(shaders.begin(), shaders.end(), glDeleteShader);
	glUseProgram(shaderProgram);

	GLint overrideColor = glGetUniformLocation(shaderProgram, "overrideColor");
	glProgramUniform4f(shaderProgram, overrideColor, 1.0f, 0.0f, 0.0f, 1.0f);

	GLuint bindingPoint = 1;

	float colorsBlock[8] = { 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0, 1.0f };
	GLuint buffer;
	glGenBuffers(1, &buffer);
	glBindBuffer(GL_ARRAY_BUFFER, buffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(colorsBlock), colorsBlock, GL_DYNAMIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	glBindBufferBase(GL_UNIFORM_BUFFER, bindingPoint, buffer);

	GLuint blockIndex = glGetUniformBlockIndex(shaderProgram, "ColorBlock");
	glUniformBlockBinding(shaderProgram, blockIndex, bindingPoint);

	glCreateVertexArrays(1, &vao);
	glBindVertexArray(vao);

	glCreateBuffers(1, &vbo);
	glNamedBufferData(vbo, sizeof(Data::vertices), Data::vertices, GL_STATIC_DRAW);

	glVertexArrayAttribBinding(vao, 0, 0);
	glVertexArrayAttribBinding(vao, 1, 0);
	glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(Vertex2));

	glEnableVertexArrayAttrib(vao, 0);
	glEnableVertexArrayAttrib(vao, 1);
	glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, GL_FALSE, 0);
	glVertexArrayAttribFormat(vao, 1, 4, GL_FLOAT, GL_FALSE, sizeof(Vertex2().position));

	return true;
}
void glVertexFormatAttribGTC(GLuint vertexFormat, GLuint attribindex, GLuint bindingindex, GLint size, GLenum internalType, GLenum externalType, GLboolean normalized, GLuint relativeoffset)
{
	assert((internalType == GL_INT && normalized == GL_FALSE) || internalType == GL_FLOAT || (internalType == GL_DOUBLE && normalized == GL_FALSE));
	glEnableVertexArrayAttrib(vertexFormat, attribindex);
	glVertexArrayAttribBinding(vertexFormat, attribindex, bindingindex);
	if (internalType == GL_INT)
		glVertexArrayAttribIFormat(vertexFormat, attribindex, size, externalType, relativeoffset);
	else if (internalType == GL_DOUBLE)
		glVertexArrayAttribLFormat(vertexFormat, attribindex, size, externalType, relativeoffset);
	else
		glVertexArrayAttribFormat(vertexFormat, attribindex, size, externalType, normalized, relativeoffset);
}
Exemplo n.º 9
0
GLuint Mesh_Setup(GLuint shader_prog, struct Vertex *verts, GLuint num_verts)
{
	GLuint vao;
	glCreateVertexArrays(1, &vao);

	GLuint pos_attrib_loc = glGetAttribLocation(shader_prog, "pos");
	GLuint col_attrib_loc = glGetAttribLocation(shader_prog, "col");

	glVertexArrayAttribFormat(vao, pos_attrib_loc, 3, GL_FLOAT, GL_FALSE, (GLuint)offsetof(struct Vertex, pos));
	glVertexArrayAttribFormat(vao, col_attrib_loc, 4, GL_FLOAT, GL_FALSE, (GLuint)offsetof(struct Vertex, col));

	glVertexArrayAttribBinding(vao, pos_attrib_loc, 0);
	glVertexArrayAttribBinding(vao, col_attrib_loc, 0);

	glEnableVertexArrayAttrib(vao, pos_attrib_loc);
	glEnableVertexArrayAttrib(vao, col_attrib_loc);

	GLuint vbo;
	glCreateBuffers(1, &vbo);
	glNamedBufferStorage(vbo, sizeof(struct Vertex) * num_verts, verts, 0);

	glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(struct Vertex));
	return vao;
}
Exemplo n.º 10
0
 __ThisData(){
     glCreateVertexArrays(1,&vao);
     glCreateBuffers(1,&buffer);
     alignas(GLfloat) constexpr const static GLfloat data_[]{
             0,0.5f,0,1,
         -0.5f,-.5f,0,1,
          0.5f,-.5f,0,1,
     };
     glNamedBufferData(buffer,sizeof(data_),data_,GL_STATIC_DRAW);
     glEnableVertexArrayAttrib(vao,0);
     glVertexArrayVertexBuffer(vao,0,buffer,0,sizeof(data_)/3);
     glVertexArrayAttribBinding(vao,0,0);
     glVertexArrayAttribFormat(vao,0,4,GL_FLOAT,false,0);
     program = createProgram({
         {GL_VERTEX_SHADER,readGLSLFile("glsl:SimpleVertexArrayObject.v.vert") },
         {GL_FRAGMENT_SHADER,readGLSLFile("glsl:SimpleVertexArrayObject.f.frag")}
     });
 }
Exemplo n.º 11
0
void rnd_util_init() {
	glCreateVertexArrays(1, &cube_up_vao);
	glCreateBuffers(1, &cube_up_vbo_verts);
	glNamedBufferData(cube_up_vbo_verts, sizeof(cube_up_verts), cube_up_verts, GL_STATIC_DRAW);
	glVertexArrayAttribBinding(cube_up_vao, 0, 0);
	glVertexArrayVertexBuffer(cube_up_vao, 0, cube_up_vbo_verts, 0, 12);
	glEnableVertexArrayAttrib(cube_up_vao, 0);
	glVertexArrayAttribFormat(cube_up_vao, 0, 3, GL_FLOAT, GL_FALSE, 0);
	/*
	glCreateVertexArrays(1, &test_box_vao);
	glCreateBuffers(1, &test_box_vbo_verts);
	glNamedBufferData(test_box_vbo_verts, sizeof(test_box_verts), test_box_verts, GL_STATIC_DRAW);
	
	glVertexArrayAttribBinding(test_box_vao, 0, 0);
	glVertexArrayVertexBuffer(test_box_vao, 0, test_box_vbo_verts, 0, 108);
	glEnableVertexArrayAttrib(test_box_vao, 0);
	glVertexArrayAttribFormat(test_box_vao, 0, 3, GL_FLOAT, GL_FALSE, 0);
	*/
}
Exemplo n.º 12
0
    GLuint VertexAssembly::createInputState(const VertexLayout& layout)
    {
        GLuint vertexArray;
        glCreateVertexArrays(1, &vertexArray);
        m_vertexArrays.push_back(vertexArray);

        if (layout.indexBuffer != 0)
            glVertexArrayElementBuffer(vertexArray, layout.indexBuffer);

        for (const auto& vb : layout.vertexBuffers)
            glVertexArrayVertexBuffer(vertexArray, vb.first, 
                vb.second.id, vb.second.offset, vb.second.stride);

        for (const auto& attrib : layout.attributes)
        {
            glEnableVertexArrayAttrib(vertexArray, attrib.second.index);
            glVertexArrayAttribBinding(vertexArray, attrib.second.index, attrib.first);
            glVertexArrayAttribFormat(vertexArray, attrib.second.index, attrib.second.size,
                GL_FLOAT, GL_FALSE, attrib.second.offset);
        }

        return vertexArray;
    }
Exemplo n.º 13
0
	/*
	attribindex : from glsl
	bindingindex : from local
	*/
	void vertexArrayAttribBinding(UnsignedInteger attribindex, UnsignedInteger bindingindex) {
		glVertexArrayAttribBinding(value_, attribindex, bindingindex);
	}
Exemplo n.º 14
0
	void OGLRenderLayout::BindVertexStreams(ShaderObjectPtr const & so, GLuint vao) const
	{
		OGLShaderObjectPtr const & ogl_so = checked_pointer_cast<OGLShaderObject>(so);

		RenderEngine& re = Context::Instance().RenderFactoryInstance().RenderEngineInstance();
		uint32_t max_vertex_streams = re.DeviceCaps().max_vertex_streams;

		std::vector<char> used_streams(max_vertex_streams, 0);
		for (uint32_t i = 0; i < this->NumVertexStreams(); ++ i)
		{
			OGLGraphicsBuffer& stream(*checked_pointer_cast<OGLGraphicsBuffer>(this->GetVertexStream(i)));
			uint32_t const size = this->VertexSize(i);
			auto const & vertex_stream_fmt = this->VertexStreamFormat(i);

			if (glloader_GL_VERSION_4_5() || glloader_GL_ARB_direct_state_access())
			{
				glVertexArrayVertexBuffer(vao, i, stream.GLvbo(), this->StartVertexLocation() * size, size);
			}

			uint32_t elem_offset = 0;
			for (auto const & vs_elem : vertex_stream_fmt)
			{
				GLint attr = ogl_so->GetAttribLocation(vs_elem.usage, vs_elem.usage_index);
				if (attr != -1)
				{
					GLintptr offset = elem_offset + this->StartVertexLocation() * size;
					GLint const num_components = static_cast<GLint>(NumComponents(vs_elem.format));
					GLenum type;
					GLboolean normalized;
					OGLMapping::MappingVertexFormat(type, normalized, vs_elem.format);
					normalized = (((VEU_Diffuse == vs_elem.usage) || (VEU_Specular == vs_elem.usage)) && !IsFloatFormat(vs_elem.format)) ? GL_TRUE : normalized;

					BOOST_ASSERT(GL_ARRAY_BUFFER == stream.GLType());
					stream.Active(true);
					if (glloader_GL_VERSION_4_5() || glloader_GL_ARB_direct_state_access())
					{
						glVertexArrayAttribFormat(vao, attr, num_components, type, normalized, elem_offset);
						glVertexArrayAttribBinding(vao, attr, i);
						glEnableVertexArrayAttrib(vao, attr);
					}
					else if (glloader_GL_EXT_direct_state_access())
					{
						glVertexArrayVertexAttribOffsetEXT(vao, stream.GLvbo(), attr, num_components, type,
							normalized, size, offset);
						glEnableVertexArrayAttribEXT(vao, attr);
					}
					else
					{
						glVertexAttribPointer(attr, num_components, type, normalized, size, reinterpret_cast<GLvoid*>(offset));
						glEnableVertexAttribArray(attr);
					}

					used_streams[attr] = 1;
				}

				elem_offset += vs_elem.element_size();
			}
		}

		if (this->InstanceStream())
		{
			OGLGraphicsBuffer& stream(*checked_pointer_cast<OGLGraphicsBuffer>(this->InstanceStream()));

			uint32_t const instance_size = this->InstanceSize();
			BOOST_ASSERT(this->NumInstances() * instance_size <= stream.Size());

			if (glloader_GL_VERSION_4_5() || glloader_GL_ARB_direct_state_access())
			{
				glVertexArrayVertexBuffer(vao, this->NumVertexStreams(), stream.GLvbo(),
					this->StartInstanceLocation() * instance_size, instance_size);
				glVertexArrayBindingDivisor(vao, this->NumVertexStreams(), 1);
			}

			size_t const inst_format_size = this->InstanceStreamFormat().size();
			uint32_t elem_offset = 0;
			for (size_t i = 0; i < inst_format_size; ++ i)
			{
				VertexElement const & vs_elem = this->InstanceStreamFormat()[i];

				GLint attr = ogl_so->GetAttribLocation(vs_elem.usage, vs_elem.usage_index);
				if (attr != -1)
				{
					GLint const num_components = static_cast<GLint>(NumComponents(vs_elem.format));
					GLenum type;
					GLboolean normalized;
					OGLMapping::MappingVertexFormat(type, normalized, vs_elem.format);
					normalized = (((VEU_Diffuse == vs_elem.usage) || (VEU_Specular == vs_elem.usage)) && !IsFloatFormat(vs_elem.format)) ? GL_TRUE : normalized;
					GLintptr offset = elem_offset + this->StartInstanceLocation() * instance_size;

					BOOST_ASSERT(GL_ARRAY_BUFFER == stream.GLType());
					stream.Active(true);
					if (glloader_GL_VERSION_4_5() || glloader_GL_ARB_direct_state_access())
					{
						glVertexArrayAttribFormat(vao, attr, num_components, type, normalized, elem_offset);
						glVertexArrayAttribBinding(vao, attr, this->NumVertexStreams());
						glEnableVertexArrayAttrib(vao, attr);
					}
					else if (glloader_GL_EXT_direct_state_access())
					{
						glVertexArrayVertexAttribOffsetEXT(vao, stream.GLvbo(), attr, num_components, type,
							normalized, instance_size, offset);
						glEnableVertexArrayAttribEXT(vao, attr);
						glVertexAttribDivisor(attr, 1);
					}
					else
					{
						glVertexAttribPointer(attr, num_components, type, normalized, instance_size,
							reinterpret_cast<GLvoid*>(offset));
						glEnableVertexAttribArray(attr);
						glVertexAttribDivisor(attr, 1);
					}

					used_streams[attr] = 1;
				}

				elem_offset += vs_elem.element_size();
			}
		}

		for (GLuint i = 0; i < max_vertex_streams; ++ i)
		{
			if (!used_streams[i])
			{
				if (glloader_GL_VERSION_4_5() || glloader_GL_ARB_direct_state_access())
				{
					glDisableVertexArrayAttrib(vao, i);
				}
				else if (glloader_GL_EXT_direct_state_access())
				{
					glDisableVertexArrayAttribEXT(vao, i);
				}
				else
				{
					glDisableVertexAttribArray(i);
				}
			}
		}
	}
Exemplo n.º 15
0
void MainWindow::setObjectFileFormat(const QString & fileName) {
    auto off_=ObjectFileFormatNormalReader::read(fileName);
    if (off_) {
        glDeleteBuffers(1,&(thisData->vao_index));
        glDeleteBuffers(1,&(thisData->vao_buffer));
        glDeleteVertexArrays(1,&(thisData->vao));

        glCreateBuffers(1,&(thisData->vao_index));
        glCreateBuffers(1,&(thisData->vao_buffer));
        glCreateVertexArrays(1,&(thisData->vao));

        glNamedBufferData(
            thisData->vao_buffer,
            off_->points.size()*sizeof(off_->points[0]),
            off_->points.data(),
            GL_STATIC_DRAW
            );

        glNamedBufferData(
            thisData->vao_index,
            off_->faces.size()*sizeof(off_->faces[0]),
            off_->faces.data(),
            GL_STATIC_DRAW
            );

        typedef std::remove_reference_t<decltype(off_->points[0])> AttribType;
        /*0 position*/
        glEnableVertexArrayAttrib(thisData->vao,0);
        glVertexArrayVertexBuffer(thisData->vao,0,thisData->vao_buffer,0,sizeof(AttribType) );
        glVertexArrayAttribBinding(thisData->vao,0,0);
        glVertexArrayAttribFormat(thisData->vao,0,3,GL_FLOAT,false, offsetof(AttribType,point));

        /*1 normal*/
        glEnableVertexArrayAttrib(thisData->vao,1);
        glVertexArrayVertexBuffer(thisData->vao,1,thisData->vao_buffer,0,sizeof(AttribType) );
        glVertexArrayAttribBinding(thisData->vao,1,1);
        glVertexArrayAttribFormat(thisData->vao,1,3,GL_FLOAT,false, offsetof(AttribType,normal) );

        /*set the index buffer*/
        glVertexArrayElementBuffer(thisData->vao,thisData->vao_index);
        thisData->elements_size=off_->faces.size()*3;

        /*reset mvp*/
        const auto left_right=off_->xMax-off_->xMin;
        const auto up_down=off_->yMax-off_->yMin;
        const auto near_far=off_->zMax-off_->zMin;
        auto max_outer_=std::max({ left_right,up_down,near_far });

        max_outer_=1.65f/max_outer_;

        const auto scale_ =glm::scale(glm::mat4(),
            glm::vec3(max_outer_,max_outer_,max_outer_)
            );

        const auto translate_=glm::translate(glm::mat4(),
            glm::vec3(
            -(off_->xMax+off_->xMin)/2,
            -(off_->yMax+off_->yMin)/2,
            -(off_->zMax+off_->zMin)/2)
            );

        thisData->mvp= scale_ * translate_ ;
        thisData->normal_mvp= glm::transpose( glm::inverse( scale_ ) )  ;

        /*redraw*/
        updateGL();
    }
    else {
        qDebug().noquote()<<("read"+fileName+"object file format error!");
    }
}
Exemplo n.º 16
0
void ImageViewerPanel::initializeGL()
{
	//glewInit();

	glGetIntegerv(GL_MAJOR_VERSION, &ogl_ver_major);
	glGetIntegerv(GL_MINOR_VERSION, &ogl_ver_minor);

	shaderP = make_unique<GLSLProgram>(
		"resources/shaders/img_vs.glsl",
		"resources/shaders/img_fs.glsl");

	if (ogl_ver_major == 4 && ogl_ver_minor >= 5)
	{
		// DSA
		// Create VAO
		glCreateBuffers(1, &vbo);
		glNamedBufferData(vbo, sizeof(frame), frame, GL_STATIC_DRAW);
		
		// IBO
		GLuint indices[] = { 0,1,2,2,3,0 };
		glCreateBuffers(1, &ibo);
		glNamedBufferData(ibo, sizeof(indices), indices, GL_STATIC_DRAW);

		// VAO
		glCreateVertexArrays(1, &vao);
		glEnableVertexArrayAttrib(vao, 0);

		// Setup the formats
		glVertexArrayAttribFormat(vao, 0, 2, GL_FLOAT, GL_FALSE, 0);
		glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(float) * 2);
		glVertexArrayAttribBinding(vao, 0, 0);

		glVertexArrayElementBuffer(vao, ibo);

		// Setup textures
		int texSize = 4;
		glCreateTextures(GL_TEXTURE_2D, 1, &tex);
		glTextureParameteri(tex, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTextureParameteri(tex, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTextureParameteri(tex, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTextureParameteri(tex, GL_TEXTURE_MAG_FILTER, GL_NEAREST);

		glTextureStorage2D(tex, 1, GL_RGB32F, imgsize[0], imgsize[1]);
		if (texLen > 0)
		{
			glTextureSubImage2D(tex, 0, 0, 0, imgsize[0], imgsize[1], GL_RGB, GL_FLOAT, textures);
		}
		texHandle = glGetTextureHandleARB(tex);
		glMakeTextureHandleResidentARB(texHandle);
		
		
	}
	else
	{
		// Non-DSA
		glGenVertexArrays(1, &vao);
		glBindVertexArray(vao);
		// Bind UV values
		glGenBuffers(1, &vbo);
		glBindBuffer(GL_ARRAY_BUFFER, vbo);
		glBufferData(GL_ARRAY_BUFFER, sizeof(frame), frame, GL_STATIC_DRAW);
		glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, nullptr);
		glEnableVertexAttribArray(0);

		glBindVertexArray(0);
		glBindBuffer(GL_ARRAY_BUFFER, 0);
	}

}
void ReplayRenderWidget::initializeGL()
{
   static auto vertexCode = R"(
      #version 420 core
      in vec2 fs_position;
      in vec2 fs_texCoord;
      out vec2 vs_texCoord;

      out gl_PerVertex {
         vec4 gl_Position;
      };

      void main()
      {
         vs_texCoord = fs_texCoord;
         gl_Position = vec4(fs_position, 0.0, 1.0);
      })";

   static auto pixelCode = R"(
      #version 420 core
      in vec2 vs_texCoord;
      out vec4 ps_color;
      uniform sampler2D sampler_0;

      void main()
      {
         ps_color = texture(sampler_0, vs_texCoord);
      })";

   initializeOpenGLFunctions();

   // Create vertex program
   mVertexProgram = glCreateShaderProgramv(GL_VERTEX_SHADER, 1, &vertexCode);

   // Create pixel program
   mPixelProgram = glCreateShaderProgramv(GL_FRAGMENT_SHADER, 1, &pixelCode);
   glBindFragDataLocation(mPixelProgram, 0, "ps_color");

   // Create pipeline
   glGenProgramPipelines(1, &mPipeline);
   glUseProgramStages(mPipeline, GL_VERTEX_SHADER_BIT, mVertexProgram);
   glUseProgramStages(mPipeline, GL_FRAGMENT_SHADER_BIT, mPixelProgram);

   // (TL, TR, BR)    (BR, BL, TL)
   // Create vertex buffer
   static const GLfloat vertices[] = {
      -1.0f,  -1.0f,   0.0f, 1.0f,
      1.0f,  -1.0f,   1.0f, 1.0f,
      1.0f, 1.0f,   1.0f, 0.0f,

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

   glCreateBuffers(1, &mVertBuffer);
   glNamedBufferData(mVertBuffer, sizeof(vertices), vertices, GL_STATIC_DRAW);

   // Create vertex array
   glCreateVertexArrays(1, &mVertArray);

   auto fs_position = glGetAttribLocation(mVertexProgram, "fs_position");
   glEnableVertexArrayAttrib(mVertArray, fs_position);
   glVertexArrayAttribFormat(mVertArray, fs_position, 2, GL_FLOAT, GL_FALSE, 0);
   glVertexArrayAttribBinding(mVertArray, fs_position, 0);

   auto fs_texCoord = glGetAttribLocation(mVertexProgram, "fs_texCoord");
   glEnableVertexArrayAttrib(mVertArray, fs_texCoord);
   glVertexArrayAttribFormat(mVertArray, fs_texCoord, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat));
   glVertexArrayAttribBinding(mVertArray, fs_texCoord, 0);

   // Create texture sampler
   glGenSamplers(1, &mSampler);

   glSamplerParameteri(mSampler, GL_TEXTURE_WRAP_S, static_cast<int>(GL_CLAMP_TO_EDGE));
   glSamplerParameteri(mSampler, GL_TEXTURE_WRAP_T, static_cast<int>(GL_CLAMP_TO_EDGE));
   glSamplerParameteri(mSampler, GL_TEXTURE_MIN_FILTER, static_cast<int>(GL_LINEAR));
   glSamplerParameteri(mSampler, GL_TEXTURE_MAG_FILTER, static_cast<int>(GL_LINEAR));
}