Exemplo n.º 1
0
void make_plant(
    float *data, float ao, float light,
    float px, float py, float pz, float n, int w, float rotation, const int blocks[256][6])
{
    static const float positions[4][4][3] = {
        {{ 0, -1, -1}, { 0, -1, +1}, { 0, +1, -1}, { 0, +1, +1}},
        {{ 0, -1, -1}, { 0, -1, +1}, { 0, +1, -1}, { 0, +1, +1}},
        {{-1, -1,  0}, {-1, +1,  0}, {+1, -1,  0}, {+1, +1,  0}},
        {{-1, -1,  0}, {-1, +1,  0}, {+1, -1,  0}, {+1, +1,  0}}
    };
    static const float normals[4][3] = {
        {-1, 0, 0},
        {+1, 0, 0},
        {0, 0, -1},
        {0, 0, +1}
    };
    static const float uvs[4][4][2] = {
        {{0, 0}, {1, 0}, {0, 1}, {1, 1}},
        {{1, 0}, {0, 0}, {1, 1}, {0, 1}},
        {{0, 0}, {0, 1}, {1, 0}, {1, 1}},
        {{1, 0}, {1, 1}, {0, 0}, {0, 1}}
    };
    static const float indices[4][6] = {
        {0, 3, 2, 0, 1, 3},
        {0, 3, 1, 0, 2, 3},
        {0, 3, 2, 0, 1, 3},
        {0, 3, 1, 0, 2, 3}
    };
    float *d = data;
    float s = 0.0625;
    float a = 0;
    float b = s;
    float du = (blocks[w][0] % 16) * s;
    float dv = (blocks[w][0] / 16) * s;
    for (int i = 0; i < 4; i++) {
        for (int v = 0; v < 6; v++) {
            int j = indices[i][v];
            *(d++) = n * positions[i][j][0];
            *(d++) = n * positions[i][j][1];
            *(d++) = n * positions[i][j][2];
            *(d++) = normals[i][0];
            *(d++) = normals[i][1];
            *(d++) = normals[i][2];
            *(d++) = du + (uvs[i][j][0] ? b : a);
            *(d++) = dv + (uvs[i][j][1] ? b : a);
            *(d++) = ao;
            *(d++) = light;
        }
    }
    float ma[16];
    float mb[16];
    mat_identity(ma);
    mat_rotate(mb, 0, 1, 0, RADIANS(rotation));
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 24, 3, 10);
    mat_translate(mb, px, py, pz);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 24, 0, 10);
}
Exemplo n.º 2
0
MatP* pivot_world_transform(const struct Pivot* pivot, Mat world_transform) {
    Mat translation = {0};
    mat_translate(NULL, pivot->position, translation);

    Mat rotation = {0};
    quat_to_mat(pivot->orientation, rotation);

    mat_mul(rotation, translation, world_transform);

    return world_transform;
}
Exemplo n.º 3
0
MatP* pivot_local_transform(const struct Pivot* pivot, Mat local_transform) {
    Mat rotation = {0};
    quat_invert(pivot->orientation, rotation);
    mat_rotate(NULL, rotation, rotation);

    Mat translation = {0};
    vec_invert(pivot->position, translation);
    mat_translate(NULL, translation, translation);

    mat_mul(translation, rotation, local_transform);

    return local_transform;
}
void palImpulseActuator::Apply()
{
	palMatrix4x4 m;
	mat_identity(&m);
	mat_translate(&m,m_fRelativePosX,m_fRelativePosY,m_fRelativePosZ);
	//printf("rel:%f %f %f  ",m._41,m._42,m._43);
	palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();
	

	palMatrix4x4 out;

	mat_multiply(&out,&bodypos,&m);
	palVector3 newpos;
	newpos.x=out._41;
	newpos.y=out._42;
	newpos.z=out._43;
//	printf("output : %f %f %f ",out._41,out._42,out._43);

//	imp_pos=out;
	
	mat_identity(&m);
	mat_translate(&m,m_fAxisX,m_fAxisY,m_fAxisZ);
	mat_multiply(&out,&bodypos,&m);

//	printf("output : %f %f %f\n",out._41,out._42,out._43);

//	imp_axis=out;

	palVector3 newaxis;
	newaxis.x=out._41-bodypos._41;
	newaxis.y=out._42-bodypos._42;
	newaxis.z=out._43-bodypos._43;
	vec_norm(&newaxis);

	m_pBody->ApplyImpulseAtPosition(newpos.x,newpos.y,newpos.z,newaxis.x*m_fImpulse,newaxis.y*m_fImpulse,newaxis.z*m_fImpulse);


	}
