ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator Problem with setting texture coordinates
person icon
dekon_17
Registered User
Quote
2024-05-26 16:06:37

So far, I've made quite some progress on my .OBJ loader (for modding purposes). Geometry seems to be loading very well, as well as face directions. Texture coordinates, however, are not working as good.
embedded external image
🔎︎
Weapon right in front of the camera is imported in engine, the on behind it (with scuffed texture) is, obviously, the model I've imported with my importer.

This is what my code (currently as a plugin, since I was testing it within the engine for now) looks like:
function ModelLoader ()
{
var file = prompt ("Specify file first!");
if (file)
{
var node = editorAddSceneNode ("plane", 0, 0, 0);
var vertColor = ccbGetMeshBufferVertexColor (node, 0, 0);

ccbRemoveMeshBuffer (node, 0);
ccbAddMeshBuffer (node);
ccbSetSceneNodeMaterialProperty (node, 0, 'Lighting', false);

var Model = ccbReadFileContent (file).split ("\n");
var Normals = [], Textures = [];
var Faces = null, FAmount = 0;
for (var l = 0; l < Model.length; l++)
{
var line = Model [l].split (" ");
switch (line [0])
{
case "v":
var coords = new vector3d (parseFloat (line [1]), parseFloat (line [2]), -parseFloat (line [3]));
ccbAddMeshBufferVertex (node, 0, coords); // Create vertices from .OBJ file
break;
case "vn":
Normals.push (new vector3d (-parseFloat (line [1]), -parseFloat (line [2]), -parseFloat (line [3]))); // Store normal coordinates
break;
case "vt":
Textures.push (new vector3d (parseFloat (line [1]), parseFloat (line [2]), 0)); // Store texture coordinates
break;
case "f":
if (Faces == null)
Faces = l; // Get a line number where face data starts
FAmount += 1; // Add to amount of faces
break;
}
}

var f, fA;
for (l = 0; l < FAmount; l++)
{
var F = Model [Faces + l].slice (2).split (" "); // Get the data about all vertices of a face
fA = [];
for (var Eh = 0; Eh < F.length; Eh++)
{
f = F [Eh].split ("/"); // Split the data for each vertex
f.forEach (function (p, id) {f [id] = parseInt (f [id], 10) - 1;}); // Turn every "string" data into proper "int"
ccbSetMeshBufferVertexColor (node, 0, f [0], vertColor);
ccbSetMeshBufferVertexTextureCoord (node, 0, f [0], Textures [f [1]]);
ccbSetMeshBufferVertexNormal (node, 0, f [0], Normals [l]);

fA.push (f [0]);

if (Eh > 1)
{
if (Eh < 3)
{
ccbAddMeshBufferIndex (node, 0, fA [Eh]);
ccbAddMeshBufferIndex (node, 0, fA [Eh - 1]);
ccbAddMeshBufferIndex (node, 0, fA [Eh - 2]);
}
else
{
ccbAddMeshBufferIndex (node, 0, fA [Eh - 3]);
ccbAddMeshBufferIndex (node, 0, fA [Eh]);
ccbAddMeshBufferIndex (node, 0, fA [Eh - 1]);
}
}
}
}
ccbUpdateSceneNodeBoundingBox (node);
}
else
alert ("Dude, why'd you even call me then?");
}

editorRegisterMenuEntry ("ModelLoader ();", "Load OBJ. file");

Whole thing with textures, normals and colors is going on lines 48-50. What am I doing wrong? I can provide model and texture in case it is needed for tests. But, really, it should support any model with a single mesh buffer and no n-gon faces... At least I guess so.

person icon
sam_grady
Registered User
Quote
2024-05-26 19:24:22

I haven't read your code (sorry, I'm a bit lazy and slightly drunk). I worked on something similar once, but I remember having issues with the rotation of the model. Since CopperCube can't create collisions from a loaded mesh, you can save the model from the CopperCube editor into your own format and load it in the game.

person icon
sam_grady
Registered User
Quote
2024-05-26 20:01:46

Here's a plugin for loading and exporting into CP3D (Copper3D):

var node = editorGetSelectedSceneNode();

function exportmesh(){
node = editorGetSelectedSceneNode();
exportMesh(editorGetFileNameFromDialog("Export cp3d", "*.cp3d", false));
}
function loadmesh(){
loadMesh(editorGetFileNameFromDialog("Import cp3d", "*.cp3d"));
}

