JoeyDeVries/LearnOpenGL

Hello Triangle example

Tsingkai opened this issue · 1 comments

Environment

  • IDE:VS2022
  • OS:Win10
  • Other:the same as example

Problem

#include <glad/glad.h>
#include <GLFW/glfw3.h>

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"{\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"}\0";

float vertices[] = {//x,y,z为一个点
-0.5f, -0.5f, 0.0f,
 0.5f, -0.5f, 0.0f,
 0.0f,  0.5f, 0.0f
};

int main() {
    glfwInit();

    unsigned int VBO;//数据缓冲
    glGenBuffers(1, &VBO);//生成一个buffer用于数据缓冲,由于数据CPU到GPU速度不够快,所以一次发送大量数据,减少发送数据,加快速度
    glBindBuffer(GL_ARRAY_BUFFER, VBO);;//将生成的一个 vbo buffer绑定为数组类型的buffer,顶点缓冲对象,可以同时绑定多个缓冲对象
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//数据填充

    //unsigned int vertexShader;//着色器对象
    //vertexShader = glCreateShader(GL_VERTEX_SHADER);//生成一个,这个和生成buffer方法不一样

    //int  status;
    //char info[512]{};
    //glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);//绑定着色器与着色器源代码
    //glCompileShader(vertexShader);//编译着色器
    //glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);//获取编译状态:成功?失败
    //if (!status)
    //{
    //    glGetShaderInfoLog(vertexShader, 512, NULL, info);
    //    std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << info << std::endl;
    //}
	return 0;
}

image

Solved by this code :

    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

NOW CODE:

#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>


void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
const char* vertexShaderSource = "#version 330 core\n"//顶点着色器源代码
                                                "layout (location = 0) in vec3 aPos;\n"
                                                "void main()\n"
                                                "{\n"
                                                "   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
                                                "}\0";
const char* fragmentShaderSource = "#version 330 core \n"//片段着色器源代码
                                                    "out vec4 FragColor;\n"
                                                    "void main()\n"
                                                    "{\n"
                                                        "FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
                                                    "}\0";

float vertices[] = {//x,y,z为一个点
-0.5f, -0.5f, 0.0f,
 0.5f, -0.5f, 0.0f,
 0.0f,  0.5f, 0.0f
};

int main() {
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    // glfw window creation
    // --------------------
    GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
    if (window == NULL)
    {
        std::cout << "Failed to create GLFW window" << std::endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    unsigned int VBO;//数据缓冲
    glGenBuffers(1, &VBO);//生成一个buffer用于数据缓冲,由于数据CPU到GPU速度不够快,所以一次发送大量数据,减少发送数据,加快速度
    glBindBuffer(GL_ARRAY_BUFFER, VBO);//将生成的一个 vbo buffer绑定为数组类型的buffer,顶点缓冲对象,可以同时绑定多个缓冲对象
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);//数据填充

    unsigned int vertexShader;//着色器对象
    vertexShader = glCreateShader(GL_VERTEX_SHADER);//生成一个着色器对象,这个和生成buffer方法不一样
    int  status;
    char info[512]{};
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);//绑定着色器与着色器源代码
    glCompileShader(vertexShader);//编译着色器
    glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &status);//获取编译状态:成功?失败
    if (!status)
    {
        glGetShaderInfoLog(vertexShader, 512, NULL, info);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << info << std::endl;
    }

    unsigned int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
    glCompileShader(fragmentShader);
	return 0;
}

void processInput(GLFWwindow* window)
{
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
        glfwSetWindowShouldClose(window, true);
}

// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
    // make sure the viewport matches the new window dimensions; note that width and 
    // height will be significantly larger than specified on retina displays.
    glViewport(0, 0, width, height);
}