簡體   English   中英

GLFW-此代碼有很多錯誤

[英]GLFW - many errors is this code

因此,我的大學講師給了我們這段代碼,但是它沒有用..從來沒有,而且到目前為止沒有人能夠使它起作用。.我們是愚蠢的還是講師給了我們破碎的資料? 我嚴重無法解決這個問題,需要幫助,我設法解決了許多錯誤,但是此后,盡管這是“ 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;

}

您的行結尾似乎被弄亂了。

您的代碼中有多行,實際代碼沒有分成兩行,因此該代碼現在與注釋位於同一行,因此不會被執行。 這是您的程序,具有正確的行尾:

//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