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); }
//[-------------------------------------------------------] //[ Public methods ] //[-------------------------------------------------------] TextureBufferDsa::TextureBufferDsa(OpenGLRenderer &openGLRenderer, uint32_t numberOfBytes, Renderer::TextureFormat::Enum textureFormat, const void *data, Renderer::BufferUsage bufferUsage) : TextureBuffer(openGLRenderer) { if (openGLRenderer.getExtensions().isGL_ARB_direct_state_access()) { { // Buffer part // Create the OpenGL texture buffer glCreateBuffers(1, &mOpenGLTextureBuffer); // Upload the data // -> Usage: These constants directly map to "GL_ARB_vertex_buffer_object" and OpenGL ES 2 constants, do not change them glNamedBufferData(mOpenGLTextureBuffer, static_cast<GLsizeiptr>(numberOfBytes), data, static_cast<GLenum>(bufferUsage)); } { // Texture part // Create the OpenGL texture instance glCreateTextures(GL_TEXTURE_BUFFER_ARB, 1, &mOpenGLTexture); // Attach the storage for the buffer object to the buffer texture glTextureBuffer(mOpenGLTexture, Mapping::getOpenGLInternalFormat(textureFormat), mOpenGLTextureBuffer); } } else { // Create the OpenGL texture buffer glGenBuffersARB(1, &mOpenGLTextureBuffer); // Create the OpenGL texture instance glGenTextures(1, &mOpenGLTexture); // Buffer part // -> Upload the data // -> Usage: These constants directly map to "GL_ARB_vertex_buffer_object" and OpenGL ES 2 constants, do not change them glNamedBufferDataEXT(mOpenGLTextureBuffer, static_cast<GLsizeiptr>(numberOfBytes), data, static_cast<GLenum>(bufferUsage)); { // Texture part #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Backup the currently bound OpenGL texture GLint openGLTextureBackup = 0; glGetIntegerv(GL_TEXTURE_BINDING_BUFFER_ARB, &openGLTextureBackup); #endif // Make this OpenGL texture instance to the currently used one glBindTexture(GL_TEXTURE_BUFFER_ARB, mOpenGLTexture); // Attaches the storage for the buffer object to the active buffer texture // -> Sadly, there's no direct state access (DSA) function defined for this in "GL_EXT_direct_state_access" glTexBufferARB(GL_TEXTURE_BUFFER_ARB, Mapping::getOpenGLInternalFormat(textureFormat), mOpenGLTextureBuffer); #ifndef OPENGLRENDERER_NO_STATE_CLEANUP // Be polite and restore the previous bound OpenGL texture glBindTexture(GL_TEXTURE_BUFFER_ARB, static_cast<GLuint>(openGLTextureBackup)); #endif } } }
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 initBuffer() { bool Validated(true); GLint UniformBufferOffset(0); glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &UniformBufferOffset); GLint UniformBlockSize = glm::max(GLint(sizeof(glm::mat4)), UniformBufferOffset); glCreateBuffers(buffer::MAX, &BufferName[0]); glNamedBufferData(BufferName[buffer::ELEMENT], ElementSize, ElementData, GL_STATIC_DRAW); glNamedBufferData(BufferName[buffer::VERTEX], VertexSize, VertexData, GL_STATIC_DRAW); glNamedBufferData(BufferName[buffer::TRANSFORM], UniformBlockSize, nullptr, GL_DYNAMIC_DRAW); return Validated; }
VertexBuffer::VertexBuffer(GLfloat *data, GLsizei size, GLenum type) { glCreateBuffers(1, &m_BufferID); #ifdef TR_CURRENT glNamedBufferData(m_BufferID, size, data, type); #else bind(); glBufferData(GL_ARRAY_BUFFER, size, data, type); unbind(); #endif }
void UnmappableBuffer::resizeBuffer(GLsizeiptr size) { GLuint newBuffer; size = nextPowerOfTwo(size); glCreateBuffers(1, &newBuffer); glNamedBufferData(newBuffer, size, nullptr, mUsage); glCopyNamedBufferSubData(mId, newBuffer, 0, 0, mSize); glDeleteBuffers(1, &mId); mId = newBuffer; mSize = size; }
static void setup_ubos(void) { static const char *names[NUM_UBOS] = { "ub_pos_size", "ub_color", "ub_rot" }; int i; glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &alignment); printf("GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT = %d\n", alignment); if (test_buffer_offset) { printf("Testing buffer offset %d\n", alignment); } else { /* we use alignment as the offset */ alignment = 0; } glCreateBuffers(NUM_UBOS, buffers); for (i = 0; i < NUM_UBOS; i++) { GLint index, size; /* query UBO index */ index = glGetUniformBlockIndex(prog, names[i]); /* query UBO size */ glGetActiveUniformBlockiv(prog, index, GL_UNIFORM_BLOCK_DATA_SIZE, &size); printf("UBO %s: index = %d, size = %d\n", names[i], index, size); /* Allocate UBO */ glNamedBufferData(buffers[i], size + alignment, NULL, GL_DYNAMIC_DRAW); /* Attach UBO */ glBindBufferRange(GL_UNIFORM_BUFFER, i, buffers[i], alignment, /* offset */ size); glUniformBlockBinding(prog, index, i); if (!piglit_check_gl_error(GL_NO_ERROR)) piglit_report_result(PIGLIT_FAIL); } }
UnmappableBuffer::UnmappableBuffer(GLsizeiptr size, void *data, GLenum usage) : mSize(nextPowerOfTwo(size)), mUsage(usage) { glCreateBuffers(1, &mId); glNamedBufferData(mId, mSize, nullptr, mUsage); if(data != nullptr) { glNamedBufferSubData(mId, 0, size, data); mOffset = size; } else mOffset = 0; }
Buffer::Buffer(const Buffer & other) noexcept : gl::Object{other} { glCreateBuffers(1, &m_name); if (other.isValid()) { GLuint otherSize {other.getByteSize()}; glNamedBufferData(m_name, otherSize, nullptr, GL_STATIC_DRAW); glCopyNamedBufferSubData(other, m_name, 0, 0, otherSize); } }
void ofBufferObject::setData(GLsizeiptr bytes, const void * data, GLenum usage){ if(!this->data) return; this->data->size = bytes; #ifdef GLEW_ARB_direct_state_access if (GLEW_ARB_direct_state_access) { glNamedBufferData(this->data->id, bytes, data, usage); return; } #endif /// --------| invariant: direct state access is not available bind(this->data->lastTarget); glBufferData(this->data->lastTarget, bytes, data, usage); unbind(this->data->lastTarget); }
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 glBufferImpl::writeData(size_t offsetInBytes, size_t rangeInBytes, const bufferPtr data) { if (_mappedBuffer) { waitRange(offsetInBytes, rangeInBytes, true); std::memcpy(((Byte*)_mappedBuffer) + offsetInBytes, data, rangeInBytes); if (_useExplicitFlush) { glFlushMappedNamedBufferRange(_handle, offsetInBytes, rangeInBytes); } } else { clearData(offsetInBytes, rangeInBytes); if (offsetInBytes == 0 && rangeInBytes == _alignedSize) { glNamedBufferData(_handle, _alignedSize, data, _usage); } else { glNamedBufferSubData(_handle, offsetInBytes, rangeInBytes, data); } } }
__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 createAndAllocBuffer(GLsizeiptr bufferSize, GLenum usageMask, GLuint& bufferIdOut, const bufferPtr data, const char* name) { glCreateBuffers(1, &bufferIdOut); if (Config::ENABLE_GPU_VALIDATION) { glObjectLabel(GL_BUFFER, bufferIdOut, -1, name != nullptr ? name : Util::StringFormat("DVD_GENERAL_BUFFER_%d", bufferIdOut).c_str()); } assert(bufferIdOut != 0 && "GLUtil::allocBuffer error: buffer creation failed"); glNamedBufferData(bufferIdOut, bufferSize, data, usageMask); }
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); */ }
bool SuzanneGL::InitScene() { std::string s = (const char *)glGetString(GL_EXTENSIONS); OutputDebugString(s.c_str()); OutputDebugString("\n"); up = glm::vec3(0.0f, 1.0f, 0.0f); eye = glm::vec3(3.0f, 3.0f, 5.0f); right = glm::vec3(1.0f, 0.0f, 0.0f); center = glm::vec3(0.0f, 0.0f, 0.0f); // COMPILE SHADERS std::vector<GLuint> shadowShaders; shadowShaders.push_back(GLUtil::CreateShader(GL_VERTEX_SHADER, shaderPath + "ShadowMappingVert.glsl")); shadowShaders.push_back(GLUtil::CreateShader(GL_FRAGMENT_SHADER, shaderPath + "ShadowMappingFrag.glsl")); shadowShaderProgram = GLUtil::CreateProgram(shadowShaders); for_each(shadowShaders.begin(), shadowShaders.end(), glDeleteShader); shadowShaders.clear(); std::vector<GLuint> shaders; shaders.push_back(GLUtil::CreateShader(GL_VERTEX_SHADER, shaderPath + "TestSceneVert.glsl")); shaders.push_back(GLUtil::CreateShader(GL_FRAGMENT_SHADER, shaderPath + "TestSceneFrag.glsl")); shaderProgram = GLUtil::CreateProgram(shaders); for_each(shaders.begin(), shaders.end(), glDeleteShader); shaders.clear(); // PREPARE MODELS glm::mat4 matrix; matrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 1.0f, 0.0f)); models.push_back(ModelGL(modelPath + "monkey.bin", modelPath + "monkey2.mtl", matrix, true)); matrix = glm::mat4(1.0f); matrix = glm::scale(matrix, glm::vec3(5.0f, 5.0f, 5.0f)); matrix = glm::translate(matrix, glm::vec3(0.0f, 0.0f, 0.0f)); models.push_back(ModelGL(modelPath + "plane.bin", modelPath + "plane.mtl", matrix, true)); // PREPARE LIGHTING lighting.ambient = Vector4(0.1f, 0.1f, 0.1f, 1.0f); Light light1; light1.position = Vector4(-5.0f, 5.0f, 5.0f, 0.0f); light1.diffuse = Vector4(0.5f, 0.5f, 0.5f, 1.0f); light1.specular = Vector4(0.5f, 0.5f, 0.5f, 1.0f); Light light2; light2.position = Vector4(5.0f, 5.0f, 5.0f, 0.0f); light2.diffuse = Vector4(0.5f, 0.5f, 0.5f, 1.0f); light2.specular = Vector4(0.5f, 0.5f, 0.5f, 1.0f); lighting.lights[0] = light1; lighting.lights[1] = light2; GLuint lightBuffer; glCreateBuffers(1, &lightBuffer); glNamedBufferData(lightBuffer, sizeof(Lighting), &lighting, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, lightBindingPoint, lightBuffer); GLuint lightBlockIndex = glGetUniformBlockIndex(shaderProgram, "Lighting"); glUniformBlockBinding(shaderProgram, lightBlockIndex, lightBindingPoint); RenderShadowMaps(); // SETUP FRAMEBUFFER glClearColor(bgColor.r, bgColor.g, bgColor.b, bgColor.a); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); glFrontFace(GL_CCW); glCullFace(GL_BACK); glViewport(0, 0, mWidth, mHeight); // PREPARE MATERIAL BUFFER glCreateBuffers(1, &materialBuffer); glNamedBufferData(materialBuffer, sizeof(Material), NULL, GL_STATIC_DRAW); glBindBufferBase(GL_UNIFORM_BUFFER, materialBindingPoint, materialBuffer); GLuint materialBlockIndex = glGetUniformBlockIndex(shaderProgram, "Material"); glUniformBlockBinding(shaderProgram, materialBlockIndex, materialBindingPoint); // UPLOAD MVP MATRICES modelMatrixIndex = glGetUniformLocation(shaderProgram, "modelMatrix"); viewMatrixIndex = glGetUniformLocation(shaderProgram, "viewMatrix"); projectionMatrixIndex = glGetUniformLocation(shaderProgram, "projectionMatrix"); glm::mat4 viewMatrix = glm::lookAt(eye, center, up); glProgramUniformMatrix4fv(shaderProgram, viewMatrixIndex, 1, GL_FALSE, glm::value_ptr(viewMatrix)); glm::mat4 projectionMatrix = glm::perspective(glm::radians(60.0f), 800.0f / 800.0f, 1.0f, 500.0f); glProgramUniformMatrix4fv(shaderProgram, projectionMatrixIndex, 1, GL_FALSE, glm::value_ptr(projectionMatrix)); for (int i = 0; i < NUMBER_OF_LIGHTS; ++i) { glActiveTexture(GL_TEXTURE0 + i); glBindTexture(GL_TEXTURE_2D, shadowMaps[i]); } return true; }
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 glBufferData(GLenum target, GLsizeiptr size, const GLvoid* data, GLenum usage) { glNamedBufferData((GLuint) boundBuffer, size, data, usage); }
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 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)); }
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 BufferImplementation_DirectStateAccessARB::setData(const Buffer * buffer, GLsizeiptr size, const GLvoid * data, GLenum usage) const { glNamedBufferData(buffer->id(), static_cast<GLsizei>(size), data, usage); }
bool InitApp() { // ---------- OpenGL 설정 초기화 ---------- // 클리어 색상(배경색) 지정 glClearColor(1.f, 1.f, 1.f, 1.f); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); glFrontFace(GL_CCW); glPolygonMode(GL_FRONT, GL_FILL); glPolygonMode(GL_BACK, GL_LINE); // ---------- 셰이더 생성 및 컴파일 ---------- // 셰이더 파일 읽기 std::string vertShaderSource = ReadStringFromFile("BasicShader.glvs"); std::string fragShaderSource = ReadStringFromFile("BasicShader.glfs"); // 셰이더 오브젝트 생성 GLuint vertShaderObj = CreateShader(GL_VERTEX_SHADER, vertShaderSource); GLuint fragShaderObj = CreateShader(GL_FRAGMENT_SHADER, fragShaderSource); // 셰이더 프로그램 오브젝트 생성 gShaderProgram = glCreateProgram(); // 셰이더 프로그램에 버텍스 및 프래그먼트 셰이더 등록 glAttachShader(gShaderProgram, vertShaderObj); glAttachShader(gShaderProgram, fragShaderObj); // 셰이더 프로그램과 셰이더 링킹(일종의 컴파일) 그리고 확인 glLinkProgram(gShaderProgram); if (!CheckProgram(gShaderProgram)) { glDeleteProgram(gShaderProgram); return false; } // 사용된 셰이더 떼어냄 glDetachShader(gShaderProgram, vertShaderObj); glDetachShader(gShaderProgram, fragShaderObj); // 셰이더 삭제 glDeleteShader(vertShaderObj); glDeleteShader(fragShaderObj); // ---------- 정점 어트리뷰트 버퍼 생성 ---------- // 정점 정의 gVertices = { glm::vec3(-0.5f, -0.5f, 0.f), glm::vec3(0.5f, -0.5f, 0.f), glm::vec3(0.5f, 0.5f, 0.f) }; // OpenGL 4.5 이전의 버퍼 생성 방식. // 정점 버퍼 생성 //glGenBuffers(1, &gVertexBufferObject); // 정점 버퍼 바인딩 및 데이터 등록 //glBindBuffer(GL_ARRAY_BUFFER, gVertexBufferObject); //glBufferData(GL_ARRAY_BUFFER, gVertices.size()*sizeof(glm::vec3), &gVertices[0], GL_STATIC_DRAW); // OpenGL 4.5의 버퍼 생성 방식. (Direct State Access 기능으로 가능해짐) glCreateBuffers(1, &gVertexBufferObject); glNamedBufferData(gVertexBufferObject, gVertices.size()*sizeof(glm::vec3), &gVertices[0], GL_STATIC_DRAW); return true; }
void VertexBuffer::data(const std::size_t size, const void* data, const UsageHint usage) { glNamedBufferData(name, size, data, static_cast<GLenum>(usage)); }
Buffer::~Buffer() { if (isValid()) { glNamedBufferData(m_name, 0, nullptr, GL_STREAM_DRAW); glDeleteBuffers(1, &m_name); } }