CS 419v/519v -- Winter Quarter 2023

Project #4

100 Points

Due: February 18

Instancing


This page was last updated: February 13, 2023


From the Protein Data Bank Your Program


Requirements

Project #4 is asking you to create a single sphere, but then use Vulkan Instancing to turn that into a 24-atom caffeine molecule.

Full Sample Code

Sample2019-P4.zip

vkuSphere( ) Code

The vkuSphere( ) code is already in the .zip file, but if you want to see it, look here:
vkuSphere.h
vkuSphere.cpp

Keyboard Keys

'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.

Orthographic Projection

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 );

The 24 Atoms in a Caffeine Molecule

Symbol Atomic Number X Y Z Radius Color
C 6 -1.7990.0220.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.5630.4040.763 0.77 Green
N 7 -0.7280.6941.163 0.70 Blue
C 6 -0.9641.7202.189 0.77 Green
N 7 1.7520.8961.139 0.70 Blue
C 6 2.7290.2870.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.0480.3090.996 0.66 Red
H 1 3.7860.4850.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.0882.6891.711 0.37 White
H 1 -0.1141.7562.868 0.37 White
H 1 -1.8641.4732.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.

Adding a Fifth Uniform Buffer and 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.

Example Code

--------------------------------------------------------------
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. );

--------------------------------------------------------------

Turn-in:

Use Teach to turn in your:

  1. Source code: .cpp, .vert, .frag
  2. A PDF with a title, your name, your email address, nice screen shots from your program, and the link to the video demonstrating that your project does what the requirements ask for.
  3. You can zip the source files, but do not put the PDF in the .zip file.

Grading:

FeaturePoints
Atom positions30
Atom colors30
Atom radii30
Toggle between perspective and orthographic10
Potential Total100