function exportMesh(path){
if(ccbGetSceneNodeMeshBufferCount(node) != 0){
let fileContent = "File Format *cp3d (Copper3D) - Copyright © Ambiera \r";
fileContent += "_ \r"
for(v = 0; v < ccbGetMeshBufferVertexCount(node, 0); ++v) fileContent += ccbGetMeshBufferVertexPosition(node, 0, v) + "v ";
fileContent += "_ \r";
for(t = 0; t < ccbGetMeshBufferVertexCount(node, 0); ++t) fileContent += ccbGetMeshBufferVertexTextureCoord(node, 0, t) + "t ";
fileContent += "_ \r";
for(n = 0; n < ccbGetMeshBufferVertexCount(node, 0); ++n) fileContent += ccbGetMeshBufferVertexNormal(node, 0, n) + "n ";
fileContent += "_ \r";
for(i = 0; i < ccbGetMeshBufferIndexCount(node, 0); ++i) fileContent += ccbGetMeshBufferIndexValue(node, 0, i) + "i ";
ccbWriteFileContent(path, fileContent);
}else{
alert('The selected node has no 3D geometry.');
}
}
function loadMesh(path){
if (ccbReadFileContent(path)){
node = editorAddSceneNode("cube", 0, 0, 0);
ccbRemoveMeshBuffer(node, 0);
ccbAddMeshBuffer(node);
let fileContent = String.split(ccbReadFileContent(path),"_");
let vertex = String.split(fileContent[1],"v");
let texture = String.split(fileContent[2],"t");
let normal = String.split(fileContent[3],"n");
let index = String.split(fileContent[4],"i");
for(v = 0; v < vertex.length-1; ++v){
ccbAddMeshBufferVertex(node, 0, toVector(vertex[v]));
ccbSetMeshBufferVertexTextureCoord(node, 0, v, toVector(texture[v]));
ccbSetMeshBufferVertexNormal(node, 0, v, toVector(normal[v]));
ccbSetMeshBufferVertexColor(node, 0, v, 0x454545);
}
for(i = 0; i < (index.length-1); ++i)
ccbAddMeshBufferIndex(node, 0, index[i]);
ccbUpdateSceneNodeBoundingBox(node);
ccbSetSceneNodeMaterialProperty(node, 0, "Type", "Solid");
ccbSetSceneNodeMaterialProperty(node, 0, "Lighting", false);
}
}
function toVector(string){ string = string.replace("(", ""); string = string.replace(")", ""); string = String.split(string, ","); return new vector3d(string[0], string[1], string[2]);}

editorRegisterMenuEntry("exportmesh()", "export cp3d");
editorRegisterMenuEntry("loadmesh()", "load cp3d");


person icon
dekon_17
Registered User
Quote
2024-05-27 07:38:27

Uh... I don't know whether I'll understand this code or not, but, to be fair, neither I expect you to understand mine.

However, I think I kind of know why this all happens. For each face, a new vertex should be made. And that is not something that happens in my code. So, the script ends up replacing texture coordinates of existing vertices with a new ones.

Guess I have something to fix.

person icon
sven
Registered User
Quote
2024-05-27 11:45:49

Some time ago i made .obj loader

https://5v3n.itch.io/coppercube-obj-file-loader-script

I had some issues also but i was able to fix them..maybe you get an idea by looking at my code.

person icon
dekon_17
Registered User
Quote
2024-06-20 14:39:20

Pardon for silence, been busy at college. Not for long though, so, here are some things I've been able to do:

Firstly, I figured that my theory about vertices and faces (from my previous reply) was, in fact, correct. I don't remember how much time it took me to fix, but, hey, it is fixed and that is all I needed.

Secondly, the thing I said firstly was not a full fix, because, for some reason, second texture coordinate should've been turned negative. I do not understand why that is, but, whatever.

Thirdly, model was loading in the wrong direction, so, Z coordinates should've been turned negative too. And indices, kind of.

At long last, I've succeeded. Kind of. Models must be triangulated first. Despite that, now all that I need to do is to "infuse" it with asset loader, so that I will be able to load weapons, food and other cool stuff right into the game, upon starting. So... Thank you guys for, kind of, help.

And, sven, I tried checking your code (for models with one texture or something like that), but as it was late in the night, plus the whole code structure that I did not understand, I was unable to learn anything from it. Doesn't matter much now, though.


Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |