C ++ OpenGL着色器错误

时间:2016-02-03 01:59:26

标签: opengl opengl-shader-builder

#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <fstream>

#include <GL\glew.h>
#include <GL\freeglut.h>
#include <GLFW\glfw3.h>

std::string LoadFileToString(const char* filepath)
{
std::string fileData;
std::ifstream stream(filepath, std::ios::in);

if (stream.is_open())
{
    std::string line = "";

    while (getline(stream, line))
    {
        fileData += "\n" + line;

    }
    stream.close();

}
return fileData;

}

GLuint LoadShaders(const char* vertShaderPath, const char* fragshaderPath)
{
GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);
GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);

std::string vertShaderSource = LoadFileToString(vertShaderPath);
std::string fragShaderSource = LoadFileToString(fragshaderPath);

const char* rawVertShaderSource = vertShaderSource.c_str();
const char* rawFragShaderSource = fragShaderSource.c_str();

glShaderSource(vertShader, 1, &rawVertShaderSource, NULL);
glShaderSource(fragShader, 1, &rawFragShaderSource, NULL);

glCompileShader(vertShader);
glCompileShader(fragShader);

GLuint program = glCreateProgram();
glAttachShader(program, vertShader);
glAttachShader(program, fragShader);
glLinkProgram(program);

return program;

}



int main()
{
if (glfwInit() == false)
{
    fprintf(stderr, "GLWF dailed to initialise");
    return -1;
}

//4 AA
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_PROFILE);

GLFWwindow* window;
window = glfwCreateWindow(640, 480, "My OpenGL", NULL, NULL);

if (!window)
{
    fprintf(stderr, "Window failed to create"); 
    glfwTerminate();
    return -1;
}

glfwMakeContextCurrent(window);
glewExperimental = true;

if (glewInit() != GLEW_OK)
{
    fprintf(stderr, "GLEW failed to initialise");
    glfwTerminate();

    return -1;


}

GLuint vaoID;
glGenVertexArrays(1, &vaoID);
glBindVertexArray(vaoID);

static const GLfloat verts[] = {
    -1.0f, -1.0f, 0.0f,
    1.0f, -1.0f, 0.0f,
    0.0f, 1.0f, 0.0f

};//end static

GLuint program = LoadShaders("shader.vertshader", "shader.fragshader");

GLuint VboID;

glGenBuffers(1, &VboID);
glBindBuffer(GL_ARRAY_BUFFER, VboID);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), verts, GL_STATIC_DRAW);

do{
    glClear(GL_COLOR_BUFFER_BIT);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER, VboID);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);

    glUseProgram(program);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    glDisableVertexAttribArray(0);

    glfwSwapBuffers(window);
    glfwPollEvents();

} while (glfwWindowShouldClose(window) == false);

return 0;

}

当我运行这个程序时,我总是在窗口上看到“窗口无法创建”,而我猜应该得到一个三角形。有人可以帮我查一下吗?

什么是''homework4.exe'(Win32):加载'C:\ Users \ Wanhua \ Desktop \ Computer graphic \ homework4 \ homework4 \ glew32.dll'。模块没有符号构建。“这意味着?? ?

,输出如下:

'homework4.exe' (Win32): Loaded 'C:\Users\Wanhua\Desktop\Computer graphic\homework4\Debug\homework4.exe'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Users\Wanhua\Desktop\Computer graphic\homework4\homework4\glew32.dll'. Module was built without symbols.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shell32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp120d.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\opengl32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\combase.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\shlwapi.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\glu32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ddraw.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dciman32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmm.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\winmmbase.dll'. Symbols     loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cfgmgr32.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\devobj.dll'. Symbols loaded.
'homework4.exe' (Win32): Loaded 'C:\Windows\SysWOW64\dwmapi.dll'. Symbols loaded.
'homework4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\winmmbase.dll'
'homework4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\devobj.dll'
'homework4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\cfgmgr32.dll'
'homework4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\winmm.dll'
'homework4.exe' (Win32): Unloaded 'C:\Windows\SysWOW64\dwmapi.dll'
The program '[14652] homework4.exe' has exited with code -1 (0xffffffff).

1 个答案:

答案 0 :(得分:1)

This call looks invalid:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_PROFILE);

The first parameter is fine, but you're passing the same value for the second parameter. The second parameter specifies which profile you want to use, where the valid options are:

  • GLFW_OPENGL_COMPAT_PROFILE: compatibility profile
  • GLFW_OPENGL_CORE_PROFILE: core profile

So for using the core profile, the correct call is:

glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);