Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (762)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (847)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [SOLVED] Changing individual triangle's color on a mesh  (Read 1380 times)
0 Members and 1 Guest are viewing this topic.
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Posted 2018-02-23 14:57:57 »

hi.

I have a for eg. icosphere and I'd like to highlight one of its triangles I'm pointing at with the camera's front vector. I was able to calculate which triangle is the target I'm pointing at. The question is, that how can I change only those three vertices' color? I'm doing the rendering instanced, so I send the mesh's vertex positions in a VBO and the individual objects' transformation matrices in instanced VBOs.

  • If I send the the colors in a simple VBO, it belongs to all the objects because of instance rendering.
  • If I send the the colors in an instanced VBO, then in the vertex shader I'll have all colors for all vertices, but the problem is that now how can I decide which is the current vertex...

So, what is the common way to change particular vertices' color on a single object when I'm instanced rendering everything?
Offline KaiHH

JGO Kernel


Medals: 521



« Reply #1 - Posted 2018-02-23 15:03:54 »

gl_InstanceID
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Reply #2 - Posted 2018-02-23 15:07:38 »

Quote
holds the integer index of the current primitive

Does "current primitive" means triangle? I thought it means one instance meaning one mesh.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline KaiHH

JGO Kernel


Medals: 521



« Reply #3 - Posted 2018-02-23 15:10:03 »

Yeah, gl_InstanceID means one particular instance of your instanced draw call. It sounded like the triangles of a mesh were your "instances". Smiley
To access the vertex index of one particular instance, use: gl_VertexID.
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Reply #4 - Posted 2018-02-23 15:31:36 »

hm. Now the problem is, if I send the colors through instanced attribs, its should be in the vertex shader like:
1  
layout (location = 11) in vec3 perVertexColorInstanced[3000];


and then I can use
1  
perVertexColorInstanced[gl_VertexID]


But the problem is
Quote
Implementation limit of 16 MAX_VERTEX_ATTRIBS (e.g., number of generic plus conventional active vec4 attributes) exceeded, shader uses up to vec4 attribute 3011.

Am I misunderstanding something? Or I'm might be too tired... sorry if I am so.
Offline KaiHH

JGO Kernel


Medals: 521



« Reply #5 - Posted 2018-02-23 16:07:29 »

When you use instanced vertex attributes, you _do not_ get a large array with ALL instance's data, but you only ever get the single value for the currently processed instance. The difference between instanced and non-instanced vertex attributes is, that instanced attributes advance only once per instance (given you did not modify the instance divisor) and non-instanced attributes advance once per vertex.
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Reply #6 - Posted 2018-02-26 18:19:39 »

I still stuck.

I did this:
  • When I store the vertices, I also store those as a triangle (in the same order). Something like this [ [VERT_1,VERT_2,VERT_3], [VERT_4,VERT_5,VERT_6], ...]
  • When I calculate the surface hit point, I also calculate the hit triangle's index
  • I send the currentlz hit triangle's index to the vertex shader, and try to figure it out if the actual vertex (gl_ VertexID) is part of that triangle

But it does not work. As I read about gl_VertexIndex more and more, I got myself more confused as well. So what is gl_VertexID? If I have a cube (with unique vertices), it can be 0-23 (6*4) OR it can be 0-2 (as an index of the actual triangle)?

So, what I have is this:
  • A hit point
  • The object's triangles in order

How can I then check in the vertex shader, if the actual vertex is in the hit triangle?

Hit point calculation:
1  
2  
3  
4  
5  
6  
7  
Vector3f pos = gameObject.getTransform().getPosition();
Spheref spheref = new Spheref(pos.x, pos.y, pos.z, gameObject.getRadius());

if (Intersectionf.intersectRaySphere(cameraRay, spheref, nearFar) && nearFar.x < closestDistance) {
    surfaceHitPoint = new Vector3f(Camera.main.getCamPosition()).add(Camera.main.getCameraDirection().mul(nearFar.x));
    int triangleIndex = getTriangleIndex(surfaceHitPoint, gameObject.getMesh().getTriangles(), gameObject.getTransform());
}


Also, I draw this way:
1  
GL31.glDrawElementsInstanced(GL_TRIANGLES, batch.getMesh().getIndices().length, GL_UNSIGNED_INT, 0, gameObjectsCount);


