SDL绘图线

时间:2016-05-25 13:44:19

标签: c++ opengl sdl-2

我试图在我的基于SDL和Modern OpenGL的引擎中添加类似于调试绘图的东西。现在渲染的工作方式是使用Modern OpenGL(因此在绘制我想要的精灵时,没有使用SDL方法)。以下是使这项工作的一些代码:

// Main Rendering loop
glClearDepth(1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

colorProgram.bind();

std::sort(objectsWithGraphicsComponentInThisScene.begin(), objectsWithGraphicsComponentInThisScene.end(), sortByRenderDepth);

for (size_t t = 0; t < objectsWithGraphicsComponentInThisScene.size(); ++t)
{
    // set texture
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, objectsWithGraphicsComponentInThisScene[t]->getComponent<GraphicsComponent>()->getTexture());

    GLint texLocation = colorProgram.getUniformLocation("texSampler");
    glUniform1i(texLocation, 0);

    glm::mat4 rot = glm::rotate(glm::mat4x4(1.0f), objectsWithGraphicsComponentInThisScene[t]->getComponent<GraphicsComponent>()->getRotation(), glm::vec3(0.0f, 0.0f, 1.0f));
    glm::mat4 scale = glm::scale(glm::mat4x4(1.0f), glm::vec3(objectsWithGraphicsComponentInThisScene[t]->getComponent<GraphicsComponent>()->getScale().x, objectsWithGraphicsComponentInThisScene[t]->getComponent<GraphicsComponent>()->getScale().y, 1.0f));

    glm::mat4 trans;
    trans = glm::translate(glm::mat4x4(1.0f), glm::vec3(objectsWithGraphicsComponentInThisScene[t]->getPosition().x, objectsWithGraphicsComponentInThisScene[t]->getPosition().y, 0));
    GLint transMatLocation = colorProgram.getUniformLocation("transformMatrix");
    trans *= rot * scale;
    glUniformMatrix4fv(transMatLocation, 1, GL_FALSE, glm::value_ptr(trans));

    // set camera Matrix
    GLint projMatLocation = colorProgram.getUniformLocation("projectionMatrix");
    glm::mat4 cameraMatrix = camera->getCameraMatrix();
    glUniformMatrix4fv(projMatLocation, 1, GL_FALSE, glm::value_ptr(cameraMatrix));

    // This runs the Sprite::draw method per object in the current scene
    objectsWithGraphicsComponentInThisScene[t]->getComponent<GraphicsComponent>()->getSprite()->draw();

    // unbind all
    glBindTexture(GL_TEXTURE_2D, 0);
}

colorProgram.unbind();

// swap front and back buffer
SDL_GL_SwapWindow(BaseSDLWindowManager::get()->getWindow());

这是getSprite()->draw()方法中的代码:

void Sprite::draw()
{
glBindBuffer(GL_ARRAY_BUFFER, vboID);

glEnableVertexAttribArray(0);

// arguments:
// 1. Which VertexAttribArray
// 2. How many items per attribute
// 3. Which type are the items in the array
// ...
//5. stride, if the data is interleaved (pos, color, ...)

// position
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, position));

//color
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, color));

// uv
glVertexAttribPointer(2, 2, GL_FLOAT, GL_TRUE, sizeof(Vertex), (void*)offsetof(Vertex, uv));

glDrawArrays(GL_TRIANGLES, 0, 6);

glDisableVertexAttribArray(0);

glBindBuffer(GL_ARRAY_BUFFER, 0);
}

现在我想做的是,添加一些东西,以便我可以渲染简单的线条等(或矩形,简单的形状)。我尝试在渲染过程中放置​​这种代码:(但在SDL_GL_SwapWindow()之前)

//Render green outlined quad
SDL_SetRenderDrawColor(BaseSDLWindowManager::get()->getRenderer(), 255, 0, 0, 255);
SDL_RenderDrawLine(BaseSDLWindowManager::get()->getRenderer(), 0, 0, 100, 100);

但是我没有完成任何渲染。什么都没有显示出来。由于我是Modern OpenGL和SDL渲染的新手,我想知道我应该在哪里放置这些代码,或者我应该如何更改它以便它进行渲染。我不知道从哪里开始寻找

0 个答案:

没有答案