- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
作为背景,我目前正在生成一个旋转表面,它的质心在 WCS 中以 (0,0,0) 为中心。旋转的表面是 y=x^2,其中 0 <= x <= 1。
我已将这个旋转表面转换为虚拟缓冲区对象,并且可以在屏幕上成功渲染它。但是,我似乎无法让 Blinn-Phong 着色对对象起作用。我相当确定问题出在我的正常计算中。
这是创建对象并计算法线的 stub :
GLfloat vp[49 * 49 * 18]; // array of vertex points
int _i = 50;
int _j = 50;
float vertices[50][50][3];
for (int i = 0; i < _i; i++) {
float fT = (float) i / (_i - 1);
float fY = fT;
float fZ = sqrt(fT);
for (int j = 0; j < _j; j++) {
float fS = 2 * M_PI * (float) j / (_j - 1);
vertices[i][j][0] = fZ * cos(fS);
vertices[i][j][1] = fY - 0.5; // offset by 0.5 to make center of mass the center
vertices[i][j][2] = fZ * sin(fS);
}
}
int curr = 0;
for (int i = 0; i < _i - 1; i++) {
for (int j = 0; j < _j - 1; j++) {
vp[curr++] = vertices[i][j][0];
vp[curr++] = vertices[i][j][1];
vp[curr++] = vertices[i][j][2];
vp[curr++] = vertices[i+1][j][0];
vp[curr++] = vertices[i+1][j][1];
vp[curr++] = vertices[i+1][j][2];
vp[curr++] = vertices[i][j+1][0];
vp[curr++] = vertices[i][j+1][1];
vp[curr++] = vertices[i][j+1][2];
vp[curr++] = vertices[i+1][j][0];
vp[curr++] = vertices[i+1][j][1];
vp[curr++] = vertices[i+1][j][2];
vp[curr++] = vertices[i+1][j+1][0];
vp[curr++] = vertices[i+1][j+1][1];
vp[curr++] = vertices[i+1][j+1][2];
vp[curr++] = vertices[i][j+1][0];
vp[curr++] = vertices[i][j+1][1];
vp[curr++] = vertices[i][j+1][2];
}
}
GLuint vao;
glGenVertexArrays (1, &vao); // generating and binding is common pattern in OpenGL
glBindVertexArray (vao); // basically setting up memory and associating it
GLuint points_vbo;
glGenBuffers(1, &points_vbo);
glBindBuffer(GL_ARRAY_BUFFER, points_vbo);
glBufferData(GL_ARRAY_BUFFER, 49 * 49 * 18 * sizeof (GLfloat), vp, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(0);
GLfloat normals[49 * 49 * 18 / 3];
curr = 0;
for (int i = 0; i < 49 * 49 * 18; i += 9){
int Ux = vp[i+3] - vp[i];
int Uy = vp[i+4] - vp[i+1];
int Uz = vp[i+5] - vp[i+2];
int Vx = vp[i+6] - vp[i];
int Vy = vp[i+7] - vp[i+1];
int Vz = vp[i+8] - vp[i+2];
normals[curr++] = Uy * Vz - Uz * Vy;
normals[curr++] = Uz * Vx - Ux * Vz;
normals[curr++] = Ux * Vy - Uy * Vx;
}
GLuint normals_vbo;
glGenBuffers(1, &normals_vbo);
glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
glBufferData(GL_ARRAY_BUFFER, 49 * 49 * 18 / 3 * sizeof(GLfloat), normals, GL_STATIC_DRAW);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glEnableVertexAttribArray(1);
#version 410
layout (location = 0) in vec3 vtxPosition;
layout (location = 1) in vec3 normal;
uniform mat4 proj_mat, view_mat, model_mat;
out vec3 Normal;
out vec3 fpos;
void main () {
gl_Position = proj_mat * view_mat * model_mat * vec4(vtxPosition, 1.0);
fpos = vec3(model_mat * vec4(vtxPosition, 1.0));
Normal = normal;
}
#version 410
// Define INPUTS from fragment shader
//uniform mat4 view_mat;
in vec3 Normal;
in vec3 fpos;
// These come from the VAO for texture coordinates.
in vec2 texture_coords;
// And from the uniform outputs for the textures setup in main.cpp.
uniform sampler2D texture00;
uniform sampler2D texture01;
out vec4 fragment_color; //RGBA color
const vec3 lightPos = vec3(0.0,0.0,5.0);
const vec3 diffColor = vec3(1.0,0.5,0.0);
const vec3 specColor = vec3(1.0,1.0,1.0);
void main () {
vec3 normal = normalize(Normal);
vec3 lightDir = normalize(lightPos - fpos);
float lamb = max(dot(lightDir, normal), 0.0);
float spec = 0.0;
if (lamb > 0.0) {
vec3 refDir = reflect(-lightDir, normal);
vec3 viewDir = normalize(-fpos);
float specAngle = max(dot(refDir, viewDir), 0.0);
spec = pow(specAngle, 4.0);
}
fragment_color = vec4(lamb * diffColor + spec * specColor, 1.0);
}
最佳答案
您必须为每个顶点坐标指定 1 个法线属性。顶点坐标及其属性形成一个元组。
此外,您必须使用数据类型 flaot
而不是 int
,用于计算法 vector :
GLfloat normals[49 * 49 * 18];
curr = 0;
for (int i = 0; i < 49 * 49 * 18; i += 9){
float Ux = vp[i+3] - vp[i];
float Uy = vp[i+4] - vp[i+1];
float Uz = vp[i+5] - vp[i+2];
float Vx = vp[i+6] - vp[i];
float Vy = vp[i+7] - vp[i+1];
float Vz = vp[i+8] - vp[i+2];
float nx = Uy * Vz - Uz * Vy;
float ny = Uz * Vx - Ux * Vz;
float nz = Ux * Vy - Uy * Vx;
for (int j = 0; j < 3; ++j) {
normals[curr++] = nx;
normals[curr++] = ny;
normals[curr++] = nz;
}
}
glBufferData(GL_ARRAY_BUFFER, 49 * 49 * 18 * sizeof(GLfloat), normals, GL_STATIC_DRAW);
vec3 normal = normalize(Normal);
vec3 viewDir = normalize(-fpos);
if (dot(normal, viewDir) < 0.0)
normal *= -1.0;
#version 410
// Define INPUTS from fragment shader
//uniform mat4 view_mat;
in vec3 Normal;
in vec3 fpos;
// These come from the VAO for texture coordinates.
in vec2 texture_coords;
// And from the uniform outputs for the textures setup in main.cpp.
uniform sampler2D texture00;
uniform sampler2D texture01;
out vec4 fragment_color; //RGBA color
const vec3 lightPos = vec3(0.0,0.0,5.0);
const vec3 diffColor = vec3(1.0,0.5,0.0);
const vec3 specColor = vec3(1.0,1.0,1.0);
void main () {
vec3 normal = normalize(Normal);
vec3 viewDir = normalize(-fpos);
if (dot(normal, viewDir) < 0.0)
normal *= -1.0;
vec3 lightDir = normalize(lightPos - fpos);
float lamb = max(dot(lightDir, normal), 0.0);
float spec = 0.0;
if (lamb > 0.0) {
vec3 refDir = reflect(-lightDir, normal);
float specAngle = max(dot(refDir, viewDir), 0.0);
spec = pow(specAngle, 4.0);
}
fragment_color = vec4(lamb * diffColor + spec * specColor, 1.0);
}
关于c++ - 如何在 OpenGl 中计算三角形网格的顶点法线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61241414/
所以昨天我写了 WaveFront .obj 3D 模型加载器,它现在工作正常(虽然不支持所有内容)所以我写了简单的测试来在屏幕上绘制任何 3D 模型,在我为场景添加照明之前一切正常.光出现了,但法线
我的代码(用 C++ 编码)在处理更新模型法线的 for 循环中存在瓶颈。我用来测试的模型有大约 ~2.2k 个顶点(指示),工作正常并且在 60 fps 限制下工作,但法线与指示器(~12k)一样多
我无法将法线和 u,v 对发送到我的着色器。如果我删除法线,一切都会按预期进行。 编辑看起来 v_normal 正在接收用于 v_coord 的值。我仍然不知道。/编辑 这是我的顶点: struct
我正在寻找一种方法来获取矩形的所有顶点,该矩形的中心、法线、长度和高度我都知道。我的数学有点弱所以请帮助我。编辑:飞机在 3D 空间中。 最佳答案 通过从中心点的 x/y 位置减去/加上宽度/高度的一
是否可以从片段着色器内访问表面法线(与片段平面相关的法线)?或者也许这可以在顶点着色器中完成? 当我们沿着着色器管道走下去时,相关几何体的所有知识是否都会丢失,或者是否有一些巧妙的方法可以在片段着色器
我无法使用 VBO 正确渲染法线。下面是我正在使用的代码,顶点是一个包含顶点的数组,法线是一个包含法线的数组: //Create the buffers and such GLuint VBOID;
我已经有了这个不再那么小的基于图 block 的游戏,这是我的第一个真正的 OpenGL 项目。我想将每个图 block 渲染为 3D 对象。因此,首先我创建了一些对象,例如立方体和球体,为它们提供了
我正在尝试添加一个垂直于我单击的面的框(称为“标记”)。 为此,单击时,我会转换一条射线,如果它击中某些物体,我会获取法线的交点值并将它们传输到“标记”作为其旋转值。在问这个问题之前,我阅读了这个答案
这与另一个问题(那里的图像)中描述的问题有关: Opengl shader problems - weird light reflection artifacts 我有一个 .obj 导入器,它创建一
我正在尝试制作一个在 OpenGL 2.1 和 Qt5 中渲染的简单 map 。但我在非常基本的问题上失败了。我在这里展示的是表面法线。 我有 4 个由单个三角形几何体组成的对象。一个简单的几何图形是
我正在开发一个类似 JavaScript/Canvas 3D FPS 的引擎,迫切需要一个法线向量(如果您愿意,也可以是观察向量)来进行近平面和远平面裁剪。我有 x 轴和 y 轴旋转 Angular
我正在编写用于在我的 DirectX 应用程序中存储、加载和渲染静态网格的类。 我知道 Box 模型可以使用 8 个顶点,但通常它使用 24 或 36 个(因为一个“空间中的顶点”实际上是 3 个具有
我无法使 phong 着色看起来正确。我很确定我的 OpenGL 调用或我加载法线的方式有问题,但我想这可能是其他问题,因为 3D 图形和 Assimp 对我来说都还很陌生。尝试加载 .obj/.mt
我是一名优秀的程序员,十分优秀!