My goal is to create a dashed line using shaders. I've defined a one-dimensional texture, and I think it should be being applied, but all my lines end up black. I'm hoping there's something simple I'm just missing.
My line vertex shader:
#version 330 core
layout (location = 0) in vec2 iPosition;
uniform mat4 transform;
void main() {
gl_Position = transform * vec4(iPosition, 0.0, 1.0);
}
My geometry shader:
#version 330 core
layout(lines) in;
layout(line_strip, max_vertices=2) out;
out float texCoord;
void main()
{
gl_Position = gl_in[0].gl_Position;
texCoord = 0.0f;
EmitVertex();
gl_Position = gl_in[1].gl_Position;
texCoord = 1.0f;
EmitVertex();
}
My fragment shader:
#version 330 core
uniform sampler1D pattern;
layout (location = 0) out vec4 fragColor;
in float texCoord;
void main() {
fragColor = texture(pattern, texCoord);
}
My code:
int[] textures = new int[1];
gl4.glGenTextures(1, textures, 0);
int textureId = textures[0];
gl4.glActiveTexture(GL4.GL_TEXTURE0);
gl4.glBindTexture(GL4.GL_TEXTURE_1D, textureId);
ByteBuffer pixels = Buffers.newDirectByteBuffer(64 * 4 * 4);
float full = Float.MAX_VALUE;
float empty = 0f;
//opaque red.
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
//transparent green.
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
//opaque red
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
//transparent green.
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.putFloat(full);
pixels.putFloat(empty);
pixels.putFloat(empty);
pixels.flip();
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_WRAP_S, GL4.GL_REPEAT);
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_WRAP_T, GL4.GL_REPEAT);
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_MIN_FILTER, GL4.GL_NEAREST);
gl4.glTexParameteri(GL4.GL_TEXTURE_1D, GL4.GL_TEXTURE_MAG_FILTER, GL4.GL_NEAREST);
gl4.glTexImage1D(GL4.GL_TEXTURE_1D, 0, 4, 32, 0, GL.GL_RGBA, GL.GL_FLOAT, pixels);
for (MapLayerData mapLayerData : mapLayerDataList) {
List<Line> lines = mapLayerData.getLines();
for (int x = 0; x < lines.size(); x++){
Line line = lines.get(x);
ArrayList<Point2D.Float> points = line.getPoints();
for (Point2D.Float point : points) {
vertBuffer.putFloat((float) point.x);
vertBuffer.putFloat((float) point.y);
}
vertBuffer.flip();
int[] bufferPointers = new int[1];
gl4.glGenBuffers(1, bufferPointers, 0);
gl4.glBindBuffer(GL.GL_ARRAY_BUFFER, bufferPointers[0]);
gl4.glBufferData(GL.GL_ARRAY_BUFFER, (points.size() * 8), vertBuffer, GL.GL_DYNAMIC_DRAW);
gl4.glVertexAttribPointer(0, 2, GL.GL_FLOAT, false, 8, 0);
gl4.glEnableVertexAttribArray(0);
gl4.glUniformMatrix4fv(lineTransformPointer, 1, false, transform.getMatrix(), 0);
gl4.glUniform1i(patternPointer, 0);
gl4.glDrawArrays(GL.GL_LINE_STRIP, 0, points.size());
gl4.glBindBuffer(GL.GL_ARRAY_BUFFER, 0);
gl4.glDeleteBuffers(bufferPointers.length, bufferPointers, 0);
vertBuffer.clear();
}
}
gl4.glDeleteTextures(1, textures,0);
4 isn't a valid internalFormat for glTexImage1D() in a Core context.
There's no evidence you have a VAO bound. Those are required in a Core context.
No reason to GL_TEXTURE_WRAP_T on a 1D texture.
No clue what patternPointer is.
Related
I was trying to implement the Crytek SSAO technique based on a shader shown in the Appendix of the following paper:
Shader from paper:
However, when implementing and running the shader, it kept giving me this graining look and I cannot seem to identify what might have caused this particular issue.
My implementation of the shader:
#version 450
layout(binding = 3) uniform sampler2D texNoise;
layout(binding = 6) uniform sampler2D depthMap;
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 uvCoords;
layout(binding = 5) uniform UniformBufferObject {
mat4 model;
mat4 view;
mat4 proj;
float time;
}camera;
layout(binding = 4) uniform KernelSample {
vec3 samples[64];
mat4 projection;
vec4 camera_eye;
vec4 camera_direction;
float z_far;
}kernelsamples;
int kernelSize = 64;
layout(location = 0) out vec4 outColor;
vec4 ambient_occlusion;
float ec_depth(in vec2 tc)
{
float buffer_z = texture(depthMap, tc).x;
return camera.proj[3][2] / (-2.0 * buffer_z + 1.0 - camera.proj[2][2]);
}
const vec2 window = vec2(2560.0, 1440.0);
void main()
{
vec2 tc_depths = gl_FragCoord.xy / uvCoords;
float ec_depth_negated = -ec_depth(tc_depths);
vec3 wc_positions = kernelsamples.camera_eye.xyz + kernelsample.camera_direction * ec_depth_negated / kernelsamples.z_far;
ambient_occlusion.a = 0.0f;
const float radius = 10.0f;
const int samples = 64;
float projection_scale_xy = 1.0 / ec_depth_negated;
float projection_scale_z = 100.0 / kernelsamples.z_far * projection_scale_xy;
float scene_depth = texture(depthMap, tc_depths).x;
vec2 inverted_random_texture_size = 1.0 / vec2(textureSize(texNoise, 0));
vec2 tc_random_texture = gl_FragCoord.xy * inverted_random_texture_size;
vec3 random_direction = texture(texNoise, tc_random_texture).xyz;
random_direction = normalize(random_direction * 2.0 - 1.0);
for(int i = 0; i < samples; i++)
{
vec3 sample_random_direction = texture(texNoise, vec2(float(i) *
inverted_random_texture_size.x, float(i / textureSize(texNoise, 0).x) *
inverted_random_texture_size.y)).xyz;
sample_random_direction = sample_random_direction * 2.0 - 1.0;
sample_random_direction = reflect(sample_random_direction, random_direction);
vec3 tc_sample_pos = vec3(tc_depths.xy, scene_depth)
+ vec3(sample_random_direction.xy * projection_scale_xy,
sample_random_direction.z * scene_depth * projection_scale_z) * radius;
float sample_depth = texture(depthMap, tc_sample_pos.xy).x;
ambient_occlusion.a += float(sample_depth > tc_sample_pos.z);
}
ambient_occlusion.a /= float(kernelSize);
outColor = ambient_occlusion;
}
C++
// Projection
ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width /
(float)swapChainExtent.height, 0.1f, 1000.0f);
ubo.proj[1][1] *= -1;
KernelSample ks{};
.....
ks.cameraEye = glm::vec4(cameraPosition, 0.0f);
ks.cameraDirection = glm::vec4(cameraPosition + cameraCenter, 1.0f);
RenderPass
VkAttachmentDescription colorAttachment{};
colorAttachment.format = VK_FORMAT_R16_SFLOAT;
colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
colorAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
colorAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
colorAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
colorAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
colorAttachment.finalLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
VkAttachmentDescription depthAttachment = {};
depthAttachment.format = format;
depthAttachment.samples = VK_SAMPLE_COUNT_1_BIT;
depthAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
depthAttachment.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
depthAttachment.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
depthAttachment.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
depthAttachment.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
depthAttachment.finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentReference depthAttachmentRef{};
depthAttachmentRef.attachment = 1;
depthAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
VkAttachmentReference colorAttatchmentRef{};
colorAttatchmentRef.attachment = 0;
colorAttatchmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
VkSubpassDescription subpass{};
subpass.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass.colorAttachmentCount = 1;
subpass.pColorAttachments = &colorAttatchmentRef;
subpass.pDepthStencilAttachment = &depthAttachmentRef;
VkSubpassDependency dependency{};
dependency.srcSubpass = VK_SUBPASS_EXTERNAL;
dependency.dstSubpass = 0;
dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.srcAccessMask = 0;
dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT;
dependency.dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT | VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
std::array<VkAttachmentDescription, 2> attachments = { colorAttachment, depthAttachment };
VkRenderPassCreateInfo renderPassInfo{};
renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
renderPassInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
renderPassInfo.pAttachments = attachments.data();
renderPassInfo.subpassCount = 1;
renderPassInfo.pSubpasses = &subpass;
renderPassInfo.dependencyCount = 1;
renderPassInfo.pDependencies = &dependency;
Result
SSAO shader output
I’m working on my own game engine AND learning OpenGL in the process, and I have got stuck in Shadowmapping for longer than I would like to admit.
I have been following this guide: https://learnopengl.com/Advanced-Lighting/Shadows/Shadow-Mapping But I don’t get to draw any shadow in my plane.
This is how the code looks in my project:
Framebuffer for creating the depth texture:
glCreateFramebuffers(1, &m_framebufferID);
glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferID);glGenTextures(1, &depthMapID);
ui32 depthMapID;
glBindTexture(GL_TEXTURE_2D, depthMapID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 1024, 1024, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferID);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthMapID, 0);
ASSERT(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE, "Framebuffer is incomplete!");
glBindFramebuffer(GL_FRAMEBUFFER, 0);
Depth texture shader (ignore the unused vertex layout):
#type vertex
#version 330 core
layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec3 vertex_normal;
layout (location = 3) in vec3 vertex_tangent;
layout (location = 4) in vec2 vertex_texcoord;
uniform mat4 u_lightViewProjectionMatrix;
uniform mat4 u_worldTransformMatrix;
void main()
{
gl_Position = u_lightViewProjectionMatrix * (u_worldTransformMatrix * vec4(vertex_position, 1.0));
}
#type fragment
#version 330 core
// Ouput data
//layout(location = 0) out float fragmentdepth;
void main(){
// Not really needed, OpenGL does it anyway
//fragmentdepth = gl_FragCoord.z;
}
This is how I create the shadow map
if (s_castingShadowMeshes.Size() == 0)
return;
float shadowDistance = 100.0f;
glm::mat4 lightView = glm::lookAt(LightManager::GetDirectionalLight().direction * -shadowDistance, glm::vec3(0.0f), MathUtils::Vector3UnitY);
glm::mat4 lightProjection = glm::ortho(-shadowDistance, shadowDistance, -shadowDistance, shadowDistance, 0.0f, shadowDistance * 2.0f);
s_lightViewProjection = lightProjection * lightView;
glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferID);
glViewport(0, 0, 1024, 1024); //Shadow height and width is 1024
glDepthMask(GL_TRUE);
glClearDepth(1.0f);
glClearColor(color.r, color.g, color.b, color.a);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT););
for (ui32 i = 0; i < s_castingShadowMeshes.Size(); ++i)
{
s_depthMapShader->Bind();
s_depthMapShader->SetUniform(UNIFORM_LIGHT_SPACE_TRANSFORM, ShaderDataType::Mat4, &(s_lightViewProjection));
s_depthMapShader->SetUniform(UNIFORM_MODEL_SPACE_TRANSFORM, ShaderDataType::Mat4, &(s_castingShadowMeshes[i]->GetWorldTransform()[0][0]));
s_rendererPlatformInterface->DrawVertexArray(s_castingShadowMeshes[i]->GetVertexArray());
s_renderStats.drawCalls++;
s_depthMapShader->Unbind();
}
s_shadowFramebuffer->Unbind();
Up to this point, RenderDoc shows me that a depth texture is actually being generated:
Now, this is how rendering the plane looks like:
shader->Bind();
ui32 useShadowMapTex = 0;
if (receiveShadows)
{
useShadowMapTex = 1;
ui32 shadowMapSlot = (ui32)Material::TextureSlots::ShadowMap;
shader->SetUniform(UNIFORM_SHADOWMAP_TEX, ShaderDataType::Int, &shadowMapSlot);
s_shadowMapTex->Bind(shadowMapSlot);
}
shader->SetUniform(UNIFORM_USE_SHADOWMAP_TEX, ShaderDataType::Int, &useShadowMapTex);
shader->SetUniform(UNIFORM_MODEL_SPACE_TRANSFORM, ShaderDataType::Mat4, &(transform[0][0]));
shader->SetUniform(UNIFORM_VIEW_PROJECTION, ShaderDataType::Mat4, &(s_sceneData.viewProjectionMatrix));
shader->SetUniform(UNIFORM_CAMERA_POS, ShaderDataType::Float3, &(s_sceneData.cameraPosition));
shader->SetUniform(UNIFORM_DIR_LIGHT_DIRECTION, ShaderDataType::Float3, &(LightManager::GetDirectionalLight().direction));
shader->SetUniform(UNIFORM_DIR_LIGHT_AMBIENT,ShaderDataType::Float3, &(LightManager::GetDirectionalLight().ambientColor));
shader->SetUniform(UNIFORM_DIR_LIGHT_DIFUSSE, ShaderDataType::Float3, &(LightManager::GetDirectionalLight().diffuseColor));
shader->SetUniform(UNIFORM_DIR_LIGHT_SPECULAR, ShaderDataType::Float3, &(LightManager::GetDirectionalLight().specularColor));
s_rendererPlatformInterface->DrawVertexArray(vertexArray);
s_renderStats.drawCalls++;
s_renderStats.vertices += vertexArray->GetIndexBuffer()->GetCount();
The shader for the plane:
#type vertex
#version 330 core
layout (location = 0) in vec3 vertex_position;
layout (location = 1) in vec3 vertex_color;
layout (location = 2) in vec3 vertex_normal;
layout (location = 3) in vec3 vertex_tangent;
layout (location = 4) in vec2 vertex_texcoord;
out VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
vec4 FragPosLightSpace;
} vs_out;
uniform mat4 u_viewProjectionMatrix;
uniform mat4 u_worldTransformMatrix;
uniform mat4 u_lightViewProjectionMatrix;
void main()
{
vs_out.FragPos = vec3(u_worldTransformMatrix * vec4(vertex_position, 1.0));
vs_out.Normal = transpose(inverse(mat3(u_worldTransformMatrix))) * vertex_normal;
vs_out.TexCoords = vertex_texcoord;
vs_out.FragPosLightSpace = u_lightViewProjectionMatrix * vec4(vs_out.FragPos, 1.0);
gl_Position = u_viewProjectionMatrix* vec4(vs_out.FragPos, 1.0);
}
#type fragment
#version 330 core
out vec4 FragColor;
in VS_OUT {
vec3 FragPos;
vec3 Normal;
vec2 TexCoords;
vec4 FragPosLightSpace;
} fs_in;
struct DirectionalLight
{
vec3 direction;
vec3 ambientColor;
vec3 diffuseColor;
vec3 specularColor;
};
uniform DirectionalLight u_directionalLight;
uniform sampler2D u_shadowMapTex;
uniform vec3 u_cameraPos;
float ShadowCalculation(vec4 fragPosLightSpace)
{
// perform perspective divide
vec3 projCoords = fragPosLightSpace.xyz / fragPosLightSpace.w;
// transform to [0,1] range
projCoords = projCoords * 0.5 + 0.5;
// get closest depth value from light's perspective (using [0,1] range fragPosLight as coords)
float closestDepth = texture(u_shadowMapTex, projCoords.xy).r;
// get depth of current fragment from light's perspective
float currentDepth = projCoords.z;
// check whether current frag pos is in shadow
float shadow = currentDepth > closestDepth ? 1.0 : 0.0;
return shadow;
}
void main()
{
vec3 normal = normalize(fs_in.Normal);
vec3 lightColor = vec3(1.0);
// ambient
vec3 ambient = u_directionalLight.ambientColor;
// diffuse
vec3 lightDir = normalize(u_directionalLight.direction);
float diff = max(dot(lightDir, normal), 0.0);
vec3 diffuse = diff * lightColor;
// specular
vec3 viewDir = normalize(u_cameraPos - fs_in.FragPos);
float spec = 0.0;
vec3 halfwayDir = normalize(lightDir + viewDir);
spec = pow(max(dot(normal, halfwayDir), 0.0), 64.0);
vec3 specular = spec * lightColor;
// calculate shadow
float shadow = ShadowCalculation(fs_in.FragPosLightSpace);
vec3 lighting = (ambient + (1.0 - shadow) * (diffuse + specular));
FragColor = vec4(lighting, 1.0);
}
This is what i get:
According to RenderDoc, the shadow map texture is actually being passed in the shader, but it never gets drawn in the red plane:
I hope somebody can help me. Many thanks in advance.
p.d: this is my first post ever in StackOverflow, excuse me if i am violating any rule.
I need render to textrue in my project.
Pass1 : Draw a color square that color is (0.5,0.5,0.5,1.0) and render target is a texture.
Pass2 : Draw a textured square that use pass1's texture and render target is a surface.
Expected result:
But I got strange result as follow:
Create image using the following:
VkImage hImageTexture = VK_NULL_HANDLE;
VkImageCreateInfo imageCreateInfo = {0};
imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
imageCreateInfo.pNext = nullptr;
imageCreateInfo.flags = 0;
imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
imageCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
imageCreateInfo.extent.width = width;
imageCreateInfo.extent.height = height;
imageCreateInfo.extent.depth = 1;
imageCreateInfo.mipLevels = 1;
imageCreateInfo.arrayLayers = 1;
imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
imageCreateInfo.usage =
VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT |
VK_IMAGE_USAGE_TRANSFER_DST_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT;
imageCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
imageCreateInfo.queueFamilyIndexCount = 0;
imageCreateInfo.pQueueFamilyIndices = nullptr;
imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
//
VkResult res = vkCreateImage(hDevice , &imageCreateInfo , nullptr , &hImageTexture);
Create image view using the following:
VkImageView hImageViewTexture = VK_NULL_HANDLE;
VkImageViewCreateInfo imageViewCreateInfo = {0};
imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
imageViewCreateInfo.pNext = nullptr;
imageViewCreateInfo.flags = 0;
imageViewCreateInfo.image = hImageTexture;
imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
imageViewCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
imageViewCreateInfo.components.r = VK_COMPONENT_SWIZZLE_R;
imageViewCreateInfo.components.g = VK_COMPONENT_SWIZZLE_G;
imageViewCreateInfo.components.b = VK_COMPONENT_SWIZZLE_B;
imageViewCreateInfo.components.a = VK_COMPONENT_SWIZZLE_A;
imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageViewCreateInfo.subresourceRange.baseMipLevel = 0;
imageViewCreateInfo.subresourceRange.levelCount = 1;
imageViewCreateInfo.subresourceRange.baseArrayLayer = 0;
imageViewCreateInfo.subresourceRange.layerCount = 1;
VkResult res=vkCreateImageView(
hDevice,
&imageViewCreateInfo,
NULL,
&hImageViewTexture);
Render loop as follows:
//Pass1
//Clear texture color
vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
vkCmdClearColorImage();//Clear color(0.0 , 0.0 ,0.0 , 1.0)
vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
//Change texture's barrier
VkImageMemoryBarrier imageMemoryBarrierForOutput = {0};
imageMemoryBarrierForOutput.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imageMemoryBarrierForOutput.pNext = nullptr;
imageMemoryBarrierForOutput.srcAccessMask = 0;
imageMemoryBarrierForOutput.dstAccessMask =
VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
imageMemoryBarrierForOutput.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageMemoryBarrierForOutput.newLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
imageMemoryBarrierForOutput.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrierForOutput.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrierForOutput.image = hImageTexture;
imageMemoryBarrierForOutput.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageMemoryBarrierForOutput.subresourceRange.baseMipLevel = 0;
imageMemoryBarrierForOutput.subresourceRange.levelCount = 1;
imageMemoryBarrierForOutput.subresourceRange.baseArrayLayer = 0;
imageMemoryBarrierForOutput.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(
hCommandBuffer,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
0,
0,
nullptr,
0,
nullptr,
1,
&imageMemoryBarrierForOutput);
//draw
vkCmdBeginRenderPass();
vkCmdSetViewport();
vkCmdSetScissor();
vkCmdBindPipeline();
vkCmdBindDescriptorSets();
vkCmdBindVertexBuffers();
vkCmdBindIndexBuffer();
vkCmdDrawIndexed();
vkCmdEndRenderPass();
//
//Pass2
//Clear surface color
vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL
vkCmdClearColorImage();//Clear color(0.5 , 0.5 ,1.0 , 1.0)
vkCmdPipelineBarrier();//Transition layout to VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL
//Change texture's barrier
VkImageMemoryBarrier imageMemoryBarrierForInput = {0};
imageMemoryBarrierForInput.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
imageMemoryBarrierForInput.pNext = nullptr;
imageMemoryBarrierForInput.srcAccessMask = 0;
imageMemoryBarrierForInput.dstAccessMask =
VK_ACCESS_SHADER_READ_BIT |
VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
imageMemoryBarrierForInput.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
imageMemoryBarrierForInput.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
imageMemoryBarrierForInput.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrierForInput.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
imageMemoryBarrierForInput.image = hImageTexture;
imageMemoryBarrierForInput.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
imageMemoryBarrierForInput.subresourceRange.baseMipLevel = 0;
imageMemoryBarrierForInput.subresourceRange.levelCount = 1;
imageMemoryBarrierForInput.subresourceRange.baseArrayLayer = 0;
imageMemoryBarrierForInput.subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(
hCommandBuffer,
VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
VK_PIPELINE_STAGE_ALL_GRAPHICS_BIT,
0,
0,
nullptr,
0,
nullptr,
1,
&imageMemoryBarrierForInput);
//draw
vkCmdBeginRenderPass();
vkCmdSetViewport();
vkCmdSetScissor();
vkCmdBindPipeline();
vkCmdBindDescriptorSets();
vkCmdBindVertexBuffers();
vkCmdBindIndexBuffer();
vkCmdDrawIndexed();
vkCmdEndRenderPass();
Pass1 vertex shader:
#version 450
layout (location=0) in vec4 inPos;
layout (location=0) out vec4 outPos;
void main(void)
{
outPos = float4(inPos.xy , 0.0 , 1.0);
gl_Position = outPos;
}
Pass1 fragment shader:
#version 450
layout (location=0) in vec4 inPos;
layout (location=0) out vec4 outColor;
void main(void)
{
outColor = float4(0.5 , 0.5 , 0.5 , 1.0);
}
Pass2 vertex shader:
#version 450
layout (location=0) in vec4 inPos;
layout (location=1) in vec2 inUV;
//
layout (location=0) out vec4 outPos;
layout (location=1) out vec2 outUV;
//
void main(void)
{
outPos = inPos;
outUV = inUV;
//
gl_Position=outPos;
}
Pass2 fragment shader:
#version 450
//
layout (location=0) in vec4 inPos;
layout (location=1) in vec2 inUV;
//
layout (binding=1) uniform sampler2D inTex;
layout (location=0) out vec4 outColor;
void main(void)
{
outColor = texture(inTex , inUV);
}
If I change image tiling to VK_IMAGE_TILING_LINEAR then I get the following:
If change image tiling to VK_IMAGE_TILING_LINEAR without clear(pass1's clear), the result was correct!
What mistakes have I made?
EDIT:
I add some code that how i change texture's barrier in render loop .
EDIT:
I set dependency when create render pass as follow
VkSubpassDependency subpassDependency[2];
subpassDependency[0].srcSubpass = VK_SUBPASS_EXTERNAL;
subpassDependency[0].dstSubpass = 0;
subpassDependency[0].srcStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
subpassDependency[0].dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
subpassDependency[0].dstAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
subpassDependency[0].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
//
subpassDependency[1].srcSubpass = 0;
subpassDependency[1].dstSubpass = VK_SUBPASS_EXTERNAL;
subpassDependency[1].srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
subpassDependency[1].dstStageMask = VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
subpassDependency[1].srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
subpassDependency[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
subpassDependency[1].dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
but nothing to changed.
I'm trying implement diffuse lighting. My vertex and fragment shader looks like this:
#version 150
attribute vec3 position;
attribute vec3 color;
attribute vec3 normal;
out vec3 inColor;
out vec3 inNormal;
out vec3 inPosition;
uniform mat4 transform;
void main(void) {
gl_Position = transform * vec4(position, 1.0);
inColor = color;
inNormal = normal;
inPosition = position;
}
------------------------------------
#version 150
out vec4 outColor;
in vec3 inColor;
in vec3 inNormal;
in vec3 inPosition;
uniform vec3 lightPosition;
void main(void) {
vec3 lightVector = normalize(lightPosition - inPosition);
float brightness = clamp(dot(lightVector, inNormal), 0, 1);
outColor = vec4(brightness, brightness, brightness, 1.0f);
}
The question is: What am I suppose to do with the normals after I load them from OBJ file?
I am following this link:
http://sbcgamesdev.blogspot.in/2013/06/etc1-textures-loading-and-alpha.html
I have two texture, SpinWheel-hd.pkm(rgb) and SpinWheel1-hd.pkm (alpha). I am trying to load an ETC1 texture with alpha.
There resulting image comes white, where in according to the link, it should be my rgb texture with alpha. What am I doing wrong? I am using cocos2dx 2.2.5
Here's my loading code:
rgbTex = CCTextureCache::sharedTextureCache()->addETCImage("SpinWheel-hd.pkm");
MainScreen1 = CCSprite::create("SpinWheel-hd.pkm");
CCSize size1 = CCDirector::sharedDirector()->getWinSize();
MainScreen1->setPosition(ccp(size1.width/2+70, size1.height/2+70));
this->addChild(MainScreen1);
alphaTex = CCTextureCache::sharedTextureCache()->addETCImage("SpinWheel1-hd.pkm");
alphaTex->setAliasTexParameters();
CCGLProgram *shader = CCShaderCache::sharedShaderCache()->programForKey("monochromeShader");
shader->reset();
/ // Same code as before to initialize the shader
shader->initWithVertexShaderByteArray(cc_MonoChromeV, cc_MonoChrome);
MainScreen1->setShaderProgram(shader);
MainScreen1->getShaderProgram()->addAttribute(kCCAttributeNamePosition, kCCVertexAttrib_Position);
MainScreen1->getShaderProgram()->addAttribute(kCCAttributeNameTexCoord, kCCVertexAttrib_TexCoords);
MainScreen1->getShaderProgram()->link();
MainScreen1->getShaderProgram()->updateUniforms();
_maskLocation = glGetUniformLocation( MainScreen1->getShaderProgram()->getProgram(),"u_colorRampTexture");
glUniform1i(_maskLocation,1);
_textureLocation = glGetUniformLocation( MainScreen1->getShaderProgram()->getProgram(),"u_texture");
glUniform1i(_textureLocation, 0);
MainScreen1->getShaderProgram()->use();
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, rgbTex->getName());
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D,alphaTex->getName());
glActiveTexture(GL_TEXTURE0);
Here is my fragment shader
"\n\
#ifdef GL_ES \n\
precision mediump float;\n\
#endif\n\
uniform sampler2D u_texture;\n\
uniform sampler2D u_colorRampTexture;\n\
varying mediump vec2 v_texture;\n\
void main(void)\n\
{\n\
gl_FragColor = vec4(texture2D(u_texture, v_texture).rgb, texture2D(u_colorRampTexture, v_texture).r);\n\
}\n\
";
Here's my vertex shader:
"\n\
attribute vec4 a_position; \n\
attribute mediump vec2 a_texture;\n\
\n\
#ifdef GL_ES\n\
varying mediump vec2 v_texture;\n\
#else \n\
varying vec2 v_texture; \n\
#endif \n\
\n\
void main(void)\n\
{\n\
gl_Position = CC_MVPMatrix * a_position;\n\
v_texture = a_texture;\n\
}\n\
";
Where am I going wrong? All I see is a pure white image.