void ParticleRenderer::display()
{
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    glDepthMask(GL_FALSE);

    glUseProgram(m_programSprites);

    // Set modelview and projection matricies
    GLint h_ModelViewMatrix = glGetUniformLocation(m_programSprites, "modelview");
    GLint h_ProjectionMatrix = glGetUniformLocation(m_programSprites, "projection");
    matrix4 modelview;
    matrix4 projection;
    mat_identity(modelview);
    mat_identity(projection);
    mat_translate(modelview, m_camera);
    mat_perspective(projection, 60, (float)m_windowWidth / (float)m_windowHeight, 0.1, 1000.0);
    glUniformMatrix4fv(h_ModelViewMatrix, 1, GL_FALSE, (GLfloat*)modelview);
    glUniformMatrix4fv(h_ProjectionMatrix, 1, GL_FALSE, (GLfloat*)projection);

    // Set point size
    GLint h_PointSize = glGetUniformLocation(m_programSprites, "size");
    glUniform1f(h_PointSize, m_spriteSize);

    // Set base and secondary colors
    GLint h_BaseColor = glGetUniformLocation(m_programSprites, "baseColor");
    GLint h_SecondaryColor = glGetUniformLocation(m_programSprites, "secondaryColor");
    glUniform4f(h_BaseColor, 1.0, 1.0, 1.0, 1.0);
    glUniform4f(h_SecondaryColor, m_baseColor[0], m_baseColor[1], m_baseColor[2], m_baseColor[3]);

    // Set position coords
    GLint h_position = glGetAttribLocation(m_programSprites, "a_position");
    glBindBuffer(GL_ARRAY_BUFFER, m_pbo);
    glEnableVertexAttribArray(h_position);
    glVertexAttribPointer(h_position, 4, GL_FLOAT, GL_FALSE, 0, 0);

    GLuint texLoc = glGetUniformLocation(m_programSprites, "splatTexture");
    glUniform1i(texLoc, 0);
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_texture);

    glDrawArrays(GL_POINTS, 0, m_numParticles);

    glDisableVertexAttribArray(h_position);

    glUseProgram(0);

    glDisable(GL_BLEND);
    glDepthMask(GL_TRUE);
}
Exemplo n.º 6
0
int		mat_cleanup(t_matrix *buf, t_matrix *pos, t_matrix *ang)
{
	t_matrix	transfo;
	t_matrix	tmp;

	mat_rotate_x(buf, M_IJ(ang, 0, 0), 1);
	mat_rotate_y(&transfo, M_IJ(ang, 1, 0), 1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	mat_rotate_z(&transfo, M_IJ(ang, 2, 0), 1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	mat_translate(&transfo, pos, 1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	return (0);
}
Exemplo n.º 7
0
void set_matrix_item(float *matrix, int width, int height) {
    float a[16];
    float b[16];
    float aspect = (float)width / height;
    float size = 64;
    float box = height / size / 2;
    float xoffset = 1 - size / width * 2;
    float yoffset = 1 - size / height * 2;
    mat_identity(a);
    mat_rotate(b, 0, 1, 0, -PI / 4);
    mat_multiply(a, b, a);
    mat_rotate(b, 1, 0, 0, -PI / 10);
    mat_multiply(a, b, a);
    mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 1);
    mat_multiply(a, b, a);
    mat_translate(b, -xoffset, -yoffset, 0);
    mat_multiply(a, b, a);
    mat_identity(matrix);
    mat_multiply(matrix, a, matrix);
}
Exemplo n.º 8
0
int		mat_setup(t_matrix *buf, t_matrix *pos, t_matrix *ang, t_matrix *scale)
{
	t_matrix	transfo;
	t_matrix	tmp;

	mat_translate(buf, pos, -1);
	mat_rotate_z(&transfo, M_IJ(ang, 2, 0), -1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	mat_rotate_y(&transfo, M_IJ(ang, 1, 0), -1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	mat_rotate_x(&transfo, M_IJ(ang, 0, 0), -1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	mat_scale(&transfo, scale, -1);
	mat_prod(&tmp, &transfo, buf);
	*buf = tmp;
	return (0);
}
Exemplo n.º 9
0
void set_matrix_item_r(float *matrix, int width, int height, float scale,
        float xoffset, float yoffset, float rx, float ry, float rz) {
    float a[16];
    float b[16];
    float aspect = (float)width / height;
    float size = 64 * scale;
    float box = height / size / 2;
    mat_identity(a);
    mat_rotate(b, 0, 1, 0, rx);
    mat_multiply(a, b, a);
    mat_rotate(b, 1, 0, 0, ry);
    mat_multiply(a, b, a);
    mat_rotate(b, 0, 0, 1, rz);
    mat_multiply(a, b, a);
    mat_ortho(b, -box * aspect, box * aspect, -box, box, -3, 2);
    mat_multiply(a, b, a);
    mat_translate(b, -xoffset, -yoffset, 0);
    mat_multiply(a, b, a);
    mat_identity(matrix);
    mat_multiply(matrix, a, matrix);
}
Exemplo n.º 10
0
static void
Redisplay(void)
{
   GLfloat rot[4][4];
   GLfloat trans[16], mvp[16];

   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

   /* Build the modelview * projection matrix */
   build_rotmatrix(rot, CurQuat);
   mat_translate(trans, 0, 0, -10);
   mat_multiply(mvp, trans, (GLfloat *) rot);
   mat_multiply(mvp, Projection, mvp);
   /* Set the MVP matrix */
   glUniformMatrix4fv(uModelViewProj, 1, GL_FALSE, (float *) mvp);

   /* Draw */
   glDrawArrays(GL_TRIANGLES, 0, 3);

   glutSwapBuffers();
}
void palPropeller::Apply() {

	palMatrix4x4 m;
	mat_identity(&m);
	mat_translate(&m,m_fRelativePosX,m_fRelativePosY,m_fRelativePosZ);
	//printf("rel:%f %f %f  ",m._41,m._42,m._43);
	palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();


	palMatrix4x4 out;

	mat_multiply(&out,&bodypos,&m);
	palVector3 newpos;
	newpos.x=out._41;
	newpos.y=out._42;
	newpos.z=out._43;
	if (newpos.y > 0.1f ) return;

	Float thrust = m_Voltage*m_a_l;
	SetImpulse(thrust);
	palImpulseActuator::Apply();
}
Exemplo n.º 12
0
void make_player(
    float *data,
    float x, float y, float z, float rx, float ry)
{
    float ao[6][4] = {0};
    make_cube_faces(
        data, ao,
        1, 1, 1, 1, 1, 1,
        226, 224, 241, 209, 225, 227,
        0, 0, 0, 0.4);
    float ma[16];
    float mb[16];
    mat_identity(ma);
    mat_rotate(mb, 0, 1, 0, rx);
    mat_multiply(ma, mb, ma);
    mat_rotate(mb, cosf(rx), 0, sinf(rx), -ry);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 36, 3, 9);
    mat_translate(mb, x, y, z);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, 36, 0, 9);
}
Exemplo n.º 13
0
void make_cube(
    float *data, float ao[6][4], float light[6][4],
    int left, int right, int top, int bottom, int front, int back,
    float x, float y, float z, float n, int w, const int blocks[256][6])
{
    int wleft = blocks[w][0];
    int wright = blocks[w][1];
    int wtop = blocks[w][2];
    int wbottom = blocks[w][3];
    int wfront = blocks[w][4];
    int wback = blocks[w][5];
    make_cube_faces(
        data, ao, light,
        left, right, top, bottom, front, back,
        wleft, wright, wtop, wbottom, wfront, wback,
        n);
    float ma[16];
    float mb[16];
    mat_identity(ma);
    mat_translate(mb, x, y, z);
    mat_multiply(ma, mb, ma);
    mat_apply(data, ma, (left + right + top + bottom + front + back)*6, 0, 10);
}
void palForceActuator::Apply() {
	palMatrix4x4 m,resp;
	mat_identity(&m);
	mat_translate(&m,m_fRelativePosX,m_fRelativePosY,m_fRelativePosZ);
	palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();
	mat_multiply(&resp,&m,&bodypos); //the acting force points resulting position.
	palVector3 tpos,respos;
	tpos.x=resp._41;	tpos.y=resp._42;	tpos.z=resp._43;
	vec_mat_mul(&respos,&bodypos,&tpos);
	//printf("body is at      : %f %f %f\n",bodypos._41,bodypos._42,bodypos._43);
	//printf("forcepoint is at: %f %f %f\n",respos.x,respos.y,respos.z);
	palVector3 axis,resaxis;
	axis.x=m_fAxisX;	axis.y=m_fAxisY;	axis.z=m_fAxisZ;
	//printf("axis was: %f %f %f\n",axis.x,axis.y,axis.z);
	vec_mat_mul(&resaxis,&bodypos,&axis);
	//printf("axis is:  %f %f %f\n",resaxis.x,resaxis.y,resaxis.z);
	//printf("forcepoint is at: %f %f %f\n",resp._41,resp._42,resp._43);
	//m_pBody->AddForceAtPosition(respos.x,respos.y,respos.z,resaxis.x*m_fForce,resaxis.y*m_fForce,resaxis.z*m_fForce);
	//m_pBody->AddForceAtPosition(respos.x,respos.y,respos.z,axis.x*m_fForce,axis.y*m_fForce,axis.z*m_fForce);
	m_pBody->ApplyForceAtPosition(tpos.x,tpos.y,tpos.z,axis.x*m_fForce,axis.y*m_fForce,axis.z*m_fForce);
	//m_pBody->AddForceAtPosition(1,1,0,axis.x*m_fForce,axis.y*m_fForce,axis.z*m_fForce);
	
}
Exemplo n.º 15
0
void update_matrix_item(float *matrix) {
    float a[16];
    float b[16];
    int width, height;
    glfwGetFramebufferSize(window, &width, &height);
    glViewport(0, 0, width, height);
    float aspect = (float)width / height;
    float size = 64;
    float box = height / size / 2;
    float xoffset = 1 - size / width * 2;
    float yoffset = 1 - size / height * 2;
    mat_identity(a);
    mat_rotate(b, 0, 1, 0, PI / 4);
    mat_multiply(a, b, a);
    mat_rotate(b, 1, 0, 0, -PI / 10);
    mat_multiply(a, b, a);
    mat_ortho(b, -box * aspect, box * aspect, -box, box, -1, 1);
    mat_multiply(a, b, a);
    mat_translate(b, -xoffset, -yoffset, 0);
    mat_multiply(a, b, a);
    mat_identity(matrix);
    mat_multiply(matrix, a, matrix);
}
Exemplo n.º 16
0
int32_t main(int32_t argc, char *argv[]) {
    printf("<<watchlist//>>\n");

    if( init_sdl2() ) {
        return 1;
    }

    uint32_t width = 1280;
    uint32_t height = 720;

    SDL_Window* window;
    sdl2_window("test-canvas", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width, height, &window);

    SDL_GLContext* context;
    sdl2_glcontext(3, 2, window, &context);

    if( init_shader() ) {
        return 1;
    }

    if( init_canvas() ) {
        return 1;
    }

    printf("sizeof(struct Canvas): %zu\n", sizeof(struct Canvas));
    printf("MAX_OGL_PRIMITIVES: %d\n", MAX_OGL_PRIMITIVES);

    struct Arcball arcball = {0};
    arcball_create(width, height, (Vec4f){1.0,2.0,8.0,1.0}, (Vec4f){0.0,0.0,0.0,1.0}, 0.01, 1000.0, &arcball);

    struct Character symbols[256] = {0};
    default_font_create(symbols);

    struct Shader shader = {0};
    shader_create(&shader);
    shader_attach(&shader, GL_VERTEX_SHADER, "prefix.vert", 1, "volumetric_lines.vert");
    shader_attach(&shader, GL_FRAGMENT_SHADER, "prefix.frag", 1, "volumetric_lines.frag");
    shader_make_program(&shader, SHADER_DEFAULT_NAMES, "lines_shader");

    struct Font font = {0};
    font_create_from_characters(L"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789.,:;", 256, symbols, 9, 3, global_default_font_palette, &font);

    struct Canvas text_canvas = {0};
    canvas_create("text_canvas", &text_canvas);
    canvas_add_attribute(&text_canvas, SHADER_ATTRIBUTE_VERTEX, 3, GL_FLOAT);
    canvas_add_attribute(&text_canvas, SHADER_ATTRIBUTE_VERTEX_COLOR, 4, GL_UNSIGNED_BYTE);
    canvas_add_attribute(&text_canvas, SHADER_ATTRIBUTE_VERTEX_TEXCOORD, 2, GL_FLOAT);
    log_assert( canvas_add_shader(&text_canvas, shader.name, &shader) < MAX_CANVAS_SHADER );
    log_assert( canvas_add_font(&text_canvas, "other_font", &font) < MAX_CANVAS_FONTS );

    struct GameTime time = {0};
    gametime_create(1.0f / 60.0f, &time);

    SDL_SetEventFilter(event_filter, NULL);
    while (true) {
        SDL_Event event;
        while( sdl2_poll_event(&event) ) {
            if( sdl2_handle_quit(event) ) {
                goto done;
            }
            sdl2_handle_resize(event);

            arcball_handle_resize(&arcball, event);
            arcball_handle_mouse(&arcball, event);
        }

        gametime_advance(&time, sdl2_time_delta());

        sdl2_debug( SDL_GL_SetSwapInterval(1) );

        ogl_debug( glClearDepth(1.0f);
                   glClearColor(.0f, .0f, .0f, 1.0f);
                   glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); );

        draw_grid(&text_canvas, 0, (Mat)IDENTITY_MAT, (Color){20, 180, 240, 255}, 0.02f, 12.0f, 12.0f, 12);

        draw_basis(&text_canvas, 1, (Mat)IDENTITY_MAT, 0.02f, 1.0f);

        Mat text_matrix = {0};
        Quat text_rotation = {0};
        quat_from_axis_angle((Vec4f){1.0, 0.0, 0.0, 1.0}, PI/2, text_rotation);
        mat_rotate(NULL, text_rotation, text_matrix);
        mat_translate(text_matrix, (Vec4f){-3.5, -1.0, 6.25, 1.0}, text_matrix);

        Vec4f world_cursor = {0,0,0,1};
        text_put_world(&text_canvas, 0, world_cursor, text_matrix, (Color){0, 255, 255, 255}, 0.5f, "other_font", L"Dies ist ein Test\n");
        text_put_world(&text_canvas, 0, world_cursor, text_matrix, (Color){255, 255, 0, 255}, 0.5f, "other_font", L"fuer einen Text");

        gametime_integrate(&time);
        Vec4f screen_cursor = {0,0,0,1};
        double fps = text_show_fps(&global_dynamic_canvas, 0, screen_cursor, 0, 0, (Color){255, 255, 255, 255}, 20.0f, "default_font", time.frame);

        /* text_show_time(&text_canvas, screen_cursor, 0, "default_font", 20.0, (Color){255, 255, 255, 255}, 0, 0, time.t); */

        /* text_put_screen(&text_canvas, screen_cursor, 0, "default_font", 20.0, (Color){255, 210, 255, 255}, 0, 0, L"LALA singt das Meerschweinchen\n"); */
        /* text_put_screen(&text_canvas, screen_cursor, 0, "default_font", 20.0, (Color){0, 210, 255, 255}, 0, 0, L"FICKEN immer und ueberall\n"); */
        /* text_put_screen(&text_canvas, screen_cursor, 0, "default_font", 20.0, (Color){20, 210, 110, 255}, 0, 0, L"FUMMELN den ganzen Tag lang\n"); */

        /* text_printf(&text_canvas, screen_cursor, 0, "default_font", 20.0, (Color){255, 40, 60, 255}, 0, 0, L"PRINTF %d Luftballons\n", 99); */

        canvas_render_layers(&text_canvas, 0, MAX_CANVAS_LAYERS, &arcball.camera, (Mat)IDENTITY_MAT);
        canvas_clear(&text_canvas);

        canvas_render_layers(&global_dynamic_canvas, 0, MAX_CANVAS_LAYERS, &arcball.camera, (Mat)IDENTITY_MAT);
        canvas_clear(&global_dynamic_canvas);

        sdl2_debug( SDL_GL_SwapWindow(window) );
    }
Exemplo n.º 17
0
void Test_1::Input(SDL_Event E) {
		int i,j;
		Float x,y,z;
		palBodyBase *pb= NULL;
		palCompoundBodyBase *pcb = NULL;
		switch(E.type) {
		case SDL_KEYDOWN:
			switch (E.key.keysym.sym) {
			case SDLK_1:
				pb = CreateBody("palBox",sfrand()*3,sfrand()*2+5.0f,sfrand()*3,ufrand()+0.1f,ufrand()+0.1f,ufrand()+0.1f,1);
				if (pb == NULL) {
					printf("Error: Could not create a box\n");
				} 
				break;
			case SDLK_q:
				pb = CreateBody("palStaticBox",sfrand()*3,sfrand()*2+3.0f,sfrand()*3,ufrand()+0.1f,ufrand()+0.1f,ufrand()+0.1f,1);
				if (pb == NULL) {
					printf("Error: Could not create a static box\n");
				} 
				break;
			case SDLK_2:
				palSphere *ps;
				ps = NULL;
				ps=dynamic_cast<palSphere *>(PF->CreateObject("palSphere"));
				if (ps) {
					ps->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,0.5f*ufrand()+0.05f,1);
					BuildGraphics(ps);
				} else {
					printf("Error: Could not create a sphere\n");
				} 
				pb = ps;
				break;
			case SDLK_w:
				pb = CreateBody("palStaticSphere",sfrand()*3,sfrand()*2+3.0f,sfrand()*3,0.5*ufrand()+0.05f,0,0,1);
				if (pb == NULL) {
					printf("Error: Could not create a static sphere\n");
				} 
				break;
			case SDLK_3:
				palCapsule *pc;
				pc = NULL;
				pc=dynamic_cast<palCapsule *>(PF->CreateObject("palCapsule"));
				if (pc) {
					float radius=0.5f*ufrand()+0.05f;
					pc->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,radius,radius+ufrand()+0.1f,1);
					BuildGraphics(pc);
				} else {
					printf("Error: Could not create a cylinder\n");
				} 
				pb = pc;
				break;
			case SDLK_4:
				{
				int dist;
				dist = 3;
				float mult;
				mult = 1.0f;
				for (j=-dist;j<dist;j++)
					for (i=-dist;i<dist;i++) {
						pb = CreateBody("palBox",i*mult,5.0f,j*mult,0.25,0.25,0.25,1.0f);
					}
				pb = 0;
				}
				break;
			case SDLK_r:
				{
				int dist;
				dist = 3;
				float mult;
				mult = 1.0f;
				for (j=-dist;j<dist;j++)
					for (i=-dist;i<dist;i++) {
						pb = CreateBody("palStaticBox",i*mult,1.0f,j*mult,0.25,0.25,0.25,1.0f);
					}
				pb = 0;
				}
				break;
			case SDLK_5:
				x = sfrand()*3;
				y = sfrand()*2+3.0f;
				z = sfrand()*3;
				pcb = dynamic_cast<palCompoundBodyBase *>(PF->CreateObject("palCompoundBody"));
				if (!pcb)
					return;
				dynamic_cast<palCompoundBody *>(pcb)->Init(x,y,z);
			case SDLK_t:
				if (!pcb) {
					x = sfrand()*3;
					y = sfrand()*2+3.0f;
					z = sfrand()*3;
					pcb = dynamic_cast<palCompoundBodyBase *>(PF->CreateObject("palStaticCompoundBody"));
					if (!pcb)
						return;
					dynamic_cast<palStaticCompoundBody *>(pcb)->Init(x,y,z);
				}
				if (pcb) {
					palBoxGeometry *pbg;
					pbg = pcb->AddBox();
					if (pbg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,1+x,y,z);
						pbg->Init(m,1,1,1,1);
					}
					pbg = pcb->AddBox();
					if (pbg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,-1+x,y,z);
						pbg->Init(m,1,1,1,1);
					}
					pcb->Finalize();
					BuildGraphics(pcb);
				} else {
					printf("Error: Could not create a compound body\n");
				}
				pb = pcb;
				break;
			case SDLK_6:
				palConvex *pcv;
				pcv = NULL;
				pcv=dynamic_cast<palConvex *>(PF->CreateObject("palConvex"));
				if (pcv) {
					Float pVerts[(36+36+1)*3];
					int nVerts = (36+36+1);
					MakeConvexCone(pVerts);
					pcv->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,pVerts,nVerts,1);
				//	float radius=0.5f*ufrand()+0.05f;
				//	pc->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,radius,radius+ufrand()+0.1f,1);
					BuildGraphics(pcv);
				} else {
					printf("Error: Could not create a convex object\n");
				} 
				pb = pcv;
				break;
			case SDLK_y:
				{
				palStaticConvex *pcv;
				pcv = NULL;
				pcv=dynamic_cast<palStaticConvex *>(PF->CreateObject("palStaticConvex"));
				if (pcv) {
					Float pVerts[(36+36+1)*3];
					int nVerts = (36+36+1);
					MakeConvexCone(pVerts);
					pcv->Init(sfrand()*3,sfrand()*2+3.0f,sfrand()*3,pVerts,nVerts);
				//	float radius=0.5f*ufrand()+0.05f;
				//	pc->Init(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,radius,radius+ufrand()+0.1f,1);
					BuildGraphics(pcv);
				} else {
					printf("Error: Could not create a static convex object\n");
				} 
				pb = pcv;
				}
				break;
			case SDLK_7:
//				palCompoundBody *pcb;
				pcb = NULL;
				pcb = dynamic_cast<palCompoundBody *>(PF->CreateObject("palCompoundBody"));
				if (pcb) {
					
					Float x = sfrand()*3;
					Float y = sfrand()*2+5.0f;
					Float z = sfrand()*3;
					
					dynamic_cast<palCompoundBody *>(pcb)->Init(x,y,z);
					

					Float pVerts[(36+36+1)*3];
					int nVerts = (36+36+1);
					MakeConvexCone(pVerts);
				
					palConvexGeometry *pcg = 0;
					pcg = pcb->AddConvex();
					if (pcg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,1+x,y,z);
						pcg->Init(m,pVerts,nVerts,1);
					}
					pcg = pcb->AddConvex();
					if (pcg) {
						palMatrix4x4 m;
						mat_identity(&m);
						mat_translate(&m,-1+x,y,z);
						pcg->Init(m,pVerts,nVerts,1);
					}

					pcb->Finalize();
					BuildGraphics(pcb);
				} else {
					printf("Error: Could not create a convex object\n");
				} 
				pb = pcb;
				break;	
			case SDLK_8:
				{
					if (bodies.size()>0) {
						int r= rand() % bodies.size();
						palBody *body = dynamic_cast<palBody*>(bodies[r]);
							if (body) {
						
						body->SetPosition(sfrand()*3,sfrand()*2+5.0f,sfrand()*3,ufrand()*M_PI,ufrand()*M_PI,ufrand()*M_PI);
						body->SetActive(true);
							}
					}
				}
				break;
			case SDLK_9:
				if (bodies.size()>0) {
				DeleteGraphics(bodies[0]);
				delete bodies[0];
				
				bodies.erase(bodies.begin());
				}
				break;
			} 
			if (pb) {
				bodies.push_back(pb);
			}
			break;
		}
	}
