void render_cloud(Cloud *cloud, CloudAttrib *attrib){ float matrix[16]; float matrix_prev[16]; glGetUniformfv(attrib->program, attrib->model, matrix_prev); glGetUniformfv(attrib->program, attrib->model, matrix); mat_identity(matrix); mat_translate(matrix, cloud->x - (cloud->hmWidth/2), CLOUD_Y_HEIGHT + cloud->y, cloud->z - (cloud->hmDepth/2)); mat_scale(matrix, cloud->sx, cloud->sy, cloud->sz); glUniform3f(attrib->cloudColour, cloud->r, cloud->g, cloud->b); int i, j; for (i = 0; i < cloud->hmWidth; i++) { for (j=0; j < cloud->hmDepth; j++) { mat_translate_existing(matrix, 0, 0, 1); int heightval = cloud->heightmap[i*(cloud->hmDepth) + j]; if(heightval > 0){ float up = heightval/2.0f; mat_translate_existing(matrix, 0, up, 0); mat_scale(matrix, 1, heightval, 1); glUniformMatrix4fv(attrib->model, 1, GL_FALSE, matrix); glDrawArrays(GL_TRIANGLES, 0, 36); mat_translate_existing(matrix, 0, -up, 0); mat_scale(matrix, 1, 1.0f/heightval, 1); } } mat_translate_existing(matrix, 1, 0, -cloud->hmDepth); } glUniformMatrix4fv(attrib->model, 1, GL_FALSE, matrix_prev); }
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); }
void mat_scalev(Mat *m, const Vec *sv) { mat_scale(m, sv->data[0], sv->data[1], sv->data[2]); }
/* Subtree version of score test */ void ff_score_tests_sub(TreeModel *mod, MSA *msa, GFF_Set *gff, mode_type mode, double *feat_pvals, double *feat_null_scales, double *feat_derivs, double *feat_sub_derivs, double *feat_teststats, FILE *logf) { int i; FeatFitData *d, *d2; Vector *grad = vec_new(2); Matrix *fim = mat_new(2, 2); double lnl, teststat; FimGrid *grid; List *inside=NULL, *outside=NULL; TreeModel *modcpy = tm_create_copy(mod); /* need separate copy of tree model with different internal scaling data for supertree/subtree case */ /* init FeatFitData -- one for null model, one for alt */ d = ff_init_fit_data(modcpy, msa, ALL, NNEUT, FALSE); d2 = ff_init_fit_data(mod, msa, SUBTREE, NNEUT, FALSE); /* mod has the subtree info, modcpy does not */ /* precompute Fisher information matrices for a grid of scale values */ grid = col_fim_grid_sub(mod); /* prepare lists of leaves inside and outside root, for use in checking for informative substitutions */ if (mod->subtree_root != NULL) { inside = lst_new_ptr(mod->tree->nnodes); outside = lst_new_ptr(mod->tree->nnodes); tr_partition_leaves(mod->tree, mod->subtree_root, inside, outside); } /* iterate through features */ for (i = 0; i < lst_size(gff->features); i++) { checkInterrupt(); d->feat = lst_get_ptr(gff->features, i); /* first check for informative substitution data in feature; if none, don't waste time computing likelihoods */ if (!ff_has_data_sub(mod, msa, d->feat, inside, outside)) { teststat = 0; vec_zero(grad); } else { vec_set(d->cdata->params, 0, d->cdata->init_scale); opt_newton_1d(ff_likelihood_wrapper_1d, &d->cdata->params->data[0], d, &lnl, SIGFIGS, d->cdata->lb->data[0], d->cdata->ub->data[0], logf, NULL, NULL); /* turns out to be faster to use numerical rather than exact derivatives (judging by col case) */ d2->feat = d->feat; d2->cdata->mod->scale = d->cdata->params->data[0]; d2->cdata->mod->scale_sub = 1; tm_set_subst_matrices(d2->cdata->mod); ff_scale_derivs_subtree(d2, grad, NULL, d2->cdata->fels_scratch); fim = col_get_fim_sub(grid, d2->cdata->mod->scale); mat_scale(fim, d->feat->end - d->feat->start + 1); /* scale column-by-column FIM by length of feature (expected values are additive) */ teststat = grad->data[1]*grad->data[1] / (fim->data[1][1] - fim->data[0][1]*fim->data[1][0]/fim->data[0][0]); if (teststat < 0) { fprintf(stderr, "WARNING: teststat < 0 (%f)\n", teststat); teststat = 0; } if ((mode == ACC && grad->data[1] < 0) || (mode == CON && grad->data[1] > 0)) teststat = 0; /* derivative points toward boundary; truncate at 0 */ mat_free(fim); } if (feat_pvals != NULL) { if (mode == NNEUT || mode == CONACC) feat_pvals[i] = chisq_cdf(teststat, 1, FALSE); else feat_pvals[i] = half_chisq_cdf(teststat, 1, FALSE); /* assumes 50:50 mix of chisq and point mass at zero */ if (feat_pvals[i] < 1e-20) feat_pvals[i] = 1e-20; /* approx limit of eval of tail prob; pvals of 0 cause problems */ if (mode == CONACC && grad->data[1] > 0) feat_pvals[i] *= -1; /* mark as acceleration */ } /* store scales and log likelihood ratios if necessary */ if (feat_null_scales != NULL) feat_null_scales[i] = d->cdata->params->data[0]; if (feat_derivs != NULL) feat_derivs[i] = grad->data[0]; if (feat_sub_derivs != NULL) feat_sub_derivs[i] = grad->data[1]; if (feat_teststats != NULL) feat_teststats[i] = teststat; } ff_free_fit_data(d); ff_free_fit_data(d2); vec_free(grad); modcpy->estimate_branchlens = TM_BRANCHLENS_ALL; /* have to revert for tm_free to work correctly */ tm_free(modcpy); col_free_fim_grid(grid); if (inside != NULL) lst_free(inside); if (outside != NULL) lst_free(outside); }
//#define WINDOWED int main() { SDL_Init(SDL_INIT_VIDEO); SDL_VideoInit(NULL); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1); SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 8); #ifdef WINDOWED SDL_Window *win = SDL_CreateWindow("hello world", 0, 0,640, 480, SDL_WINDOW_OPENGL); #define ASPECT 640.0/480 #else SDL_Window *win = SDL_CreateWindow("hello world", 0, 0,2560, 1440, SDL_WINDOW_OPENGL|SDL_WINDOW_FULLSCREEN); #define ASPECT 2560.0/1440 #endif SDL_ShowWindow(win); SDL_GLContext context_dontneedtosavethis = SDL_GL_CreateContext(win); SDL_GL_SetSwapInterval(0); #ifdef WINDOWED sm_screensize(640,480); // glViewport(0,0,640,480); #else sm_screensize(2560,1440); // glViewport(0,0,1366,768); #endif glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glClearColor(0.3,0.5,1,0); glClear(GL_COLOR_BUFFER_BIT); if(sm_load("example"))return; smm_open("examplemodel.tmd"); GLuint examplemodelvao = smm_upload(); int examplemodelnumtris =smm_numtris(); //========================feedback============================== if(sm_load("examplefeedback"))return; smm_open("exampleinstancing.tmd");//load the model to be instanced GLuint instancedobjectsvao = smm_upload(); int instancedobjectsnumtris = smm_numtris(); glBindVertexArray(instancedobjectsvao); GLuint instancedobjectspositionvbo; glGenBuffers(1, &instancedobjectspositionvbo);//this is where we'll put the position offsets generated by the examplefeedback shader glBindBuffer(GL_ARRAY_BUFFER, instancedobjectspositionvbo); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*500, NULL, GL_DYNAMIC_COPY);//allocate memory on gpu for 500 position vectors glEnableVertexAttribArray(2);//enables the third attribute (offset) glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, sizeof(float)*3, 0);//associates the attribute with the location and with the active vbo (instancedobjectspositionvbo) glVertexAttribDivisor(2, 1);//set attribute 2 (offset) to change per instance instead of per vertex glBindBuffer(GL_ARRAY_BUFFER, 0); GLuint feedbackquery;//to count how many instanced objects got through the geometry shader (if used) glGenQueries(1, &feedbackquery); //========================/feedback============================== if(sm_load("examplepp"))return; if(sm_load("drawtext"))return; unsigned char printtext[100] = {0}; GLuint textvao, textvbo;//manually setup the vao glGenVertexArrays(1,&textvao); glBindVertexArray(textvao); glGenBuffers(1,&textvbo); glBindBuffer(GL_ARRAY_BUFFER, textvbo); glBufferData(GL_ARRAY_BUFFER, /*sizeof(vertexdata)*/strlen(printtext), printtext, GL_STREAM_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 1, GL_UNSIGNED_BYTE, GL_FALSE, 0, 0);//associates first attribute with vertexdata, also sets source to vbo float modelviewproj[16]; mat_identity(modelviewproj); mat_proj2(modelviewproj, 1.7453, ASPECT, 300, 0.1); long tstart = SDL_GetTicks(); int i; float rot =0; float transx=0,transy=-5,transz=5; float rotmat[16]; float wrot = 0; float wroty = 0; float speed = 0.001; mat_identity(rotmat); //SDL_SetRelativeMouseMode(SDL_TRUE);//this is not supported on all platforms, warping the mouse is more portable for(i=0;i>=0;i++) { sm_use("example"); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glBindVertexArray(examplemodelvao); glDrawElements(GL_TRIANGLES, examplemodelnumtris*3, GL_UNSIGNED_SHORT, NULL); sm_use("examplefeedback"); float wavetime = SDL_GetTicks() * 0.01; glUniform1fv(sm_uniloc("wavetime"), 1,&wavetime); glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, instancedobjectspositionvbo);//set output for transform feedback glBeginQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN, feedbackquery);//start counting { glBeginTransformFeedback(GL_POINTS); { glDrawArrays(GL_POINTS, 0, 500);//generate 500 position vectors }glEndTransformFeedback(); }glEndQuery(GL_TRANSFORM_FEEDBACK_PRIMITIVES_WRITTEN); GLuint feedbacknumgenerated; glGetQueryObjectuiv(feedbackquery, GL_QUERY_RESULT, &feedbacknumgenerated);//get how many position vectors were generated sm_use("example"); glBindVertexArray(instancedobjectsvao); glDrawElementsInstanced(GL_TRIANGLES, instancedobjectsnumtris*3, GL_UNSIGNED_SHORT, NULL, feedbacknumgenerated);//draw the instances sm_use("examplepp"); glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glDrawArrays(GL_POINTS, 0, 1);//we're using attributeless rendering, so we don't need a vao sm_use("drawtext"); glDisable(GL_DEPTH_TEST); glBindVertexArray(textvao); glEnable(GL_DEPTH_TEST); if(i%5==0)//calculate framerate every 5 frames { static long timesincelast; memset(printtext, 0, 100); long timethisframe = timesincelast-SDL_GetTicks(); if(timethisframe != 0) SDL_itoa(1000*5/timethisframe, printtext, 10); strcat(printtext, "FPS"); timesincelast = SDL_GetTicks(); glBufferData(GL_ARRAY_BUFFER, strlen(printtext), printtext, GL_STREAM_DRAW);//upload the text as an ordinary string } glDisable(GL_DEPTH_TEST); glDrawArrays(GL_POINTS, 0,strlen(printtext));//draw the text to the screen glEnable(GL_DEPTH_TEST); SDL_GL_SwapWindow(win); SDL_Event evt; while(SDL_PollEvent(&evt)) { if(evt.type == SDL_QUIT) i= -i; } const char *keys = SDL_GetKeyboardState(NULL); speed = 0.1; if(keys[SDL_SCANCODE_LSHIFT]) speed = 2.8; if(keys[SDL_SCANCODE_W]) { transz -=speed*cos(wrot); transx +=speed*sin(wrot); } if(keys[SDL_SCANCODE_A]) { transz +=speed*cos(wrot-1.570796); transx -=speed*sin(wrot-1.570796); } if(keys[SDL_SCANCODE_S]) { transz +=speed*cos(wrot); transx -=speed*sin(wrot); } if(keys[SDL_SCANCODE_D]) { transz -=speed*cos(wrot-1.570796); transx +=speed*sin(wrot-1.570796); } if(keys[SDL_SCANCODE_SPACE]) transy -=speed; if(keys[SDL_SCANCODE_LCTRL]) transy +=speed; int mousex, mousey; //SDL_GetRelativeMouseState(&mousex, &mousey); SDL_GetMouseState(&mousex, &mousey); SDL_WarpMouseInWindow(win, 400,400); mousex = mousex-400; mousey = mousey-400; wrot -= mousex/1000.0; wroty-= mousey/1000.0; wroty = wroty > 1.57?1.57:wroty<-1.57?-1.57:wroty; float temp[16]; mat_identity(temp); mat_mul(temp, modelviewproj); mat_rot(temp, 1,0,0,wroty); mat_rot(temp, 0,1,0,wrot); mat_trans(temp, transx, transy, transz); mat_mul(temp, rotmat); mat_scale(temp, 10, 10, 10); sm_use("example"); glUniformMatrix4fv(sm_uniloc("modelviewproj"), 1, GL_TRUE, temp);//upload the model-view-projection matrix } i = -i; printf("last sdl error:%s\n", SDL_GetError()); printf("vendor:%s\n", glGetString(GL_RENDERER)); }