CXX: problems with getting TextureAttribs to work

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

I think your problem is this line:

GeomVertexWriter twriter(vdata, "texture");

There is no column called “texture”, so this is creating a writer that does nothing. (I’m surprised it’s not printing any error messages when you try to use it; I should fix that.) The name for the texture coordinate column is “texcoord”.

As to the deprecated interface, the multitexture way to do the same thing would be:

CPT(TextureAttrib) txa = TextureAttrib::make();
txa = txa->add_on_stage(TextureStage::get_default(), tex);
CPT(RenderState) state = RenderState::make(txa);

But this is clumsy enough that I think the original, simpler TextureAttrib interface should be used instead. We decided not to deprecate it since single-texturing is so common and useful. I’ll remove the stale comments about deprecation.

David

David,

You’re the man! Switching from “texture” to “texcoord” solved the problem.
And btw, it actually did complain about the mis-spelled column name, but I didn’t notice because I had the output level for the gsg set to ‘spam’, so I receive a lot of spew each frame and just hadn’t noticed.

Anyway, it works now and I’m happy! :smiley:

Thanks,

Erik