forked from Groovounet/ogl-samples
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added gl-410-buffer-uniform-array sample
- Loading branch information
1 parent
351f6bb
commit 1b9c5e3
Showing
13 changed files
with
317 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#version 410 core | ||
#define FRAG_COLOR 0 | ||
#define MATERIAL 0 | ||
|
||
precision highp float; | ||
precision highp int; | ||
layout(std140, column_major) uniform; | ||
|
||
uniform material | ||
{ | ||
vec4 Diffuse[2]; | ||
} Material; | ||
|
||
in block | ||
{ | ||
flat int Instance; | ||
} In; | ||
|
||
layout(location = FRAG_COLOR, index = 0) out vec4 Color; | ||
|
||
void main() | ||
{ | ||
Color = Material.Diffuse[In.Instance]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#version 410 core | ||
|
||
#define POSITION 0 | ||
#define DRAW_ID 5 | ||
#define TRANSFORM0 1 | ||
#define TRANSFORM1 2 | ||
|
||
precision highp float; | ||
precision highp int; | ||
layout(std140, column_major) uniform; | ||
|
||
uniform transform | ||
{ | ||
mat4 MVP; | ||
} Transform[2]; | ||
|
||
out gl_PerVertex | ||
{ | ||
vec4 gl_Position; | ||
}; | ||
|
||
out block | ||
{ | ||
flat int Instance; | ||
} Out; | ||
|
||
layout(location = POSITION) in vec2 Position; | ||
layout(location = DRAW_ID) in int Instance; | ||
|
||
void main() | ||
{ | ||
Out.Instance = Instance; | ||
gl_Position = Transform[Instance].MVP * vec4(Position, 0.0, 1.0); | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
/////////////////////////////////////////////////////////////////////////////////// | ||
/// OpenGL Samples Pack (ogl-samples.g-truc.net) | ||
/// | ||
/// Copyright (c) 2004 - 2014 G-Truc Creation (www.g-truc.net) | ||
/// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
/// of this software and associated documentation files (the "Software"), to deal | ||
/// in the Software without restriction, including without limitation the rights | ||
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
/// copies of the Software, and to permit persons to whom the Software is | ||
/// furnished to do so, subject to the following conditions: | ||
/// | ||
/// The above copyright notice and this permission notice shall be included in | ||
/// all copies or substantial portions of the Software. | ||
/// | ||
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
/// THE SOFTWARE. | ||
/////////////////////////////////////////////////////////////////////////////////// | ||
|
||
#include "test.hpp" | ||
|
||
namespace | ||
{ | ||
char const * VERT_SHADER_SOURCE("gl-410/buffer-uniform-array.vert"); | ||
char const * FRAG_SHADER_SOURCE("gl-410/buffer-uniform-array.frag"); | ||
|
||
GLsizei const VertexCount(4); | ||
GLsizeiptr const PositionSize = VertexCount * sizeof(glm::vec2); | ||
glm::vec2 const PositionData[VertexCount] = | ||
{ | ||
glm::vec2(-1.0f,-1.0f) * 0.8f, | ||
glm::vec2( 1.0f,-1.0f) * 0.8f, | ||
glm::vec2( 1.0f, 1.0f) * 0.8f, | ||
glm::vec2(-1.0f, 1.0f) * 0.8f | ||
}; | ||
|
||
GLsizei const ElementCount(6); | ||
GLsizeiptr const ElementSize = ElementCount * sizeof(GLushort); | ||
GLushort const ElementData[ElementCount] = | ||
{ | ||
0, 1, 2, | ||
2, 3, 0 | ||
}; | ||
|
||
namespace buffer | ||
{ | ||
enum type | ||
{ | ||
VERTEX, | ||
INSTANCE, | ||
ELEMENT, | ||
TRANSFORM, | ||
MATERIAL, | ||
MAX | ||
}; | ||
}//namespace buffer | ||
}//namespace | ||
|
||
class instance : public test | ||
{ | ||
public: | ||
instance(int argc, char* argv[]) : | ||
test(argc, argv, "gl-410-buffer-uniform-array", test::CORE, 4, 1), | ||
VertexArrayName(0), | ||
ProgramName(0) | ||
{} | ||
|
||
private: | ||
std::array<GLuint, buffer::MAX> BufferName; | ||
GLuint ProgramName; | ||
GLuint VertexArrayName; | ||
GLint UniformBufferAlignment; | ||
GLint UniformInstance; | ||
|
||
bool initProgram() | ||
{ | ||
bool Validated = true; | ||
|
||
compiler Compiler; | ||
|
||
if(Validated) | ||
{ | ||
compiler Compiler; | ||
GLuint VertShaderName = Compiler.create(GL_VERTEX_SHADER, getDataDirectory() + VERT_SHADER_SOURCE, "--version 410 --profile core"); | ||
GLuint FragShaderName = Compiler.create(GL_FRAGMENT_SHADER, getDataDirectory() + FRAG_SHADER_SOURCE, "--version 410 --profile core"); | ||
|
||
ProgramName = glCreateProgram(); | ||
glAttachShader(ProgramName, VertShaderName); | ||
glAttachShader(ProgramName, FragShaderName); | ||
glLinkProgram(ProgramName); | ||
|
||
Validated = Validated && Compiler.check(); | ||
Validated = Validated && Compiler.checkProgram(ProgramName); | ||
} | ||
|
||
if(Validated) | ||
{ | ||
this->UniformInstance = glGetUniformLocation(ProgramName, "Instance"); | ||
|
||
GLint UniformMaterial = glGetUniformBlockIndex(ProgramName, "material"); | ||
GLint UniformTransform0 = glGetUniformBlockIndex(ProgramName, "transform[0]"); | ||
GLint UniformTransform1 = glGetUniformBlockIndex(ProgramName, "transform[1]"); | ||
|
||
glUniformBlockBinding(ProgramName, UniformMaterial, semantic::uniform::MATERIAL); | ||
glUniformBlockBinding(ProgramName, UniformTransform0, semantic::uniform::TRANSFORM0); | ||
glUniformBlockBinding(ProgramName, UniformTransform1, semantic::uniform::TRANSFORM1); | ||
} | ||
|
||
return Validated; | ||
} | ||
|
||
bool initVertexArray() | ||
{ | ||
glGenVertexArrays(1, &VertexArrayName); | ||
glBindVertexArray(VertexArrayName); | ||
glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]); | ||
glVertexAttribPointer(semantic::attr::POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0); | ||
glEnableVertexAttribArray(semantic::attr::POSITION); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::INSTANCE]); | ||
glVertexAttribIPointer(semantic::attr::DRAW_ID, 1, GL_INT, 0, 0); | ||
glVertexAttribDivisor(semantic::attr::DRAW_ID, 1); | ||
glEnableVertexAttribArray(semantic::attr::DRAW_ID); | ||
|
||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[buffer::ELEMENT]); | ||
glBindVertexArray(0); | ||
|
||
return true; | ||
} | ||
|
||
bool initBuffer() | ||
{ | ||
glGetIntegerv(GL_UNIFORM_BUFFER_OFFSET_ALIGNMENT, &this->UniformBufferAlignment); | ||
|
||
glGenBuffers(buffer::MAX, &BufferName[0]); | ||
|
||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, BufferName[buffer::ELEMENT]); | ||
glBufferData(GL_ELEMENT_ARRAY_BUFFER, ElementSize, ElementData, GL_STATIC_DRAW); | ||
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); | ||
|
||
glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::VERTEX]); | ||
glBufferData(GL_ARRAY_BUFFER, PositionSize, PositionData, GL_STATIC_DRAW); | ||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
|
||
int const Instance[] = {0, 1}; | ||
glBindBuffer(GL_ARRAY_BUFFER, BufferName[buffer::INSTANCE]); | ||
glBufferData(GL_ARRAY_BUFFER, sizeof(Instance), Instance, GL_STATIC_DRAW); | ||
glBindBuffer(GL_ARRAY_BUFFER, 0); | ||
|
||
std::size_t UniformBufferSize = std::max<std::size_t>(this->UniformBufferAlignment, sizeof(glm::mat4)) * 2; | ||
|
||
{ | ||
glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); | ||
glBufferData(GL_UNIFORM_BUFFER, UniformBufferSize, nullptr, GL_DYNAMIC_DRAW); | ||
glBindBuffer(GL_UNIFORM_BUFFER, 0); | ||
} | ||
|
||
{ | ||
glm::vec4 const Diffuse[2] = | ||
{ | ||
glm::vec4(1.0f, 0.5f, 0.0f, 1.0f), | ||
glm::vec4(0.0f, 0.5f, 1.0f, 1.0f) | ||
}; | ||
|
||
glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::MATERIAL]); | ||
glBufferData(GL_UNIFORM_BUFFER, sizeof(Diffuse), &Diffuse[0][0], GL_STATIC_DRAW); | ||
glBindBuffer(GL_UNIFORM_BUFFER, 0); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool begin() | ||
{ | ||
bool Validated = true; | ||
|
||
if(Validated) | ||
Validated = initProgram(); | ||
if(Validated) | ||
Validated = initBuffer(); | ||
if(Validated) | ||
Validated = initVertexArray(); | ||
|
||
glBindFramebuffer(GL_FRAMEBUFFER, 0); | ||
glDrawBuffer(GL_BACK); | ||
if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) | ||
return false; | ||
|
||
return Validated; | ||
} | ||
|
||
bool end() | ||
{ | ||
glDeleteVertexArrays(1, &VertexArrayName); | ||
glDeleteBuffers(buffer::MAX, &BufferName[0]); | ||
glDeleteProgram(ProgramName); | ||
|
||
return true; | ||
} | ||
|
||
bool render() | ||
{ | ||
glm::vec2 WindowSize(this->getWindowSize()); | ||
|
||
std::size_t UniformBufferOffset = std::max<std::size_t>(this->UniformBufferAlignment, sizeof(glm::mat4)); | ||
std::size_t UniformBufferRange = UniformBufferOffset * 2; | ||
|
||
{ | ||
glBindBuffer(GL_UNIFORM_BUFFER, BufferName[buffer::TRANSFORM]); | ||
glm::byte* Pointer = (glm::byte*)glMapBufferRange(GL_UNIFORM_BUFFER, 0, UniformBufferRange, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT); | ||
|
||
glm::mat4 const Projection = glm::perspective(glm::pi<float>() * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f); | ||
glm::mat4 const Model0 = glm::translate(glm::mat4(1.0f), glm::vec3( 1, 0, 0)); | ||
glm::mat4 const Model1 = glm::translate(glm::mat4(1.0f), glm::vec3(-1, 0, 0)); | ||
|
||
*reinterpret_cast<glm::mat4*>(Pointer + UniformBufferOffset * 0) = Projection * this->view() * Model0; | ||
*reinterpret_cast<glm::mat4*>(Pointer + UniformBufferOffset * 1) = Projection * this->view() * Model1; | ||
|
||
// Make sure the uniform buffer is uploaded | ||
glUnmapBuffer(GL_UNIFORM_BUFFER); | ||
glBindBuffer(GL_UNIFORM_BUFFER, 0); | ||
} | ||
|
||
glViewport(0, 0, static_cast<GLsizei>(WindowSize.x), static_cast<GLsizei>(WindowSize.y)); | ||
glClearBufferfv(GL_COLOR, 0, &glm::vec4(1.0f)[0]); | ||
|
||
glUseProgram(ProgramName); | ||
|
||
// Attach the buffer to UBO binding point semantic::uniform::MATERIAL | ||
glBindBufferBase(GL_UNIFORM_BUFFER, semantic::uniform::MATERIAL, BufferName[buffer::MATERIAL]); | ||
glBindVertexArray(VertexArrayName); | ||
|
||
// Attach the buffer to UBO binding point semantic::uniform::TRANSFORM0 | ||
glBindBufferRange(GL_UNIFORM_BUFFER, semantic::uniform::TRANSFORM0, BufferName[buffer::TRANSFORM], 0, sizeof(glm::mat4)); | ||
glBindBufferRange(GL_UNIFORM_BUFFER, semantic::uniform::TRANSFORM1, BufferName[buffer::TRANSFORM], UniformBufferOffset, sizeof(glm::mat4)); | ||
|
||
glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, ElementCount, GL_UNSIGNED_SHORT, nullptr, 1, 0, 0); | ||
glDrawElementsInstancedBaseVertexBaseInstance(GL_TRIANGLES, ElementCount, GL_UNSIGNED_SHORT, nullptr, 1, 0, 1); | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
int main(int argc, char* argv[]) | ||
{ | ||
int Error(0); | ||
|
||
instance Test(argc, argv); | ||
Error += Test(); | ||
|
||
return Error; | ||
} |