Il seguente programma consente di modellare solidi utilizzando le OpenGL, e ruotarne la posizione nello spazio. Utilizzando la libreria GLEXT e GLU.
#include <windows.h> // File intestazione necessario
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glext.h>
HDC hDC=NULL; // GDI privato
HGLRC hRC=NULL; // Contesto di rendering
HWND hWnd=NULL; // Gestisce la finestra
HINSTANCE hInstance; // Gestisce istanza
bool tasti[256]; // Vettore per le routine tastiera
bool attivo=TRUE; // Flag per la finestra attiva
bool schermopieno=TRUE; // Flag per lo schermopieno
GLfloat rtri; // Angolo per i triangoli
GLfloat rquad; // Angolo per i quadrati
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); // Dichiarazione di WndProc
GLvoid RidimensioneScenaGL(GLsizei larghezza, GLsizei altezza) // Ridimensiona ed inizializza GL Window
{
if (altezza==0) // Previene divisione per 0
{
altezza=1;
}
glViewport(0,0,larghezza,altezza); // Reset dello schermo corrente
glMatrixMode(GL_PROJECTION); // Seleziona la matrice di proiezione
glLoadIdentity(); // Azzera la finestra Matrice
// Calcola l'aspetto della finstra e le sue dimensioni
gluPerspective(100.0f,(GLfloat)larghezza/(GLfloat)altezza,0.1f,100.0f);
glMatrixMode(GL_MODELVIEW); // Seleziona la matrice del Modelview
glLoadIdentity();
}
int InizGL(GLvoid) // Impostazioni per OpenGL
{
glShadeModel(GL_SMOOTH); // Abilita ombreggiatura
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Schermo a sfondo nero
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
return TRUE; // Inizializzazione OK
}
int DisegnaScenaGL(GLvoid) // Qui vi sono le istruzioni di disegno
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(-1.5f,0.0f,-6.0f);
glRotatef(rtri,0.0f,1.0f,0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f, -1.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f, -1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 0.0f, 1.0f, 0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd(); // Disegno della Piramide
glLoadIdentity();
glTranslatef(1.5f,0.0f,-6.0f);
glRotatef(rquad,1.0f,1.0f,1.0f);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f, 1.0f,-1.0f);
glVertex3f( 1.0f, 1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Disegno del cubo
glEnd();
glLoadIdentity();
glTranslatef(5.5f,0.0f,-8.0f);
glRotatef(rquad*2,1.0f,0.0f,0.0f);
glBegin(GL_QUADS);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f( 1.0f, 2.0f,-1.0f);
glVertex3f(-1.0f, 2.0f,-1.0f);
glVertex3f(-1.0f, 2.0f, 1.0f);
glVertex3f( 1.0f, 2.0f, 1.0f);
glColor3f(1.0f,0.5f,0.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f);
glColor3f(1.0f,0.0f,0.0f);
glVertex3f( 1.0f, 2.0f, 1.0f);
glVertex3f(-1.0f, 2.0f, 1.0f );
glVertex3f(-1.0f,-1.0f, 1.0f );
glVertex3f( 1.0f,-1.0f, 1.0f );
glColor3f(1.0f,1.0f,0.0f);
glVertex3f( 1.0f,-1.0f,-1.0f );
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f, 2.0f,-1.0f);
glVertex3f( 1.0f, 2.0f,-1.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(-1.0f, 2.0f, 1.0f);
glVertex3f(-1.0f, 2.0f,-1.0f);
glVertex3f(-1.0f,-1.0f,-1.0f);
glVertex3f(-1.0f,-1.0f, 1.0f);
glColor3f(1.0f,0.0f,1.0f);
glVertex3f( 1.0f, 2.0f,-1.0f);
glVertex3f( 1.0f, 2.0f, 1.0f);
glVertex3f( 1.0f,-1.0f, 1.0f);
glVertex3f( 1.0f,-1.0f,-1.0f); // Disegno della scatola
glEnd();
rtri+=0.2f; // Aumenta la rotazione per il triangolo
rquad-=0.15f; // Diminuisce la rotazione per il quadrato
return TRUE; // Continua...
}
Nessun commento:
Posta un commento