我检查了一些内容,得到了关于 C++ & OpenGL & GLSL 性能的奇怪结果。
在第一个程序中,我使用片段着色器将像素绘制到纹理,然后渲染纹理。纹理的 mag\min 为GL_NEAREST。
在第二个程序中,我使用片段着色器并直接渲染到屏幕上。
为什么第二个程序更快?渲染纹理不是比重复相同的动作更快吗?
这就像拍摄 AAA 游戏的视频,然后在同一台计算机上显示它,并随着视频获得较低的 FPS。
片段着色器是:
uniform int mx,my;
void main(void) {
vec2 p=gl_FragCoord.xy;
p-=vec2(mx,my);
if (p.x<0.0)
p.x=-p.x;
if (p.y<0.0)
p.y=-p.y;
float dis=sqrt(p.x*p.x+p.y*p.y);
dis+=(abs(p.x)+abs(p.y))-(abs(p.x)-abs(p.y));
p.x/=dis;
p.y/=dis;
gl_FragColor=vec4(p.x,p.y,0.0,1.0);
}
Run Code Online (Sandbox Code Playgroud) 我如何设置 opengl 光泽度浮动,以便我可以在着色器程序中使用它gl_FrontMaterial.shininess?
我尝试了这个glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100f);,但亮点并没有变小,所以我猜光泽没有改变。
编辑: 这是我使用的片段着色器程序:
varying vec4 varyingColor;
varying vec3 varyingNormal;
varying vec4 varyingVertex;
varying vec2 varyingTexCoord;
uniform sampler2D my_color_texture;
varying vec3 varyingEyeVec;
void main() {
vec3 vertexPosition = (gl_ModelViewMatrix * varyingVertex).xyz;
vec3 surfaceNormal = normalize((gl_NormalMatrix * varyingNormal).xyz);
vec3 lightDirection = normalize(gl_LightSource[0].position.xyz - vertexPosition);
float diffuseLightIntensity = max(0, dot(surfaceNormal, lightDirection));
gl_FragColor.rgb = diffuseLightIntensity * varyingColor.rgb;
gl_FragColor += gl_LightModel.ambient;
vec3 reflectionDirection = normalize(reflect(lightDirection, surfaceNormal));
vec3 eyeVecNormal = normalize(varyingEyeVec);
float specular = max(0.0, dot(eyeVecNormal, reflectionDirection)); …Run Code Online (Sandbox Code Playgroud) 我尝试在一个 GLSL 着色器中使用多个统一块,并使用以下代码调试我的着色器以打印可用的统一块:
void printUniformBlocks(void)
{
GLint numBlocks;
GLint nameLen;
vector<string> nameList;
glGetProgramiv(this->program, GL_ACTIVE_UNIFORM_BLOCKS, &numBlocks);
nameList.reserve(numBlocks);
cout << "found " << numBlocks << " block in shader" << endl;
for (int blockIx = 0; blockIx < numBlocks; blockIx++) {
glGetActiveUniformBlockiv(this->program, blockIx, GL_UNIFORM_BLOCK_NAME_LENGTH, &nameLen);
vector<GLchar> name;
name.resize(nameLen);
glGetActiveUniformBlockName(this->program, blockIx, nameLen, NULL, &name[0]);
nameList.push_back(std::string());
nameList.back().assign(name.begin(), name.end() - 1); //Remove the null terminator.
}
for (unsigned int il = 0; il < nameList.size(); il++) {
cout << "Block name: " << nameList[il] << …Run Code Online (Sandbox Code Playgroud) 我知道使用一个非常简单的顶点着色器
attribute vec3 aVertexPosition;
attribute vec4 aVertexColor;
uniform mat4 uMVMatrix;
uniform mat4 uPMatrix;
varying vec4 vColor;
void main(void) {
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
vColor = aVertexColor;
}
Run Code Online (Sandbox Code Playgroud)
和一个非常简单的片段着色器,比如
precision mediump float;
varying vec4 vColor;
void main(void) {
gl_FragColor = vColor;
}
Run Code Online (Sandbox Code Playgroud)
绘制一个带有红色、蓝色和绿色顶点的三角形最终会得到一个像这样的三角形

我的问题是:
作为学习片段着色器/矢量数学的练习,我正在尝试编写一个后处理着色器,它根据 P 和屏幕 C 中心之间的矢量 PC 的角度(以弧度为单位)为屏幕上的每个点 P 着色。
为了简单起见,我将在灰度中进行此操作,但是可以在这里看到我想要的效果的一个很好的说明,色调随着角度的变化而变化,并且色调形成一个循环。
http://demosthenes.info/assets/images/hsl-color-wheel-trans.png
我四处搜索,寻找有关查找向量之间的角度的信息,并从这些示例中我得到了这里:
#version 110
uniform sampler2D tex0; //Color info
void main()
{
vec2 ScreenCenter = vec2(0.5 , 0.5);
vec2 texCoord = gl_TexCoord[0].st;
vec2 deltaTexCoord = ( texCoord - ScreenCenter.xy);
float angle = dot(deltaTexCoord , vec2(0,-1));
//I've made attempts here to mess with acos as well as angle=pow(angle, somefloat) and
//have not gotten desired results
gl_FragColor = vec4( angle , angle, angle, 1.0 );
}
Run Code Online (Sandbox Code Playgroud)
然而,这段代码产生线性渐变,而不是我想要的效果。
我是 OpenGL 的新手,今天我试图编译一些着色器程序,以便在 android 操作系统中使用 OpenGL 的 GPGPU。我有两个问题:1)我只想编写片段着色器,是否也需要编写顶点着色器?2) 我在编译着色器源代码时遇到了一些错误。我的源代码定义为:
final String src = "#version 310 es\n" +
"uniform sampler2D texUnit;\n" +
"uniform int sequence;\n" +
"void main(void)\n" +
"{\n" +
"const vec3 DotValue = {0.299f , 0.587f , 0.114f};\n" +
"vec2 texCoord = gl_TexCoord[0].xy;\n" +
"vec4 CurrentPixelData = texture2D(texUnit, texCoord);\n" +
"float temp = CurrentPixelData.x * DotValue.x + CurrentPixelData.y * DotValue.y + CurrentPixelData.z * DotValue.z;\n" +
"vec4 result = {temp,temp,temp,CurrentPixelData.w};\n" +
"gl_FragColor = result;\n" +
"}\n";Run Code Online (Sandbox Code Playgroud)
创建着色器的代码是:
int fragment = …Run Code Online (Sandbox Code Playgroud)我想在我的程序中使用计算着色器。我使用 C# 和 OpenTK 来使用着色器。我的问题:我所有的计算机都有 0 workGroupCount 和 0 workGroupSize。我用这个 OpenTK 代码得到这些值:
int workGroupCount = GL.GetInteger((GetPName)All.MaxComputeWorkGroupCount);
int workGroupSize = GL.GetInteger((GetPName)All.MaxComputeWorkGroupSize);
int workGroupInvocations = GL.GetInteger((GetPName)All.MaxComputeWorkGroupInvocations);
Run Code Online (Sandbox Code Playgroud)
Vertex、Fragment 和 Tesselation 着色器在我的计算机上运行良好。OpenGL 4.3 及以上版本。为什么我有 0 个工作组计数?
我正在渲染一个纹理,我被困在需要从某些特定索引中选择值以更新当前索引的地方。
对于 EG :
float someColor = texture2D(u_image, vTexCoord).r; //assume u_image is 10*10 image
if (someColor.r > 0.5) {
someColor = someColorPalette[(zr*zcols)+(zc-1)]; //someColorPalette is another texture
//where (zr*zcols)+(zc-1) is getting the pixel value from some index using the calculation
}
Run Code Online (Sandbox Code Playgroud)
`
在上面的代码片段中,我知道这someColorPalette[(zr*zcols)+(zc-1)]是一个 CPU 语句,在 opengl 中不起作用。任何人都可以建议一些替代解决方案来读取纹理索引吗?
我正在尝试解决如何围绕特定枢轴点/轴扭曲 Three.js 场景中的所有坐标。描述它的最好方法是,好像我在场景中的某个地方放置了一根管子,场景中的其他所有东西都将围绕该轴弯曲,并与该轴保持相同的距离。
如果有帮助,这张图就是我想要实现的。上半部分就像是从侧面看场景,下半部分就像是从某个角度看。红点/线是枢轴点所在的位置。
更复杂的是,我想阻止曲线/经线绕回自身,因此曲线在水平或垂直时停止,如图中右上角的示例。
任何有关如何使用 GLSL 着色器实现这一目标的见解,最好是在 Three.js 中,但如果它们可以以其他方式清楚地描述,我会尝试进行翻译?
我也愿意接受替代方法,因为我不确定如何最好地描述我所追求的。基本上我想要一个倒置的“弯曲世界”效果,其中场景弯曲并远离你。
我刚刚从Wikipedia 中为 3D 投影做了一些数学运算,因为我注意到它们很简单,不需要库。它确实有效,但立方体在移动时会留下痕迹。请注意,立方体实际上并没有移动,我实际上是在改变相机位置,使立方体看起来像是在移动。
没有必要指出我正在做的 100 个坏习惯,我知道,这只是一个快速测试。
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <glad/glad.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_opengl.h>
#include <math.h>
#include "utils.h"
#include "keys.h"
char p = 1;
typedef struct Vec3 {
float x;
float y;
float z;
} Vec3;
void Mat_iden(float *m, Uint32 s) {
Uint32 i = 0;
Uint32 unt = s + 1;
while (i < s) {
m[unt * i] = 1;
i++;
}
}
float one[3][3];
float two[3][3];
float three[3][3]; …Run Code Online (Sandbox Code Playgroud)