Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anterior Revisión previa Próxima revisión | Revisión previa | ||
|
fw:ogl:xtra [2008/08/16 19:47] alfred |
fw:ogl:xtra [2020/05/09 09:25] (actual) |
||
|---|---|---|---|
| Línea 2: | Línea 2: | ||
| ===== Más funciones y trucos con OpenGL ===== | ===== Más funciones y trucos con OpenGL ===== | ||
| + | |||
| Línea 53: | Línea 54: | ||
| cosf(x), sinf(y), 0.0f); | cosf(x), sinf(y), 0.0f); | ||
| </code> | </code> | ||
| + | === Ejemplos === | ||
| + | * {{fw:ogl:matrixprojection.zip|Ejemplo}} | ||
| ==== Cambiar de repente vista perspectiva a ortogonal ==== | ==== Cambiar de repente vista perspectiva a ortogonal ==== | ||
| Línea 250: | Línea 253: | ||
| * GL_SAMPLE_ALPHA_TO_ONE. Con alpha a 1. | * GL_SAMPLE_ALPHA_TO_ONE. Con alpha a 1. | ||
| * GL_SAMPLE_COVERAGE. Con alpha y el valor pasado a ''glSampleCoverage'', esta función permite especificar un valor de alpha. | * GL_SAMPLE_COVERAGE. Con alpha y el valor pasado a ''glSampleCoverage'', esta función permite especificar un valor de alpha. | ||
| + | |||
| + | |||
| + | |||
| + | |||
| ==== Multitexturas ==== | ==== Multitexturas ==== | ||
| Línea 255: | Línea 262: | ||
| El funcionamiento de estas se basa en los distintos niveles de texturas. Para definirlas tendrás que seguir los siguientes pasos: | El funcionamiento de estas se basa en los distintos niveles de texturas. Para definirlas tendrás que seguir los siguientes pasos: | ||
| - Indicar el nivel que queremos utilizar, mediante la función ''glActiveTexture'' pasandole ''GL_TEXTURE//n//'', siendo //n// el nivel (que por defecto es 0). | - Indicar el nivel que queremos utilizar, mediante la función ''glActiveTexture'' pasandole ''GL_TEXTURE//n//'', siendo //n// el nivel (que por defecto es 0). | ||
| - | - | + | - Activar la textura actual en dicho nivel, mediante ''glBind''. |
| + | - Activar las texturas en dicho nivel, mediante ''glEnable(GL_TEXTURE_2D)''. | ||
| + | Una vez hecho esto estaremos en el nivel ''n'', para volver a utilizar texturas por defecto iremos al 0 mediante ''glActiveTexture''. \\ | ||
| + | Para aplicarlas utilizaremos ''glMultiTexCoord2f'' en vez de ''glTexCoord2f'' al cual habrá que pasar el nivel. | ||
| + | |||
| + | <code cpp> | ||
| + | glActiveTexture(GL_TEXTURE1); | ||
| + | glBindTexture(GL_TEXTURE_2D, text2); | ||
| + | glEnable(GL_TEXTURE_2D); | ||
| + | |||
| + | glActiveTexture(GL_TEXTURE2); | ||
| + | glBindTexture(GL_TEXTURE_2D, text1); | ||
| + | glEnable(GL_TEXTURE_2D); | ||
| + | |||
| + | glActiveTexture(GL_TEXTURE0); | ||
| + | glEnable(GL_TEXTURE_2D); | ||
| + | |||
| + | ... | ||
| + | |||
| + | glBindTexture (GL_TEXTURE_2D, text2); | ||
| + | glPushMatrix (); | ||
| + | glTranslatef(3.0f, 3.0f, 0.0f); | ||
| + | glBegin(GL_QUADS); | ||
| + | glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); | ||
| + | glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); | ||
| + | glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, -1.0f); | ||
| + | glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f); | ||
| + | glEnd(); | ||
| + | glPopMatrix(); | ||
| + | |||
| + | ... | ||
| + | |||
| + | glBegin(GL_QUADS); | ||
| + | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 1.0f); glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 1.0f); glVertex2f(-1.0f, 1.0f); | ||
| + | glMultiTexCoord2f(GL_TEXTURE1, 0.0f, 0.0f); glMultiTexCoord2f(GL_TEXTURE2, 0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); | ||
| + | glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 0.0f); glMultiTexCoord2f(GL_TEXTURE2, 1.0f, 0.0f); glVertex2f(1.0f, -1.0f); | ||
| + | glMultiTexCoord2f(GL_TEXTURE1, 1.0f, 1.0f); glMultiTexCoord2f(GL_TEXTURE2, 1.0f, 1.0f); glVertex2f(1.0f, 1.0f); | ||
| + | glEnd(); | ||
| + | </code> | ||
| + | Debemos recordar activar la GL_TEXTURE0, al acabar de definir las anteriores, que es por la que trabajamos por defecto. | ||
| + | |||
| + | |||
| + | ==== Polygon Tesselation ==== | ||
| + | * [[http://www.songho.ca/opengl/gl_tessellation.html]] | ||
| + | * {{fw:ogl:polygon_tesselation_examples.zip|Ejemplos}} | ||
| + | Para dibujar polígonos concavos (que desde cualquier punto NO se puede trazar una línea a otro punto del polígono sin dejar de pasar por su superficie) es necesario utilizar esta técnica. \\ | ||
| + | - Crearemos con ''gluNewTess'' (y al final destruiremos con ''gluDeleteTess'') un objeto ''GLUtessellator''. | ||
| + | - Indicaremos qué funciones se utilizarán para dibujar con ''gluTessCallback'', estas funciones podrán ser para iniciar la figura con el parámetro ''GLU_TESS_BEGIN'', para dibujar un vértice con ''GLU_TESS_VERTEX''... | ||
| + | - Indicaremos las propiedades de dibujo del polígono con ''glPolygonMode''. | ||
| + | - Iniciaremos el dibujo de un polígono con ''gluTessBeginPolygon'' y lo finalizaremos con ''gluTessEndPolygon''. | ||
| + | - Añadiremos porciones del polígono (por ejemplo, puede que tenga agujeros, cada polígono dentro del polígono se representa con estas rutinas) mediante ''gluTessBeginContour'' y ''gluTessEndContour''. | ||
| + | - Añadiremos vértices con ''gluTessVertex''. | ||
| + | <code cpp> | ||
| + | void tessVertexCB2(const GLvoid *data) { | ||
| + | const GLdouble *ptr = (const GLdouble*)data; | ||
| + | glTexCoord2dv(ptr+2); | ||
| + | glVertex3dv(ptr); | ||
| + | } | ||
| + | void tessVertexCB(const GLvoid *data) { | ||
| + | const GLdouble *ptr = (const GLdouble*)data; | ||
| + | glColor3dv(ptr+3); | ||
| + | glVertex2dv(ptr); | ||
| + | cout << " glColor3d(" << *(ptr+3) << ", " << *(ptr+4) << ", " << *(ptr+5) << ");\n"; | ||
| + | cout << " glVertex3d(" << *ptr << ", " << *(ptr+1) << ", " << *(ptr+2) << ");\n"; | ||
| + | } | ||
| + | void TessCombine(double coords[3],double* data[4],float weight[4],double** result) { | ||
| + | *result = (double*) malloc(3 * sizeof(double)); | ||
| + | (*result)[0] = coords[0]; | ||
| + | (*result)[1] = coords[1]; | ||
| + | (*result)[2] = coords[2]; | ||
| + | } | ||
| + | void TessError(GLenum err) { | ||
| + | fprintf(stderr,"Tessellation Error: %s\n",gluErrorString(err)); | ||
| + | exit(1); | ||
| + | } | ||
| + | void display(void) | ||
| + | { | ||
| + | GLdouble vertices[4][4] = { {-0.5,-0.5, 1.0, 1.0}, {-0.5,0.5, 1, 0}, {0.5,0.5, 0,0}, {0.5,-0.5, 0,1} }; | ||
| + | |||
| + | glClear(GL_COLOR_BUFFER_BIT); | ||
| + | glBindTexture(GL_TEXTURE_2D, texture); | ||
| + | |||
| + | GLUtesselator *tess = gluNewTess(); | ||
| + | glPolygonMode(GL_FRONT_AND_BACK,GL_FILL); | ||
| + | |||
| + | gluTessCallback(tess, GLU_TESS_BEGIN, (GLvoid (*) ()) &glBegin); | ||
| + | // gluTessCallback(tess,GLU_TESS_VERTEX ,(GLvoid (*) ()) &glVertex2dv); | ||
| + | gluTessCallback(tess,GLU_TESS_VERTEX ,(GLvoid (*) ()) &tessVertexCB2); | ||
| + | gluTessCallback(tess, GLU_TESS_END, (GLvoid (*) ()) &glEnd); | ||
| + | gluTessCallback(tess,GLU_TESS_COMBINE,(GLvoid (*) ()) &TessCombine); | ||
| + | gluTessCallback(tess,GLU_TESS_ERROR ,(GLvoid (*) ()) &TessError); | ||
| + | |||
| + | gluTessBeginPolygon(tess, NULL); | ||
| + | gluTessBeginContour(tess); | ||
| + | for (int k=0; k<4; k++) { | ||
| + | gluTessVertex(tess, vertices[k], vertices[k]); | ||
| + | } | ||
| + | gluTessEndContour(tess); | ||
| + | gluTessEndPolygon(tess); | ||
| + | |||
| + | gluDeleteTess(tess); | ||
| + | |||
| + | glFlush(); | ||
| + | glutSwapBuffers(); | ||
| + | } | ||
| + | </code> | ||
| ===== Carga de imágenes ===== | ===== Carga de imágenes ===== | ||
| Línea 732: | Línea 844: | ||
| ContVel++; | ContVel++; | ||
| </code> | </code> | ||
| + | |||
| Línea 852: | Línea 965: | ||
| int alto = devmode.dmPelsHeight; | int alto = devmode.dmPelsHeight; | ||
| </code> | </code> | ||
| + | |||
| + | |||
| + | |||
| ===== Detalle de funciones ===== | ===== Detalle de funciones ===== | ||