Hi,
I’d like to create some texture quads on the fly from CXX. I’ve managed the dynamic creation of the quads so far, but I can’t get texturing to work. I just can’t seem to set up the RenderState properly.
Look at the code below (sorry for the lenght, but I wanted to paste the whole, working program)
#include "pandaFramework.h"
#include "pandaSystem.h"
#include "geomVertexFormat.h"
#include "geomVertexArrayFormat.h"
#include "geomVertexData.h"
#include "geomVertexWriter.h"
#include "geomTriangles.h"
#include "geom.h"
#include "geomNode.h"
#include "renderState.h"
#include "TextureAttrib.h"
#include "TextureStage.h"
#include "TexturePool.h"
PandaFramework framework;
int main(int argc, char *argv[]) {
// init framework, open window
framework.open_framework(argc, argv);
framework.set_window_title("My Panda3D Window");
WindowFramework *window = framework.open_window();
NodePath cam = window->get_camera_group(); //get the camera and store it
//load the environment model
NodePath environ = window->load_model(framework.get_models(),"models/environment.egg");
environ.reparent_to(window->get_render());
environ.set_scale(0.25,0.25,0.25);
environ.set_pos(-8,42,0);
//load an additional texure for the textured quad
Texture* tex = TexturePool::load_texture("maps/panda-model.jpg");
// build the geometry
const GeomVertexFormat* gf = GeomVertexFormat::get_v3t2();
PT(GeomVertexData) vdata = new GeomVertexData("test", gf, GeomEnums::UsageHint::UH_static);
GeomVertexWriter vwriter(vdata, "vertex");
GeomVertexWriter twriter(vdata, "texture");
vwriter.add_data3f(-1,0,-1);
twriter.add_data2f(0,0);
vwriter.add_data3f(0,0,-1);
twriter.add_data2f(1,0);
vwriter.add_data3f(0,0,0);
twriter.add_data2f(1,1);
vwriter.add_data3f(-1,0,0);
twriter.add_data2f(0,1);
PT(GeomTriangles) prim = new GeomTriangles(GeomEnums::UsageHint::UH_static);
prim->add_vertices(0,1,2);
prim->close_primitive();
prim->add_vertices(0,2,3);
prim->close_primitive();
PT(Geom) geom = new Geom(vdata);
geom->add_primitive(prim);
// create the render state
CPT(RenderState) state = RenderState::make(TextureAttrib::make(tex));
PT(GeomNode) node = new GeomNode("gnode");
node->add_geom(geom, state);
window->get_render_2d().attach_new_node(node);
window->setup_trackball();
framework.main_loop();
framework.close_framework();
return (0);
}
This program works, but it renders the quad untextured (white).
I’m guessing that either 1) the setup of the renderstate is wrong:
// create the render state
CPT(RenderState) state = RenderState::make(TextureAttrib::make(tex));
PT(GeomNode) node = new GeomNode("gnode");
node->add_geom(geom, state);
or that 2) maybe I need to enable texturing for this node somehow?
And furthermore I’m a bit uneasy because the inline doc says that the call to
TextureAttrib::make(Texture)
is deprecated. But I’m completely lost with the new multi-texture-stage…
Any help is (as usual) appreciated.
Thanks in advance,
Erik