我正在为一个我写过的游戏引擎的小编辑工作。我已经设置了一个Qt 5.6的文件菜单,它可以选择添加一个模型来创建一个新模型并将其添加到渲染引擎管理器。在Mac OSX 10.11(使用OpenGL 3或4)上,这可以正常工作。在Ubuntu 16.04上它只是稍微有效。我可以在主循环之外初始化尽可能多的模型。但是在主循环中我只能初始化我在主循环外部初始化的模型。当我在IDE中提取gdb时,问题似乎与VAO有关。在OSX上,无论我在何处创建网格,都会为每个模型初始化新的VAO。在Ubuntu上,在主循环之外,为每个网格生成新的VAO。在主循环内部,绘制所有网格的网格与主循环外部先前创建的网格具有相同的VAO。一旦我创建了比在主循环之外创建的模型更多的模型并且生成了新的VAO,模型就不会绘制。
总而言之,在Ubuntu中,主循环外的所有模型都有自己的VAO。在主循环内部,VAO GLuint句柄再次从2开始重新计数。一旦手柄/标签计数过去,然而许多模型在主循环之外被初始化,模型就会停止绘制。它适用于一个Mac OSX。我怀疑这是一个OpenGL / Qt问题,所以即使引擎有很多额外的代码,我认为问题是公平的。链接:https://github.com/BennetLeff/engine
由于有很多文件,我只会包含我认为必要的文件,但我也会链接到github项目。很抱歉大型代码转储,但告诉我是否还有其他我可以提供的。
#include "Mesh.h"
#include "Transform.h"
#include <stdio.h>
Mesh::Mesh(std::vector<glm::vec3> vertices, std::vector<glm::vec3> normals, std::vector<glm::vec2> textures, std::vector<GLuint> indices)
{
drawCount_ = indices.size();
glGenVertexArrays(1, &vertexArrayObject_);
glBindVertexArray(vertexArrayObject_);
glGenBuffers(NUMBUFFERS, vertexBufferObject_);
// Position Attrib
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject_[POSITION_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices[0]) * vertices.size(), vertices.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);
// Texture Attrib
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject_[TEXCOORD_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(textures[0]) * textures.size(), textures.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Normals Attrib
glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject_[NORMAL_VB]);
glBufferData(GL_ARRAY_BUFFER, sizeof(normals[0]) * normals.size(), normals.data(), GL_STATIC_DRAW);
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertexBufferObject_[INDEX_VB]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices[0]) * indices.size(), indices.data(), GL_STATIC_DRAW);
glBindVertexArray(0);
fprintf(stderr, "vao val %d\n", vertexArrayObject_);
}
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &vertexArrayObject_);
}
void Mesh::draw()
{
glBindVertexArray(vertexArrayObject_);
glDrawElements(GL_TRIANGLES, drawCount_, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
if (glGetError())
printf("GL error %d", glGetError());
}
#include <stdio.h>
#ifdef __APPLE__
#include <OpenGL/gl3.h>
#else
#include <GL/glew.h>
#endif
#include "Camera.h"
#include "Model.h"
#include "Editor.h"
#include "RenderEngine.h"
#include <QApplication>
bool quit = false;
int main(int argc, char* argv[])
{
auto WIDTH = 1024;
auto HEIGHT = 800;
/*
* Sets up a QApplication
* with the proper formatting. This allows
* GL versions to be set and so forth. Then
* the QApplication is polled in the main loop
* for events.
*/
QApplication app(argc, argv);
QSurfaceFormat format;
format.setSamples(16);
format.setDepthBufferSize(24);
format.setStencilBufferSize(8);
format.setVersion(4, 3);
format.setProfile(QSurfaceFormat::CoreProfile);
QSurfaceFormat::setDefaultFormat(format);
// Sets up Rendering Engine and Editor.
auto cam = new Camera(glm::vec3(0, 6, -20), 70.0f, (float) WIDTH / (float) HEIGHT, 0.01f, 1000.0f);
RenderEngine* engine = new RenderEngine(cam);
Editor editor(engine, WIDTH, HEIGHT);
editor.showEditor();
/*
* Must call Editor.show() before any other
* OpenGL calls. This is mostly because of Qt.
*/
// auto house = Model("./res/farm house/OBJ/Farmhouse OBJ.obj", "./res/farm house/Textures/Farmhouse Texture.jpg");
// house.transform->getPosition()->z = 40;
// auto model = Model("./res/Alfred/Alfred.obj", "./res/Alfred/alfred_dif.png");
// auto model2 = Model("./res/Alfred/Alfred.obj", "./res/Alfred/alfred_dif.png");
float counter = 0.0f;
while (editor.isVisible())
{
app.processEvents();
// model.transform->getRotation()->y = float(editor.getSliderValue()) / 10;
// model.transform->getPosition()->z = editor.getManValue();
counter += 0.1f;
/*
* Just updating window for now because
* it may be faster. Need to benchmark this
* and determine what is necessary.
*/
editor.getWindow()->update();
}
return 0;
}
#include <GL/glew.h>
#include "GUIWindow.h"
GUIWindow::GUIWindow(QWidget* parent, RenderEngine* engine) :
QOpenGLWidget(parent), engine(engine) { }
void GUIWindow::initializeGL()
{
// Set up the rendering context, load shaders and other resources, etc.:
initializeOpenGLFunctions();
// If not on OSX we need to include
// OpenGL as an extension
#ifndef __APPLE__
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err) {
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
#endif
// Depth test not enabled by default.
glEnable(GL_DEPTH_TEST);
}
void GUIWindow::resizeGL(int w, int h) { }
void GUIWindow::paintGL()
{
// Draw the scene
clear(0.1, 0.4, 0.6, 1.0);
// Draw all Models
engine->draw();
}
void GUIWindow::clear(float r, float g, float b, float a)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glClearColor(r, g, b, a);
}
GUIWindow::~GUIWindow() { }
void Model::draw(Camera* cam)
{
shader.draw();
shader.update(transform, cam);
tex.bind(0);
modelMesh->draw();
}
void Model::bindTexture(Texture tex)
{
this->tex = tex;
}
#include "RenderEngine.h"
RenderEngine::RenderEngine(Camera* cam)
: cam(cam)
{
this->init();
}
void RenderEngine::init()
{
// If not on OSX we need to include
// OpenGL as an extension
#ifndef __APPLE__
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err) {
/* Problem: glewInit failed, something is seriously wrong. */
fprintf(stderr, "Error: %s\n", glewGetErrorString(err));
}
glEnable(GL_DEPTH_TEST);
#endif
}
void RenderEngine::addModel(Model model)
{
printf("added model \n");
this->models.push_back(model);
for (int i = 0; i < this->models.size(); i++)
printf("Model Pos: (%g, %g, %g) \n", models[i].transform->getPosition()->x,
models[i].transform->getPosition()->y,
models[i].transform->getPosition()->z);
printf("there are %d models now \n", this->models.size());
}
void RenderEngine::draw()
{
for (int i = 0; i < this->models.size(); i++)
models[i].draw(this->cam);
}
#include "Editor.h"
Editor::Editor(RenderEngine* renderEngine, int width, int height)
{
frame = 0;
this->width = width;
this->height = height;
this->engine = renderEngine;
this->window = new GUIWindow(0, renderEngine);
}
void Editor::initialize()
{
// Set up the rendering context, load shaders and other resources, etc.:
// initializeOpenGLFunctions();
glViewport(0, 0, width, height);
setupWidgets();
}
float Editor::getRandNum()
{
srand(time(0));
float num = rand() % 10 + 1;
fprintf(stderr, "%d \n", num);
return num;
}
void Editor::addModelToScene()
{
srand (time(NULL));
auto trans = new Transform();
trans->getPosition()->x = rand() % 10 + 1;
trans->getPosition()->y = rand() % 10 + 1;
trans->getPosition()->z = rand() % 10 + 1;
engine->addModel(Model("./res/Alfred/Alfred.obj", "./res/Alfred/alfred_dif.png", trans));
fprintf(stderr, "Add a Model \n");
}
void Editor::showEditor()
{
// Sets up the rest of the widgets locations.
setupWidgets();
// Sets up the QMainWindow.
this->show();
}
void Editor::addModel(Model model)
{
engine->addModel(model);
}
答案 0 :(得分:1)
我不知道为什么我被投了票,但我终于找到了问题。在Editor::addModelToScene()
我需要在致电window.makeCurrent()
之前致电addModel()
。我不知道为什么这会对OSX起作用。还要感谢this answer让我尝试这个想法。