Recent

Author Topic: Drawing a cubemap onto a cube not working  (Read 1408 times)

deadbeef

  • New Member
  • *
  • Posts: 17
Drawing a cubemap onto a cube not working
« on: February 18, 2022, 04:30:33 pm »
4.5.14802 Core Profile/Debug Context 22.1.2 30.0.14023.3004
glDebugMessageCallback reports nothing

I'm trying to render a cubemap onto a simple cube but it stays black.
2D array textures are showing up so texture code should be fine, glBindTextureUnit is used for binding.

Shoudn't the normal of a vertex point to the desired face of the cubemap?

Vertex
Code: glSlang  [Select][+][-]
  1. #version 450 core
  2.  
  3.  
  4. // https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)
  5. layout(location = 0) in vec4 in_position;
  6. layout(location = 1) in vec4 in_normals;
  7. layout(location = 2) in vec4 in_tex_coord;
  8. layout(location = 3) in vec4 in_color;
  9.  
  10.  
  11. uniform mat4  transform;
  12. uniform float block_size;
  13.  
  14.  
  15. out vec4 TexCoord;
  16. out vec4 TexColor;
  17.  
  18.  
  19. void main() {
  20.         gl_Position = transform * vec4(vec3(in_position.xyz) * block_size, 1.0);
  21.         TexCoord    = in_normals;
  22.         //TexCoord    = in_tex_coord;
  23.         TexColor    = in_color.bgra;
  24. }
  25.  

Fragment
Code: glSlang  [Select][+][-]
  1. #version 450 core
  2.  
  3.  
  4. #pragma tmu0 "Texture0"
  5. uniform samplerCube Texture0;
  6.  
  7.  
  8. in  vec4 TexCoord;
  9. in  vec4 TexColor;
  10. out vec4 FragColor;
  11.  
  12.  
  13. void main() {
  14.         FragColor = texture(Texture0, -TexCoord.xyz);// * TexColor;
  15.         //FragColor = TexColor;
  16. }
  17.  

