Forum > OpenGL
Drawing a cubemap onto a cube not working
(1/1)
deadbeef:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#version 450 core // https://www.khronos.org/opengl/wiki/Layout_Qualifier_(GLSL)layout(location = 0) in vec4 in_position;layout(location = 1) in vec4 in_normals;layout(location = 2) in vec4 in_tex_coord;layout(location = 3) in vec4 in_color; uniform mat4 transform;uniform float block_size; out vec4 TexCoord;out vec4 TexColor; void main() { gl_Position = transform * vec4(vec3(in_position.xyz) * block_size, 1.0); TexCoord = in_normals; //TexCoord = in_tex_coord; TexColor = in_color.bgra;}
Fragment
--- Code: glSlang [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#version 450 core #pragma tmu0 "Texture0"uniform samplerCube Texture0; in vec4 TexCoord;in vec4 TexColor;out vec4 FragColor; void main() { FragColor = texture(Texture0, -TexCoord.xyz);// * TexColor; //FragColor = TexColor;}
Cube code
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit cubesidestest_min; {$mode Delphi} interface uses Classes, SysUtils, XIntVector, GR32, dglOpenGL; type PYCubeVertex = ^TYCubeVertex; TYCubeVertex = packed record // 20 byte Position : TXIntVector8; Normal : TXIntVector8; TexCoord : TXIntVector16; Color : TColor32Entry; // BGRA end; PYCubeBlockDataIbo = ^TYCubeBlockDataIbo; TYCubeBlockDataIbo = packed array[0..36] of GLuint; type TYCubeChunkMin = class strict private var FData : PYCubeVertex; FDataIbo : PYCubeBlockDataIbo; FVisibleBlocks: GLint; FVao : GLuint; FVbo : GLuint; FIbo : GLuint; procedure InitVertexData; procedure UpdateIbo; public constructor Create; destructor Destroy; override; procedure Draw; end; implementation constructor TYCubeChunkMin.Create;begin glCreateVertexArrays(1, @FVao); glCreateBuffers(1, @FVbo); glCreateBuffers(1, @FIbo); // bind vbo to vao glVertexArrayVertexBuffer(FVao, 0, FVbo, 0, SizeOf(TYCubeVertex)); // map attributes to vbo glVertexArrayAttribBinding(FVao, 0, 0); glVertexArrayAttribBinding(FVao, 1, 0); glVertexArrayAttribBinding(FVao, 2, 0); glVertexArrayAttribBinding(FVao, 3, 0); // enable attributes glEnableVertexArrayAttrib(FVao, 0); glEnableVertexArrayAttrib(FVao, 1); glEnableVertexArrayAttrib(FVao, 2); glEnableVertexArrayAttrib(FVao, 3); // define attributes glVertexArrayAttribFormat(FVao, 0, 4, GL_BYTE , GL_FALSE, GLuint(@TYCubeVertex(nil^).Position)); glVertexArrayAttribFormat(FVao, 1, 4, GL_BYTE , GL_FALSE, GLuint(@TYCubeVertex(nil^).Normal)); glVertexArrayAttribFormat(FVao, 2, 2, GL_SHORT , GL_TRUE , GLuint(@TYCubeVertex(nil^).TexCoord)); glVertexArrayAttribFormat(FVao, 3, 4, GL_UNSIGNED_BYTE, GL_TRUE , GLuint(@TYCubeVertex(nil^).Color)); // add ibo glVertexArrayElementBuffer(FVao, FIbo); InitVertexData; UpdateIbo;end; destructor TYCubeChunkMin.Destroy;begin glUnmapNamedBuffer(FIbo); glUnmapNamedBuffer(FVbo); glDeleteBuffers (1, @FIbo); glDeleteBuffers (1, @FVbo); glDeleteVertexArrays(1, @FVao);end; procedure TYCubeChunkMin.InitVertexData;var x, y, z, i: NativeInt; p : PYCubeVertex;begin // allocate glNamedBufferStorage( FVbo, SizeOf(TYCubeVertex) * 8, nil, GL_DYNAMIC_STORAGE_BIT or GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT ); // map FData:= PYCubeVertex(glMapNamedBufferRange( FVbo, 0, SizeOf(TYCubeVertex) * 8, GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT )); p:= FData; // 0 p^.Position := TXIntVector8.Create( 0, 0, 0, 0); p^.Normal := TXIntVector8.Create(-1, -1, -1, 0); p^.TexCoord := TXIntVector16.Create( High(Int16), High(Int16), High(Int16), 0); p^.Color.ARGB := Color32(0, 0, 0); Inc(p); // 1 p^.Position := TXIntVector8.Create( 1, 0, 0, 0); p^.Normal := TXIntVector8.Create( 1, -1, -1, 0); p^.TexCoord := TXIntVector16.Create( Low(Int16), High(Int16), High(Int16), 0); p^.Color.ARGB := Color32(255, 0, 0); Inc(p); // 2 p^.Position := TXIntVector8.Create( 0, 1, 0, 0); p^.Normal := TXIntVector8.Create(-1, 1, -1, 0); p^.TexCoord := TXIntVector16.Create( High(Int16), Low(Int16), High(Int16), 0); p^.Color.ARGB := Color32(0, 255, 0); Inc(p); // 3 p^.Position := TXIntVector8.Create( 1, 1, 0, 0); p^.Normal := TXIntVector8.Create( 1, 1, -1, 0); p^.TexCoord := TXIntVector16.Create( Low(Int16), Low(Int16), High(Int16), 0); p^.Color.ARGB := Color32(255, 255, 0); Inc(p); // 4 p^.Position := TXIntVector8.Create( 0, 0, 1, 0); p^.Normal := TXIntVector8.Create(-1, -1, 1, 0); p^.TexCoord := TXIntVector16.Create( High(Int16), High(Int16), Low(Int16), 0); p^.Color.ARGB := Color32(0, 0, 255); Inc(p); // 5 p^.Position := TXIntVector8.Create( 1, 0, 1, 0); p^.Normal := TXIntVector8.Create( 1, -1, 1, 0); p^.TexCoord := TXIntVector16.Create( Low(Int16), High(Int16), Low(Int16), 0); p^.Color.ARGB := Color32(255, 0, 255); Inc(p); // 6 p^.Position := TXIntVector8.Create( 0, 1, 1, 0); p^.Normal := TXIntVector8.Create(-1, 1, 1, 0); p^.TexCoord := TXIntVector16.Create( High(Int16), Low(Int16), Low(Int16), 0); p^.Color.ARGB := Color32(0, 255, 255); Inc(p); // 7 p^.Position := TXIntVector8.Create( 1, 1, 1, 0); p^.Normal := TXIntVector8.Create( 1, 1, 1, 0); p^.TexCoord := TXIntVector16.Create( Low(Int16), Low(Int16), Low(Int16), 0); p^.Color.ARGB := Color32(255, 255, 255); end; procedure TYCubeChunkMin.UpdateIbo;var x, y, z, o, s: NativeInt; p : PYCubeVertex; b : PGLuint;begin glNamedBufferStorage( FIbo, SizeOf(GLuint) * 36, nil, GL_DYNAMIC_STORAGE_BIT or GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT ); FDataIbo:= PYCubeBlockDataIbo(glMapNamedBufferRange( FIbo, 0, SizeOf(GLuint) * 36, GL_MAP_WRITE_BIT or GL_MAP_READ_BIT or GL_MAP_PERSISTENT_BIT or GL_MAP_COHERENT_BIT )); FVisibleBlocks:= 0; p:= @FData; b:= @FDataIbo^[0]; // +X b^:= 7; Inc(b); b^:= 5; Inc(b); b^:= 1; Inc(b); b^:= 1; Inc(b); b^:= 3; Inc(b); b^:= 7; Inc(b); Inc(FVisibleBlocks, 6); // -X b^:= 0; Inc(b); b^:= 4; Inc(b); b^:= 6; Inc(b); b^:= 6; Inc(b); b^:= 2; Inc(b); b^:= 0; Inc(b); Inc(FVisibleBlocks, 6); // +Y b^:= 2; Inc(b); b^:= 6; Inc(b); b^:= 7; Inc(b); b^:= 7; Inc(b); b^:= 3; Inc(b); b^:= 2; Inc(b); Inc(FVisibleBlocks, 6); // -Y b^:= 5; Inc(b); b^:= 4; Inc(b); b^:= 0; Inc(b); b^:= 0; Inc(b); b^:= 1; Inc(b); b^:= 5; Inc(b); Inc(FVisibleBlocks, 6); // +Z b^:= 6; Inc(b); b^:= 4; Inc(b); b^:= 5; Inc(b); b^:= 5; Inc(b); b^:= 7; Inc(b); b^:= 6; Inc(b); Inc(FVisibleBlocks, 6); // -Z b^:= 1; Inc(b); b^:= 0; Inc(b); b^:= 2; Inc(b); b^:= 2; Inc(b); b^:= 3; Inc(b); b^:= 1; Inc(b); Inc(FVisibleBlocks, 6); end; procedure TYCubeChunkMin.Draw;begin glBindVertexArray(FVao); glDrawElements(GL_TRIANGLES, FVisibleBlocks, GL_UNSIGNED_INT, nil);end; end.
deadbeef:
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:
It's even possible to get proper normals for the cube faces.
--- Code: glSlang [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#version 450 core layout(location = 0) in vec4 in_position;layout(location = 1) in vec4 in_normals;//layout(location = 2) in vec4 in_tex_coord;layout(location = 3) in vec4 in_color; uniform mat4 transform;uniform float block_size; out vec4 TexCoord;out vec4 TexColor; void main() { gl_Position = transform * vec4(in_position.xyz * block_size, 1.0); TexCoord = in_normals; TexColor = in_color.bgra;}
--- Code: glSlang [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---#version 450 core #pragma tmu0 "Texture0"uniform samplerCubeArray Texture0;uniform int shader_output; in vec4 TexCoord;in vec4 TexColor;out vec4 FragColor; vec3 major_axis(vec3 xyz) { // negative faces vec3 nf = xyz; nf = nf * vec3(lessThan(xyz, xyz.yzx)); nf = nf * vec3(lessThan(xyz, xyz.zxy)); // positive faces vec3 pf = xyz; pf = pf * vec3(greaterThan(xyz, xyz.yzx)); pf = pf * vec3(greaterThan(xyz, xyz.zxy)); return length(pf) > length(nf) ? pf : nf;} void main() { switch (shader_output) { // texture * color case 0: FragColor = texture(Texture0, vec4(TexCoord.xyz, 0)) * TexColor; break; // texture only case 1: FragColor = texture(Texture0, vec4(TexCoord.xyz, 0)); break; // texture coords case 2: FragColor = TexCoord; break; // normals case 3: vec3 normals = normalize(major_axis(TexCoord.xyz)); if (normals.x < 0) normals.x = 0.25; if (normals.y < 0) normals.y = 0.25; if (normals.z < 0) normals.z = 0.25; FragColor = vec4(normals, 1.0); break; // color only case 4: FragColor = TexColor; break; }}
Does anyone know how cubemap texture lookups perform against 2d texture lookups?
Navigation
[0] Message Index