void palFakeBuoyancy::Apply() {
		palMatrix4x4 m;
		m=m_pBody->GetLocationMatrix();
		palBoxGeometry *pbg;
		pbg = dynamic_cast<palBoxGeometry *>(m_pBody->m_Geometries[0]);
		if (pbg ){
			/*
			float box_height = pbg->m_fHeight;
			if (m._42 < box_height) {
				float hunder = box_height - m._42; //how much of the box's height is under water?
				if (hunder > box_height) hunder = box_height; //maximum is box height

				float volume = pbg->m_fWidth*pbg->m_fDepth * hunder;

				float gravity = 9.8f;

				float fb = volume * gravity * m_density;

				m_pBody->AddForce(0,fb,0);
			}
			*/
			Float volume = pbg->m_fWidth*pbg->m_fDepth * pbg->m_fHeight;
			Float quatervolume = volume/4;
			Float quatersphereradius = (Float) pow((3/4.0)*quatervolume / M_PI,1/3.0);

			Float r = quatersphereradius;
			//r = pbg->m_fHeight

			palMatrix4x4 m;
			palMatrix4x4 bodypos = m_pBody->GetLocationMatrix();
			palMatrix4x4 out;

			mat_identity(&m);
			mat_translate(&m,pbg->m_fWidth*0.5f,0,0);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
				CalcSphereForce(r,out._42,m_density),0);
//					CalcSphereForce(pbg->m_fHeight,out._42,m_density,volume*0.5f),0);

			mat_identity(&m);
			mat_translate(&m,-pbg->m_fWidth*0.5f,0,0);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
					CalcSphereForce(r,out._42,m_density),0);
