Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 用现代OpenGL绘制我的第一个三角形_C++_Opengl_Shader - Fatal编程技术网

C++ 用现代OpenGL绘制我的第一个三角形

C++ 用现代OpenGL绘制我的第一个三角形,c++,opengl,shader,C++,Opengl,Shader,我想使用现代OpenGL来渲染我的第一个三角形。(我正在使用SFML) 我有两门课: global ,其中我使用它初始化全局sfml变量等(与我的问题无关) 这将加载一个obj文件加载器。我用搅拌器生成的。 这是: #ifndef MODEL_H #define MODEL_H #include <GL/glew.h> #include "global.h" struct Vertex{ float x,y,z; Vertex(){}; Vertex(f

我想使用现代OpenGL来渲染我的第一个三角形。(我正在使用SFML) 我有两门课:

global
,其中我使用它初始化全局sfml变量等(与我的问题无关)

这将加载一个obj文件加载器。我用搅拌器生成的。 这是:

#ifndef MODEL_H
#define MODEL_H

#include <GL/glew.h>
#include "global.h"

struct Vertex{
    float x,y,z;
    Vertex(){};
    Vertex(float a,float b,float c){x=a;y=b;z=c;};
};

struct TextureCoordinate{
    float x;
    float y;
    TextureCoordinate(){};
    TextureCoordinate(float a,float b){x=a;y=b;};
};

struct FaceTexture{
    string texture;
    int vertex[3];
};

class Model
{
    public:
        Model();
        vector<Vertex> vertices;
        vector<FaceTexture> faces;
        vector<TextureCoordinate> textureCoordinates;
        vector<Vertex> normals;
        void loadModel(const char *fileName);
        void setupMesh();
        void draw();
    private:
        GLuint VAO, VBO;
};

#endif // MODEL_H
以及frag着色器:

#version 330 core

in vec3 ourColor;

out vec4 color;

void main()
{
    color = vec4(ourColor, 1.0f);
}
Main.cpp:

#include <GL/glew.h>
#include <global.h>

#include "Model.h"

using namespace std;

Model model;

int main()
{
    glewInit();
    initShader();

    glEnable(GL_DEPTH_TEST);
    model.loadModel("models/1/cube.obj");

    sf::Shader::bind(&defaultShader);
    while (window.isOpen())
    {
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        while (window.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
                window.close();
        }
        model.draw();
        window.display();
    }
    return 0;
}
#包括
#包括
#包括“Model.h”
使用名称空间std;
模型;
int main()
{
glewInit();
initShader();
glEnable(GLU深度试验);
model.loadModel(“models/1/cube.obj”);
sf::Shader::bind(&defaultShader);
while(window.isOpen())
{
glClear(GL_颜色_缓冲_位| GL_深度_缓冲_位);
while(window.pollEvent(事件))
{
如果(Event.type==sf::Event::Closed)
window.close();
}
model.draw();
window.display();
}
返回0;
}
为什么程序会给我显示一个黑屏?
当我使用旧函数(
glBegin,glEnd
)时,程序运行良好。

您使用变量
VAO
,而无需初始化它或创建VAO。你得打电话

glGenVertexArrays(1, &VAO);
获取有效的顶点数组

编辑1

下一个问题:推送到VBO的数据大小错误

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
sizeof(顶点)
在这种情况下等于
sizeof(vector)
,这是向量对象在内存中的大小,而不是向量数据的大小。你想要的是

glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);

我做到了。还是黑屏你是对的。我更改了代码,但我还是有黑屏。
#include <GL/glew.h>
#include <global.h>

#include "Model.h"

using namespace std;

Model model;

int main()
{
    glewInit();
    initShader();

    glEnable(GL_DEPTH_TEST);
    model.loadModel("models/1/cube.obj");

    sf::Shader::bind(&defaultShader);
    while (window.isOpen())
    {
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
        while (window.pollEvent(Event))
        {
            if (Event.type == sf::Event::Closed)
                window.close();
        }
        model.draw();
        window.display();
    }
    return 0;
}
glGenVertexArrays(1, &VAO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);