简单的OpenGL问题,大佬们帮一把!

如题,最近在学OpenGL。今天自己写了一个画三角形的程序,但是,出来的结果不对,坐标和我设置的不一样。于是我就试着画了两条垂直的直线,结果还是不对。
具体情况看下图:我想要画(-1,0)(1,0)(0,-1)(0,1)两条直线,结果如下:
图片说明
代码如下:

#include<gl/glfw3.h>
#include<iostream>
#pragma comment(lib,"glfw3.lib")
using namespace std;
void framebuffer_size_callback(GLFWwindow * window,int width,int height);
void processInput(GLFWwindow *window);
//定义顶点着色器
const char *VertexShaderSource = "#version 430 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"
"}\n\0";
//定义片元着色器
const char *FragmentShaderSource = "#version 430 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"{\n"
"FragColor=vec4(1.0,0.5,0.2,1.0);"
"}\n\0";
int main()
{
    glfwInit();//初始化GLFW
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);//配置GLFW:想要配置什么选项:指定OpenGL最低主版本号
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);//指定OpenGL最低副版本号
    glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE);//指定使用OpenGL库
    GLFWwindow * window = glfwCreateWindow(600,600,"LearnOpenGL",NULL,NULL);//创建窗口(该函数返回一个GLFWwindow对象)
                                                                            //参数:第一二个参数指定窗口的宽和高,
                                                                            //第三个指定窗口名称。最后两个参数可以忽略
    if (window==NULL)
    {
        cout << "Failed to create GLFW window" << endl;
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        cout << "Failed to initialize GLAD" << endl;
        return -1;
    }
    //初始化顶点着色器
    int vertexShader=glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader,1,&VertexShaderSource,NULL);
    glCompileShader(vertexShader);
    int success;
    char infoLog[512];
    glGetShaderiv(vertexShader,GL_COMPILE_STATUS,&success);
    if (!success)
    {
        glGetShaderInfoLog(vertexShader,512,NULL,infoLog);
        cout << "Failed to compile vertexshader"<<endl;
    }
    //初始化片元着色器
    int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader,1,&FragmentShaderSource,NULL);
    glCompileShader(fragmentShader);
    glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderSource(fragmentShader,512,NULL,infoLog);
        cout << "Failed to compile fragment shader" << endl;
    }
    //定义并初始化Shader Programe
    int programe = glCreateProgram();
    glAttachShader(programe,vertexShader);
    glAttachShader(programe,fragmentShader);
    glLinkProgram(programe);
    glGetProgramiv(programe,GL_LINK_STATUS,&success);
    if (!success)
    {
        glGetProgramInfoLog(programe,512,NULL,infoLog);
        cout << "Failed to Link Programe" << endl;
    }
    glValidateProgram(programe);
    glGetProgramiv(programe,GL_VALIDATE_STATUS,&success);
    if (!success)
    {
        glGetProgramInfoLog(programe,512,NULL,infoLog);
        cout<<"Failed to validate programe"<<endl;
    }
    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);
    glUseProgram(programe);

    //定义顶点数组
    float vertex[] = 
    {
        -1.0,0.0,
        1.0,0.0,
        0.0,-1.0,
        0.0,1.0
    };
    //定义VBO,VAO
    unsigned int VBO,VAO;
    glGenVertexArrays(1,&VAO);
    glGenBuffers(1,&VBO);
    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER,VBO);
    glBufferData(GL_ARRAY_BUFFER,sizeof(vertex),vertex,GL_STATIC_DRAW);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,sizeof(float),(void*)0);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,0);
    glBindVertexArray(0);

    glViewport(0,0,600,600);
    glfwSetFramebufferSizeCallback(window,framebuffer_size_callback);    //指定窗口尺寸改变时所使用的回调函数
    while (!glfwWindowShouldClose(window))                              //当我们告知GLFW结束时,在关闭窗口
    {
        processInput(window);
        glClearColor(0.2,0.3,0.3,1.0);
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(programe);
        glBindVertexArray(VAO);
        glDrawArrays(GL_LINES,0,4);

        glfwSwapBuffers(window);
        glfwPollEvents();                                               //检查是否有事件触发(如输入或者鼠标移动操作),然后
                                                                        //更新窗口状态
    }
    glDeleteVertexArrays(1,&VAO);                                       //绘制结束之后销毁所有的VAO与VBO
    glDeleteBuffers(1,&VBO);
    glfwTerminate();
    return 0;
}

void framebuffer_size_callback(GLFWwindow* window,int width,int height)  //改变窗口大小的回调函数
{
    glViewport(0,0,width,height);
}
void processInput(GLFWwindow * window)
{
    if ((glfwGetKey(window,GLFW_KEY_BACKSPACE)==GLFW_PRESS))
    {
        glfwSetWindowShouldClose(window,true);
    }
}

2个回答

已解决,此贴终结!我又把代码重新敲了一遍,居然发现一切正常。灵异的很

avder123
干掉那个BUG 能告诉我是什么问题么?
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!