Cube code
Code: Pascal  [Select][+][-]
  1. unit cubesidestest_min;
  2.  
  3. {$mode Delphi}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, XIntVector, GR32, dglOpenGL;
  9.  
  10.  
  11. type
  12.   PYCubeVertex            = ^TYCubeVertex;
  13.   TYCubeVertex            = packed record // 20 byte
  14.     Position              : TXIntVector8;
  15.     Normal                : TXIntVector8;
  16.     TexCoord              : TXIntVector16;
  17.     Color                 : TColor32Entry; // BGRA
  18.   end;
  19.  
  20.   PYCubeBlockDataIbo = ^TYCubeBlockDataIbo;
  21.   TYCubeBlockDataIbo = packed array[0..36] of GLuint;
  22.  
  23.  
  24. type
  25.   TYCubeChunkMin = class
  26.     strict private
  27.       var
  28.         FData         : PYCubeVertex;
  29.         FDataIbo      : PYCubeBlockDataIbo;
  30.         FVisibleBlocks: GLint;
  31.  
  32.         FVao          : GLuint;
  33.         FVbo          : GLuint;
  34.         FIbo          : GLuint;
  35.  
  36.       procedure   InitVertexData;
  37.       procedure   UpdateIbo;
  38.  
  39.     public
  40.       constructor Create;
  41.       destructor  Destroy; override;
  42.  
  43.       procedure   Draw;
  44.  
  45.   end;
  46.  
  47.  
  48.  
  49. implementation
  50.  
  51.  
  52.  
  53. constructor TYCubeChunkMin.Create;
  54. begin
  55.   glCreateVertexArrays(1, @FVao);
  56.   glCreateBuffers(1, @FVbo);
  57.   glCreateBuffers(1, @FIbo);
  58.  
  59.   // bind vbo to vao
  60.   glVertexArrayVertexBuffer(FVao, 0, FVbo, 0, SizeOf(TYCubeVertex));
  61.  
  62.   // map attributes to vbo
  63.   glVertexArrayAttribBinding(FVao, 0, 0);
  64.   glVertexArrayAttribBinding(FVao, 1, 0);
  65.   glVertexArrayAttribBinding(FVao, 2, 0);
  66.   glVertexArrayAttribBinding(FVao, 3, 0);
  67.  
  68.   // enable attributes
  69.   glEnableVertexArrayAttrib(FVao, 0);
  70.   glEnableVertexArrayAttrib(FVao, 1);
  71.   glEnableVertexArrayAttrib(FVao, 2);
  72.   glEnableVertexArrayAttrib(FVao, 3);
  73.  
  74.   // define attributes
  75.   glVertexArrayAttribFormat(FVao, 0, 4, GL_BYTE         , GL_FALSE, GLuint(@TYCubeVertex(nil^).Position));
  76.   glVertexArrayAttribFormat(FVao, 1, 4, GL_BYTE         , GL_FALSE, GLuint(@TYCubeVertex(nil^).Normal));
  77.   glVertexArrayAttribFormat(FVao, 2, 2, GL_SHORT        , GL_TRUE , GLuint(@TYCubeVertex(nil^).TexCoord));
  78.   glVertexArrayAttribFormat(FVao, 3, 4, GL_UNSIGNED_BYTE, GL_TRUE , GLuint(@TYCubeVertex(nil^).Color));
  79.  
  80.   // add ibo
  81.   glVertexArrayElementBuffer(FVao, FIbo);
  82.  
  83.   InitVertexData;
  84.   UpdateIbo;
  85. end;
  86.  
  87.  
  88. destructor TYCubeChunkMin.Destroy;
  89. begin
  90.   glUnmapNamedBuffer(FIbo);
  91.   glUnmapNamedBuffer(FVbo);
  92.  
  93.   glDeleteBuffers     (1, @FIbo);
  94.   glDeleteBuffers     (1, @FVbo);
  95.   glDeleteVertexArrays(1, @FVao);
  96. end;
  97.  
  98.  
  99. procedure TYCubeChunkMin.InitVertexData;
  100. var x, y, z, i: NativeInt;
  101.     p         : PYCubeVertex;
  102. begin
  103.   // allocate
  104.   glNamedBufferStorage(
  105.     FVbo,
  106.     SizeOf(TYCubeVertex) * 8,
  107.     nil,
  108.     GL_DYNAMIC_STORAGE_BIT  or GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT
  109.     );
  110.  
  111.   // map
  112.   FData:= PYCubeVertex(glMapNamedBufferRange(
  113.     FVbo,
  114.     0,
  115.     SizeOf(TYCubeVertex) * 8,
  116.     GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT
  117.     ));
  118.  
  119.  
  120.   p:= FData;
  121.  
  122.   // 0
  123.   p^.Position   := TXIntVector8.Create( 0,  0,  0, 0);
  124.   p^.Normal     := TXIntVector8.Create(-1, -1, -1, 0);
  125.   p^.TexCoord   := TXIntVector16.Create(
  126.     High(Int16),
  127.     High(Int16),
  128.     High(Int16),
  129.     0);
  130.   p^.Color.ARGB := Color32(0, 0, 0);
  131.   Inc(p);
  132.  
  133.   // 1
  134.   p^.Position   := TXIntVector8.Create( 1,  0,  0, 0);
  135.   p^.Normal     := TXIntVector8.Create( 1, -1, -1, 0);
  136.   p^.TexCoord   := TXIntVector16.Create(
  137.     Low(Int16),
  138.     High(Int16),
  139.     High(Int16),
  140.     0);
  141.   p^.Color.ARGB := Color32(255, 0, 0);
  142.   Inc(p);
  143.  
  144.   // 2
  145.   p^.Position   := TXIntVector8.Create( 0,  1,  0, 0);
  146.   p^.Normal     := TXIntVector8.Create(-1,  1, -1, 0);
  147.   p^.TexCoord   := TXIntVector16.Create(
  148.     High(Int16),
  149.     Low(Int16),
  150.     High(Int16),
  151.     0);
  152.   p^.Color.ARGB := Color32(0, 255, 0);
  153.   Inc(p);
  154.  
  155.   // 3
  156.   p^.Position   := TXIntVector8.Create( 1,  1,  0, 0);
  157.   p^.Normal     := TXIntVector8.Create( 1,  1, -1, 0);
  158.   p^.TexCoord   := TXIntVector16.Create(
  159.     Low(Int16),
  160.     Low(Int16),
  161.     High(Int16),
  162.     0);
  163.   p^.Color.ARGB := Color32(255, 255, 0);
  164.   Inc(p);
  165.  
  166.  
  167.   // 4
  168.   p^.Position   := TXIntVector8.Create( 0,  0,  1, 0);
  169.   p^.Normal     := TXIntVector8.Create(-1, -1,  1, 0);
  170.   p^.TexCoord   := TXIntVector16.Create(
  171.     High(Int16),
  172.     High(Int16),
  173.     Low(Int16),
  174.     0);
  175.   p^.Color.ARGB := Color32(0, 0, 255);
  176.   Inc(p);
  177.  
  178.   // 5
  179.   p^.Position   := TXIntVector8.Create( 1,  0,  1, 0);
  180.   p^.Normal     := TXIntVector8.Create( 1, -1,  1, 0);
  181.   p^.TexCoord   := TXIntVector16.Create(
  182.     Low(Int16),
  183.     High(Int16),
  184.     Low(Int16),
  185.     0);
  186.   p^.Color.ARGB := Color32(255, 0, 255);
  187.   Inc(p);
  188.  
  189.   // 6
  190.   p^.Position   := TXIntVector8.Create( 0,  1,  1, 0);
  191.   p^.Normal     := TXIntVector8.Create(-1,  1,  1, 0);
  192.   p^.TexCoord   := TXIntVector16.Create(
  193.     High(Int16),
  194.     Low(Int16),
  195.     Low(Int16),
  196.     0);
  197.   p^.Color.ARGB := Color32(0, 255, 255);
  198.   Inc(p);
  199.  
  200.   // 7
  201.   p^.Position   := TXIntVector8.Create( 1,  1,  1, 0);
  202.   p^.Normal     := TXIntVector8.Create( 1,  1,  1, 0);
  203.   p^.TexCoord   := TXIntVector16.Create(
  204.     Low(Int16),
  205.     Low(Int16),
  206.     Low(Int16),
  207.     0);
  208.   p^.Color.ARGB := Color32(255, 255, 255);
  209.  
  210. end;
  211.  
  212.  
  213. procedure TYCubeChunkMin.UpdateIbo;
  214. var x, y, z, o, s: NativeInt;
  215.     p            : PYCubeVertex;
  216.     b            : PGLuint;
  217. begin
  218.   glNamedBufferStorage(
  219.     FIbo,
  220.     SizeOf(GLuint) * 36,
  221.     nil,
  222.     GL_DYNAMIC_STORAGE_BIT  or GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT
  223.     );
  224.  
  225.   FDataIbo:= PYCubeBlockDataIbo(glMapNamedBufferRange(
  226.     FIbo,
  227.     0,
  228.     SizeOf(GLuint) * 36,
  229.     GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT
  230.     ));
  231.  
  232.  
  233.   FVisibleBlocks:= 0;
  234.   p:= @FData;
  235.   b:= @FDataIbo^[0];
  236.  
  237.   // +X
  238.   b^:= 7;
  239.   Inc(b);
  240.   b^:= 5;
  241.   Inc(b);
  242.   b^:= 1;
  243.   Inc(b);
  244.  
  245.   b^:= 1;
  246.   Inc(b);
  247.   b^:= 3;
  248.   Inc(b);
  249.   b^:= 7;
  250.   Inc(b);
  251.   Inc(FVisibleBlocks, 6);
  252.  
  253.   // -X
  254.   b^:= 0;
  255.   Inc(b);
  256.   b^:= 4;
  257.   Inc(b);
  258.   b^:= 6;
  259.   Inc(b);
  260.  
  261.   b^:= 6;
  262.   Inc(b);
  263.   b^:= 2;
  264.   Inc(b);
  265.   b^:= 0;
  266.   Inc(b);
  267.   Inc(FVisibleBlocks, 6);
  268.  
  269.   // +Y
  270.   b^:= 2;
  271.   Inc(b);
  272.   b^:= 6;
  273.   Inc(b);
  274.   b^:= 7;
  275.   Inc(b);
  276.  
  277.   b^:= 7;
  278.   Inc(b);
  279.   b^:= 3;
  280.   Inc(b);
  281.   b^:= 2;
  282.   Inc(b);
  283.   Inc(FVisibleBlocks, 6);
  284.  
  285.   // -Y
  286.   b^:= 5;
  287.   Inc(b);
  288.   b^:= 4;
  289.   Inc(b);
  290.   b^:= 0;
  291.   Inc(b);
  292.  
  293.   b^:= 0;
  294.   Inc(b);
  295.   b^:= 1;
  296.   Inc(b);
  297.   b^:= 5;
  298.   Inc(b);
  299.   Inc(FVisibleBlocks, 6);
  300.  
  301.   // +Z
  302.   b^:= 6;
  303.   Inc(b);
  304.   b^:= 4;
  305.   Inc(b);
  306.   b^:= 5;
  307.   Inc(b);
  308.  
  309.   b^:= 5;
  310.   Inc(b);
  311.   b^:= 7;
  312.   Inc(b);
  313.   b^:= 6;
  314.   Inc(b);
  315.   Inc(FVisibleBlocks, 6);
  316.  
  317.   // -Z
  318.   b^:= 1;
  319.   Inc(b);
  320.   b^:= 0;
  321.   Inc(b);
  322.   b^:= 2;
  323.   Inc(b);
  324.  
  325.   b^:= 2;
  326.   Inc(b);
  327.   b^:= 3;
  328.   Inc(b);
  329.   b^:= 1;
  330.   Inc(b);
  331.   Inc(FVisibleBlocks, 6);
  332.  
  333.  
  334.  
  335.  
  336. end;
  337.  
  338.  
  339. procedure TYCubeChunkMin.Draw;
  340. begin
  341.   glBindVertexArray(FVao);
  342.   glDrawElements(GL_TRIANGLES, FVisibleBlocks, GL_UNSIGNED_INT, nil);
  343. end;
  344.  
  345.  
  346.  
  347. end.
  348.  

deadbeef

  • New Member
  • *
  • Posts: 17
Re: Drawing a cubemap onto a cube not working
« Reply #1 on: February 19, 2022, 08:33:03 am »
Turns out this actually works, I was just using GL_TEXTURE_CUBE_MAP_ARRAY instead of GL_TEXTURE_CUBE_MAP but no error was generated.
Changing samplerCube to samplerCubeArray and using a vec4 for texture lookup is all that was needed.

deadbeef

  • New Member
  • *
  • Posts: 17
Re: Drawing a cubemap onto a cube not working
« Reply #2 on: February 19, 2022, 10:26:07 am »
It's even possible to get proper normals for the cube faces.
Code: glSlang  [Select][+][-]
  1. #version 450 core
  2.  
  3. layout(location = 0) in vec4 in_position;
  4. layout(location = 1) in vec4 in_normals;
  5. //layout(location = 2) in vec4 in_tex_coord;
  6. layout(location = 3) in vec4 in_color;
  7.  
  8. uniform mat4  transform;
  9. uniform float block_size;
  10.  
  11. out vec4 TexCoord;
  12. out vec4 TexColor;
  13.  
  14. void main() {
  15.         gl_Position = transform * vec4(in_position.xyz * block_size, 1.0);
  16.         TexCoord    = in_normals;
  17.         TexColor    = in_color.bgra;
  18. }
  19.  

