Skip to content

Instantly share code, notes, and snippets.

@gonsolo
Last active December 15, 2019 14:55
Show Gist options
  • Save gonsolo/94e28d199045ac1ddaf253f24a6263dd to your computer and use it in GitHub Desktop.
Save gonsolo/94e28d199045ac1ddaf253f24a6263dd to your computer and use it in GitHub Desktop.
How to write a Ptex quad face
Basically a hello world for using the Ptex API for writing quad face.
The second example writes random colors to triangle meshes.
TARGET = ptex
test: $(TARGET)
./$(TARGET)
ptxinfo -d test.ptx
$(TARGET): $(TARGET).cc
g++ -Wall -o $(TARGET) $(TARGET).cc -lPtex
c: clean
clean:
rm -f $(TARGET) test.ptx
e: edit
edit:
vi $(TARGET).cc
// Write one Ptex quad face (ptex.us)
#include <iostream>
#include <cmath>
#include "Ptexture.h"
using namespace std;
int main()
{
string filename = "test.ptx";
auto meshType = Ptex::mt_quad;
auto dataType = Ptex::dt_uint8;
auto channels = 3;
auto alpha = -1;
auto numberFaces = 1;
Ptex::String error;
PtexPtr<PtexWriter> writer(PtexWriter::open(filename.c_str(),
meshType,
dataType,
channels,
alpha,
numberFaces,
error));
int height = 2;
int width = 2;
const int noFace = -1;
const int noEdge = 0;
int adjFaces[4] = {noFace, noFace, noFace, noFace};
int adjEdges[4] = {noEdge, noEdge, noEdge, noEdge};
Ptex::Res resolution(log2(width), log2(height));
Ptex::FaceInfo faceInfo(resolution, adjFaces, adjEdges);
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
};
Color texels[height * width];
const int white = 255;
const int gray = 128;
for (int j = 0; j < height * width; j++)
{
texels[j].r = j;
texels[j].g = gray;
texels[j].b = white;
}
auto faceId = 0;
writer->writeFace(faceId, faceInfo, texels);
}
// Write a ptex file with random colors for a triangle mesh.
//
// You only have to specify the number of triangles that you can find out
// easily for obj files with "grep '^f' bla.obj|wc -l".
// No adjency information is given.
#include <iostream>
#include <cmath>
#include <random>
#include <vector>
#include "Ptexture.h"
using namespace std;
//auto numberFaces = 69666; // bunny
auto numberFaces = 15744; // suzanne
struct Color {
uint8_t r;
uint8_t g;
uint8_t b;
};
Color red {255, 0, 0};
Color white {255, 255, 255};
Color black {0, 0, 0};
Color randomColor() {
random_device device;
mt19937 engine(device());
uniform_int_distribution<uint8_t> distribution(0, 255);
auto r = distribution(engine);
auto g = distribution(engine);
auto b = distribution(engine);
return Color{r, g, b};
}
vector<Color> texelsFromColor(int count, Color color) {
vector<Color> texels(count);
for (int i = 0; i < count; i++)
texels[i] = color;
return texels;
}
int main()
{
string filename = "test.ptx";
auto meshType = Ptex::mt_triangle;
auto dataType = Ptex::dt_uint8;
auto channels = 3;
auto alpha = -1;
Ptex::String error;
PtexPtr<PtexWriter> writer(PtexWriter::open(filename.c_str(),
meshType,
dataType,
channels,
alpha,
numberFaces,
error));
int height = 2;
int width = 2;
const int noFace = -1;
const int noEdge = 0;
int adjFaces[4] = {noFace, noFace, noFace, noFace};
int adjEdges[4] = {noEdge, noEdge, noEdge, noEdge};
Ptex::Res resolution(log2(width), log2(height));
Ptex::FaceInfo faceInfo(resolution, adjFaces, adjEdges);
int numberTexels = width * height;
for (auto faceId = 0; faceId < numberFaces; faceId++) {
auto texels = texelsFromColor(numberTexels, randomColor());
writer->writeFace(faceId, faceInfo, texels.data());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment