- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
首先 - 代码:
std::vector<glm::vec3> verticescopy;
std::vector<glm::vec2> newUVs;
/*Generating new UV Coordinates*/
void keyfunction(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_R && action == GLFW_PRESS)
{
glm::vec3 coords2D;
GLdouble point2DX, point2DY, point2DZ;
GLdouble model_view[16];
glGetDoublev(GL_MODELVIEW_MATRIX, model_view);
GLdouble projection[16];
glGetDoublev(GL_PROJECTION_MATRIX, projection);
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
for (int i = 0; i < verticescopy.size(); i++)
{
gluProject((double)verticescopy[i].x, (double)verticescopy[i].y, (double)verticescopy[i].z,
model_view, projection, viewport,
&point2DX, &point2DY, &point2DZ);
glm::vec2 UV;
UV.x = point2DX / 600;
UV.y = (point2DY - 800) / 800; /* I make Y - 800, because values from glProject are generated from (0,1), not (0,0) */
newUVs.push_back(UV);
}
}
}
int main(void)
{
if (!glfwInit())
{
fprintf(stderr, "Failed to initialize GLFW\n");
getchar();
return -1;
}
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
window = glfwCreateWindow(600, 800, "Window", NULL, NULL);
if (window == NULL)
{
fprintf(stderr, "Nie utworzono okna GLFW.\n");
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
fprintf(stderr, "Blad GLEW.\n");
getchar();
glfwTerminate();
return -1;
}
glfwSetInputMode(window, GLFW_STICKY_KEYS, GL_TRUE);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
glfwPollEvents();
glfwSetCursorPos(window, 1024 / 2, 768 / 2);
glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
GLuint VertexArrayID;
glGenVertexArrays(1, &VertexArrayID);
glBindVertexArray(VertexArrayID);
GLuint programID = LoadShaders("TransformVertexShader.vertexshader", "TextureFragmentShader.fragmentshader");
GLuint MatrixID = glGetUniformLocation(programID, "MVP");
GLuint Texture = loadBMP_custom("skull-xray.bmp");
GLuint TextureID = glGetUniformLocation(programID, "myTextureSampler");
std::vector<glm::vec3> vertices;
std::vector<glm::vec2> uvs;
std::vector<glm::vec3> normals;
bool res = loadOBJ("czaszkazeby.obj", vertices, uvs, normals);
for (int i = 0; i < vertices.size(); i++)
{
verticescopy.push_back(vertices[i]);
}
GLuint vertexbuffer;
glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), &vertices[0], GL_STATIC_DRAW);
unsigned int Texture2DTextureID = loadBMP_custom("skull1.bmp");
unsigned int Texture2DVertexBufferID;
unsigned int Texture2DUVBufferID;
unsigned int Texture2DShaderID = LoadShaders("TextVertexShader.vertexshader", "TextVertexShader.fragmentshader");
unsigned int Texture2DUniformID = glGetUniformLocation(Texture2DShaderID, "myTextureSampler");
glGenBuffers(1, &Texture2DVertexBufferID);
glGenBuffers(1, &Texture2DUVBufferID);
glfwSetKeyCallback(window, keyfunction);
do{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(programID);
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
GLuint uvbuffer;
glGenBuffers(1, &uvbuffer);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glBufferData(GL_ARRAY_BUFFER, newUVs.size() * sizeof(glm::vec2), &newUVs[0], GL_DYNAMIC_DRAW);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, Texture);
glUniform1i(TextureID, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, uvbuffer);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glDrawArrays(GL_TRIANGLES, 0, vertices.size());
/* HUD drawing, not really important for the problem (I know this is bad, but this is not the main problem) */
std::vector<glm::vec2> verticesTexture;
std::vector<glm::vec2> UVsTexture;
glm::vec2 vertex_up_left = glm::vec2(0, 600);
glm::vec2 vertex_up_right = glm::vec2(800, 600);
glm::vec2 vertex_down_right = glm::vec2(800, 0);
glm::vec2 vertex_down_left = glm::vec2(0, 0);
verticesTexture.push_back(vertex_up_left);
verticesTexture.push_back(vertex_down_left);
verticesTexture.push_back(vertex_up_right);
verticesTexture.push_back(vertex_down_right);
verticesTexture.push_back(vertex_up_right);
verticesTexture.push_back(vertex_down_left);
glm::vec2 uv_up_left = glm::vec2(0, 1);
glm::vec2 uv_up_right = glm::vec2(1, 1);
glm::vec2 uv_down_right = glm::vec2(1, 0);
glm::vec2 uv_down_left = glm::vec2(0, 0);
UVsTexture.push_back(uv_up_left);
UVsTexture.push_back(uv_down_left);
UVsTexture.push_back(uv_up_right);
UVsTexture.push_back(uv_down_right);
UVsTexture.push_back(uv_up_right);
UVsTexture.push_back(uv_down_left);
glBindBuffer(GL_ARRAY_BUFFER, Texture2DVertexBufferID);
glBufferData(GL_ARRAY_BUFFER, verticesTexture.size() * sizeof(glm::vec2), &verticesTexture[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, Texture2DUVBufferID);
glBufferData(GL_ARRAY_BUFFER, UVsTexture.size() * sizeof(glm::vec2), &UVsTexture[0], GL_STATIC_DRAW);
glUseProgram(Texture2DShaderID);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, Texture2DTextureID);
glUniform1i(Texture2DUniformID, 0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, Texture2DVertexBufferID);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, Texture2DUVBufferID);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDrawArrays(GL_TRIANGLES, 0, verticesTexture.size());
/* End of HUD code */
glDisable(GL_BLEND);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
glfwSwapBuffers(window);
glfwPollEvents();
}
while (glfwGetKey(window, GLFW_KEY_ESCAPE) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0);
glDeleteBuffers(1, &vertexbuffer);
glDeleteProgram(programID);
glDeleteTextures(1, &TextureID);
glDeleteVertexArrays(1, &VertexArrayID);
glfwTerminate();
return 0;
}
还有 result
如您所想,这不是我要寻找的效果。我想要的是将您可以在图像上看到的头骨 X 射线纹理放在头骨模型的前面(就像人们为万圣节等在脸上画画一样)
我使用 gluProject 将 3D 坐标转换为 2D 坐标(世界到屏幕)。当我得到点的 2D 坐标时,我只需将它除以纹理的宽度或高度(取决于坐标类型),这会得到该点的 UV 坐标。我的想法中是否有什么漏洞导致了这种看起来很奇怪的纹理?
最佳答案
I used gluProject to convert 3D coordinates to 2D coordinates (world to screen). When I get 2D coordiantes of point I just divide it by width or height of texture (depends on coordinates type) which gets me UV coordinate for this point. Is there some hole in my thinking which causes this strange looking texturing?
是的。显然,您认为纹理坐标在某种程度上取决于模型的方向及其投影方式。这意味着如果您四处移动“相机”,则纹理坐标将不得不改变。这显然是错误的(很明显,如果知道纹理坐标应该如何工作的话)。顺便说一句,在 OpenGL 中,纹理坐标表示为 S、T、R 和 Q,而不是 UV。
纹理坐标将顶点与图像中的特定位置相关联。这个图像空间位置可能完全独立于顶点在模型空间中的位置;程序纹理或反射贴图可能存在关系,但通常它们是完全独立的。
通常不会生成纹理坐标,而是由艺术家在创建 3D 模型时手动定义,然后作为另一部分存储到模型数据中。
What I want is to put skull X-ray texture, which you can see on image, on front of skull model (just like people paint their faces for Halloween etc.)
假设您真的想采用纹理生成方法,公平地说,这将是纹理空间到模型空间的平面投影。你必须知道你的头骨是哪个方向。为了写这篇文章,头顶)是 Y+,左侧是 X+,鼻翼是 Z+,那么你的 X 射线图像就会投影到 XY 平面上。我们还假设您的 X 射线图像被裁剪得恰到好处。定义纹理空间以使图像适合范围 ]0,1[^n(其中 n 是图像的维度)。因此,这意味着无论顶点位置的 X 和 Y 分量的取值范围是多少,都必须分别映射到范围 ]0,1[。
这给出了以下伪代码:
min_X = +inf
max_X = -inf
min_Y = +inf
max_Y = -inf
foreach vertex in model:
min_X = min(min_X, vertex.position.x)
min_Y = min(min_Y, vertex.position.y)
max_X = max(max_X, vertex.position.x)
max_Y = max(max_Y, vertex.position.y)
k_X = 1/(max_X - min_X)
k_Y = 1/(max_Y - min_Y)
foreach vertex in model:
vertex.texturecoordinate.s = (vertex.position.x - min_X) * k_X
vertex.texturecoordinate.t = (vertex.position.y - min_Y) * k_Y
关于c++ - OpenGL/C++ - 生成 UV 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35141677/
我正在尝试实现凸多边形的正确纹理。我有一个带有 n 个三角形的多边形,对于每个三角形,我正在计算重心坐标,它们是每个三角形的 uv,但在每个三角形的 [0..1] 中,而不是整个多边形。如何插入每个
我目前正在开发一款 map 工具,可以为我的游戏生成自定义 map 。我的网格生成工作得很好,但我看不出如何正确地对我的脸进行 UV 贴图以使纹理保持一致。我已经通过使用相应的坐标对使 UV 贴图在地
我打算编写一个纹理图集管理器,但我遇到了问题。当我修改原始 UV 坐标时,我的程序不再渲染任何内容?这是我的抽奖代码: - (void)drawFrame { [(EAGLView *)self.vi
我有一个 3D 地形(体素网格,我的“任意网格”)。我知道如何从网格上方“拍打”纹理,但在垂直或陡峭的斜坡上它会弄脏。 我可以访问每个顶点的法线和位置。我将如何生成 UV(不使用着色器,因此没有真正的
我有几个 (3-4) 带(基本上是长三角形带),它们共享相同的纹理并且基本上从相同的位置开始。我通过将每一帧的偏移量添加到 V 坐标来垂直滚动该纹理。该偏移量只是我添加到每一帧的一个成员变量(是的,它
在 Android 上为 OpenGL 创建缓冲区时,有什么方法可以为 UV 使用与顶点不同的索引吗? 例如,如果您有 5000 个顶点和 12000 个 UV,并且不希望只为每个唯一的 UV 顶点对
Nginx: PV、UV、独立IP 做网站的都知道,平常经常要查询下网站PV、UV等网站的访问数据,当然如果网站做了CDN的话,nginx本地的日志就没什么意义了,下面就对nginx网站的日志访问
我有从二维形状生成三角形网格的代码。因为在大多数情况下这些形状在表面上的顶点分布不均匀,所以我在生成 UV 时遇到了问题,因此它不会导致纹理失真。任何人都可以推荐一些讨论平面网格上 UV 计算技术的文
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 4 年前。 Improve th
我有一张 UV 贴图: 我想像这样在连接的网格边周围获取纹理像素(以红色选中): 如何使用 Python Blender API 做这样的事情? 最佳答案 制作“UV”bmesh 给定上面带有 UV
摘要 我正在尝试将位移贴图(高度贴图)应用于一个相当简单的对象(六角形平面),并且得到了一些意想不到的结果。我使用的是灰度,因此,我的印象是我的高度图应该只影响网格的 Z 值。然而,我创建的置换贴图在
我有一张 UV 贴图: 我想像这样在连接的网格边周围获取纹理像素(以红色选中): 如何使用 Python Blender API 做这样的事情? 最佳答案 制作“UV”bmesh 给定上面带有 UV
在对多重采样纹理进行采样时,我们必须使用整数坐标,即 ivec2 Texcoord = ivec2(textureSize(texsampler) * In.Texcoord); vec4 color
我正在尝试将纹理应用于我的 .md2 模型。我使用 Gouraud 着色给它上色(底部/顶部平面三角形的标准算法),我必须使用类似的代码来处理纹理坐标 U 和 V。但我真的不明白如何对它们进行插值。从
这个问题在这里已经有了答案: When is @uncheckedVariance needed in Scala, and why is it used in GenericTraversableT
我不知道“膨胀”是否是正确的术语,但这是我现在能想到的全部。我正在尝试沿着三角形 strip 映射一个 1 像素宽(尽管从技术上讲每个纹理都会有这个问题)的图像来模拟激光。当它完全均匀且正方形时,它看
我正在尝试为我在代码中生成的网格计算 uv。这是一个非常简单的圆圈 (2d) 我想像下面这样构造它 并将此代码用于 uvs uvs[i] = new Vector2((verts[i].x+radiu
我正在使用三个 v.73我有来自 raycaster 交叉点的 UV 坐标。我也有这个物体的纹理。我怎样才能在 UV 坐标处获得所用纹理的颜色(RGB 或 RGBA)? 我曾尝试使用从纹理中获取图像的
假设我们有一个 3D 网格,每个顶点都有纹理坐标,所以如果我渲染它展开,我会得到这样的东西(忽略红色方 block ): 现在我正在尝试找到合适的算法来使用顶点 UV 唯一标识这些区域并存储具有此唯一
我正在尝试在 Unity 中更改和调整放置在 bilt-in 立方体面上的纹理。没有任何问题,我设法用这段代码在每张脸上放置不同的图像: void Start() { Mesh mesh =
我是一名优秀的程序员,十分优秀!