//					CalcSphereForce(pbg->m_fHeight,out._42,m_density,volume*0.5f),0);

			//the following are the z-axis floats:
			mat_identity(&m);
			mat_translate(&m,0,0,pbg->m_fDepth*0.5f);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
				CalcSphereForce(r,out._42,m_density),0);

			mat_identity(&m);
			mat_translate(&m,0,0,-pbg->m_fDepth*0.5f);
			mat_multiply(&out,&bodypos,&m);
			m_pBody->ApplyForceAtPosition(out._41,out._42,out._43,0,
				CalcSphereForce(r,out._42,m_density),0);

		} 
		palSphereGeometry *psg;
		psg = dynamic_cast<palSphereGeometry *>(m_pBody->m_Geometries[0]);
		if (psg) {
			m_pBody->ApplyForce(0,
				CalcSphereForce(psg->m_fRadius,m._42,m_density),0);
			/*
			float sphere_radius = psg->m_fRadius;
			if (m._42 < sphere_radius*2) {
				float hunder = sphere_radius*2 - m._42; //how much of the box's height is under water?
				if (hunder > sphere_radius*2) hunder = sphere_radius*2; //maximum is box height
				//float volume = sphere_radius*sphere_radius * hunder; //wrong wrong wrong.

				float volume =  M_PI * hunder * (3 * sphere_radius * hunder - hunder*hunder) / 3.0f;

				float gravity = 9.8f;
				float fb = volume * gravity * m_density;
				m_pBody->AddForce(0,fb,0);
			}
			*/
		}
	}
