/** * Dibuje el triángulo formado por los vértices [(-1,-1,-2), (1,-1,-2), (0,1,-2)]. */ int main(int argc, char* argv[]) { // Crear una ventana de 500x500 pixels: int cw = 500; int ch = 500; cg_init(cw, ch, NULL); // Utilizando la función glGetString, imprima por consola el número de versión de OpenGL que utiliza su sistema. printf("GL Version: %s\n", glGetString(GL_VERSION)); // specifies the red, green, blue, and alpha values used by glClear to clear the color buffers. glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // Actualizar la pantalla: glMatrixMode(GL_MODELVIEW); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glViewport(0,0,cw, ch); glFrustum(-1,1,-1,1,1,1000); glClear(GL_COLOR_BUFFER_BIT); glPointSize(10.0f); glBegin(GL_TRIANGLES); glColor3f(0.3f, 0.8f, 0.1f); glVertex3f(-1.0f, -1.0f, -2.0f); glVertex3f(0.0f,1.0f, -2.0f); glVertex3f(-2.0f, 1.0f, -2.0f); glEnd(); cg_repaint(); int done = 0; while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym != SDLK_ESCAPE) break; case SDL_QUIT : done = 1; } } cg_repaint(); } // Liberar recursos: cg_close(); return 0; }
// CG solver kernels void run_cg_init( Chunk* chunk, Settings* settings, double rx, double ry, double* rro) { START_PROFILING(settings->kernel_profile); cg_init(chunk->ext->raja_lists, chunk->x, chunk->y, settings->halo_depth, settings->coefficient, rx, ry, rro, chunk->density, chunk->energy, chunk->u, chunk->p, chunk->r, chunk->w, chunk->kx, chunk->ky); STOP_PROFILING(settings->kernel_profile, __func__); }
/** * 2. Determinación de superficies ocultas por medio de Z-Buffer y optimización por Backface culling. */ int main(int argc, char* argv[]) { // Crear una ventana de 750x750 pixels. int cw = 750; int ch = 750; cg_init(cw, ch, NULL); // Actualizar la pantalla: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glViewport(0,0,cw, ch); glFrustum(-1,1,-1,1,1,1000); Obj* obj = obj_load("../Models/knight.obj"); float ang = 0.0f; float pitch = 0.0f; float ang_vel = 1.0f; char done = 0; char wireframe = 0; char bfc = 0; glEnable(GL_DEPTH_TEST); char zbuff = 1; unsigned char key_pressed[1024]; memset(key_pressed, 0, 1024); while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: key_pressed[event.key.keysym.sym] = 1; if (event.key.keysym.sym == SDLK_z) { zbuff = !zbuff; if(zbuff) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); break; } else if (event.key.keysym.sym == SDLK_b) { bfc = !bfc; if(bfc) { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); } else glDisable(GL_CULL_FACE); break; } else if (event.key.keysym.sym == SDLK_m) { wireframe = !wireframe; if(wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; } else if (event.key.keysym.sym != SDLK_ESCAPE) break; case SDL_QUIT : done = 1; break; case SDL_KEYUP: key_pressed[event.key.keysym.sym] = 0; } } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -50.0f); glRotatef(pitch, 1.0f, 0.0f, 0.0f); glRotatef(ang, 0.0f, 1.0f, 0.0f); glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); if(key_pressed[SDLK_RIGHT]) ang += ang_vel; if(key_pressed[SDLK_LEFT]) ang -= ang_vel; if(key_pressed[SDLK_UP]) pitch += ang_vel; if(key_pressed[SDLK_DOWN]) pitch -= ang_vel; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); obj_render(obj); cg_repaint(); } // Liberar recursos: obj_free(obj); cg_close(); return 0; }
int main(int argc, char* argv[]) { // Crear una ventana de 500x500 pixels: int cw = 640; int ch = 480; cg_init(cw, ch, NULL); init_cggl(cw, ch); Model* model = model_new("knight"); int done = 0; while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: switch(event.key.keysym.sym) { case SDLK_ESCAPE: done = 1; break; case SDLK_UP: rot_speed += 5.0f; break; case SDLK_DOWN: rot_speed -= 5.0f; break; case SDLK_RIGHT: frame += 1; if (frame >= model->md2_model->header.num_frames) frame = 0; break; case SDLK_LEFT: frame -= 1; if (frame < 0) frame = model->md2_model->header.num_frames - 1; break; case SDLK_m: if (mode == CG_TRIANGLES) mode = CG_LINES; else mode = CG_TRIANGLES; break; default: break; } break; case SDL_QUIT : done = 1; break; default: break; } } cg_clear(); paint_model(model); // Actualizar la pantalla: cg_repaint(); double delta = get_delta(); rot_x += rot_speed*delta; rot_y += rot_speed*delta; rot_z += rot_speed*delta; } // Liberar recursos: model_free(model); cgCloseCGGL(); cg_close(); return 0; }
int main(int argc, char* argv[]) { int cw = 750; int ch = 750; cg_init(cw, ch, NULL); glClearColor(0,0,1,1); // Actualizar la pantalla: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glViewport(0,0,cw, ch); glFrustum(-1,1,-1,1,1,1000); glEnable(GL_LIGHTING); // glEnable(GL_NORMALIZE); float l0[] = {1.0f,1.0f,1.0f,1.0f}; float la[] = {0.10f,0.10f,0.10f,1.0f}; float l0p[]= {1.0f,1.0f,1.0f,1.0f}; float ls[] = {1.0f,1.0f,1.0f,1.0f}; glLightfv(GL_LIGHT0, GL_AMBIENT, la); glLightfv(GL_LIGHT0, GL_DIFFUSE, l0); glLightfv(GL_LIGHT0, GL_POSITION, l0p); glLightfv(GL_LIGHT0, GL_SPECULAR, ls); glEnable(GL_LIGHT0); float cyan[] = {1.0f, 1.0f, 1.0f, 1.f}; glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan); glMaterialfv(GL_FRONT, GL_SPECULAR, ls); glMateriali(GL_FRONT, GL_SHININESS, 16); float ang = 0.0f; float pitch = 0.0f; float ang_vel = 1.0f; char done = 0; char wireframe = 0; char bfc = 0; glEnable(GL_DEPTH_TEST); char zbuff = 1; unsigned char key_pressed[1024]; memset(key_pressed, 0, 1024); char use_shader = 0; char specular = 0; Shader gouraud = shader_new("shaders/gouraud_vp.glsl", "shaders/gouraud_fp.glsl"); GLint uniform_especular = shader_get_unif_loc(gouraud, "especular"); GLint uniform_tex = shader_get_unif_loc(gouraud, "tex"); Obj* objPtr ; int example = atoi(argv[1]); if (example==1 || example==3) objPtr = obj_load("../Models/knight_texturas.obj"); else if (example==2) objPtr = obj_load("../Models/box_texturas.obj"); //Cargo la imagen de disco usando SDL_image SDL_Surface* surface; // = IMG_Load("../Models/knight.png"); if (example==1 ) surface = IMG_Load("../Models/knight.png"); else if (example==2) surface = IMG_Load("../Models/box.jpg"); else if (example==3) surface = IMG_Load("../Models/knight_good.png"); // Obj* objPtr = obj_load("../Models/knight_texturas.obj"); // Obj* objPtr = obj_load("../Models/box_texturas.obj"); //Cargo la imagen de disco usando SDL_image // SDL_Surface* surface = IMG_Load("../Models/knight.png"); // SDL_Surface* surface = IMG_Load("../Models/knight_good.png"); // SDL_Surface* surface = IMG_Load("../Models/box.jpg"); if (surface==NULL) { //Si falla la carga de la imagen, despliego el mensaje de error correspondiente y termino el programa. printf("Error: \"%s\"\n", SDL_GetError()); return 1; } GLuint texture; //Creo un espacio para alojar una textura en memoria de video glGenTextures(1,&texture); //Activo la textura nro 0 glActiveTexture(GL_TEXTURE0); //Habilito la carga para la textura recien creada glBindTexture(GL_TEXTURE_2D,texture); //Cargo los datos de la imagen en la textura. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0, GL_RGB,GL_UNSIGNED_BYTE, surface->pixels); //Luego de copiada la imagen a memoria de video, puedo liberarla sin problemas SDL_FreeSurface(surface); //Seteo el filtro a usar cuando se mapea la textura a una superficie mas chica (GL_LINEAR = filtro bilineal) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Seteo el filtro a usar cuando se mapea la textura a una superficie mas grande (GL_LINEAR = filtro bilineal) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); //Seteo el comportamiento cuando encuentro coordenadas de textura fuera del rango [0,1] //GL_REPEAT es el comportamiento por defecto. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: key_pressed[event.key.keysym.sym] = 1; if (event.key.keysym.sym == SDLK_p) { use_shader = !use_shader; break; } else if (event.key.keysym.sym == SDLK_s) { specular = !specular; break; } else if (event.key.keysym.sym == SDLK_z) { zbuff = !zbuff; if(zbuff) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); break; } else if (event.key.keysym.sym == SDLK_m) { wireframe = !wireframe; if(wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; } else if (event.key.keysym.sym == SDLK_b) { bfc = !bfc; if(bfc) { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); } else glDisable(GL_CULL_FACE); break; } else if (event.key.keysym.sym != SDLK_ESCAPE) break; case SDL_QUIT : done = 1; break; case SDL_KEYUP: key_pressed[event.key.keysym.sym] = 0; } } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); if (example==1 || example==3) glTranslatef(0.0f, 0.0f, -50.0f); else if (example==2) glTranslatef(0.0f, 0.0f, -2.0f); // glTranslatef(0.0f, 0.0f, -50.0f); // glTranslatef(0.0f, 0.0f, -2.0f); glRotatef(pitch, 1.0f, 0.0f, 0.0f); glRotatef(ang, 0.0f, 1.0f, 0.0f); glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); if(key_pressed[SDLK_RIGHT]) ang += ang_vel; if(key_pressed[SDLK_LEFT]) ang -= ang_vel; if(key_pressed[SDLK_UP]) pitch += ang_vel; if(key_pressed[SDLK_DOWN]) pitch -= ang_vel; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); //Informo a OpenGL que para todas las operaciones a continuaci�n utilice las texturas 2D cargadas glEnable(GL_TEXTURE_2D); if(use_shader) { shader_use(gouraud); glUniform1i(uniform_especular, specular); //Le digo al shader que el sampler2D de nombre "tex" se corresponde con GL_TEXTURE0 //que es donde cargu� mi textura. glUniform1i(uniform_tex, 0); //Luego asocio la textura con el id "texture" glBindTexture(GL_TEXTURE_2D,texture); obj_render2(objPtr); shader_stop(gouraud); } else { glBindTexture(GL_TEXTURE_2D,texture); obj_render2(objPtr); } cg_repaint(); } // while // Liberar recursos: obj_free(objPtr); shader_free(gouraud); glDeleteTextures(1,&texture); cg_close(); return 0; }
int main(int argc, char **argv) { int optch, return_value = 0; char *input = NULL, *output = NULL, *inc_paths = NULL; char *font_paths = NULL, *img_paths = NULL; cg_back_t do_back = BACK_NO; cg_paper_t do_paper = A4; yvalue_t do_width, do_height, do_margin_width, do_margin_height; ybool_t set_back = YFALSE, set_paper = YFALSE, set_pdf = YFALSE; ybool_t set_width = YFALSE, set_height = YFALSE, set_margin = YFALSE; ybool_t set_margin_width = YFALSE, set_margin_height = YFALSE; ybool_t do_reverse = YFALSE, set_reverse = YFALSE; ybool_t set_landscape = YFALSE, set_quiet = YFALSE; cg_pdf_version_t pdf_version = PDF_14; char *author = NULL, *title = NULL, *log_file = NULL;; cg_t *carta; /* parse the command line */ while ((optch = getopt(argc, argv, "p:w:h:l:m:x:y:b:v:r:i:o:d:e:t:L:q")) != -1) switch (optch) { case 'p': /* paper type */ set_paper = YTRUE; do_paper = cg_get_paper(optarg); do_width = cg_get_paper_width(do_paper); do_height = cg_get_paper_height(do_paper); break ; case 'w': /* paper width */ set_width = YTRUE; do_width = yvalue_read(optarg, YUNIT_MM); break ; case 'h': /* paper height */ set_height = YTRUE; do_height = yvalue_read(optarg, YUNIT_MM); break ; case 'l': /* landscape mode */ if (!strcasecmp(optarg, "yes") || !strcasecmp(optarg, "true")) set_landscape = YTRUE; break ; case 'm': /* margin size */ set_margin = YTRUE; do_margin_width = yvalue_read(optarg, YUNIT_MM); do_margin_height = do_margin_width; break ; case 'x': /* margin width */ set_margin_width = YTRUE; do_margin_width = yvalue_read(optarg, YUNIT_MM); break ; case 'y': /* margin height */ set_margin_height = YTRUE; do_margin_height = yvalue_read(optarg, YUNIT_MM); break ; case 'b': /* cards' back configuration */ set_back = YTRUE; if (!strcasecmp(optarg, "width")) do_back = BACK_WIDTH; else if (!strcasecmp(optarg, "height")) do_back = BACK_HEIGHT; else if (!strcasecmp(optarg, "no")) do_back = BACK_NO; else set_back = YFALSE; break ; case 'v': /* PDF version */ set_pdf = YTRUE; if (!strcmp(optarg, "1.3")) pdf_version = PDF_13; else if (!strcmp(optarg, "1.4")) pdf_version = PDF_14; else if (!strcmp(optarg, "1.5")) pdf_version = PDF_15; else set_pdf = YFALSE; break ; case 'r': /* inverse page order */ if (!strcasecmp(optarg, "yes") || !strcasecmp(optarg, "true")) { set_reverse = YTRUE; do_reverse = YTRUE; } else if (!strcasecmp(optarg, "no") || !strcasecmp(optarg, "false")) { set_reverse = YTRUE; do_reverse = YFALSE; } break ; case 'd': /* include path directories */ inc_paths = strdup(optarg); break ; case 'f': /* font path directories */ font_paths = strdup(optarg); break ; case 'g': /* image path directories */ img_paths = strdup(optarg); break ; case 'i': /* input file */ input = strdup(optarg); break ; case 'o': /* output file */ output = strdup(optarg); break ; case 'e': /* author */ author = strdup(optarg); break ; case 't': /* title */ title = strdup(optarg); break ; case 'L': /* log file */ log_file = strdup(optarg); break ; case 'q': /* quiet mode */ set_quiet = YTRUE; break ; default: _usage(); exit(1); } if (!input || !output) { _usage(); exit(1); } /* logging init */ if (set_quiet && log_file) YLOG_INIT_FILE(log_file); else if (set_quiet && !log_file) YLOG_INIT_SYSLOG(); else if (!set_quiet && log_file) YLOG_INIT_STDERR_FILE(log_file); else YLOG_INIT_STDERR(); /* read input file */ if (!(carta = cg_init(input, inc_paths, font_paths, img_paths))) { YLOG_ADD(YLOG_ERR, "Unable to read input file '%s'", input); exit(1); } /* update with command line options */ if (set_back) carta->do_back = do_back; if (set_paper || set_width || set_height || set_landscape || set_margin || set_margin_width || set_margin_height) { cg_deck_t *deck; int i; for (i = 0; i < yv_len(carta->decks); ++i) { deck = carta->decks[i]; if (set_paper || set_width) deck->paper_width = do_width; if (set_paper || set_height) deck->paper_height = do_height; if (set_landscape) { yvalue_t tmp_val = deck->paper_width; deck->paper_width = deck->paper_height; deck->paper_height = tmp_val; } if (set_margin || set_margin_width) deck->paper_margin_w = do_margin_width; if (set_margin || set_margin_height) deck->paper_margin_h = do_margin_height; } } if (set_pdf) carta->pdf_version = pdf_version; if (set_reverse) carta->reverse = do_reverse; if (author) { free0(carta->author); carta->author = author; } if (title) { free0(carta->title); carta->title = title; } /* compute cols and rows */ if (cg_compute_cols_rows(carta) == YENOERR) { /* create the PDF file */ if (cg_create_document(output, carta) != YENOERR) return_value = 2; } else return_value = 3; /* free memory */ yv_del(&carta->xml_doms, free_dom, NULL); yv_del(&carta->decks, free_deck, NULL); yv_del(&carta->fonts, free_font, NULL); yv_del(&carta->images, free_image, NULL); yv_del(&carta->masks, free_image, NULL); yv_del(&carta->templates, NULL, NULL); yv_del(&carta->variables, NULL, NULL); yv_del(&carta->inc_paths, free_path, NULL); yv_del(&carta->img_paths, free_path, NULL); yv_del(&carta->text_defines, free_text_define, NULL); if (carta->expr) { yv_del(&carta->expr->vars, free_var, NULL); yv_del(&carta->expr->funcs, free_func, NULL); free0(carta->expr); } free0(carta->author); free0(carta->title); free0(carta->subject); free0(carta->keywords); free0(carta->copyright); free0(carta->version); free0(carta->language); free0(carta->note); free0(carta->master_password); free0(carta->user_password); free0(carta); return (return_value); }
/** * Dibuje el modelo definido por el archivo knight.obj 1 disponible en webasignatura. El * formato del archivo OBJ se encuentra definido en el anexo I de este documento. Cada * triángulo del modelo debe ser dibujado usando un tono de gris randómico en sus vértices. * Nota: Para parsear el archivo obj, puede utilizar las funciones fgets (definida en stdio.h), * strcmp, strtok (definidas en string.h), atof y atoi (definidas en stdlib.h). */ int main(int argc, char* argv[]) { // Crear una ventana de 500x500 pixels: int cw = 900; int ch = 900; cg_init(cw, ch, NULL); #ifdef WIN32 freopen( "CON", "w", stdout ); freopen( "CON", "w", stderr ); #endif printf("GL Version: %s\n", glGetString(GL_VERSION)); // Actualizar la pantalla: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glViewport(0,0,cw, ch); glFrustum(-1,1,-1,1,1,1000); //Habilito la iluminación del pipeline estático de OpenGL. glEnable(GL_LIGHTING); //Creo un Vec4 para representar el color (RGBA) y la Intensidad de la luz. //P.e.: (1,1,1,1) = Luz Blanca intensa, (0.5,0.5,0.5,1) = Luz blanca tenue, (0.5,0,0,1) = Luz roja tenue. float l0[] = {1.0f,1.0f,1.0f,1.0f}; //Creo un Vec4 para representar el color (RGBA) y la intensidad de la iluminación ambiente de la luz float la[] = {0.10f,0.10f,0.10f,1.0f}; //Creo un Vec4 para representar la posición de la luz. El cuarto elemento representa el tipo de luz: 1=puntual, 0=direccional float l0p[]= {1.0f,1.0f,1.0f,1.0f}; //Creo un Vec4 para representar el color (RGBA) y la intensidad especular de la luz float ls[] = {1.0f,1.0f,1.0f,1.0f}; //Cargo la intesidad ambiente de la Luz Nro 0 del pipline estático. glLightfv(GL_LIGHT0, GL_AMBIENT, la); //Cargo la intesidad difusa de la Luz Nro 0 del pipline estático. glLightfv(GL_LIGHT0, GL_DIFFUSE, l0); //Cargo la posición de la Luz Nro 0 del pipline estático. glLightfv(GL_LIGHT0, GL_POSITION, l0p); //Cargo la intesidad especular de la Luz Nro 0 del pipline estático. glLightfv(GL_LIGHT0, GL_SPECULAR, ls); //Prendo la Luz nro 0 del pipline estático. Hay 8 luces, representadas por las constantes GL_LIGHT0 a GL_LIGHT7 //Por defecto está todas apagadas al inicio. glEnable(GL_LIGHT0); //Creo un Vec4 para representar el color difuso(RGBA) del material del objeto a dibujar. float cyan[] = {1.0f, 0.0f, 1.0f, 1.f}; //Cargo el color difuso del la cara de adelante del objeto a dibujar. glMaterialfv(GL_FRONT, GL_DIFFUSE, cyan); //Cargo el color especular del la cara de adelante del objeto a dibujar. glMaterialfv(GL_FRONT, GL_SPECULAR, ls); //Cargo el coeficiente especular de la cara de adelante del objeto a dibujar. glMateriali(GL_FRONT, GL_SHININESS, 32); float ang = 0.0f; float pitch = 0.0f; float ang_vel = 1.0f; Obj* obj = obj_load("Models/knight.obj"); printf("num of faces %d\n", obj->numfaces); // delete printf("num of vertices %d\n", obj->numverts); // delete char done = 0; char wireframe = 0; char bfc = 0; glEnable(GL_DEPTH_TEST); char zbuff = 1; unsigned char key_pressed[1024]; memset(key_pressed, 0, 1024); while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: key_pressed[event.key.keysym.sym] = 1; if (event.key.keysym.sym == SDLK_z) { zbuff = !zbuff; if(zbuff) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); break; } else if (event.key.keysym.sym == SDLK_m) { wireframe = !wireframe; if(wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; } else if (event.key.keysym.sym == SDLK_b) { bfc = !bfc; if(bfc) { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); } else glDisable(GL_CULL_FACE); break; } else if (event.key.keysym.sym != SDLK_ESCAPE) break; case SDL_QUIT : done = 1;break; case SDL_KEYUP: key_pressed[event.key.keysym.sym] = 0; } } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -50.0f); glRotatef(pitch, 1.0f, 0.0f, 0.0f); glRotatef(ang, 0.0f, 1.0f, 0.0f); glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); if(key_pressed[SDLK_RIGHT]) ang += ang_vel; if(key_pressed[SDLK_LEFT]) ang -= ang_vel; if(key_pressed[SDLK_UP]) pitch += ang_vel; if(key_pressed[SDLK_DOWN]) pitch -= ang_vel; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); obj_render(obj); cg_repaint(); } obj_free(obj); // Liberar recursos: cg_close(); // Ejemplo del modulo de Manejo de Memoria (MM): int* pint = (int *)cg_malloc(10*sizeof(int)); printf("pint is a pointer: %p\n", pint); cg_free(pint); // olvidarse de liberar este objeto produce un mensaje return 0; }
void plot_something(char *progname, int use_stdout) { int rot= 1; /* Portrait=1, Landscape=0 */ int expand= 1; /* left over from PC version, not used in NeXT Cgraph */ float scale= 1.0; /* scaling factor */ float radius1, radius2; /* analysis or plot parameters */ char comment[4096]; /* reserve sufficient space for header comments for EPS file */ char strbuff[128]; int i; float x, y, angle; if(use_stdout) cg_use_stdout(use_stdout); /* decide where to send output */ cg_useflexcolor(2); /* let the device capability dictate color/monochrome */ cg_setboundingbox("0 0 216 216"); cg_settitle(prog_version); cg_setcreator(progname); /* Prepare comment block for EPS for data source tracing */ radius1= 0.8; radius2= 1.2; /* PostScript comment lines must start with a % character. */ sprintf(comment, "%% %s\n", prog_version); sprintf(comment+strlen(comment), "%% Original data file: %s\n", progname); sprintf(comment+strlen(comment), "%% Analysis parameter-1: %g\n", radius1); sprintf(comment+strlen(comment), "%% Analysis parameter-1: %g\n", radius2); /* ... more stuff to record as needed .. */ cg_set_top_comments(comment); /* set the comments for EPS header */ cg_init(rot, expand, scale); /* expand is not used in cg_init() but for compatibility */ cg_aorigin(1.5, 1.5); /* move origin to center of BoundingBox */ /* interesting shape */ sprintf(strbuff, "\n\n%% Radius %g\n", radius1); cg_postscript(strbuff); /* enter comment into file for possible later editing */ for(i=0; i<=200; i++) { angle = PI*(float)i/100.0; x = radius2 * cos(3.0*angle); y = radius2 * sin(5.0*angle); if(i) cg_line(x, y); else cg_move(x, y); /* first point */ } cg_closepath(); /* cg_postscript("\n\n%% Demo of falling back on injection of raw PostScript code.\n"); cg_postscript("\n gsave 0.6 1.0 0.6 0.6 setgrayrgbcolor eofill grestore\n"); */ /* 4 lines below will do the same thing as the above raw PS code. */ cg_gsave(); cg_grayrgbcolor(0.6, 1.0, 0.6, 0.6); cg_eofill(); cg_grestore(); /* void cg_grayrgbcolor(float gray, float r, float g, float b); */ cg_grayrgbcolor(0.0, 0.0, 0.0, 1.0); /* black for gray, and blue for color */ cg_stroke(); /* strokes the two circles */ cg_postscript("\n%% End of my plot.\n"); cg_showpage(); }
int main(int argc, char *argv[]) { if (argc != 2) { printf("Usage: %s file.cgl\n", argv[0]); exit(-1); } SDL_Surface *screen; SDL_Surface *gfx = load_gfx("data/GRAVITY.GFX"); if (!gfx) { fprintf(stderr, "read_gfx: %s\n", SDL_GetError()); abort(); } SDL_Surface *png = load_png("font.png"); if (!png) { fprintf(stderr, "load_png: %s\n", SDL_GetError()); abort(); } SDL_Surface *osd = load_png("osd.png"); if (!osd) { fprintf(stderr, "load_png: %s\n", SDL_GetError()); abort(); } struct cgl *cgl = read_cgl(argv[1], NULL); if (!cgl) { fprintf(stderr, "read_cgl: %s\n", SDL_GetError()); abort(); } cgl_preprocess(cgl); cg_init(cgl); make_collision_map(gfx, cmap); if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "SDL failed: %s\n", SDL_GetError()); abort(); } screen = SDL_SetVideoMode(SCREEN_W, SCREEN_H, 0, MODE); gl_resize_viewport(screen->w, screen->h); struct texmgr *ttm = tm_request_texture(gfx); struct texmgr *fnt = tm_request_texture(png); struct texmgr *otm = tm_request_texture(osd); gl_init(cgl, ttm, fnt, otm); int t = SDL_GetTicks(), nt = t, time = t, fr = 0; running = 1; mouse = 0; SDL_Event e; while (running) { while (SDL_PollEvent(&e)) process_event(&e); time = SDL_GetTicks(); nt = time - t; cg_step(cgl, time / 1000.0); if (nt > 100) { printf("%d frames in %d ms - %.1f fps\n", gl.frame - fr, nt, (float)(gl.frame - fr) / nt * 1000); if (cgl->status == Lost) printf("Dead. Game over!"); if (cgl->status == Victory) printf("You won!"); fflush(stdout); t += nt; fr = gl.frame; } gl.cam.nx = cgl->ship->x + SHIP_W/2.0; gl.cam.ny = cgl->ship->y + SHIP_H/2.0; gl_update_window(time / 1000.0); } free_cgl(cgl); return 0; }
/** * http://cse.csusb.edu/tongyu/courses/cs420/notes/lighting.php * Sombreado por medio de la técnica Gouraud Shading. */ int main(int argc, char* argv[]) { // Crear una ventana de 750x750 pixels. int cw = 750; int ch = 750; cg_init(cw, ch, NULL); // Actualizar la pantalla: glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glViewport(0,0,cw, ch); glFrustum(-1,1,-1,1,1,1000); //Habilito la iluminación del pipeline estático de OpenGL. glEnable(GL_LIGHTING); glEnable(GL_DEPTH_TEST); // GL_LIGHT0 Vec4 *vec4La0 = vec4Contructor(0.10f, 0.10f, 0.10f, 1.0f); Vec4 *vec4Ld0 = vec4Contructor(1.0f, 1.0f, 1.0f, 1.0f); Vec4 *vec4Ls0 = vec4Contructor(1.0f, 1.0f, 1.0f, 1.0f); Vec4 *vec4Lp0 = vec4Contructor(1.0f, 1.0f, 1.0f, 0.0f); loadLight(GL_LIGHT0, vec4La0, vec4Ld0, vec4Lp0, vec4Ls0); // GL_LIGHT1 Vec4 *vec4La1 = vec4Contructor(0.5f,0.5f,0.5f,1.0f); Vec4 *vec4Ld1 = vec4Contructor(0.5f, 0.3f, 0.2f, 1.0f); Vec4 *vec4Ls1 = vec4Contructor(1.0f,1.0f,1.0f,1.0f); Vec4 *vec4Lp1 = vec4Contructor(1.0f, 1.0f, 1.0f, 0.0f); loadLight(GL_LIGHT1, vec4La1, vec4Ld1, vec4Lp1, vec4Ls1); // GLfloat spot_direction[] = { -1.0, -1.0, 0.0 }; // glLightf(GL_LIGHT1, GL_CONSTANT_ATTENUATION, 1.5); // glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, 0.5); // glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION, 0.2); // glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 45.0); // glLightfv(GL_LIGHT1, GL_SPOT_DIRECTION, spot_direction); // glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 2.0); // GL_LIGHT2 Vec4 *vec4La2 = vec4Contructor( 0.2f, 0.2f, 0.2f, 1.0f); Vec4 *vec4Ld2 = vec4Contructor(0.8f, 0.8f, 0.8, 1.0f ); Vec4 *vec4Lp2 = vec4Contructor(-1.5f, 1.0f, -4.0f, 1.0f ); Vec4 *vec4Ls2 = vec4Contructor(0.5f, 0.5f, 0.5f, 1.0f ); loadLight(GL_LIGHT2, vec4La2, vec4Ld2, vec4Lp2, vec4Ls2); // TODO, GL_LIGHT3, GL_LIGHT4, GL_LIGHT5, GL_LIGHT6, GL_LIGHT7 Obj* obj = obj_load("../Models/knight.obj"); float ang = 0.0f; float pitch = 0.0f; float ang_vel = 1.0f; char done = 0; char wireframe = 0; char gourandShading = 0; char bfc = 0; char zbuff = 1; char enableLight0 = 0; char enableLight1 = 0; char enableLight2 = 0; char enableLight3 = 0; char enableLight4 = 0; char enableLight5 = 0; char enableLight6 = 0; char enableLight7 = 0; unsigned char key_pressed[1024]; memset(key_pressed, 0, 1024); while (!done) { SDL_Event event; while(SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: key_pressed[event.key.keysym.sym] = 1; if (event.key.keysym.sym == SDLK_z) { zbuff = !zbuff; if(zbuff) glEnable(GL_DEPTH_TEST); else glDisable(GL_DEPTH_TEST); break; } else if (event.key.keysym.sym == SDLK_b) { bfc = !bfc; if(bfc) { glEnable(GL_CULL_FACE); glCullFace(GL_BACK); glFrontFace(GL_CW); } else glDisable(GL_CULL_FACE); break; } else if (event.key.keysym.sym == SDLK_m) { wireframe = !wireframe; if(wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); break; } else if (event.key.keysym.sym == SDLK_g) { gourandShading = !gourandShading; if(gourandShading) glShadeModel(GL_FLAT); else glShadeModel(GL_SMOOTH); break; } else if (event.key.keysym.sym == SDLK_0) { enableLight0 = !enableLight0; if(enableLight0) glEnable(GL_LIGHT0); else glDisable(GL_LIGHT0); break; } else if (event.key.keysym.sym == SDLK_1) { enableLight1 = !enableLight1; if(enableLight1) glEnable(GL_LIGHT1); else glDisable(GL_LIGHT1); break; } else if (event.key.keysym.sym == SDLK_2) { enableLight2 = !enableLight2; if(enableLight2) glEnable(GL_LIGHT2); else glDisable(GL_LIGHT2); break; } else if (event.key.keysym.sym == SDLK_3) { enableLight3 = !enableLight3; if(enableLight3) glEnable(GL_LIGHT3); else glDisable(GL_LIGHT3); break; } else if (event.key.keysym.sym == SDLK_4) { enableLight4 = !enableLight4; if(enableLight4) glEnable(GL_LIGHT4); else glDisable(GL_LIGHT4); break; } else if (event.key.keysym.sym == SDLK_5) { enableLight5 = !enableLight5; if(enableLight5) glEnable(GL_LIGHT5); else glDisable(GL_LIGHT5); break; } else if (event.key.keysym.sym == SDLK_6) { enableLight6 = !enableLight6; if(enableLight6) glEnable(GL_LIGHT6); else glDisable(GL_LIGHT6); break; } else if (event.key.keysym.sym == SDLK_7) { enableLight7 = !enableLight7; if(enableLight7) glEnable(GL_LIGHT7); else glDisable(GL_LIGHT7); break; } else if (event.key.keysym.sym != SDLK_ESCAPE) break; case SDL_QUIT : done = 1; break; case SDL_KEYUP: key_pressed[event.key.keysym.sym] = 0; } } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0f, 0.0f, -50.0f); glRotatef(pitch, 1.0f, 0.0f, 0.0f); glRotatef(ang, 0.0f, 1.0f, 0.0f); glRotatef(-90.0f, 1.0f, 0.0f, 0.0f); if(key_pressed[SDLK_RIGHT]) ang += ang_vel; if(key_pressed[SDLK_LEFT]) ang -= ang_vel; if(key_pressed[SDLK_UP]) pitch += ang_vel; if(key_pressed[SDLK_DOWN]) pitch -= ang_vel; glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); obj_render(obj); cg_repaint(); } // Liberar recursos: obj_free(obj); cg_close(); return 0; }