Skip to content

Commit

Permalink
Switch screentri to a cube
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jan 13, 2024
1 parent 2a040e9 commit ab2d544
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
46 changes: 36 additions & 10 deletions 08-Cubemap/cubemap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,33 @@
struct Cubemap : public ExampleFramework {

RGLCommandBufferPtr commandBuffer;
RGLBufferPtr vertexBuffer;
RGLBufferPtr vertexBuffer, indBuffer;
RGLRenderPipelinePtr pipeline;
RGLRenderPassPtr renderPass;
RGLTexturePtr cubemapTexture;
RGLSamplerPtr sampler;

struct ubo {
glm::ivec2 screenDim;
glm::mat4 viewProj;
float timeSinceStart;
} ubo;

void sampleinit(int argc, char** argv) final {
vertexBuffer = device->CreateBuffer({
{.VertexBuffer = true},
sizeof(BasicObjects::ScreenTriangle::Vertex),
BasicObjects::ScreenTriangle::vertices,
sizeof(BasicObjects::Cube::Vertex),
BasicObjects::Cube::vertices,
RGL::BufferAccess::Private
});
vertexBuffer->SetBufferData(BasicObjects::ScreenTriangle::vertices);
vertexBuffer->SetBufferData(BasicObjects::Cube::vertices);

indBuffer = device->CreateBuffer({
{.IndexBuffer = true},
sizeof(BasicObjects::Cube::indices[0]),
BasicObjects::Cube::indices,
RGL::BufferAccess::Private
});
indBuffer->SetBufferData(BasicObjects::Cube::indices);


auto layout = device->CreatePipelineLayout({
Expand All @@ -43,7 +52,7 @@ struct Cubemap : public ExampleFramework {
},
},
.constants = {
{ubo, 0, RGL::StageVisibility::Fragment}
{ubo, 0, RGL::StageVisibility::Vertex}
}
});

Expand All @@ -61,13 +70,25 @@ struct Cubemap : public ExampleFramework {
.vertexConfig = {
.vertexBindings = {{
.binding = 0,
.stride = sizeof(BasicObjects::ScreenTriangle::Vertex),
.stride = sizeof(BasicObjects::Cube::Vertex),
}},
.attributeDescs = {
{
.location = 0,
.binding = 0,
.offset = offsetof(BasicObjects::ScreenTriangle::Vertex,pos),
.offset = offsetof(BasicObjects::Cube::Vertex,pos),
.format = RGL::VertexAttributeFormat::R32G32B32_SignedFloat,
},
{
.location = 1,
.binding = 0,
.offset = offsetof(BasicObjects::Cube::Vertex,normal),
.format = RGL::VertexAttributeFormat::R32G32B32_SignedFloat,
},
{
.location = 2,
.binding = 0,
.offset = offsetof(BasicObjects::Cube::Vertex,uv),
.format = RGL::VertexAttributeFormat::R32G32_SignedFloat,
},
}
Expand All @@ -83,7 +104,7 @@ struct Cubemap : public ExampleFramework {
.extent = {width, height}
},
.rasterizerConfig = {
.windingOrder = RGL::WindingOrder::Counterclockwise,
.windingOrder = RGL::WindingOrder::Clockwise,
},
.colorBlendConfig{
.attachments = {
Expand Down Expand Up @@ -164,6 +185,7 @@ struct Cubemap : public ExampleFramework {
}

void tick() final {

RGL::SwapchainPresentConfig presentConfig{
};

Expand All @@ -174,6 +196,8 @@ struct Cubemap : public ExampleFramework {
commandBuffer->Begin();
auto nextimg = swapchain->ImageAtIndex(presentConfig.imageIndex);
auto nextImgSize = nextimg->GetSize();

ubo.viewProj = camera.GenerateViewProjMatrix(nextImgSize.width, nextImgSize.height);

renderPass->SetAttachmentTexture(0, nextimg->GetDefaultView());

Expand All @@ -189,9 +213,10 @@ struct Cubemap : public ExampleFramework {
commandBuffer->BindRenderPipeline(pipeline);
commandBuffer->SetVertexBytes(ubo, 0);
commandBuffer->SetVertexBuffer(vertexBuffer);
commandBuffer->SetIndexBuffer(indBuffer);
commandBuffer->SetFragmentSampler(sampler, 0);
commandBuffer->SetFragmentTexture(cubemapTexture->GetDefaultView(), 1);
commandBuffer->Draw(3);
commandBuffer->DrawIndexed(std::size(BasicObjects::Cube::indices));

commandBuffer->EndRendering();
commandBuffer->End();
Expand All @@ -209,6 +234,7 @@ struct Cubemap : public ExampleFramework {
sampler.reset();
cubemapTexture.reset();
renderPass.reset();
indBuffer.reset();
vertexBuffer.reset();
pipeline.reset();
commandBuffer.reset();
Expand Down
6 changes: 4 additions & 2 deletions 08-Cubemap/cubemap.frag
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
layout(binding = 0) uniform sampler cubeSampler;
layout(binding = 1) uniform textureCube tex;


layout(location = 0) in vec3 inUVdir;
layout(location = 0) out vec4 outcolor;

void main(){

vec3 cubeColor = texture(samplerCube(tex,cubeSampler),vec3(0,0,1)).xyz;
vec3 cubeColor = texture(samplerCube(tex,cubeSampler), inUVdir).xyz;

outcolor = vec4(cubeColor,1);
}
}
28 changes: 25 additions & 3 deletions 08-Cubemap/cubemap.vert
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@

layout(location = 0) in vec2 position;
layout(push_constant) uniform UniformBufferObject{
mat4 viewProj;
float timeSinceStart;
} ubo;

layout(location = 0) in vec3 inPosition;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec2 inUV;

layout(location = 0) out vec3 outUVDir;

void main(){
gl_Position = vec4(position, 1,1);
}

float scaleFactor = 1;

mat4 model = mat4(
vec4(scaleFactor, 0.0, 0.0, 0.0),
vec4(0.0, scaleFactor, 0.0, 0.0),
vec4(0.0, 0.0, scaleFactor, 0.0),
vec4(0,0,0, 1.0)
);

vec4 worldpos = model * vec4(inPosition,1);

gl_Position = ubo.viewProj * worldpos;
outUVDir = inPosition;
}

0 comments on commit ab2d544

Please sign in to comment.