Esempio n. 1
0
static void getArrowVertices(Vector2x position, Vector3x normal, struct vertex_p2f_c4f * outVertices, GLuint * outIndexes, unsigned int * ioVertexCount, unsigned int * ioIndexCount) {
    if (outVertices != NULL) {
        struct vertex_p2f_c4f vertex;
        Vector2f positionf = VECTOR2f(xtof(position.x), xtof(position.y));
        Vector2f normalf = VECTOR2f(xtof(normal.x), xtof(normal.y));

        vertex.color[0] = 1.0f;
        vertex.color[1] = 0.0f;
        vertex.color[2] = 0.0f;
        vertex.color[3] = 1.0f;
        vertex.position[0] = positionf.x - normalf.x * ARROW_RADIUS;
        vertex.position[1] = positionf.y - normalf.y * ARROW_RADIUS;
        outVertices[*ioVertexCount + 0] = vertex;
        vertex.position[0] = positionf.x + normalf.x * ARROW_RADIUS;
        vertex.position[1] = positionf.y + normalf.y * ARROW_RADIUS;
        outVertices[*ioVertexCount + 1] = vertex;
        vertex.position[0] = positionf.x + normalf.y * ARROW_RADIUS * 0.5f;
        vertex.position[1] = positionf.y - normalf.x * ARROW_RADIUS * 0.5f;
        outVertices[*ioVertexCount + 2] = vertex;
        vertex.position[0] = positionf.x - normalf.y * ARROW_RADIUS * 0.5f;
        vertex.position[1] = positionf.y + normalf.x * ARROW_RADIUS * 0.5f;
        outVertices[*ioVertexCount + 3] = vertex;
    }

    if (outIndexes != NULL) {
        outIndexes[*ioIndexCount + 0] = *ioVertexCount + 0;
        outIndexes[*ioIndexCount + 1] = *ioVertexCount + 1;
        outIndexes[*ioIndexCount + 2] = *ioVertexCount + 1;
        outIndexes[*ioIndexCount + 3] = *ioVertexCount + 2;
        outIndexes[*ioIndexCount + 4] = *ioVertexCount + 1;
        outIndexes[*ioIndexCount + 5] = *ioVertexCount + 3;
    }

    *ioVertexCount += 4;
    *ioIndexCount += 6;
}
Esempio n. 2
0
	f32 CFileReader::readFloat()
	{
		return xtof(readNext(m_buffer_2, BUF_2_SIZE));
	}
