This page was last updated: February 13, 2023
From the Protein Data Bank | Your Program |
Project #4 is asking you to create a single sphere, but then use Vulkan Instancing to turn that into a 24-atom caffeine molecule.
The vkuSphere( ) code is already in the .zip file, but if you want to see it, look here:
vkuSphere.h |
vkuSphere.cpp |
'l' (ell) | Toggle lighting on and off |
'm' | Toggle display mode (textures vs. colors) |
'o' | Toggle between perspective and orthographic projection |
'r' | Toggle rotation-animation and using the mouse |
Esc, 'q' | Exit the program |
Feel free to add more keyboard inputs if you want.
Sometimes it is handy to be able to view the scene in an orthographic projection. This is true in this case in order to see if your scee matches the image on the left above. You already know how to do perspective in GLM. To use orthographic, you do this:
Scene.uProjection = glm::ortho( -5.f, 5.f, -5.f, 5.f, -1000.f, 1000.f );
Symbol | Atomic Number | X | Y | Z | Radius | Color |
---|---|---|---|---|---|---|
C | 6 | -1.799 | 0.022 | 0.602 | 0.77 | Green |
N | 7 | -1.586 | -0.945 | -0.363 | 0.70 | Blue |
C | 6 | -2.731 | -1.654 | -0.954 | 0.77 | Green |
C | 6 | -0.301 | -1.248 | -0.776 | 0.77 | Green |
C | 6 | 0.789 | -0.574 | -0.214 | 0.77 | Green |
C | 6 | 0.563 | 0.404 | 0.763 | 0.77 | Green |
N | 7 | -0.728 | 0.694 | 1.163 | 0.70 | Blue |
C | 6 | -0.964 | 1.720 | 2.189 | 0.77 | Green |
N | 7 | 1.752 | 0.896 | 1.139 | 0.70 | Blue |
C | 6 | 2.729 | 0.287 | 0.454 | 0.77 | Green |
N | 7 | 2.158 | -0.638 | -0.400 | 0.70 | Blue |
C | 6 | 2.871 | -1.524 | -1.331 | 0.77 | Green |
O | 8 | -0.101 | -2.186 | -1.712 | 0.66 | Red |
O | 8 | -3.048 | 0.309 | 0.996 | 0.66 | Red |
H | 1 | 3.786 | 0.485 | 0.553 | 0.37 | White |
H | 1 | -2.947 | -2.544 | -0.368 | 0.37 | White |
H | 1 | -2.491 | -1.941 | -1.976 | 0.37 | White |
H | 1 | -3.601 | -1.000 | -0.955 | 0.37 | White |
H | 1 | -1.088 | 2.689 | 1.711 | 0.37 | White |
H | 1 | -0.114 | 1.756 | 2.868 | 0.37 | White |
H | 1 | -1.864 | 1.473 | 2.748 | 0.37 | White |
H | 1 | 2.981 | -1.026 | -2.293 | 0.37 | White |
H | 1 | 2.305 | -2.444 | -1.462 | 0.37 | White |
H | 1 | 3.855 | -1.757 | -0.929 | 0.37 | White |
This is all nicely encapsulated for you in the file molecule.cpp which is already in the .zip file. But, to get this information into your vertex shader, you will need to place them in a new Uniform Buffer and then include it in the shader with a new Descriptor Set.
To make this work, you will need to add a fifth uniform buffer to hold the molecule information.
This means that you need to study and understand the Desciptor Set code.
To make this easier, I have surrounded everything that needs to be changed with the line:
//************************P4
Search for the phrase "P4" and you will find the places that have changed.
If you see question marks there ("?"), that shows that there is something that you still need to change.
--------------------------------------------------------------
In the global variables:
#define NUMDS 5
VkDescriptorSetLayout DescriptorSetLayouts[NUMDS];
VkDescriptorSet DescriptorSets[NUMDS];
. . .
#include "molecule.cpp"
. . .
MyBuffer MyAtomsUniformBuffer; // array of atom structs
--------------------------------------------------------------
In InitGraphics( ):
Init05UniformBuffer( sizeof(Atoms), &MyAtomsUniformBuffer );
Fill05DataBuffer( MyAtomsUniformBuffer, (void *) &Atoms );
MyVertexDataBuffer = vkuSphere( 1., 20, 20 );
--------------------------------------------------------------
In Init13DescriptorSetLayouts( ):
VkDescriptorSetLayoutBinding AtomsSet[1];
AtomsSet[0].binding = 0;
AtomsSet[0].descriptorType = ?? what type of buffer does this need to be ??
AtomsSet[0].descriptorCount = 1;
AtomsSet[0].stageFlags = ?? which shader will this need to be used in ??
AtomsSet[0].pImmutableSamplers = (VkSampler *)nullptr;
. . .
VkDescriptorSetLayoutCreateInfo vdslc4;
vdslc4.sType = ?? what type of structure is this ??
vdslc4.pNext = nullptr;
vdslc4.flags = 0;
vdslc4.bindingCount = 1;
vdslc4.pBindings = &AtomsSet[0];
. . .
result = vkCreateDescriptorSetLayout( LogicalDevice, &vdslc4, PALLOCATOR, OUT &DescriptorSetLayouts[4] );
--------------------------------------------------------------
In Init13DescriptorSets( ):
VkDescriptorBufferInfo vdbi4;
vdbi4.buffer = MyAtomsUniformBuffer.buffer;
vdbi4.offset = 0; // bytes
vdbi4.range = sizeof(Atoms);
. . .
VkWriteDescriptorSet vwds4;
vwds4.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
vwds4.pNext = nullptr;
vwds4.dstSet = DescriptorSets[4];
vwds4.dstBinding = 0;
vwds4.dstArrayElement = 0;
vwds4.descriptorCount = 1;
vwds4.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
vwds4.pBufferInfo = &vdbi4;
vwds4.pImageInfo = (VkDescriptorImageInfo *)nullptr;
vwds4.pTexelBufferView = (VkBufferView *)nullptr;
. . .
vkUpdateDescriptorSets( LogicalDevice, 1, IN &vwds4, IN copyCount, (VkCopyDescriptorSet *)nullptr );
--------------------------------------------------------------
In RenderScene( ):
vkCmdBindDescriptorSets( CommandBuffers[nextImageIndex], VK_PIPELINE_BIND_POINT_GRAPHICS, GraphicsPipelineLayout,
0, NUMDS, DescriptorSets, 0, (uint32_t *)nullptr );
. . .
const uint32_t instanceCount = NUMATOMS;
--------------------------------------------------------------
In UpdateScene( ):
Fill05DataBuffer( MyAtomsUniformBuffer, (void *) &Atoms ); // really only need to do this once...
--------------------------------------------------------------
In the vertex shader:
layout ( location = 0 ) out vec3 vColor;
. . .
struct atom
{
vec3 position;
int atomicNumber;
};
layout( std140, set = 4, binding = 0 ) uniform moleculeBuf
{
atom atoms[24];
};
. . .
int atomicNumber = atoms[?].atomicNumber;
vec3 position = atoms[?].position;
float radius;
if( atomicNumber == 1 )
{
?
?
} else if( atomicNumber == 6 )
{
?
?
}
else if( atomicNumber == 7 )
{
?
?
}
else if( atomicNumber == 8 )
{
?
?
}
else
{
radius = 0.75;
vColor = vec3( 1., 0., 1. ); // big magenta ball to tell us something is wrong
}
vec3 bVertex = aVertex;
bVertex.xyz *= radius;
bVertex.xyz += position;
gl_Position = PVM * vec4( bVertex, 1. );
--------------------------------------------------------------
Use Teach to turn in your:
Feature | Points |
---|---|
Atom positions | 30 |
Atom colors | 30 |
Atom radii | 30 |
Toggle between perspective and orthographic | 10 |
Potential Total | 100 |