And here is how I check if I hit the triangle (maybe there is the problem):
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
    private int getTriangleIndex(Vector3f surfaceHitPoint, List<List<Vector3f>> triangles, Transform transform) {
        for (int i = 0; i < triangles.size(); i++) {
            if (isInThisTriangle(surfaceHitPoint, triangles.get(i), transform)) {
                return i;
            }
        }

        return -1;
    }

    private boolean isInThisTriangle(Vector3f hitPoint, List<Vector3f> triangle, Transform transform) {
        Vector3f a = new Vector3f(triangle.get(0));
        Vector3f b = new Vector3f(triangle.get(1));
        Vector3f c = new Vector3f(triangle.get(2));

        transform.notifyDataChanged();
        Vector4f p0 = new Vector4f(a.x, a.y, a.z, 1).mul(transform.getTransformationMatrix());
        Vector4f p1 = new Vector4f(b.x, b.y, b.z, 1).mul(transform.getTransformationMatrix());
        Vector4f p2 = new Vector4f(c.x, c.y, c.z, 1).mul(transform.getTransformationMatrix());

        a.set(p0.x, p0.y, p0.z);
        b.set(p1.x, p1.y, p1.z);
        c.set(p2.x, p2.y, p2.z);

        // 1. the unit normal of triange (A, B, P)  - call it N1
        // 2. the unit normal of triangle (B, C, P) - call it N2
        // 3. the unit normal (C,A,P) called N3
        Vector3f n1 = b.sub(a).cross(new Vector3f(hitPoint).sub(a)).normalize();
        Vector3f n2 = c.sub(b).cross(new Vector3f(hitPoint).sub(b)).normalize();
        Vector3f n3 = a.sub(c).cross(new Vector3f(hitPoint).sub(c)).normalize();

        float res1 = new Vector3f(n1).dot(n2);
        float res2 = new Vector3f(n2).dot(n3);

        if (res1 > 0.99 && res2 > 0.99) {
            return true;
        }
        return false;
    }
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Reply #7 - Posted 2018-02-26 19:47:51 »

UPDATE: as I continue debugging, it seems that the problem is might be around the correct triangle selection. I tested it with a cube. When my vector hit a point on the sphere, I put a cube there, and cubes appear correctly. I also debugged through what I send to the shader and how I use gl_VertexID, and it also looks good, I tested it with manual parameters and colored the right triangle.

So in conclusion, now I think the triangle index, where the hit has happened is the bad guy...
Offline KaiHH

JGO Kernel


Medals: 521



« Reply #8 - Posted 2018-02-26 20:50:42 »

Quote
So in conclusion, now I think the triangle index, where the hit has happened is the bad guy...
If you suspect this to be the case, you can use Intersectionf.testRayTriangleFront().
Offline orange451

JGO Kernel


Medals: 417
Projects: 7
Exp: 7 years


Your face? Your ass? What's the difference?


« Reply #9 - Posted 2018-02-28 17:48:30 »

Side-note. I had no idea joml had these intersection tests included in it. Is there are speed test comparisons to other library approaches?

First Recon. A java made online first person shooter!
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline mudlee

Junior Devvie


Medals: 5
Exp: 5 years



« Reply #10 - Posted 2018-03-01 19:51:51 »

Finally I did it!

https://youtu.be/7Z6-8c8RLq8

The problem was that I load the object from .obj file, and I collected the vertices into triangles in a wrong order. Before joml, I used this, I dont know if it faster or not:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
        // 1. the unit normal of triange (A, B, P)  - call it N1
        // 2. the unit normal of triangle (B, C, P) - call it N2
        // 3. the unit normal (C,A,P) called N3
        Vector3f n1 = b.sub(a).cross(new Vector3f(hitPoint).sub(a)).normalize();
        Vector3f n2 = c.sub(b).cross(new Vector3f(hitPoint).sub(b)).normalize();
        Vector3f n3 = a.sub(c).cross(new Vector3f(hitPoint).sub(c)).normalize();

        float res1 = new Vector3f(n1).dot(n2);
        float res2 = new Vector3f(n2).dot(n3);

        //LOGGER.debug("TRI: {}",triangle);

        if (res1 > 0.99 && res2 > 0.99) {
            return true;
        }
        return false;
Pages: [1]
  ignore  |  Print  
 
 

 
EgonOlsen (367 views)
2018-06-10 19:43:48

EgonOlsen (374 views)
2018-06-10 19:43:44

EgonOlsen (305 views)
2018-06-10 19:43:20

DesertCoockie (549 views)
2018-05-13 18:23:11

nelsongames (874 views)
2018-04-24 18:15:36

nelsongames (857 views)
2018-04-24 18:14:32

ivj94 (1317 views)
2018-03-24 14:47:39

ivj94 (428 views)
2018-03-24 14:46:31

ivj94 (1098 views)
2018-03-24 14:43:53

Solater (448 views)
2018-03-17 05:04:08
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!