Esempio n. 3
0
static void getCollisionObjectVertices(SingleFrameScreen2D * self, struct vertex_p2f_c4f * outVertices, GLuint * outIndexes, unsigned int * ioVertexCount, unsigned int * ioIndexCount) {
    size_t objectIndex;
    CollisionObject * object;
    struct vertex_p2f_c4f vertex;
    bool colliding;
    CollisionRecord collision;

    for (objectIndex = 0; objectIndex < self->resolver->objectCount; objectIndex++) {
        object = self->resolver->objects[objectIndex];
        colliding = CollisionResolver_querySingle(self->resolver, object, &collision);
        switch (object->shapeType) {
        case COLLISION_SHAPE_RECT_2D: {
            CollisionRect2D * rect = (CollisionRect2D *) object;
            unsigned int vertexIndex;

            if (outVertices != NULL) {
                if (objectIndex == self->selectedObjectIndex) {
                    setVertexColor(&vertex, COLOR_RECT_LAST_POSITION_HIGHLIGHT);
                } else {
                    setVertexColor(&vertex, COLOR_RECT_LAST_POSITION);
                }
                vertex.position[0] = xtof(rect->lastPosition.x);
                vertex.position[1] = xtof(rect->lastPosition.y);
                outVertices[*ioVertexCount + 0] = vertex;
                vertex.position[0] = xtof(rect->lastPosition.x + rect->lastSize.x);
                outVertices[*ioVertexCount + 1] = vertex;
                vertex.position[1] = xtof(rect->lastPosition.y + rect->lastSize.y);
                outVertices[*ioVertexCount + 2] = vertex;
                vertex.position[0] = xtof(rect->lastPosition.x);
                outVertices[*ioVertexCount + 3] = vertex;

                if (colliding) {
                    if (objectIndex == self->selectedObjectIndex) {
                        setVertexColor(&vertex, COLOR_RECT_POSITION_COLLIDING_HIGHLIGHT);
                    } else {
                        setVertexColor(&vertex, COLOR_RECT_POSITION_COLLIDING);
                    }
                } else {
                    if (objectIndex == self->selectedObjectIndex) {
                        setVertexColor(&vertex, COLOR_RECT_POSITION_HIGHLIGHT);
                    } else {
                        setVertexColor(&vertex, COLOR_RECT_POSITION);
                    }
                }
                vertex.position[0] = xtof(rect->position.x);
                vertex.position[1] = xtof(rect->position.y);
                outVertices[*ioVertexCount + 4] = vertex;
                vertex.position[0] = xtof(rect->position.x + rect->size.x);
                outVertices[*ioVertexCount + 5] = vertex;
                vertex.position[1] = xtof(rect->position.y + rect->size.y);
                outVertices[*ioVertexCount + 6] = vertex;
                vertex.position[0] = xtof(rect->position.x);
                outVertices[*ioVertexCount + 7] = vertex;
            }

            if (outIndexes != NULL) {
                bool solid[4] = {rect->solidBottom, rect->solidRight, rect->solidTop, rect->solidLeft};

                for (vertexIndex = 0; vertexIndex < 4; vertexIndex++) {
                    if (solid[vertexIndex]) {
                        outIndexes[(*ioIndexCount)++] = *ioVertexCount + vertexIndex;
                        outIndexes[(*ioIndexCount)++] = *ioVertexCount + (vertexIndex + 1) % 4;
                    }
                }
                for (vertexIndex = 0; vertexIndex < 4; vertexIndex++) {
                    outIndexes[(*ioIndexCount)++] = *ioVertexCount + 0 + vertexIndex;
                    outIndexes[(*ioIndexCount)++] = *ioVertexCount + 4 + vertexIndex;
                }
                for (vertexIndex = 0; vertexIndex < 4; vertexIndex++) {
                    if (solid[vertexIndex]) {
                        outIndexes[(*ioIndexCount)++] = *ioVertexCount + 4 + vertexIndex;
                        outIndexes[(*ioIndexCount)++] = *ioVertexCount + 4 + (vertexIndex + 1) % 4;
                    }
                }

            } else {
                *ioIndexCount += 8 + 4 * rect->solidLeft + 4 * rect->solidRight + 4 * rect->solidBottom + 4 * rect->solidTop;
            }
            *ioVertexCount += 8;

            if (colliding) {
                Vector2x collidingPosition = Vector2x_interpolate(rect->lastPosition, rect->position, collision.time);
                Vector2x collidingSize = Vector2x_interpolate(rect->lastSize, rect->size, collision.time);

                if (outVertices != NULL) {
                    if (objectIndex == self->selectedObjectIndex) {
                        setVertexColor(&vertex, COLOR_RECT_POSITION_HIGHLIGHT);
                    } else {
                        setVertexColor(&vertex, COLOR_RECT_POSITION);
                    }
                    vertex.position[0] = xtof(collidingPosition.x);
                    vertex.position[1] = xtof(collidingPosition.y);
                    outVertices[*ioVertexCount + 0] = vertex;
                    vertex.position[0] = xtof(collidingPosition.x + collidingSize.x);
                    outVertices[*ioVertexCount + 1] = vertex;
                    vertex.position[1] = xtof(collidingPosition.y + collidingSize.y);
                    outVertices[*ioVertexCount + 2] = vertex;
                    vertex.position[0] = xtof(collidingPosition.x);
                    outVertices[*ioVertexCount + 3] = vertex;
                }
                if (outIndexes != NULL) {
                    bool solid[4] = {rect->solidBottom, rect->solidRight, rect->solidTop, rect->solidLeft};

                    for (vertexIndex = 0; vertexIndex < 4; vertexIndex++) {
                        if (solid[vertexIndex]) {
                            outIndexes[(*ioIndexCount)++] = *ioVertexCount + vertexIndex;
                            outIndexes[(*ioIndexCount)++] = *ioVertexCount + (vertexIndex + 1) % 4;
                        }
                    }

                } else {
                    *ioIndexCount += 2 * rect->solidLeft + 2 * rect->solidRight + 2 * rect->solidBottom + 2 * rect->solidTop;
                }
                *ioVertexCount += 4;

                getArrowVertices(VECTOR2x(collidingPosition.x + collidingSize.x / 2, collidingPosition.y + collidingSize.y / 2), collision.normal, outVertices, outIndexes, ioVertexCount, ioIndexCount);
            }
            break;
        }
        case COLLISION_SHAPE_CIRCLE: {
            CollisionCircle * circle = (CollisionCircle *) object;
            unsigned int tesselationIndex;

            if (outVertices != NULL) {
                if (objectIndex == self->selectedObjectIndex) {
                    setVertexColor(&vertex, COLOR_CIRCLE_LAST_POSITION_HIGHLIGHT);
                } else {
                    setVertexColor(&vertex, COLOR_CIRCLE_LAST_POSITION);
                }
                for (tesselationIndex = 0; tesselationIndex < CIRCLE_TESSELATIONS; tesselationIndex++) {
                    vertex.position[0] = xtof(circle->lastPosition.x) + xtof(circle->radius) * cos(tesselationIndex * M_PI * 2 / CIRCLE_TESSELATIONS);
                    vertex.position[1] = xtof(circle->lastPosition.y) + xtof(circle->radius) * sin(tesselationIndex * M_PI * 2 / CIRCLE_TESSELATIONS);
                    outVertices[*ioVertexCount + tesselationIndex] = vertex;
                }

                if (colliding) {
                    if (objectIndex == self->selectedObjectIndex) {
                        setVertexColor(&vertex, COLOR_CIRCLE_POSITION_COLLIDING_HIGHLIGHT);
                    } else {
                        setVertexColor(&vertex, COLOR_CIRCLE_POSITION_COLLIDING);
                    }
                } else {
                    if (objectIndex == self->selectedObjectIndex) {
                        setVertexColor(&vertex, COLOR_CIRCLE_POSITION_HIGHLIGHT);
                    } else {
                        setVertexColor(&vertex, COLOR_CIRCLE_POSITION);
                    }
                }
                for (tesselationIndex = 0; tesselationIndex < CIRCLE_TESSELATIONS; tesselationIndex++) {
                    vertex.position[0] = xtof(circle->position.x) + xtof(circle->radius) * cos(tesselationIndex * M_PI * 2 / CIRCLE_TESSELATIONS);
                    vertex.position[1] = xtof(circle->position.y) + xtof(circle->radius) * sin(tesselationIndex * M_PI * 2 / CIRCLE_TESSELATIONS);
                    outVertices[*ioVertexCount + CIRCLE_TESSELATIONS + tesselationIndex] = vertex;
                }
            }

            if (outIndexes != NULL) {
                float angle;
                int bestEdgeIndex;

                for (tesselationIndex = 0; tesselationIndex < CIRCLE_TESSELATIONS; tesselationIndex++) {
                    outIndexes[*ioIndexCount + tesselationIndex * 2 + 0] = *ioVertexCount + tesselationIndex;
                    outIndexes[*ioIndexCount + tesselationIndex * 2 + 1] = *ioVertexCount + (tesselationIndex + 1) % CIRCLE_TESSELATIONS;
                }

                angle = atan2(circle->position.y - circle->lastPosition.y, circle->position.x - circle->lastPosition.x);
                bestEdgeIndex = round((angle + M_PI / 2) * CIRCLE_TESSELATIONS / (M_PI * 2));
                bestEdgeIndex = (bestEdgeIndex % CIRCLE_TESSELATIONS + CIRCLE_TESSELATIONS) % CIRCLE_TESSELATIONS;
                outIndexes[*ioIndexCount + CIRCLE_TESSELATIONS * 2 + 0] = *ioVertexCount + bestEdgeIndex;
                outIndexes[*ioIndexCount + CIRCLE_TESSELATIONS * 2 + 1] = *ioVertexCount + bestEdgeIndex + CIRCLE_TESSELATIONS;
                bestEdgeIndex += CIRCLE_TESSELATIONS / 2;
                bestEdgeIndex %= CIRCLE_TESSELATIONS;
                outIndexes[*ioIndexCount + CIRCLE_TESSELATIONS * 2 + 2] = *ioVertexCount + bestEdgeIndex;
                outIndexes[*ioIndexCount + CIRCLE_TESSELATIONS * 2 + 3] = *ioVertexCount + bestEdgeIndex + CIRCLE_TESSELATIONS;

                for (tesselationIndex = 0; tesselationIndex < CIRCLE_TESSELATIONS; tesselationIndex++) {
                    outIndexes[*ioIndexCount + CIRCLE_TESSELATIONS * 2 + 4 + tesselationIndex * 2] = *ioVertexCount + CIRCLE_TESSELATIONS + tesselationIndex;
                    outIndexes[*ioIndexCount + CIRCLE_TESSELATIONS * 2 + 5 + tesselationIndex * 2] = *ioVertexCount + CIRCLE_TESSELATIONS + (tesselationIndex + 1) % CIRCLE_TESSELATIONS;
                }
            }

            *ioVertexCount += CIRCLE_TESSELATIONS * 2;
            *ioIndexCount += CIRCLE_TESSELATIONS * 4 + 4;

            if (colliding) {
                Vector2x collidingPosition = Vector2x_interpolate(circle->lastPosition, circle->position, collision.time);

                if (outVertices != NULL) {
                    if (objectIndex == self->selectedObjectIndex) {
                        setVertexColor(&vertex, COLOR_CIRCLE_POSITION_HIGHLIGHT);
                    } else {
                        setVertexColor(&vertex, COLOR_CIRCLE_POSITION);
                    }
                    for (tesselationIndex = 0; tesselationIndex < CIRCLE_TESSELATIONS; tesselationIndex++) {
                        vertex.position[0] = xtof(collidingPosition.x) + xtof(circle->radius) * cos(tesselationIndex * M_PI * 2 / CIRCLE_TESSELATIONS);
                        vertex.position[1] = xtof(collidingPosition.y) + xtof(circle->radius) * sin(tesselationIndex * M_PI * 2 / CIRCLE_TESSELATIONS);
                        outVertices[*ioVertexCount + tesselationIndex] = vertex;
                    }
                }

                if (outIndexes != NULL) {
                    for (tesselationIndex = 0; tesselationIndex < CIRCLE_TESSELATIONS; tesselationIndex++) {
                        outIndexes[*ioIndexCount + tesselationIndex * 2 + 0] = *ioVertexCount + tesselationIndex;
                        outIndexes[*ioIndexCount + tesselationIndex * 2 + 1] = *ioVertexCount + (tesselationIndex + 1) % CIRCLE_TESSELATIONS;
                    }
                }

                *ioVertexCount += CIRCLE_TESSELATIONS;
                *ioIndexCount += CIRCLE_TESSELATIONS * 2;

                getArrowVertices(collidingPosition, collision.normal, outVertices, outIndexes, ioVertexCount, ioIndexCount);
            }
            break;
        }
        }
    }
}