OpenGL着色器不绘图

时间:2017-03-19 20:29:44

标签: c opengl

我目前正在使用OpenGL 4.5,并阅读OpenGL Superbible第七版。现在,我没有使用他们提供的项目,但我改为使用我自己的项目。但是,根据我到目前为止的理解,它应该在我的屏幕上放一些东西,但我什么都没得到。

我有3个文件。 Source.cLoader.hLoader.c

Source.c代码

#include <stdio.h>
#include <stdlib.h>
#include <GL\glew.h>
#include <GL\GL.h>
#include <GL\GLU.h>
#include <GL\wglew.h>
#include <GLFW\glfw3.h>
#include "Loader.h"

#define CL_BUFFER (GLfloat[4]) { 0.2f, 0.2f, 0.2f, 1.0f }

int main(void);
void err(int error, const char * msg);
void keypress(GLFWwindow *window, int key, int scancode, int action, int mods);

int main(void) {
    if (!glfwInit())
        return -1;


    GLFWwindow *window = glfwCreateWindow(1024, 768, "Fididdler", NULL, NULL);
    glfwWindowHint(GLFW_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_VERSION_MINOR, 5);
    if (window == NULL)
        return -1;

    glfwSetErrorCallback(err);
    glfwSetKeyCallback(window, keypress);
    glfwMakeContextCurrent(window);

    if (glewInit() != GLEW_OK)
        return -1;

    printf("GL Version String: %s \n", glGetString(GL_VERSION));

    GLuint RProg = LoadAllShaders();

    printf("Loading complete");
    while (!glfwWindowShouldClose(window)) {
        glClearBufferfv(GL_COLOR, 0, CL_BUFFER);
        glUseProgram(RProg);
        glDrawArrays(GL_TRIANGLES, 0, 3); 
        /* End drawing logic*/

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 1;
}

void err(int error, const char * msg) {
    printf("Error: %s", msg);
}

void keypress(GLFWwindow *window, int key, int scancode, int action, int mods) {
    if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
        glfwSetWindowShouldClose(window, GLFW_TRUE);
    }
}

Loader标题:

#include <GL\glew.h>
#include <GL\GL.h>
#include <GL\GLU.h>


#pragma once

extern GLuint LoadAllShaders();
extern void LogCompileStatus(GLuint Shader, char* ShaderName);

Loader C文件

#include "Loader.h"
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include <GLFW/glfw3.h>

static const GLchar* VSource[] = {
    "#version 450 core\n",
    "void main(void)\n",
    "{\n",
    // Right here is what I am expecting to be drawn, but it is not
    "   const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0),",
    "                                    vec4(-0.25, -0.25, 0.5, 1.0),",
    "                                    vec4(0.25, 0.25, 0.5, 1.0));\n",
    "   gl_Position = vertices[gl_VertexID];\n",
    "}\n"
};

static const GLchar* FSource[] = {
    "#version 450 core\n",
    "out vec4 color;\n",
    "void main(void)\n",
    "{\n",
    "   color = vec4(0.0, 0.8, 1.0, 1.0);\n",
    "}\n"
};

GLuint LoadAllShaders() {
    GLuint VShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(VShader, 1, VSource, NULL);
    glCompileShader(VShader);
    LogCompileStatus(VShader, "VShader");

    GLuint FShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(FShader, 1, FSource, NULL);
    glCompileShader(FShader);
    LogCompileStatus(FShader, "FShader");


    GLuint Program = glCreateProgram();

    glAttachShader(Program, VShader);
    glAttachShader(Program, FShader);
    glLinkProgram(Program);

    glDeleteShader(VShader);
    glDeleteShader(FShader);

    return Program;
}

void LogCompileStatus(GLuint Shader, char* ShaderName) {
    // Checking compile status of VShader
    if (ShaderName == NULL || sizeof(ShaderName) == 0)
        ShaderName = ("Unnamed Shader with ID: %i" + (char)&Shader);
    GLuint ShaderSuccess = GL_FALSE;
    glGetShaderiv(Shader, GL_COMPILE_STATUS, &ShaderSuccess);

    if (ShaderSuccess == GL_TRUE)
        printf("Shader %s successfully compiled\n", ShaderName);
    else {
        GLint LogLength;
        glGetShaderiv(Shader, GL_INFO_LOG_LENGTH, &LogLength);

        char* buffer = (char*)malloc(LogLength);
        glGetShaderInfoLog(Shader, LogLength, NULL, buffer);

        printf("%s failed to compile.\n%s\n", ShaderName, buffer);
        free(buffer);
    }
}

据我所知,我正在做所有必要的事情&#34;从教程,但我没有得到相同的结果。这是我得到的输出。

VS + OpenGL + Console

1 个答案:

答案 0 :(得分:0)

我知道为什么我声明VSource的方式不正确,但重新引用教科书中的来源显示在引号末尾(对于GLSL源)使用逗号是打破它的原因。

代码是

/// EOL commas broke this
static const GLchar* VSource[] = {
    "#version 450 core\n",
    "void main(void)\n",
    "{\n",
    "   const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0),",
    "                                    vec4(-0.25, -0.25, 0.5, 1.0),",
    "                                    vec4(0.25, 0.25, 0.5, 1.0));\n",
    "   gl_Position = vertices[gl_VertexID];\n",
    "}\n"
};

static const GLchar* FSource[] = {
    "#version 450 core\n"
    "out vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "   color = vec4(0.0, 0.8, 1.0, 1.0);\n"
    "}\n"
};

代码应

static const GLchar* VSource[] = {
    "#version 450 core\n",
    "void main(void)\n",
    "{\n",
    "   const vec4 vertices[3] = vec4[3](vec4(0.25, -0.25, 0.5, 1.0),\n",
    "                                    vec4(-0.25, -0.25, 0.5, 1.0),\n",
    "                                    vec4(0.25, 0.25, 0.5, 1.0));\n",
    "   gl_Position = vertices[gl_VertexID];\n",
    "}\n"
};

static const GLchar* FSource[] = {
    "#version 450 core\n"
    "out vec4 color;\n"
    "void main(void)\n"
    "{\n"
    "   color = vec4(1.0, 1.0, 1.0, 1.0);\n"
    "}\n"
};

编辑:

我只知道它为什么不起作用。 &#34;着色器代码&#34;人眼是正确的,但它只是读取顶点着色器的第一行,这是一个空的着色器程序。 GLSL编译器读取1个条目,而不是所有条目。