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(); }
bool MandelbrotDrawable::init() { if (!Drawable::init()) { return false; } // Setup drawable(s) glGenVertexArrays(1, &vao_); glBindVertexArray(vao_); { glGenBuffers(2, vbo_); // Location 0 --> vertex position glBindBuffer(GL_ARRAY_BUFFER, vbo_[0]); { glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), points, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexArrayAttrib(vao_, 0); } glBindBuffer(GL_ARRAY_BUFFER, 0); // Location 1: Vertex color glBindBuffer(GL_ARRAY_BUFFER, vbo_[1]); { glBufferData(GL_ARRAY_BUFFER, 12 * sizeof(GLfloat), colors, GL_STATIC_DRAW); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, nullptr); glEnableVertexArrayAttrib(vao_, 1); } glBindBuffer(GL_ARRAY_BUFFER, 0); } glBindVertexArray(0); return true; }
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; }
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 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); }
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; }
__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; }
BoxCellModel(const char* verfile, const char* fragfile) :vertextId(0), bufferId(0), shaderId(0), indexId(0){ GLfloat pos[24] = { 0.5, 0.5, 0.5, 0.5, 0.5, -0.5, -0.5, 0.5, -0.5, -0.5, 0.5, 0.5, // up face point 0.5, -0.5, 0.5, 0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, -0.5, 0.5 // back face point }; GLfloat colors[24] = { 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 normals[24] = { 0 }; GLfloat uvs[16] = { 1.0f, 0.0f, // 0 1.0f, 1.0f, // 1 0.0f, 1.0f, // 2 0.0f, 0.0f, // 3 0.0f, 0.0f, // 4 1.0f, 0.0f, // 5 1.0f, 1.0f, // 6 0.0f, 1.0f, // 7 }; GLushort indexs[36] = { 0, 1, 2, 2, 3, 0, // up 4, 6, 5, 4, 7, 6, // down 1, 5, 6, 6, 2, 1, // front 0, 3, 7, 7, 4, 0, // back 3, 2, 6, 6, 7, 3, // left 0, 4, 5, 5, 1, 0 // right }; //init VAO glGenVertexArrays(1, &vertextId); glBindVertexArray(vertextId); // init IBO glGenBuffers(1, &indexId); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexs), indexs,GL_STATIC_DRAW); // init VBO glGenBuffers(1, &bufferId); glBindBuffer(GL_ARRAY_BUFFER, bufferId); glBufferData(GL_ARRAY_BUFFER, sizeof(pos) + sizeof(colors) + sizeof(normals) + sizeof(uvs), 0, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(pos), pos); glBufferSubData(GL_ARRAY_BUFFER, sizeof(pos), sizeof(colors), colors); glBufferSubData(GL_ARRAY_BUFFER, sizeof(pos) + sizeof(colors), sizeof(normals), normals); glBufferSubData(GL_ARRAY_BUFFER, sizeof(pos) + sizeof(colors) + sizeof(normals), sizeof(uvs), uvs); glEnable(GL_TEXTURE_2D); glGenTextures(1, &textId); glBindTexture(GL_TEXTURE_2D, textId); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Linear Filtered glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int width = 0; int height = 0; GLenum format = 0; unsigned char* data = vtarga::load_targa("vase_plant.tga", format, width, height); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, data); // bind vectext attribute glEnableVertexArrayAttrib(vertextId, 0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexArrayAttrib(vertextId, 1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (void*)sizeof(pos)); glEnableVertexArrayAttrib(vertextId, 2); glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(pos) + sizeof(colors))); glEnableVertexArrayAttrib(vertextId, 3); glVertexAttribPointer(3, 2, GL_FLOAT, GL_FALSE, 0, (void*)(sizeof(pos) + sizeof(colors) + sizeof(normals))); ShaderInfo infos[] = { { GL_VERTEX_SHADER, verfile }, { GL_FRAGMENT_SHADER, fragfile }, { GL_NONE, nullptr } }; shaderId = LoadShaders(infos); worldMatrixLoc = glGetUniformLocation(shaderId, "worldMatrix"); viewMatrixLoc = glGetUniformLocation(shaderId, "viewMatrix"); projectMatrixLoc = glGetUniformLocation(shaderId, "projectMatrix"); texLoc = glGetUniformLocation(shaderId, "tex"); }
void enableVertexArrayAttrib(UnsignedInteger index) { glEnableVertexArrayAttrib(value_, index); }
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); } } } }
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(); }
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 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)); }
//[-------------------------------------------------------] //[ 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 } } }