OpenGL/GLSL在C中工作在台式PC上,但不在笔记本电脑上

5ssjco0h  于 2023-06-28  发布在  其他
关注(0)|答案(1)|浏览(103)

我试图简单地渲染一个四边形目前在OpenGL与纹理的球员。它在我的PC上工作得很好,向我展示了在四元机上渲染的纹理。我已经附上了纹理图像这个问题的情况下,你想看看(注:它是PNG)。但是在我的笔记本电脑上,它显示我一个黑屏。我知道它把每一个都读成黑色,因为我做了一个快速的GLSL调试:if (texture(u_Texture, Texcoord).xyz == vec3(.0f, .0f, .0f)) v_Texcoord = vec4(1.0f, 1.0f, 1.0f, 1.0f);,它输出一个纯白色的四边形,这表明调试代码认为每个像素都是黑色的。顺便说一下,我松散地遵循了Cherno的GLSL和OpenGL教程。我改变了我的电脑屏幕的赫兹和分辨率,看看这是否是问题所在,它不是。PS:图像文件被找到,图像在我的PC和笔记本电脑上都被正确加载。如果不是,因为图像存储在项目路径中,它也不会在我的PC上工作。
以下是我的程序的一些分类代码片段(用C编写):播放器初始化:

void PlayerInit() {
    playerOrigScale.x /= (float)mode->width / 1000.0f;
    playerOrigScale.y /= (float)mode->height / 1000.0f;
    {
        float playerVerticiesInit[origPlrVertLen] = {
            -1.0f, -1.0f,
            1.0f, -1.0f,
            1.0f, 1.0f,
            -1.0f, 1.0f,
        };
        float texCoordInit[8] = {
            .0f, .0f,
             1.0f, .0f,
            1.0f, 1.0f,
             .0f, 1.0f,
        };
        int i;
        int texCoordLen = lengthOf(texCoordInit);
        for (i = 0; i < texCoordLen; i++) {
            playerVerticies[i + ((i - i / 2) + 1 - (i % 2)) * 2] = texCoordInit[i];
        }
        for (i = 0; i < origPlrVertLen; i++) {
            origPlrPos[i] = playerVerticiesInit[i];
            playerVerticies[i + (i - (i % 2))] = origPlrPos[i] * (i % 2 == 0 ? playerOrigScale.x : playerOrigScale.y);
        }
    }
    verticiesLen = malloc(sizeof(byte));
    *verticiesLen = lengthOf(playerVerticies);
    unsigned int indicies[] = {
        0, 1, 2,
        2, 3, 0
    };
    unsigned int vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    printf("%s\n", glGetString(GL_VERSION));
    unsigned int vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(playerVerticies), playerVerticies, GL_DYNAMIC_DRAW);
    glEnableVertexAttribArray(0);
    int* texAttrib = malloc(sizeof(int));
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (const void*)0);
    unsigned int ibo;
    glGenBuffers(1, &ibo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indicies), indicies, GL_DYNAMIC_DRAW);
    ShaderProgramSource shaderSrc;
    {
        char* filepath = "Basic.shader";
        shaderSrc = ParseShader(filepath);
    }
    printf("vertex:\n%s\n", shaderSrc.vertexSource);
    printf("framgent:\n%s\n", shaderSrc.fragmentSource);
    shader = CreateShader(shaderSrc.vertexSource, shaderSrc.fragmentSource);
    glUseProgram(shader);
    glUniform4f(glGetUniformLocation(shader, "u_Color"), .1f, .6f, .9f, 1.0f);
    glUniform1i(glGetUniformLocation(shader, "u_Texture"), 0);
    glGenTextures(1, &texture);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER);
    glGenerateMipmap(GL_TEXTURE_2D);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, playerAnimator->frames[0][6]);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 4, (void*)(sizeof(float) * 2));
    shouldDraw = true;
}

主函数片段:

