Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ glsl-光线行进不在片段着色器中工作_C++_Opengl_Glsl_Glfw - Fatal编程技术网

C++ glsl-光线行进不在片段着色器中工作

C++ glsl-光线行进不在片段着色器中工作,c++,opengl,glsl,glfw,C++,Opengl,Glsl,Glfw,我现在正在学习GLSL和ray marching。我试图将其转换为我的c++OpenGL项目,但失败了 以下是我的代码: Main.cpp: GLuint screenWidth = 1280, screenHeight = 720; int main () { GLFWwindow* window = NULL; const GLubyte* renderer; const GLubyte* version; if (!glfwInit ()) {

我现在正在学习GLSL和ray marching。我试图将其转换为我的
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 dir = rayDirection(45.0, vec2(1280,720), surfacePosition.xy);//<-- explicitly my screen resolution, just to test.
vec3 dir=光线方向(45.0,vec2(1280720),surfacePosition.xy)//
始终记得检查编译、验证和链接状态。问题是您定义了
gl\u FragCoord
,这与内置的
gl\u FragCoord
冲突

假设您的
着色器
类在默认情况下不调用
glUseProgram()
。然后需要在调用任何
glUniform*()
之前显式调用它。 请注意,任何
glGetUniformLocation()
glGetAttriblLocation()
glGetActiveUniform()
glGetActiveAttrib()
调用都没有这种需要(出于明显的原因)

您还启用了深度测试,但从未清除深度缓冲区。因此,要么禁用深度测试,要么清除深度缓冲区

最后,ShaderToy的
fragCoord
相当于
gl_fragCoord
。但是,您的
表面位置
不是。所以只要使用
gl_FragCoord
就可以了

因此,使用
gl_FragCoord
而不是
surfacePosition
您应该看到(如ShaderToy上的):

另外,
glGenBuffers(1,&EBO)
是多余的,我不知道为什么要将视口设置为其大小的两倍。对
glGetAttriblLocation()
的调用也是多余的,不仅仅是因为您没有使用它。但因为您正在使用布局限定符

请注意,您还有
时间
鼠标
,如果最终使用它们,同样需要指定它们


正如我前面提到的,因为您可以使用
gl\u FragCoord
。这仍然意味着不需要
surfacePosition
screenRatio

我有:
GLint resolutionLocation=glGetUniformLocation(ourShader.Program,“resolution”);glUniform2f(分辨率位置、屏幕宽度、屏幕高度),但它仍然不工作。顺便说一下,我正在使用
OSX
Xcode
,没有收到您的错误。甚至不是片段着色器的编译错误。这是链接错误,不是编译错误。另外,你在哪里定义
屏幕宽度
屏幕高度
?我想你是对的,
分辨率
没有通过。我只是用
glUniform2f(glGetUniformLocation(ourShader.Program,“resolution”)、1.0f(GLfloat)屏幕高度做了一个相当好的测试
,然后将
fragment_着色器
更改为仅
color=vec4(分辨率.x,0.0,0.0,1.0)。我希望看到红色,但我还是会变黑。链接之间的某个地方有问题。是的,您确实需要在任何
glUniform*()
之前调用
glUseProgram()
。请注意,
glGetUniformLocation()
glGetAttriblLocation()
glGetActiveUniform()
glGetActiveAttrib()
没有这种需要(原因很明显)。
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"