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); }
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); }
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(); }
//??glVertexArrayVertexBuffer //void glVertexArrayVertexBuffer(GLuint vaobj, // GLuint bindingindex, // GLuint buffer, // GLintptr offset, // GLsizei stride); // 此函数用于解包 glBindVertexBuffer void vertexArrayVertexBuffer( GLuint bindingindex, GLuint buffer, GLintptr offset/*包的起始位置*/, GLsizei stride/*包的大小*/ ) { glVertexArrayVertexBuffer(value_, bindingindex, buffer, offset, stride); }
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; }
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; }
__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")} }); }
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); */ }
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; }
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; }
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); } } } }
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); } }
//[-------------------------------------------------------] //[ Public methods ] //[-------------------------------------------------------] VertexArrayVaoDsa::VertexArrayVaoDsa(OpenGLRenderer &openGLRenderer, const Renderer::VertexAttributes& vertexAttributes, uint32_t numberOfVertexBuffers, const Renderer::VertexArrayVertexBuffer *vertexBuffers, IndexBuffer *indexBuffer) : VertexArrayVao(openGLRenderer, numberOfVertexBuffers, vertexBuffers, indexBuffer) { // Vertex buffer reference handling is done within the base class "VertexArrayVao" const bool isARB_DSA = openGLRenderer.getExtensions().isGL_ARB_direct_state_access(); if (isARB_DSA) { // Create the OpenGL vertex array glCreateVertexArrays(1, &mOpenGLVertexArray); } else { // Create the OpenGL vertex array glGenVertexArrays(1, &mOpenGLVertexArray); } // Loop through all attributes // -> We're using "glBindAttribLocationARB()" when linking the program so we have known attribute locations (the vertex array can't know about the program) GLuint attributeLocation = 0; const Renderer::VertexAttribute *attributeEnd = vertexAttributes.attributes + vertexAttributes.numberOfAttributes; for (const Renderer::VertexAttribute *attribute = vertexAttributes.attributes; attribute < attributeEnd; ++attribute, ++attributeLocation) { // Set the OpenGL vertex attribute pointer // TODO(co) Add security check: Is the given resource one of the currently used renderer? const Renderer::VertexArrayVertexBuffer& vertexArrayVertexBuffer = vertexBuffers[attribute->inputSlot]; if (isARB_DSA) { // Enable attribute glEnableVertexArrayAttrib(mOpenGLVertexArray, attributeLocation); // Set up the format for my attribute glVertexArrayAttribFormat(mOpenGLVertexArray, attributeLocation, Mapping::getOpenGLSize(attribute->vertexAttributeFormat), Mapping::getOpenGLType(attribute->vertexAttributeFormat), static_cast<GLboolean>(Mapping::isOpenGLVertexAttributeFormatNormalized(attribute->vertexAttributeFormat)), static_cast<GLuint>(attribute->alignedByteOffset)); // Bind vertex buffer to buffer point glVertexArrayVertexBuffer(mOpenGLVertexArray, attributeLocation, static_cast<VertexBuffer*>(vertexArrayVertexBuffer.vertexBuffer)->getOpenGLArrayBuffer(), 0, // No offset to the first element of the buffer static_cast<GLsizei>(vertexArrayVertexBuffer.strideInBytes)); // Per-instance instead of per-vertex requires "GL_ARB_instanced_arrays" if (attribute->instancesPerElement > 0 && openGLRenderer.getExtensions().isGL_ARB_instanced_arrays()) { glVertexArrayBindingDivisor(mOpenGLVertexArray, attributeLocation, attribute->instancesPerElement); } } else { glVertexArrayVertexAttribOffsetEXT(mOpenGLVertexArray, static_cast<VertexBuffer*>(vertexArrayVertexBuffer.vertexBuffer)->getOpenGLArrayBuffer(), attributeLocation, Mapping::getOpenGLSize(attribute->vertexAttributeFormat), Mapping::getOpenGLType(attribute->vertexAttributeFormat), static_cast<GLboolean>(Mapping::isOpenGLVertexAttributeFormatNormalized(attribute->vertexAttributeFormat)), static_cast<GLsizei>(vertexArrayVertexBuffer.strideInBytes), static_cast<GLintptr>(attribute->alignedByteOffset)); // Per-instance instead of per-vertex requires "GL_ARB_instanced_arrays" if (attribute->instancesPerElement > 0 && openGLRenderer.getExtensions().isGL_ARB_instanced_arrays()) { // Sadly, DSA has no support for "GL_ARB_instanced_arrays", so, we have to use the bind way // -> Keep the bind-horror as local as possible #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Backup the currently bound OpenGL vertex array GLint openGLVertexArrayBackup = 0; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &openGLVertexArrayBackup); #endif // Bind this OpenGL vertex array glBindVertexArray(mOpenGLVertexArray); // Set divisor glVertexAttribDivisorARB(attributeLocation, attribute->instancesPerElement); #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Be polite and restore the previous bound OpenGL vertex array glBindVertexArray(static_cast<GLuint>(openGLVertexArrayBackup)); #endif } // Enable OpenGL vertex attribute array glEnableVertexArrayAttribEXT(mOpenGLVertexArray, attributeLocation); } } // Check the used index buffer // -> In case of no index buffer we don't bind buffer 0, there's not really a point in it if (nullptr != indexBuffer) { if (isARB_DSA) { // Bind the index buffer glVertexArrayElementBuffer(mOpenGLVertexArray, indexBuffer->getOpenGLElementArrayBuffer()); } else { // Sadly, EXT DSA has no support for element array buffer, so, we have to use the bind way // -> Keep the bind-horror as local as possible #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Backup the currently bound OpenGL vertex array GLint openGLVertexArrayBackup = 0; glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &openGLVertexArrayBackup); // Backup the currently bound OpenGL element array buffer GLint openGLElementArrayBufferBackup = 0; glGetIntegerv(GL_ELEMENT_ARRAY_BUFFER_BINDING_ARB, &openGLElementArrayBufferBackup); #endif // Bind this OpenGL vertex array glBindVertexArray(mOpenGLVertexArray); // Bind OpenGL element array buffer glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, indexBuffer->getOpenGLElementArrayBuffer()); #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Be polite and restore the previous bound OpenGL vertex array glBindVertexArray(static_cast<GLuint>(openGLVertexArrayBackup)); // Be polite and restore the previous bound OpenGL element array buffer glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, static_cast<GLuint>(openGLElementArrayBufferBackup)); #endif } } }
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!"); } }
int main(int /*argc*/, char ** /*argv*/) { BaseApp app; ProgramObject programEnv, program; auto mainWindow = app.getMainWindow(); PerspectiveCamera cam; OrbitManipulator manipulator(&cam); manipulator.setupCallbacks(app); manipulator.setZoom(3); GLuint diffuseTexture; GLuint specularTexture; GLuint vao; GLuint vaoEmpty; GLuint vbo; int32_t sphereSizeX = 20; int32_t sphereSizeY = 20; app.addInitCallback([&]() { std::string prefix = app.getResourceDir() + "Shaders/Tutorial/"; auto vs = compileShader(GL_VERTEX_SHADER, "#version 450\n", Loader::text(prefix + "uv.vp")); auto fs = compileShader(GL_FRAGMENT_SHADER, "#version 450\n", Loader::text(prefix + "lighting.vp"), Loader::text(prefix + "uv.fp")); program = createProgram(vs, fs); std::string texPrefix = app.getResourceDir() + "Textures/Tutorial/"; diffuseTexture = Loader::texture(texPrefix + "earth.png"); specularTexture = Loader::texture(texPrefix + "earth_s.png"); glCreateBuffers(1, &vbo); const uint32_t floatsPerVertex = 6; const uint32_t vertiesPerFace = 6; float*vertices = new float[sphereSizeX*sphereSizeY*vertiesPerFace*floatsPerVertex]; for (int32_t y = 0; y<sphereSizeY; ++y) { for (int32_t x = 0; x<sphereSizeX; ++x) { for (uint32_t k = 0; k<vertiesPerFace; ++k) { const int32_t xOffset[] = { 0,1,0,0,1,1 }; const int32_t yOffset[] = { 0,0,1,1,0,1 }; float u = (float)(x + xOffset[k]) / sphereSizeX; float v = (float)(y + yOffset[k]) / sphereSizeY; float xAngle = -u*glm::two_pi<float>(); float yAngle = v*glm::pi<float>(); uint32_t faceId = y*sphereSizeX + x; uint32_t faceVertex = faceId*vertiesPerFace + k; vertices[faceVertex*floatsPerVertex + 0] = glm::cos(xAngle)*glm::sin(yAngle); vertices[faceVertex*floatsPerVertex + 1] = -glm::cos(yAngle); vertices[faceVertex*floatsPerVertex + 2] = glm::sin(xAngle)*glm::sin(yAngle); vertices[faceVertex*floatsPerVertex + 3] = 1; vertices[faceVertex*floatsPerVertex + 4] = u; vertices[faceVertex*floatsPerVertex + 5] = v; } } } glNamedBufferData(vbo, sizeof(float)*sphereSizeX*sphereSizeY*vertiesPerFace*floatsPerVertex, vertices, GL_STATIC_DRAW); delete[]vertices; glCreateVertexArrays(1, &vao); glEnableVertexArrayAttrib(vao, 0); glVertexArrayAttribFormat(vao, 0, 4, GL_FLOAT, 0, 0); glVertexArrayVertexBuffer(vao, 0, vbo, 0, sizeof(float)*floatsPerVertex); glEnableVertexArrayAttrib(vao, 1); glVertexArrayAttribFormat(vao, 1, 2, GL_FLOAT, 0, 0); glVertexArrayVertexBuffer(vao, 1, vbo, sizeof(float) * 4, sizeof(float)*floatsPerVertex); glClearColor(0, 0, 0, 1); glDisable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS); }); app.addResizeCallback([&](int w, int h) { glViewport(0, 0, w, h); cam.setAspect(float(w) / float(h)); }); app.addDrawCallback([&]() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); program.use(); glBindTextureUnit(0, diffuseTexture); glBindTextureUnit(1, specularTexture); program.setMatrix4fv("p", value_ptr(cam.getProjection())); program.setMatrix4fv("v", value_ptr(cam.getView())); glBindVertexArray(vao); glDrawArrays(GL_TRIANGLES, 0, sphereSizeX*sphereSizeY * 6); glBindVertexArray(0); }); return app.run(); }