1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| #include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#define width 640
#define height 480
#define h_scale 5
#define w_scale 5
#define grad_size 0.02
#define subdiv_nb 2
//affichage de la grille (arrière plan, constitué de deux parties, les axes et le quadrillage)
void display_grid(void)
{
int i=0;
int j=0;
//first grid
glBegin(GL_POINTS);
glColor3f(0.0,0.5,0.0);
for(i=1; i<= 2*w_scale*subdiv_nb; i++)
{
for(j=1; j<= 2*h_scale*subdiv_nb; j++)
{
glVertex2f(((1/((GLfloat)w_scale*subdiv_nb))*(GLfloat)i)-1.0,((1/((GLfloat)h_scale*subdiv_nb))*(GLfloat)j)-1.0);
}
}
//second grid
glColor3f(0.0,0.8,0.0);
for(i=1; i<= 2*w_scale; i++)
{
for(j=1; j<= 2*h_scale; j++)
{
glVertex2f(((1/((GLfloat)w_scale))*(GLfloat)i)-1.0,((1/((GLfloat)h_scale))*(GLfloat)j)-1.0);
}
}
glEnd();
glBegin(GL_LINES);
glColor3f(0.0,0.8,0.0);
glVertex2f(-1.0,0.0);
glVertex2f(1.0,0.0);
glVertex2f(0.0,-1.0);
glVertex2f(0.0,1.0);
for(i=1; i<= 2*w_scale; i++)
{
glVertex2f(((1/(GLfloat)w_scale)*(GLfloat)i)-1.0,grad_size);
glVertex2f(((1/(GLfloat)w_scale)*(GLfloat)i)-1.0,-grad_size);
}
for(i=1; i<= 2*h_scale; i++)
{
glVertex2f(-grad_size,((1/(GLfloat)h_scale)*(GLfloat)i)-1.0);
glVertex2f(grad_size,((1/(GLfloat)h_scale)*(GLfloat)i)-1.0);
}
glEnd();
}
//affichage des courbes : ici c'est simplement un ensemble de sinusoides
void display_curves(int valeur)
{
int i=0;
float decalage;
decalage = (float)valeur/100;
//sinusoide pour le test
glBegin(GL_LINE_STRIP);
glColor3f(1.0,0.0,0.0);
for(i=0; i<=200; i++)
{
glVertex2f(((float)i)/100.0-1.0, cos((float)i/10.0)+decalage);
}
glEnd();
}
//fonction d'affichage principale
void display()
{
int i=0;
glClear(GL_COLOR_BUFFER_BIT);
//display_grid();
for(i=0; i<100; i++)
{
display_grid();
display_curves(i);
glutSwapBuffers();
}
}
void Reshape()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-1,1,-1,1);
}
int WindowName;
int main(int argc, char *argv[])
{
//paramètre d'initialisation de la fenetre avec glut
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE|GLUT_DEPTH);
glutInitWindowPosition(200,200);
glutInitWindowSize(width,height);
WindowName=glutCreateWindow("ma première fenetre opengl");
//début du programme principal avec glut
//glutReshapeFunc(Reshape);
glutDisplayFunc(display);
glutMainLoop();
//fin normale
return EXIT_SUCCESS;
} |
Partager