简体   繁体   English

GLFW-此代码有很多错误

[英]GLFW - many errors is this code

So my university lecturer gave us this code and it doesn't work.. it never has and no one has been able to get it to work so far.. are we being stupid or is our lecturer giving us broken material? 因此,我的大学讲师给了我们这段代码,但是它没有用..从来没有,而且到目前为止没有人能够使它起作用。.我们是愚蠢的还是讲师给了我们破碎的资料? I seriously can't figure this out and need help, i managed to get part way through in fixing many mistakes but after that the issues got harder and harder to solve despite this being '100% working' code.... side note: all the directories are formatted correctly and additional dependencies have all been set up correctly to the best of my knowledge. 我严重无法解决这个问题,需要帮助,我设法解决了许多错误,但是此后,尽管这是“ 100%有效”的代码,但问题却越来越难解决。...旁注:据我所知,所有目录的格式都正确,其他依赖项也都已正确设置。

//First Shader Handling Program

#include "stdafx.h"

#include "gl_core_4_3.hpp"

#include <GLFW/glfw3.h>

int _tmain(int argc, _TCHAR* argv[])
{
    //Select the 4.3 core profile
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //Start the OpenGL context and open a window using the //GLFW helper library

    if (!glfwInit()) {
        fprintf(stderr, "ERROR: could not start GLFW3\n");
        glfwTerminate();
        return 1;
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "First GLSL Triangle", NULL, NULL);

    if (!window) {
        fprintf(stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }

    glfwMakeContextCurrent(window);


    //Load the OpenGL functions for C++ gl::exts::LoadTest didLoad = gl::sys::LoadFunctions(); if (!didLoad) {
    //Load failed
    fprintf(stderr, "ERROR: GLLoadGen failed to load functions\n");

    glfwTerminate();
    return 1;
}

printf("Number of functions that failed to load : %i.\n", didLoad.GetNumMissing());

//Tell OpenGL to only draw a pixel if its shape is closer to //the viewer

//i.e. Enable depth testing with smaller depth value //interpreted as being closer gl::Enable(gl::DEPTH_TEST); gl::DepthFunc(gl::LESS);

//Set up the vertices for a triangle
float points[] = {
    0.0f, 0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f
};


//Create a vertex buffer object to hold this data GLuint vbo=0;

gl::GenBuffers(1, &vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(gl::ARRAY_BUFFER, 9 * sizeof(float), points,
    gl::STATIC_DRAW);


//Create a vertex array object
GLuint vao = 0;
gl::GenVertexArrays(1, &vao);
gl::BindVertexArray(vao);
gl::EnableVertexAttribArray(0);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::VertexAttribPointer(0, 3, gl::FLOAT, FALSE, 0, NULL);

//The shader code strings which later we will put in //separate files

//The Vertex Shader
const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main() {"
        " gl_Position = vec4(vp, 1.0);"
"}";

//The Fragment Shader
const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"

"void main() {"
        " frag_colour = vec4(1.0, 0.5, 0.0, 1.0);"
"}";

//Load the strings into shader objects and compile GLuint vs = gl::CreateShader(gl::VERTEX_SHADER); gl::ShaderSource(vs, 1, &vertex_shader, NULL); gl::CompileShader(vs);

GLuint fs = gl::CreateShader(gl::FRAGMENT_SHADER); gl::ShaderSource(fs, 1, &fragment_shader, NULL); gl::CompileShader(fs);

//Compiled shaders must be compiled into a single executable //GPU shader program

//Create empty program and attach shaders GLuint shader_program = gl::CreateProgram(); gl::AttachShader(shader_program, fs); gl::AttachShader(shader_program, vs); gl::LinkProgram(shader_program);

//Now draw
while (!glfwWindowShouldClose(window)) {
    //Clear the drawing surface
    gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
    gl::UseProgram(shader_program);
    gl::BindVertexArray(vao);

    //Draw point 0 to 3 from the currently bound VAO with
    //current in-use shader
    gl::DrawArrays(gl::TRIANGLES, 0, 3);

    //update GLFW event handling
    glfwPollEvents();

    //Put the stuff we have been drawing onto the display glfwSwapBuffers(window);

}

//Close GLFW and end
glfwTerminate();

return 0;

}

Your line endings seems to been mangled. 您的行结尾似乎被弄乱了。

There are multiple lines in your code where actual code was not broken into two lines, so that code is now on the same line as a comment and therefor not being executed. 您的代码中有多行,实际代码没有分成两行,因此该代码现在与注释位于同一行,因此不会被执行。 This is your program with proper line endings: 这是您的程序,具有正确的行尾:

//First Shader Handling Program

#include "stdafx.h"

#include "gl_core_4_3.hpp"

#include <GLFW/glfw3.h>

int _tmain(int argc, _TCHAR* argv[])
{
    //Select the 4.3 core profile
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, TRUE);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    //Start the OpenGL context and open a window using the 
    //GLFW helper library

    if (!glfwInit()) {
        fprintf(stderr, "ERROR: could not start GLFW3\n");
        glfwTerminate();
        return 1;
    }

    GLFWwindow* window = glfwCreateWindow(640, 480, "First GLSL Triangle", NULL, NULL);

    if (!window) {
        fprintf(stderr, "ERROR: could not open window with GLFW3\n");
        glfwTerminate();
        return 1;
    }

    glfwMakeContextCurrent(window);


    //Load the OpenGL functions for C++ 
    gl::exts::LoadTest didLoad = gl::sys::LoadFunctions(); 
    if (!didLoad) {
        //Load failed
        fprintf(stderr, "ERROR: GLLoadGen failed to load functions\n");

        glfwTerminate();
        return 1;
    }

printf("Number of functions that failed to load : %i.\n", didLoad.GetNumMissing());

//Tell OpenGL to only draw a pixel if its shape is closer to 
//the viewer

//i.e. Enable depth testing with smaller depth value
//interpreted as being closer 
gl::Enable(gl::DEPTH_TEST);
gl::DepthFunc(gl::LESS);

//Set up the vertices for a triangle
float points[] = {
    0.0f, 0.5f, 0.0f,
    0.5f, -0.5f, 0.0f,
    -0.5f, -0.5f, 0.0f
};

//Create a vertex buffer object to hold this data 
GLuint vbo=0;

gl::GenBuffers(1, &vbo);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::BufferData(gl::ARRAY_BUFFER, 9 * sizeof(float), points, gl::STATIC_DRAW);

//Create a vertex array object
GLuint vao = 0;
gl::GenVertexArrays(1, &vao);
gl::BindVertexArray(vao);
gl::EnableVertexAttribArray(0);
gl::BindBuffer(gl::ARRAY_BUFFER, vbo);
gl::VertexAttribPointer(0, 3, gl::FLOAT, FALSE, 0, NULL);

//The shader code strings which later we will put in 
//separate files

//The Vertex Shader
const char* vertex_shader =
"#version 400\n"
"in vec3 vp;"
"void main() {"
        " gl_Position = vec4(vp, 1.0);"
"}";

//The Fragment Shader
const char* fragment_shader =
"#version 400\n"
"out vec4 frag_colour;"

"void main() {"
        " frag_colour = vec4(1.0, 0.5, 0.0, 1.0);"
"}";

//Load the strings into shader objects and compile 
GLuint vs = gl::CreateShader(gl::VERTEX_SHADER);
gl::ShaderSource(vs, 1, &vertex_shader, NULL);
gl::CompileShader(vs);

GLuint fs = gl::CreateShader(gl::FRAGMENT_SHADER); 
gl::ShaderSource(fs, 1, &fragment_shader, NULL);
gl::CompileShader(fs);

//Compiled shaders must be compiled into a single executable 
//GPU shader program

//Create empty program and attach shaders 
GLuint shader_program = gl::CreateProgram(); 
gl::AttachShader(shader_program, fs);
gl::AttachShader(shader_program, vs);
gl::LinkProgram(shader_program);

//Now draw
while (!glfwWindowShouldClose(window)) {
    //Clear the drawing surface
    gl::Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
    gl::UseProgram(shader_program);
    gl::BindVertexArray(vao);

    //Draw point 0 to 3 from the currently bound VAO with
    //current in-use shader
    gl::DrawArrays(gl::TRIANGLES, 0, 3);

    //update GLFW event handling
    glfwPollEvents();

    //Put the stuff we have been drawing onto the display 
    glfwSwapBuffers(window);
}

//Close GLFW and end
glfwTerminate();

return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM