¿Primero que es OpenGL?:
OpenGL (Open Graphics Library) es una especificación estándar que define una API multilenguaje y multiplataforma para escribir aplicaciones que produzcan gráficos 2D y 3D. La interfaz consiste en más de 250 funciones diferentes que pueden usarse para dibujar escenas tridimensionales complejas a partir de primitivas geométricas simples, tales como puntos, líneas y triángulos.
procederé a definir escuetamente la función seno:
En trigonometría el seno de un ángulo en un triángulo rectángulo se define como la razón entre el cateto opuesto y la hipotenusa:
O también como la ordenada correspondiente a un punto que pertenece a una circunferencia unitaria centrada en el origen (c=1):
a continuación el código para dibujar esta función en OpenGL:
//////////codigo para la funcion senoidal
void sinoid(int xIni, int yIni, int xFin, int yFin,float r,float g,float b)
{
int dx = xFin - xIni, dy = yFin -yIni, pasos, k;
float xIncremento, yIncremento, x = xIni, y = yIni,ycalc;
if (abs(dx)> abs(dy))
pasos = 100*abs(dx);
else
pasos = 100*abs(dy);
xIncremento = float(dx) / float(pasos);
yIncremento = float(dy) / float(pasos);
** ycalc=50*sin(x/8);
setPixel(round(x), round(ycalc)+370,r,g,b);
for(k=0; k < pasos; k++)
{
x += xIncremento;
y += yIncremento;
**ycalc=50*sin(x/8);
setPixel(round(x), round(ycalc)+370,r,g,b);
}
}
Después otros códigos que pueden ser útiles para otro tipo de graficaciones:
/////////////// codigo para graficar una funcion
void plotFunc3D(float d)
{
glNewList(GLobj, GL_COMPILE);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
float i,j,k;
float u[3], v[3], w[3], x[3], n[3];
for (i = x_min; i<x_max; i+= d){
for (j = y_min; j<<y_max; j+= d){
glBegin(GL_TRIANGLES);
evalfunc(i,j, &k);
u[0] = i; u[1] = j; u[2] = k;//cos(i*i+j*j);
evalfunc(i+d,j, &k);
v[0] = i+d; v[1] = j; v[2] = k;//cos((i+d)*(i+d)+j*j);
evalfunc(i+d,j+d, &k);
w[0] = i+d; w[1] = j+d; w[2] = k;//cos((i+d)*(i+d)+(j+d)*(j+d));
evalfunc(i,j+d, &k);
x[0] = i; x[1] = j+d; x[2] = k;//cos(i*i+(j+d)*(j+d));
cross(&n[0],u,v,w);
glNormal3f(n[0], n[1], n[2]);
glVertex3f(u[0], u[1], u[2]);
glVertex3f(v[0], v[1], v[2]);
glVertex3f(w[0], w[1], w[2]);
cross(&n[0],u,w,x);
glNormal3f(n[0], n[1], n[2]);
glVertex3f(u[0], u[1], u[2]);
glVertex3f(w[0], w[1], w[2]);
glVertex3f(x[0], x[1], x[2]);
glEnd();
}
}
glDisable(GL_LIGHTING);
glDisable(GL_NORMALIZE);
#if 0
glLineWidth(0.5);
glColor3f(1.0, 1.0, 0.0);
glBegin(GL_LINE_LOOP);
glVertex3f(x_min, y_min, z_min);
glVertex3f(x_min, y_min, z_max);
glVertex3f(x_min, y_max, z_max);
glVertex3f(x_min, y_max, z_min);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(x_max, y_min, z_min);
glVertex3f(x_max, y_min, z_max);
glVertex3f(x_max, y_max, z_max);
glVertex3f(x_max, y_max, z_min);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(x_min, y_min, z_min);
glVertex3f(x_min, y_max, z_min);
glVertex3f(x_max, y_max, z_min);
glVertex3f(x_max, y_min, z_min);
glEnd();
glBegin(GL_LINE_LOOP);
glVertex3f(x_min, y_min, z_max);
glVertex3f(x_min, y_max, z_max);
glVertex3f(x_max, y_max, z_max);
glVertex3f(x_max, y_min, z_max);
glEnd();
#endif
glEndList();
}
//////////codigo para renderizar una esfera
void Sphere::Render( int slices, int stacks )
{
float x0, y0, z0;
float x1, y1, z1;
float sinxy;
int i, j, v= 0;
unsigned int numVertex = 2*slices*stacks;
float xzAngle = 2.0f*PI/(float)slices;
float xyAngle = 2.0f*PI/(float)stacks;
if ( numVertex <= MAX_VERTEX_SPHERE )
{
for (i=0; i<slices; i++)
{
for (j=0; j<stacks; j++)
{
sinxy= sin(j*xyAngle);
x0= sinxy * cos(i*xzAngle);
y0= cos(j*xyAngle);
z0= sinxy * sin(i*xzAngle);
// Vértice
m_Vertex[v].x = m_Radius*x0;
m_Vertex[v].y = m_Radius*y0;
m_Vertex[v].z = m_Radius*z0;
// Normal
m_Vertex[v].nx= x0;
m_Vertex[v].ny= y0;
m_Vertex[v].nz= z0;
if ( IVideoDriver::GetVideoDriver()->GetAxisSystem() == eRightHanded )
m_Vertex[v].su= 1.0f-i/(float)slices;
else
m_Vertex[v].su= i/(float)slices;
m_Vertex[v].tv= ((float)stacks-2.0f*j)/(float)stacks;
v++;
x1= sinxy * cos((i+1)*xzAngle);
y1= y0;
z1= sinxy * sin((i+1)*xzAngle);
m_Vertex[v].x = m_Radius*x1;
m_Vertex[v].y = m_Radius*y1;
m_Vertex[v].z = m_Radius*z1;
m_Vertex[v].nx= x1;
m_Vertex[v].ny= y1;
m_Vertex[v].nz= z1;
if ( IVideoDriver::GetVideoDriver()->GetAxisSystem() == eRightHanded )
m_Vertex[v].su= 1.0f-(i+1)/(float)slices;
else
m_Vertex[v].su= (i+1)/(float)slices;
m_Vertex[v].tv= m_Vertex[v-1].tv;
v++;
}
}
IVideoDriver::GetVideoDriver()->RenderVertexArray( eTriangleStrip, (BYTE*)m_Vertex, sizeof(Vertex),
0, numVertex, eFVF_XYZ|eFVF_NORMAL|eFVF_TEX1 );
}
}
///////////codigo para dibujaruna escena
int DrawGLScene (GLvoid) L
{ (
int x, y;
float float_x, float_y, float_xb, float_yb;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glLoadIdentity ();
glTranslatef(0.0f,0.0f,-12.0f);
glRotatef(xrot,1.0f,0.0f,0.0f);
glRotatef(yrot,0.0f,1.0f,0.0f);
glRotatef(zrot,0.0f,0.0f,1.0f);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glBegin(GL_QUADS); // Start Drawing Our Quads glBegin (GL_QUADS);
for( x = 0; x < 44; x++ ) // Loop Through The X Plane (44 Points) for (x = 0; x <44; x + +)
{ (
for( y = 0; y < 44; y++ ) // Loop Through The Y Plane (44 Points) para (y = 0; y <44; y + +)
{ (
float_x = float(x)/44.0f; // Create A Floating Point X Value float_x = float (x) / 44.0f;
float_y = float(y)/44.0f; // Create A Floating Point Y Value float_y = float (y) / 44.0f;
float_xb = float(x+1)/44.0f; // Create A Floating Point Y Value+0.0227f float_xb = float (x +1) / 44.0f;
float_yb = float(y+1)/44.0f; // Create A Floating Point Y Value+0.0227f float_yb = float (y +1) / 44.0f;
glTexCoord2f( float_x, float_y); // First Texture Coordinate (Bottom Left) glTexCoord2f (float_x, float_y);
glVertex3f( points[x][y][0], points[x][y][1], points[x][y][2] ); glVertex3f ([puntos x] [y] [0], [puntos x] [y] [1], puntos [x] [y] [2]);
glTexCoord2f( float_x, float_yb ); // Second Texture Coordinate (Top Left) glTexCoord2f (float_x, float_yb);
glVertex3f( points[x][y+1][0], points[x][y+1][1], points[x][y+1][2] ); glVertex3f ([x puntos] y [1] [0], puntos [x] [y +1] [1], [x puntos] y [1] [2]);
glTexCoord2f( float_xb, float_yb ); // Third Texture Coordinate (Top Right) glTexCoord2f (float_xb, float_yb);
glVertex3f( points[x+1][y+1][0], points[x+1][y+1][1], points[x+1][y+1][2] ); glVertex3f ([puntos x +1] [y +1] [0], [x +1 puntos] [y +1] [1], [x +1 puntos] [y +1] [2]);
glTexCoord2f( float_xb, float_y ); // Fourth Texture Coordinate (Bottom Right) glTexCoord2f (float_xb, float_y);
glVertex3f( points[x+1][y][0], points[x+1][y][1], points[x+1][y][2] ); glVertex3f ([puntos x +1] [y] [0], [x +1 puntos] [y] [1], puntos [x +1] [y] [2]);
} )
} )


Deja un comentario