Code: glSlang  [Select][+][-]
  1. #version 450 core
  2.  
  3. #pragma tmu0 "Texture0"
  4. uniform samplerCubeArray Texture0;
  5. uniform int shader_output;
  6.  
  7. in  vec4 TexCoord;
  8. in  vec4 TexColor;
  9. out vec4 FragColor;
  10.  
  11. vec3 major_axis(vec3 xyz) {
  12.         // negative faces
  13.         vec3 nf = xyz;
  14.         nf = nf * vec3(lessThan(xyz, xyz.yzx));
  15.         nf = nf * vec3(lessThan(xyz, xyz.zxy));
  16.        
  17.         // positive faces
  18.         vec3 pf = xyz;
  19.         pf = pf * vec3(greaterThan(xyz, xyz.yzx));
  20.         pf = pf * vec3(greaterThan(xyz, xyz.zxy));
  21.        
  22.         return length(pf) > length(nf) ? pf : nf;
  23. }
  24.  
  25. void main() {
  26.         switch (shader_output) {
  27.                 // texture * color
  28.                 case 0:
  29.                         FragColor = texture(Texture0, vec4(TexCoord.xyz, 0)) * TexColor;
  30.                         break;
  31.                
  32.                 // texture only
  33.                 case 1:
  34.                         FragColor = texture(Texture0, vec4(TexCoord.xyz, 0));
  35.                         break;
  36.                
  37.                 // texture coords
  38.                 case 2:
  39.                         FragColor = TexCoord;
  40.                         break;
  41.                
  42.                 // normals
  43.                 case 3:
  44.                         vec3 normals = normalize(major_axis(TexCoord.xyz));
  45.                         if (normals.x < 0) normals.x = 0.25;
  46.                         if (normals.y < 0) normals.y = 0.25;
  47.                         if (normals.z < 0) normals.z = 0.25;
  48.                         FragColor = vec4(normals, 1.0);
  49.                        
  50.                         break;
  51.                
  52.                 // color only
  53.                 case 4:
  54.                         FragColor = TexColor;
  55.                         break;
  56.         }
  57. }
  58.  

Does anyone know how cubemap texture lookups perform against 2d texture lookups?

 

TinyPortal © 2005-2018