This is what I have:
Code: Select all
#include "Ogre.h"
#include <iostream>
#include <stdio.h>
using namespace Ogre;
class Writer
{
public:
void setVertexBuffer(size_t count)
{
// data init:
std::vector<Vector3> pos;
pos.push_back(Vector3(-10, 0, -20));
pos.push_back(Vector3(0, 10, -20));
pos.push_back(Vector3(10, 0, -20));
std::vector<ColourValue> col;
col.push_back(ColourValue::Red);
col.push_back(ColourValue::Green);
col.push_back(ColourValue::Blue);
std::vector<float> vertData;
for (int i = 0; i < pos.size(); i++)
{
vertData.push_back(pos[i].x);
vertData.push_back(pos[i].y);
vertData.push_back(pos[i].z);
vertData.push_back(col[i].r);
vertData.push_back(col[i].g);
vertData.push_back(col[i].b);
vertData.push_back(col[i].a);
}
// basic initialisation of render op
mRenderOp.vertexData = OGRE_NEW Ogre::VertexData();
mRenderOp.operationType = Ogre::RenderOperation::OT_TRIANGLE_FAN;
mRenderOp.useIndexes = false;
// Setup our render operation to match the type
Ogre::VertexDeclaration* vd = mRenderOp.vertexData->vertexDeclaration;
vd->addElement(0, 0, Ogre::VET_FLOAT3, Ogre::VES_POSITION);
vd->addElement(0, sizeof(float) * 3, Ogre::VET_COLOUR, Ogre::VES_DIFFUSE);
// vertex size: 3 for position + 4 for color (RGBA)
mVertexBuffer = Ogre::HardwareBufferManager::getSingleton().
createVertexBuffer(7*sizeof(float), pos.size(),
Ogre::HardwareBuffer::HBU_DYNAMIC_WRITE_ONLY_DISCARDABLE,
false);
assert(!mVertexBuffer.isNull() && "Error creating a vertex buffer");
void* copy_target = mVertexBuffer->lock(
Ogre::HardwareVertexBuffer::HBL_DISCARD);
assert(copy_target && "Ogre vertex buffer is invalid");
std::memcpy(copy_target, &vertData[0], vertData.size()*
sizeof(float));
mVertexBuffer->unlock();
mRenderOp.vertexData->vertexCount = pos.size();
mRenderOp.vertexData->vertexBufferBinding->setBinding(0, mVertexBuffer);
}
void draw()
{
Ogre::Root::getSingleton().getRenderSystem()->_render(mRenderOp);
}
RenderOperation mRenderOp;
HardwareVertexBufferSharedPtr mVertexBuffer;
};
int main()
{
Root* root = new Root();
bool succ = root->restoreConfig() || root->showConfigDialog();
if (!succ) { delete root; return -1; }
RenderWindow* wnd = root->initialise(true);
SceneManager* mgr = root->createSceneManager(ST_GENERIC);
Camera* cam = mgr->createCamera("");
cam->setNearClipDistance(0.1f);
cam->setFarClipDistance(1000);
mgr->setAmbientLight(ColourValue::White);
Viewport* vp = wnd->addViewport(cam);
vp->setBackgroundColour(ColourValue(0.3, 0.3, 0.3));
Writer w;
w.setVertexBuffer(3);
while (!wnd->isClosed())
{
w.draw();
root->renderOneFrame();
Ogre::WindowEventUtilities::messagePump();
}
return 0;
}
Full exception:
Code: Select all
OGRE EXCEPTION(3:RenderingAPIException): Failed to DrawPrimitive : Invalid call in D3D9RenderSystem::_render at F:/Libraries/ogre/Source/RenderSystems/Direct3D9/src/OgreD3D9RenderSystem.cpp (line 3581)