gpt4 book ai didi

c++ - 使用现代 OpenGL 渲染我的第一个三角形

转载 作者:行者123 更新时间:2023-11-30 05:35:57 25 4
gpt4 key购买 nike

我想使用现代 OpenGL 渲染我的第一个三角形。 (我正在使用 SFML)我有 2 个类(class):

global

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

#ifndef GLOBAL_H
#define GLOBAL_H

#include <SFML/OpenGL.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <glm/glm.hpp>

using namespace std;

extern sf::Event Event;
extern sf::Shader defaultShader;
extern sf::ContextSettings settings;
extern sf::Window window;

void initShader();

#endif // GLOBAL_H

Model

这会加载一个 obj 文件加载器。我使用 Blender 生成了它。这是它:

#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

和 cpp:

#include "Model.h"

Model::Model()
{

}

void Model::loadModel(const char *fileName)
{
int i;
vector<string>line;
vector<string>tmp;
vector<int>V1;vector<int>V2;vector<int>V3;vector<int>T1;vector<int>T2;vector<int>T3;vector<int>N1;vector<int>N2;vector<int>N3;
vector<TextureCoordinate>tc;
vector<Vertex>nc;
ifstream fin(fileName);
char aux[200],matFileName[200];
float v1,v2,v3,t1,t2,t3,n1,n2,n3;
while(!fin.eof())
{
fin.getline(aux,256);
line.push_back(aux);
}
fin.close();

sscanf(line[2].c_str(),"mtllib %s",&matFileName);
fin.open(("models/"+string("matFileName")).c_str());
while(!fin.eof())
{
fin.getline(aux,200);
tmp.push_back(aux);
}
///***///
for(i=3;i<line.size();i++)
{
char matName[100];
if(line[i][0]=='v'&& line[i][1]==' ')
{
Vertex v;
sscanf(line[i].c_str(),"v %f %f %f",&v.x,&v.y,&v.z);
vertices.push_back(v);
continue;
}
if(line[i][0]=='v'&&line[i][1]=='t')
{
TextureCoordinate t;
sscanf(line[i].c_str(),"vt %f %f",&t.x,&t.y);
textureCoordinates.push_back(t);
continue;
}
if(line[i][0]=='v'&&line[i][1]=='n')
{
Vertex v;
sscanf(line[i].c_str(),"vn %f %f %f",&v.x,&v.y,&v.z);
normals.push_back(v);
continue;
}
if(line[i][0]=='u'&&line[i][1]=='s'&&line[i][2]=='e')
{
sscanf(line[i].c_str(),"usemtl %s",&matName);
continue;
}
if(line[i][0]=='f'&&line[i][1]==' ')
{
FaceTexture f;
sscanf(line[i].c_str(),"f %d/%d/%d %d/%d/%d %d/%d/%d ",&v1,&v2,&v3,&t1,&t2,&t3,&n1,&n2,&n3);
f.texture=matName;
faces.push_back(f);
V1.push_back(v1);V2.push_back(v2);V3.push_back(v3);
T1.push_back(t1);T2.push_back(t2);T3.push_back(t3);
N1.push_back(n1);N2.push_back(n2);N3.push_back(n3);
continue;
}
}
tc.resize(vertices.size());
nc.resize(vertices.size());
textureCoordinates.resize(vertices.size());
normals.resize(vertices.size());
for(i=0;i<faces.size();i++)
{
tc[V1[i]]=textureCoordinates[T1[i]];
nc[V1[i]]=normals[N1[i]];
tc[V2[i]]=textureCoordinates[T2[i]];
nc[V2[i]]=normals[N2[i]];
tc[V3[i]]=textureCoordinates[T3[i]];
nc[V3[i]]=normals[N3[i]];
}
for(i=0;i<vertices.size();i++)
{
textureCoordinates[i]=tc[i];
normals[i]=nc[i];
}
for(i=0;i<faces.size();i++)
{
faces[i].vertex[0]=V1[i];
faces[i].vertex[1]=V2[i];
faces[i].vertex[2]=V3[i];
}
setupMesh();
}

void Model::setupMesh()
{
glGenBuffers(1, & VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (GLvoid*)0);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindVertexArray(0);
}

void Model::draw()
{
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}

顶点着色器:

#version 330 core

layout (location = 0) in vec3 position;
//layout (location = 1) in vec3 color;

out vec3 ourColor;

void main()
{
ourColor=vec3(1,0,0);
gl_Position = vec4(position, 1.0f);
//ourColor = color;
}

片段着色器:

#version 330 core

in vec3 ourColor;

out vec4 color;

void main()
{
color = vec4(ourColor, 1.0f);
}

主要.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;
}

为什么程序显示黑屏?当我使用旧函数时程序运行良好 (glBegin,glEnd)

最佳答案

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

glGenVertexArrays(1, &VAO);

获取一个有效的顶点数组。

编辑 1

下一个问题:你推送给VBO的数据大小不对

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);

sizeof(vertices)在这种情况下等于 sizeof(vector<Vertex>)这是 vector 对象在内存中的大小,而不是 vector 数据的大小。你想要的是

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

关于c++ - 使用现代 OpenGL 渲染我的第一个三角形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33682240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com