Ejemplo n.º 1
0
bool isConvexPolygon(const vector<vec4>& verts)
{
	if(verts.empty() || verts.size() < 3)
		return false;

	vec3 normal= vec3(0);
	vec3 t = vec3(0);  // t is for "temp normal" or "test normal"

	vec3 startPoint = vec3(verts.at(0));
	vec3 startPointPlus1 = vec3(verts.at(1));	
	vec3 endPoint = vec3(verts.at(verts.size()-2));
	//Normal = v[1]-v[0] X v[n-1]-v[0] where n=size of array of vertices
	normal = cross( startPointPlus1-startPoint , endPoint-startPoint );
	//For each vertex 'iter' after the 0th index in verts
	for(int i = 1; i < (int)(verts.size()-2); ++i)
	{
		//T= (v[i+1]-v[i]) cross (v[v-1]-v[i])
		t = cross( vec3(verts.at(i+1)) - vec3(verts.at(i)) , vec3(verts.at(i-1)) - vec3(verts.at(i)) );

		if( (t.y < 0 && normal.y > 0) || (t.y > 0 && normal.y < 0) )
		{
			return false;
		}
	}
	return true;
}
Ejemplo n.º 2
0
void GLWidget::mouseMoveEvent(QMouseEvent *event) {
    last.x = event->x();
    last.y = event->y();

    vec3 begin = pointOnVirtualTrackball(first);
    vec3 end = pointOnVirtualTrackball(last);

    float dotProduct = dot(normalize(begin), normalize(end));
    float angle = acos(dotProduct);
    vec3 crossP = cross(begin, end);

    if(length(crossP) > .00001f)
    {
        rotationMatrix = rotate(mat4(1.0), angle, normalize(crossP)) * rotationMatrix;
        glUseProgram(cubeProg);
        glUniformMatrix4fv(cubeRotationMatrixLoc, 1, false, value_ptr(rotationMatrix));
        glUseProgram(gridProg);
        glUniformMatrix4fv(gridRotationMatrixLoc, 1, false, value_ptr(rotationMatrix));
        update();
    }


    first.x = last.x;
    first.y = last.y;
}
Ejemplo n.º 3
0
void easygl::init()
{
    target = vec3(0, 1, 0);
    up = vec3(0, 1, 0);
    orientation = quat();

    direction = vec3(0, 0, -1);
    right = cross(up, direction);
    up = cross(direction, right);
    position = vec3(5.0f, 5.0f, 5.0f);
    
	fieldOfView = 60.0f;
	near = 0.1f, far = 1000.0f;
	aspectRatio = 1.0;
    dragl = false;
    dragr = false;

    //sphere.load("sphere.stl", vec4(1.0f, 0.0f, 1.0f, 1.0f));
    for(layer &l : d.layers){
        l.findcontours();
        l.offset(1);
        l.show();
    }
}
Ejemplo n.º 4
-1
void sceneRender()
{
    using glm::cross;
    using glm::clamp;
    using glm::vec3;
    using glm::normalize;

    // TODO: Save scene pixels as png
    int width = g_theScene->output_size.x;
    int height = g_theScene->output_size.y;
    const char* output = isEmpty(g_theScene->output) ? "out.png" : g_theScene->output;
    unsigned char* pixels = (unsigned char*) malloc(c_bpp * width * height);

    camera_t& cam = g_theScene->camera;
    float halfFov = cam.fov / 2;
    float tanFov = tan(halfFov);
    float halfWidth = width / 2.0f;
    float halfHeight = height / 2.0f;

    vec3 w = normalize(cam.look_from - cam.look_at);
    vec3 u = normalize(cross(cam.up, w));
    vec3 v = normalize(cross(u, w));

    ray_t ray;
    ray.origin = cam.look_from;

    ray_query_t best;

    float multi = tanFov / halfHeight;
    unsigned char* currentPixel = pixels;
    for (int y = 0; y < height; ++y)
    {
        float cy = halfHeight - (y + 0.5f);
        for (int x = 0; x < width; currentPixel += c_bpp, ++x)
        {
            // reset best query.
            best.obj = NULL;
            best.t = FLT_MAX;

            // Get Ray through pixel.
            float cx = (x + 0.5f) - halfWidth;
            float a = cx * multi;
            float b = cy * multi;

            vec3 rayDirection = normalize((a * u) + (b * v) - w);

            // Find intersection with scene.
            ray_query_t query;
            vec3* vertices = g_theScene->vertices;
            triangle_t* tri = g_theScene->triangles;
            for (int t = 0; t < g_theScene->triangle_count; ++t, ++tri)
            {
                glm::vec4 rayOriginT = tri->xform_inv * glm::vec4(cam.look_from, 1.0f);
                glm::vec4 rayDirectionT = tri->xform_inv * glm::vec4(rayDirection, 0.0f);

                ray.origin = vec3(rayOriginT.x, rayOriginT.y, rayOriginT.z);
                ray.direction = vec3(rayDirectionT.x, rayDirectionT.y, rayDirectionT.z);

                int* indices = tri->indicies;
                if (intersectRayTriangle(ray, vertices[indices[0]], vertices[indices[1]], vertices[indices[2]], query))
                {
                    query.obj = tri;
                    if (query.t < best.t) best = query;
                }
            }

            sphere_t* sph = g_theScene->spheres;
            for (int s = 0; s < g_theScene->sphere_count; ++s, ++sph)
            {
                glm::vec4 rayOriginT = sph->xform_inv * glm::vec4(cam.look_from, 1.0f);
                glm::vec4 rayDirectionT = sph->xform_inv * glm::vec4(rayDirection, 0.0f);

                ray.origin = vec3(rayOriginT.x, rayOriginT.y, rayOriginT.z);
                ray.direction = vec3(rayDirectionT.x, rayDirectionT.y, rayDirectionT.z);
                
                if (intersectRaySphere(ray, *sph, query))
                {
                    query.obj = sph;
                    if (query.t < best.t) best = query;
                }
            }

            // TODO: Light object
            if (best.obj != NULL)
            {
                material_t& mat = best.obj->material;
                vec3 color = mat.ambient;

                // final color conversion.
                currentPixel[0] = (unsigned char) (clamp(color.b, 0.0f, 1.0f) * 255.0f);
                currentPixel[1] = (unsigned char) (clamp(color.g, 0.0f, 1.0f) * 255.0f);
                currentPixel[2] = (unsigned char) (clamp(color.r, 0.0f, 1.0f) * 255.0f);
            }
        }
    }

    printf("Rendering scene to %s...\n", output);
    FIBITMAP *img = FreeImage_ConvertFromRawBits(pixels, width, height, width * c_bpp, c_bpp * 8, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, false);
    FreeImage_Save(FIF_PNG, img, isEmpty(g_theScene->output) ? "out.png" : g_theScene->output, 0);

    free(pixels);

    printf("Render complete!\n");
}