我在做一个需要画球体的程序。我使用的方法是创建一个八面体

使用镶嵌着色器把它细分成一个球体,但是有一个球体没有被渲染,我不知道是什么导致了它。请记住,当我停止通过评估着色器规范UVW坐标时,情况是一样的。


这是我的镶嵌评价着色器
#version 450 core
// from control shader
layout(triangles, equal_spacing, cw) in;
// input from control shader
in vec3 vertex_coord[];
// output vec
out vec3 vert;
// allows for object transformations and for movement
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main()
{
// get / make the barycentric coordinates of the octahedron
vec3 u = gl_TessCoord.x * vertex_coord[0];
vec3 v = gl_TessCoord.y * vertex_coord[1];
vec3 w = gl_TessCoord.z * vertex_coord[2];
// normalizing the positions of the triangles allows for every point to be
// the same distance away from the center, forming a sphere
vec3 pos = normalize(u + v + w);
// output patch point position in clip space
gl_Position = projection * view * model * vec4(pos, 1.0);
}这是我的镶嵌控制着色器
#version 450 core
// specify control points per output per patch
// control size of input and output arrays
layout(vertices=3) out;
// input from vertex shader
in vec3 vert_coord[];
// output to evaluation shader
out vec3 vertex_coord[];
// for tessellation
uniform mat4 view;
uniform mat4 model;
void main()
{
// pass attributes through
gl_out[gl_InvocationID].gl_Position = gl_in[gl_InvocationID].gl_Position;
vertex_coord[gl_InvocationID] = vert_coord[gl_InvocationID];
// control tessellation
if(gl_InvocationID==0)
{
// dynamic LOD step by step
// first: define rendering constants to control tessellation
const float MIN_TESS_LEVEL = 4;
const float MAX_TESS_LEVEL = 64;
const float MIN_DISTANCE = 20;
const float MAX_DISTANCE = 800;
// second: transform each vertex into each eye
vec4 eye_space_pos_1 = view * model * gl_in[0].gl_Position;
vec4 eye_space_pos_2 = view * model * gl_in[1].gl_Position;
vec4 eye_space_pos_3 = view * model * gl_in[2].gl_Position;
// third: distance from camera scaled between 0 and 1
float distance_1 = clamp((abs(eye_space_pos_1.z)-MIN_DISTANCE)/(MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0);
float distance_2 = clamp((abs(eye_space_pos_2.z)-MIN_DISTANCE)/(MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0);
float distance_3 = clamp((abs(eye_space_pos_3.z)-MIN_DISTANCE)/(MAX_DISTANCE-MIN_DISTANCE), 0.0, 1.0);
// fourth: interpolate edge tessellation level based on closer vertex
float tess_level_1 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance_3, distance_1));
float tess_level_2 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance_1, distance_2));
float tess_level_3 = mix(MAX_TESS_LEVEL, MIN_TESS_LEVEL, min(distance_2, distance_1));
// fifth: set the corresponding outer tessellation levels
gl_TessLevelOuter[0] = tess_level_1;
gl_TessLevelOuter[1] = tess_level_2;
gl_TessLevelOuter[2] = tess_level_3;
// sixth: set the inner tessellation levels
gl_TessLevelInner[0] = tess_level_1;
gl_TessLevelInner[1] = tess_level_2;
}
}这是我的顶点着色器
#version 450 core
// position of the object
layout (location = 0) in vec3 pos;
layout (location = 1) in vec3 vert;
// vertices to make the sphere
out vec3 vert_coord;
void main()
{
// position object
gl_Position = vec4(pos, 1.0f);
// put the vertices into a different vector
vert_coord = vert;
}最后,这里是我的八面体顶点和缓冲区,并绘制调用。
float vertices[] = {
//top-north-east
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
//top-north-west
0.0f, 1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
//top-south-west
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
-1.0f, 0.0f, 0.0f,
//top-south-east
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f,
//bottom-north-east
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
//bottom-north-west
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 0.0f,
//bottom-south-west
0.0f, -1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f,
//bottom-south-east
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, 0.0f
};
unsigned int indices[] = {
// first triangle
0, 1, 2,
// second triangle
3, 4, 5,
// third triangle
6, 7, 8,
// fourth triangle
9, 10, 11,
// fifth triangle
12, 13, 14,
// sixth triangle
15, 16, 17,
// seventh triangle
18, 19, 20,
// eighth triangle
21, 22, 23
};
unsigned int vbo, vao, ebo;
glGenVertexArrays(1, &vao);
glGenBuffers(1, &vbo);
glGenBuffers(1, &ebo);
glBindVertexArray(vao);
// upload vertex data to gpu
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices) * sizeof(float), &vertices[0], GL_STATIC_DRAW);
// upload index data to gpu
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices) * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
// position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
// normal attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
// max tessellation points / patches
glPatchParameteri(GL_PATCH_VERTICES, 3);
glDrawElements(GL_PATCHES, 24, GL_UNSIGNED_INT, 0);作为一种奖励,如果有帮助的话,这就是我修改代码的教程。制造它的人用的是二十面体,当我尝试它的时候,它不起作用,https://prideout.net/blog/old/blog/index.html@p=48.html。
发布于 2023-04-17 14:25:05
于是我去了另一个论坛,问了这个问题,有人帮我解决了这个问题。原来八面体的坐标是错的。有问题的有上、东北、下、东南两方面。我将给出一个新的和改进的坐标列表,以及它的输出。
float vertices[] = {
//top-north-east
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
1.0f, 0.0f, 0.0f,
//top-north-west
0.0f, 1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
//top-south-west
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
-1.0f, 0.0f, 0.0f,
//top-south-east
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f,
//bottom-north-east
0.0f, -1.0f, 0.0f,
1.0f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f,
//bottom-north-west
0.0f, -1.0f, 0.0f,
0.0f, 0.0f, 1.0f,
-1.0f, 0.0f, 0.0f,
//bottom-south-west
0.0f, -1.0f, 0.0f,
-1.0f, 0.0f, 0.0f,
0.0f, 0.0f, -1.0f,
//bottom-south-east
0.0f, 1.0f, 0.0f,
0.0f, 0.0f, -1.0f,
1.0f, 0.0f, 0.0f
};
两个月后终于解决了。
https://computergraphics.stackexchange.com/questions/13385
复制相似问题