Exemplo n.º 19
0
void * glclient_thread(void * arg)
{
  server_thread_args_t * a = (server_thread_args_t *)arg;
  static graphics_context_t gc;

  static struct js_event joy;
  int joy_fd;
  static char button[32];

  glclient_context_t *glcc = a->user_context_ptr;

  joy_fd = open(glcc->joy_dev, O_RDONLY);
  if (joy_fd == -1)
  {
    printf("Error: Joystick device open\n");
  }
  if (joy_fd != -1)
  {
    fcntl(joy_fd, F_SETFL, O_NONBLOCK);
  }

  gls_init(a);

  gls_cmd_get_context();
  gc.screen_width = glsc_global.screen_width;
  gc.screen_height = glsc_global.screen_height;
  printf("width:%d height:%d\n",glsc_global.screen_width,glsc_global.screen_height);
  init_gl(&gc);

  float aspect = (float)gc.screen_width / (float)gc.screen_height;

  mat_perspective(proj_mat, aspect, 0.1f, 1024.0f, 60.0f);
  glUniform4fv(gc.uloc_light, 1, light_pos);

  srand(0x12345678);
  int j;
  for (j = 0; j < 1024; j++)
  {
    obj[j].z = randf() * 8.0f - 10.0f;
    obj[j].x = (randf() * 2.0f - 1.0f) * obj[j].z;
    obj[j].y = (randf() * 1.0f - 0.5f) * obj[j].z;
    obj[j].dx = randf() * 0.0f - 0.0f;
    obj[j].dy = randf() * 0.0f - 0.0f;
    obj[j].dz = randf() * 0.0f - 0.0f;
    obj[j].rx = randf() * 6.28;
    obj[j].ry = randf() * 6.28;
    obj[j].rz = randf() * 6.28;
    obj[j].drx = randf() * 0.1f - 0.05f;
    obj[j].dry = randf() * 0.1f - 0.05f;
    obj[j].drz = randf() * 0.1f - 0.05f;
  }

  float x = 1.57f;
  float y = 0.0f;
  float z = -2.0f;
  int k = 1;

  int i;
  for (i = 0; i < 432000; i++)
  {
    struct timeval times, timee;
    gettimeofday(&times, NULL);

    if (joy_fd != -1)
    {
      while (read(joy_fd, &joy, sizeof(struct js_event)) == sizeof(struct js_event))
      {
        if ((joy.type & JS_EVENT_BUTTON) == JS_EVENT_BUTTON)
        {
          button[joy.number] = joy.value;
        }
      }

      if (button[4] > 0)
      {
        y += 0.01f;
      }
      if (button[6] > 0)
      {
        y += -0.01f;
      }
      if (button[5] > 0)
      {
        x += 0.01f * aspect;
      }
      if (button[7] > 0)
      {
        x += -0.01f * aspect;
      }
      if (button[12] > 0)
      {
        z += -0.01f;
      }
      if (button[13] > 0)
      {
        k++;
        k = (k > 45) ? 45 : k;
      }
      if (button[14] > 0)
      {
        z += 0.01f;
      }
      if (button[15] > 0)
      {
        k--;
        k = (k < 1) ? 1 : k;
      }
    }

    glUseProgram(gc.program);
    glBindBuffer(GL_ARRAY_BUFFER, gc.vbo_pos);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, gc.vbo_ind);
    glEnableVertexAttribArray(gc.vloc_pos);
    glEnableVertexAttribArray(gc.vloc_nor);
    glEnableVertexAttribArray(gc.vloc_tex);

    glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    for (j = 0; j < k; j++)
    {
      obj[j].rx += obj[j].drx;
      obj[j].ry += obj[j].dry;
      obj[j].rz += obj[j].drz;

      if (j == 0)
      {
        obj[j].x = 0.0f;
        obj[j].y = 0.0f;
        obj[j].z = z;
        obj[j].rx = -y;
        obj[j].ry = x;
        obj[j].rz = 0.0f;
      }

      mat_identity(model_mat);
      mat_translate(model_mat, obj[j].x, obj[j].y, obj[j].z);
      mat_rotate_x(model_mat, obj[j].rx);
      mat_rotate_y(model_mat, obj[j].ry);
      mat_rotate_z(model_mat, obj[j].rz);

      mat_copy(nor_mat, model_mat);
      mat_invert(nor_mat);
      mat_transpose(nor_mat);
      glUniformMatrix4fv(gc.uloc_nor, 1, GL_FALSE, nor_mat);
      mat_copy(obj[j].nor_mat, nor_mat);

      mat_copy(modelproj_mat, proj_mat);
      mat_mul(modelproj_mat, model_mat);
      glUniformMatrix4fv(gc.uloc_model, 1, GL_FALSE, modelproj_mat);
      mat_copy(obj[j].modelproj_mat, modelproj_mat);

      glDrawElements(GL_TRIANGLES, sizeof(ind_model) / sizeof(GLushort), GL_UNSIGNED_SHORT, 0);
    }
    glDisableVertexAttribArray(gc.vloc_tex);
    glDisableVertexAttribArray(gc.vloc_nor);
    glDisableVertexAttribArray(gc.vloc_pos);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    gls_cmd_flip(i);
    gettimeofday(&timee, NULL);
    //printf("%d:%f ms ", i, get_diff_time(times, timee) * 1000.0f);
  }
  glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  gls_cmd_flip(i);
  release_gl(&gc);
  gls_free();
  if (joy_fd != -1)
  {
    close(joy_fd);
  }
  pthread_exit(NULL);
}
Exemplo n.º 20
0
void
mat_translatev(Mat *m, const Vec *tv)
{
	mat_translate(m, tv->data[0], tv->data[1], tv->data[2]);
}