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); }
bool initVertexArray() { bool Validated(true); glCreateVertexArrays(1, &VertexArrayName); glVertexArrayElementBuffer(VertexArrayName, BufferName[buffer::ELEMENT]); return Validated; }
bool initVertexArray() { bool Validated(true); glCreateVertexArrays(pipeline::MAX, &VertexArrayName[0]); glVertexArrayElementBuffer(VertexArrayName[pipeline::RENDER], BufferName[buffer::ELEMENT]); return Validated; }
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; }
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; }
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!"); } }
void bindElementBuffer(const Buffer & buffer){ glVertexArrayElementBuffer( value_,(GLuint)buffer ); }