我想用现代的OpenGL来渲染我的第一个三角形。 (我正在使用SFML) 我有2个班级:
public void run() {
BluetoothSocket rfcSocket = mSPPCtrl.rfcConnect();
if (rfcSocket.isConnected()) {
mtvStatus.post(new Runnable() {
@Override
public void run() {
mtvStatus.setText("RFC-SOCKET CONNECTED");
}
});
} else {
mtvStatus.post(new Runnable() {
@Override
public void run() {
mtvStatus.setText("RFC-SOCKET NOT CONNECTED");
}
});
return;
}
// the rest of your logic
}
,我用它来初始化全局sfml变量等(与我的问题无关)
global
和
#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
这会加载一个obj文件加载器。我使用Blender生成它。 这是它:
Model
和cpp:
#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
顶点着色器:
#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;
}
Main.cpp的:
#version 330 core
in vec3 ourColor;
out vec4 color;
void main()
{
color = vec4(ourColor, 1.0f);
}
为什么程序会显示黑屏?
当我使用旧函数(#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;
}
)
答案 0 :(得分:1)
您可以使用变量VAO
而无需初始化或创建VAO。你必须致电
glGenVertexArrays(1, &VAO);
获取有效的顶点数组。
修改1
下一个问题:推送到VBO的数据大小错误
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
在这种情况下, sizeof(vertices)
等于sizeof(vector<Vertex>)
,这是矢量对象在内存中的大小,而不是矢量数据的大小。你想要的是
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex) * vertices.size(), &vertices[0], GL_STATIC_DRAW);