{
    stbi_set_flip_vertically_on_load(1);
    playerAnimator = malloc(sizeof(Animator));
    playerAnimator->frames = malloc(sizeof(unsigned char*) * 1);
    playerAnimator->animFrameNo[idle] = 7;
    *playerAnimator->frames = malloc(sizeof(unsigned char*) * playerAnimator->animFrameNo[idle]);
    Init(playerAnimator);
    {
        QueryPerformanceCounter(&pTime);
        char* save = ReadSaveFile("saves/save 1.txt", 500);
        printf("%s\n", save);
        free(save);
        printf("\nLoading save file took %f seconds. Keep in mind not printing save file will improve save access time to around .00125 seconds with lorem ipsum benchmark. This time will increase however with the encryption and the decryption which is yet to be implemented.\n", deltaTime());
    }
    glfwInit();
    monitor = glfwGetPrimaryMonitor();
    mode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    //glfwWindowHint(GLFW_MAXIMIZED, true);
    window = glfwCreateWindow(mode->width, mode->height, title, fullScreen ? monitor : NULL, NULL);
    glfwSetWindowMonitor(window, NULL, 0, 0, mode->width, mode->height, mode->refreshRate);
    glfwMakeContextCurrent(window);
    DWORD frameWaitTime = (DWORD)(1.0f / (float)mode->refreshRate * 1000.0f);
    glfwSwapInterval(vsyncCount);
    if (glewInit() != GLEW_OK) ThrowError("your program is broken");
    PlayerInit();
    float fullScreenET = 0;
    //glUseProgram(0);
    while (!glfwWindowShouldClose(window)) {
        if (!glfwGetWindowAttrib(window, GLFW_FOCUSED)) {
            glfwPollEvents();
            Sleep(frameWaitTime * 10);
            continue;
        }
        if (pressing(GetKeyState(q)) && pressing(GetKeyState(leftControl))) break;
        //37, 39
        if (shouldDraw) {
            glClear(GL_COLOR_BUFFER_BIT);
            shouldDraw = false;
        }
        //glUseProgram(shader);
        //glBindVertexArray(vao);
        //glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, NULL);
        glfwSwapBuffers(window);
        glfwPollEvents();
        Update();
        printf("deltatime is %lf\n", DeltaTime());
        if (fullScreenET > .2f)
        {
            if (pressing(GetKeyState(key_return)) && pressing(GetKeyState(leftAlt))) {
                fullScreen = !fullScreen;
                glfwSetWindowMonitor(window, fullScreen ? monitor : NULL, 0, 0, mode->width, mode->height, mode->refreshRate);
                glfwSwapInterval(vsyncCount);
                fullScreenET = 0;
            }
        }
        else fullScreenET += deltaTime();
        //10000000
        if (shouldDraw) glBufferData(GL_ARRAY_BUFFER, sizeof(playerVerticies), playerVerticies, GL_DYNAMIC_DRAW);
        QueryPerformanceCounter(&pTime);
        //Sleep(frameWaitTime);
    }
    glDeleteTextures(1, &texture);

Basic.shader:

#shader vertex
#version 430 core
layout(location = 0) in vec4 position;
layout(location = 1) in vec2 texcoord;
out vec2 Texcoord;
void main() {
    Texcoord = texcoord;
    gl_Position = position;
}
#shader fragment
#version 430 core
layout(location = 0) in vec4 color;
layout(location = 1) in vec2 Texcoord;
out vec4 v_Texcoord;
uniform vec4 u_Color;
uniform sampler2D u_Texture;
void main() {
    v_Texcoord = texture(u_Texture, Texcoord);
}
//don't add empty line at end of file otherwise it breaks the shader interpretation
moiiocjp

moiiocjp1#

这是我的片段着色器布局的手动规范。非常感谢Erdal Küçük和BDL以及其他帮助的人。奇怪的是,有某种不同的设置在我的个人电脑到我的笔记本电脑。这可能是因为我的PC上的版本非常新。

相关问题