我正在学习 GLSL
和 ray-marching。我试图转换这个 shader-toy进入我的 c++ OpenGL
项目但失败了。
下面是我的代码:
Main.cpp:
GLuint screenWidth = 1280, screenHeight = 720;
int main ()
{
GLFWwindow* window = NULL;
const GLubyte* renderer;
const GLubyte* version;
if (!glfwInit ()) {
fprintf (stderr, "ERROR: could not start GLFW3\n");
return 1;
}
glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 1);
glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow (screenWidth, screenHeight, "OpenGL", NULL, NULL);
if (!window) {
fprintf (stderr, "ERROR: could not open window with GLFW3\n");
glfwTerminate ();
return 1;
}
glfwMakeContextCurrent (window);
glewExperimental = GL_TRUE;
glewInit ();
glViewport(0, 0, screenWidth*2, screenHeight*2);
glEnable (GL_DEPTH_TEST);
glDepthFunc (GL_LESS);
Shader ourShader("vertex_shader.glsl", "fragment_shader.glsl");
GLint positionLocation = glGetAttribLocation(ourShader.Program, "a_position");
glUniform2f(glGetUniformLocation(ourShader.Program, "resolution"), (GLfloat)screenWidth, (GLfloat)screenHeight);
GLint mouseLocation = glGetUniformLocation(ourShader.Program, "mouse");
glUniform4f(mouseLocation, 0.0, 0.0, 0.0, 0.0);
GLfloat mx = glm::max(screenWidth, screenHeight);
GLfloat xdivmx = screenWidth/mx; //Coordinates range from [-1,1].
GLfloat ydivmx = screenHeight/mx;
GLint screenRatioLocation = glGetUniformLocation(ourShader.Program, "screenRatio");
glUniform2f(screenRatioLocation, xdivmx, ydivmx);
GLfloat timeLocation = glGetUniformLocation(ourShader.Program, "time");
glUniform1f(timeLocation, deltaTime);
GLfloat vertices[] = {
-1.0f, -1.0f,
1.0f, -1.0f,
-1.0f, 1.0f,
-1.0f, 1.0f,
1.0f, -1.0f,
1.0f, 1.0f // Top Left
};
GLuint VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
ourShader.Use();
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 6);=
glBindVertexArray(0);
glfwSwapBuffers(window);
}
// Properly de-allocate all resources once they've outlived their purpose
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
// Terminate GLFW, clearing any resources allocated by GLFW.
glfwTerminate();
return 0;
}
顶点代码:
vertex_shader.glsl:
#version 410 core
layout (location = 0) in vec2 a_position;
out vec2 surfacePosition;
uniform vec2 screenRatio;
uniform vec2 resolution;
void main() {
surfacePosition = a_position * screenRatio;
gl_Position = vec4(a_position, 0, 1);
}
片段代码:
#version 410 core
in vec2 surfacePosition;
out vec4 color;
vec4 gl_FragCoord;
uniform float time;
uniform vec2 resolution;
uniform vec4 mouse;
const int MAX_MARCHING_STEPS = 255;
const float MIN_DIST = 0.0;
const float MAX_DIST = 100.0;
const float EPSILON = 0.0001;
#define PI 3.1415926535898 // That was from memory, so if things start flying off the screen...
float sphereSDF(vec3 samplePoint) {
return length(samplePoint) - 1.0;
}
float sceneSDF(vec3 samplePoint) {
return sphereSDF(samplePoint);
}
float shortestDistanceToSurface(vec3 eye, vec3 marchingDirection, float start, float end) {
float depth = start;
for (int i = 0; i < MAX_MARCHING_STEPS; i++) {
float dist = sceneSDF(eye + depth * marchingDirection);
if (dist < EPSILON) {
return depth;
}
depth += dist;
if (depth >= end) {
return end;
}
}
return end;
}
vec3 rayDirection(float fieldOfView, vec2 size, vec2 fragCoord) {
vec2 xy = fragCoord - size / 2.0;
float z = size.y / tan(radians(fieldOfView) / 2.0);
return normalize(vec3(xy, -z));
}
void main() {
vec3 dir = rayDirection(45.0, resolution.xy, surfacePosition.xy);
vec3 eye = vec3(0.0f, 0.0f, 5.0f);
float dist = shortestDistanceToSurface(eye, dir, MIN_DIST, MAX_DIST);
if (dist > MAX_DIST - EPSILON) {
// Didn't hit anything
color = vec4(0.0, 0.0, 0.0, 0.0);
return;
}
color = vec4(1.0, 0.0, 0.0, 1.0);
}
有了这个,我只看到黑屏。但是,如果我更改为 vec3 eye = vec3(0.0f, 0.0f, 1.0f);
,我将让整个屏幕变红。我怀疑与 surfacePosition.xy
有关,但我不确定如何解决这个问题。我也试过 gl_FragCoord.xy
但还是黑屏。
更新 01
似乎我所有的 uniform
数据都没有通过着色器。
我只是做了一个完全测试:
glUniform2f(glGetUniformLocation(ourShader.Program, "resolution"), 0.5, (GLfloat)screenHeight);
而且我仍然黑屏。当我转到 fragment Shader
并更改此行时:
vec3 dir = rayDirection(45.0, vec2(1280,720), surfacePosition.xy);//<-- explicitly my screen resolution, just to test.
然后我如愿得到了红圈。
最佳答案
Link Failure: Fragment info
-------------
0(6) : error C5109: variable "gl_FragCoord" domain conflicts with semantics "WPOS"
永远记得检查编译、验证和链接状态。问题是您定义了 gl_FragCoord
,这与内置的 gl_FragCoord
冲突。
假设您的 Shader
类在默认情况下不调用 glUseProgram()
。然后您需要在调用任何 glUniform*()
之前显式调用它。
请注意,任何 glGetUniformLocation()
、glGetAttribLocation()
、glGetActiveUniform()
和 glGetActiveAttrib()
调用都没有这种需要(出于显而易见的原因)。
您还启用了深度测试,但从不清除深度缓冲区。因此,要么禁用深度测试,要么清除深度缓冲区。
最后,ShaderToy 的 fragCoord
等同于 gl_FragCoord
。然而,您的 surfacePosition
不是。所以只需使用 gl_FragCoord
它就可以保留。
因此使用 gl_FragCoord
而不是 surfacePosition
你应该看到(就像在 ShaderToy 上一样):
另外 glGenBuffers(1, &EBO)
是多余的,我不知道为什么要将视口(viewport)设置为两倍大小。对 glGetAttribLocation()
的调用也是多余的,不仅仅是因为您没有使用它。但是因为无论如何您都在使用布局限定符。
请注意,您还有 time
和 mouse
,如果您最终要使用它们,同样需要指定它们。
也像我之前提到的那样,因为您可以使用 gl_FragCoord
。这仍然意味着不需要 surfacePosition
和 screenRatio
。
关于c++ - glsl - 光线行进在片段着色器中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43390669/