
2003년에 그래픽스 수업시간에 만들었던 비행기입니다.
openGL로 만들었구요, 저 썬글라스가 포인트입니다 ㅎㅎ자세한 모양은 첨부파일 실행시켜서 보세요(악성코드 같은거 없습니다.)
소스는 ...
// Standard Windows header file.
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h> // GLU library
#include <gl\glaux.h>
#include <math.h> // Define for sine and cosine
// Standard Windows header file.
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h> // GLU library
#include <gl\glaux.h>
#include <math.h> // Define for sine and cosine
#define GL_PI 3.1415f
GLint cnt;
GLint cnt2;
GLint clr;
bool Shadow = false; // 그림자인지 여부확인
GLint cnt2;
GLint clr;
bool Shadow = false; // 그림자인지 여부확인
HPALETTE hPalette = NULL;
// Forward declaration for the message handler.
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK MyWndProc(HWND, UINT, WPARAM, LPARAM);
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat nRange = 140.0f;
{
GLfloat nRange = 140.0f;
if(h == 0) h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glLoadIdentity();
if (w <= h)
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
glOrtho (-nRange, nRange, -nRange*h/w, nRange*h/w, -nRange, nRange);
else
glOrtho (-nRange*w/h, nRange*w/h, -nRange, nRange, -nRange, nRange);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(30.0f, 1.0f, 0.0f, 0.0f);
}
void SetupLight()
{
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
{
GLfloat specref[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat ambientLight[] = {0.1f,0.1f,0.1f};
GLfloat diffuseLight[] = {1.0f,1.0f,1.0f};
GLfloat specularLight[] = {1.0f,1.0f,1.0f};
GLfloat diffuseLight[] = {1.0f,1.0f,1.0f};
GLfloat specularLight[] = {1.0f,1.0f,1.0f};
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularLight);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, specref);
glMateriali(GL_FRONT, GL_SHININESS, 128);
}
void SetupRC()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
glColor3f(0.0f, 1.0f, 0.0f);
glShadeModel(GL_FLAT);
glFrontFace(GL_CW);
SetupLight();
}
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f );
glColor3f(0.0f, 1.0f, 0.0f);
glShadeModel(GL_FLAT);
glFrontFace(GL_CW);
SetupLight();
}
void IdleFunction()
{
cnt += 5;
cnt = cnt % 360;
cnt2++;
cnt2 = cnt2 % 360;
clr++; //불꽃색깔
clr = clr % 10;
}
{
cnt += 5;
cnt = cnt % 360;
cnt2++;
cnt2 = cnt2 % 360;
clr++; //불꽃색깔
clr = clr % 10;
}
void RenderCone(void)
{
GLint i;
GLfloat x, y, z, y2, x2;
glPushMatrix();
glRotatef((GLfloat) cnt2, 0.0f, 1.0f, 0.0f);
glTranslatef(100.0f, 0.0f, 0.0f);
//앞부분
if(Shadow == false)
glColor3f(0.9f, 0.8f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 20.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 10 * cos(i * GL_PI / 180);
x = 10 * sin(i * GL_PI / 180);
glVertex3f(x, y + 20.0f, 15.0f);
}
glEnd();
{
GLint i;
GLfloat x, y, z, y2, x2;
glPushMatrix();
glRotatef((GLfloat) cnt2, 0.0f, 1.0f, 0.0f);
glTranslatef(100.0f, 0.0f, 0.0f);
//앞부분
if(Shadow == false)
glColor3f(0.9f, 0.8f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 20.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 10 * cos(i * GL_PI / 180);
x = 10 * sin(i * GL_PI / 180);
glVertex3f(x, y + 20.0f, 15.0f);
}
glEnd();
//안경
if(Shadow == false)
glColor3f(0.1f, 0.1f, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 20.0f, 3.0f);
for(i = 330; i <= 390; i++)
{
y = 11 * cos(i * GL_PI / 180);
x = 11 * sin(i * GL_PI / 180);
glVertex3f(x, y + 16.0f, 10.0f);
}
glEnd();
//몸체
if(Shadow == false)
glColor3f(0.8f, 0.8f, 0.7f);
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -10 * (float) sin(i * GL_PI / 180);
x = 10 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y + 20.0f, 15.0f);
glVertex3f(x, y + 20.0f, 30.0f);
}
glEnd();
//배기부
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 12; i++)
{
y = -10 * (float) sin(i * 2 * GL_PI / 12);
x = 10 * (float) cos(i * 2 * GL_PI / 12);
y2 = -8 * (float) sin(i * 2 * GL_PI / 12);
x2 = 8 * (float) cos(i * 2 * GL_PI / 12);
if(Shadow == false)
glColor3f(0.8f, 0.9f, 0.7f);
glVertex3f(x, y + 20.0f, 30.0f);
if(Shadow == false)
glColor3f(0.7f, 0.8f, 0.9f);
glVertex3f(x2, y2 + 20.0f, 40.0f);
}
glEnd();
if(Shadow == false)
glColor3f(0.1f, 0.1f, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 20.0f, 3.0f);
for(i = 330; i <= 390; i++)
{
y = 11 * cos(i * GL_PI / 180);
x = 11 * sin(i * GL_PI / 180);
glVertex3f(x, y + 16.0f, 10.0f);
}
glEnd();
//몸체
if(Shadow == false)
glColor3f(0.8f, 0.8f, 0.7f);
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -10 * (float) sin(i * GL_PI / 180);
x = 10 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y + 20.0f, 15.0f);
glVertex3f(x, y + 20.0f, 30.0f);
}
glEnd();
//배기부
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 12; i++)
{
y = -10 * (float) sin(i * 2 * GL_PI / 12);
x = 10 * (float) cos(i * 2 * GL_PI / 12);
y2 = -8 * (float) sin(i * 2 * GL_PI / 12);
x2 = 8 * (float) cos(i * 2 * GL_PI / 12);
if(Shadow == false)
glColor3f(0.8f, 0.9f, 0.7f);
glVertex3f(x, y + 20.0f, 30.0f);
if(Shadow == false)
glColor3f(0.7f, 0.8f, 0.9f);
glVertex3f(x2, y2 + 20.0f, 40.0f);
}
glEnd();
//불꽃
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 12; i++)
{
y = -8 * (float) sin(i * 2 * GL_PI / 12);
x = 8 * (float) cos(i * 2 * GL_PI / 12);
y2 = -1 * (float) sin(i * 2 * GL_PI / 12);
x2 = 1 * (float) cos(i * 2 * GL_PI / 12);
if(Shadow == false)
glColor3f((GLfloat) clr / 10, 0.1f, 0.1f);
glVertex3f(x, y + 20.0f, 38.0f);
if(Shadow == false)
glColor3f(1.0f, (GLfloat) clr / 10, (GLfloat) clr / 10);
glVertex3f(x2, y2 + 20.0f, 60.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 12; i++)
{
y = -8 * (float) sin(i * 2 * GL_PI / 12);
x = 8 * (float) cos(i * 2 * GL_PI / 12);
y2 = -1 * (float) sin(i * 2 * GL_PI / 12);
x2 = 1 * (float) cos(i * 2 * GL_PI / 12);
if(Shadow == false)
glColor3f((GLfloat) clr / 10, 0.1f, 0.1f);
glVertex3f(x, y + 20.0f, 38.0f);
if(Shadow == false)
glColor3f(1.0f, (GLfloat) clr / 10, (GLfloat) clr / 10);
glVertex3f(x2, y2 + 20.0f, 60.0f);
}
glEnd();
//날개
if(Shadow==false)
glColor3f(0.7f, 0.6f, 0.7f);
glBegin(GL_QUAD_STRIP);
glVertex3f(-30.0f, 25.0f, 20.0f);
glVertex3f(-30.0f, 25.0f, 25.0f);
glVertex3f(30.0f, 25.0f, 25.0f);
glVertex3f(30.0f, 25.0f, 20.0f);
glEnd();
//프로펠라
glPushMatrix();
glTranslatef(0.0f, 20.0f, 0.0f);
if(Shadow == false)
glColor3f(0.8f, 0.9f, 0.9f);
auxSolidSphere(2);
glPushMatrix();
glRotatef((GLfloat) cnt, 0.0f, 0.0f, 1.0f);
if(Shadow == false)
glColor3f(0.7f, 0.7f, 0.8f);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-2.0f, 10.0f, -2.0f);
glVertex3f(2.0f, 10.0f, 2.0f);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-2.0f, -10.0f, -2.0f);
glVertex3f(2.0f, -10.0f, 2.0f);
glEnd();
glPopMatrix();
glPopMatrix();
//정찰안테나
glPushMatrix();
glTranslatef(0.0f, 0.0f, 25.0f);
glRotatef(-(GLfloat) cnt, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 32.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
x = 10 * cos(i * GL_PI / 180);
z = 10 * sin(i * GL_PI / 180);
if(Shadow == false)
{
if((i % 30) <= 15)
glColor3f(0.9f, 0.9f, 0.6f);
else
glColor3f(0.8f, 0.8f, 0.5f);
}
glVertex3f(x, 32.0f, z);
}
glEnd();
glPopMatrix();
if(Shadow==false)
glColor3f(0.7f, 0.6f, 0.7f);
glBegin(GL_QUAD_STRIP);
glVertex3f(-30.0f, 25.0f, 20.0f);
glVertex3f(-30.0f, 25.0f, 25.0f);
glVertex3f(30.0f, 25.0f, 25.0f);
glVertex3f(30.0f, 25.0f, 20.0f);
glEnd();
//프로펠라
glPushMatrix();
glTranslatef(0.0f, 20.0f, 0.0f);
if(Shadow == false)
glColor3f(0.8f, 0.9f, 0.9f);
auxSolidSphere(2);
glPushMatrix();
glRotatef((GLfloat) cnt, 0.0f, 0.0f, 1.0f);
if(Shadow == false)
glColor3f(0.7f, 0.7f, 0.8f);
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-2.0f, 10.0f, -2.0f);
glVertex3f(2.0f, 10.0f, 2.0f);
glEnd();
glBegin(GL_TRIANGLE_STRIP);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(-2.0f, -10.0f, -2.0f);
glVertex3f(2.0f, -10.0f, 2.0f);
glEnd();
glPopMatrix();
glPopMatrix();
//정찰안테나
glPushMatrix();
glTranslatef(0.0f, 0.0f, 25.0f);
glRotatef(-(GLfloat) cnt, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 32.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
x = 10 * cos(i * GL_PI / 180);
z = 10 * sin(i * GL_PI / 180);
if(Shadow == false)
{
if((i % 30) <= 15)
glColor3f(0.9f, 0.9f, 0.6f);
else
glColor3f(0.8f, 0.8f, 0.5f);
}
glVertex3f(x, 32.0f, z);
}
glEnd();
glPopMatrix();
//미사일
glPushMatrix();
glTranslatef(-20.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-20.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-26.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glTranslatef(-26.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(20.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glTranslatef(20.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(26.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glTranslatef(26.0f, 23.0f, 15.0f);
if(Shadow == false)
glColor3f(0.5f, 0.5f, 0.7f);
glBegin(GL_TRIANGLE_FAN);
glVertex3f(0.0f, 0.0f, 0.0f);
for(i = 0; i <= 360; i++)
{
y = 2 * cos(i * GL_PI / 180);
x = 2 * sin(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
}
glEnd();
glBegin(GL_TRIANGLE_STRIP);
for(i = 0; i <= 360; i++)
{
y = -2 * (float) sin(i * GL_PI / 180);
x = 2 * (float) cos(i * GL_PI / 180);
glVertex3f(x, y, 5.0f);
glVertex3f(x, y, 12.0f);
}
glEnd();
glPopMatrix();
glPopMatrix();
}
}
void Calc_shadowMatrix(GLfloat light_pos[4],
GLfloat shad_plane[3][3],
GLfloat shad_matrix[4][4])
{
GLfloat tmp, plane_coeff[4];
GLfloat vect_a[3], vect_b[3];
GLint i;
GLfloat shad_plane[3][3],
GLfloat shad_matrix[4][4])
{
GLfloat tmp, plane_coeff[4];
GLfloat vect_a[3], vect_b[3];
GLint i;
for (i = 0; i < 3; i++)
{
shad_plane[i][0] -= light_pos[i];
shad_plane[i][1] -= light_pos[i];
shad_plane[i][2] -= light_pos[i];
}
{
shad_plane[i][0] -= light_pos[i];
shad_plane[i][1] -= light_pos[i];
shad_plane[i][2] -= light_pos[i];
}
vect_a[0] = shad_plane[0][1]-shad_plane[0][0];
vect_a[1] = shad_plane[1][1]-shad_plane[1][0];
vect_a[2] = shad_plane[2][1]-shad_plane[2][0];
vect_a[1] = shad_plane[1][1]-shad_plane[1][0];
vect_a[2] = shad_plane[2][1]-shad_plane[2][0];
vect_b[0] = shad_plane[0][2]-shad_plane[0][0];
vect_b[1] = shad_plane[1][2]-shad_plane[1][0];
vect_b[2] = shad_plane[2][2]-shad_plane[2][0];
vect_b[1] = shad_plane[1][2]-shad_plane[1][0];
vect_b[2] = shad_plane[2][2]-shad_plane[2][0];
plane_coeff[0] = vect_a[2]*vect_b[1] - vect_a[1]*vect_b[2];
plane_coeff[1] = vect_a[0]*vect_b[2] - vect_a[2]*vect_b[0];
plane_coeff[2] = vect_a[1]*vect_b[0] - vect_a[0]*vect_b[1];
plane_coeff[1] = vect_a[0]*vect_b[2] - vect_a[2]*vect_b[0];
plane_coeff[2] = vect_a[1]*vect_b[0] - vect_a[0]*vect_b[1];
tmp = sqrt(plane_coeff[0]*plane_coeff[0] +
plane_coeff[1]*plane_coeff[1] +
plane_coeff[2]*plane_coeff[2]);
plane_coeff[0] /= tmp;
plane_coeff[1] /= tmp;
plane_coeff[2] /= tmp;
plane_coeff[1]*plane_coeff[1] +
plane_coeff[2]*plane_coeff[2]);
plane_coeff[0] /= tmp;
plane_coeff[1] /= tmp;
plane_coeff[2] /= tmp;
plane_coeff[3]= plane_coeff[0] * shad_plane[0][0] +
plane_coeff[1] * shad_plane[1][0] +
plane_coeff[2] * shad_plane[2][0];
shad_matrix[0][0] = plane_coeff[3];
shad_matrix[1][1] = plane_coeff[3];
shad_matrix[2][2] = plane_coeff[3];
shad_matrix[0][3] = plane_coeff[0];
shad_matrix[1][3] = plane_coeff[1];
shad_matrix[2][3] = plane_coeff[2];
}
shad_matrix[1][1] = plane_coeff[3];
shad_matrix[2][2] = plane_coeff[3];
shad_matrix[0][3] = plane_coeff[0];
shad_matrix[1][3] = plane_coeff[1];
shad_matrix[2][3] = plane_coeff[2];
}
void RenderScene(void)
{
{
GLfloat lightpos[] = {20.0f, 500.0f, 100.0f, 1.0f};
GLfloat shad_plane[3][3]={{10, 0, -10}, {0, 0, 0}, {10, -5, 10}};
GLfloat shad_plane[3][3]={{10, 0, -10}, {0, 0, 0}, {10, -5, 10}};
GLfloat shad_matrix[4][4] = {{0,0,0,0}, {0,0,0,0}, {0,0,0,0}, {0,0,0,0}};
glLightfv(GL_LIGHT0, GL_POSITION, lightpos);
Calc_shadowMatrix(lightpos, shad_plane, shad_matrix);
Calc_shadowMatrix(lightpos, shad_plane, shad_matrix);
glPushMatrix();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
Shadow = false;
RenderCone();
Shadow = false;
RenderCone();
glPushAttrib(GL_LIGHTING_BIT);
glDisable(GL_LIGHTING);
glColor3f(0.2f, 0.2f, 0.2f);
glPushMatrix();
glTranslatef(lightpos[0], lightpos[1], lightpos[2]);
glMultMatrixf((GLfloat *) shad_matrix);
glTranslatef(-lightpos[0], -lightpos[1], -lightpos[2]);
Shadow = true;
RenderCone();
glPopMatrix();
glPopAttrib();
glDisable(GL_LIGHTING);
glColor3f(0.2f, 0.2f, 0.2f);
glPushMatrix();
glTranslatef(lightpos[0], lightpos[1], lightpos[2]);
glMultMatrixf((GLfloat *) shad_matrix);
glTranslatef(-lightpos[0], -lightpos[1], -lightpos[2]);
Shadow = true;
RenderCone();
glPopMatrix();
glPopAttrib();
glPopMatrix();
glFlush();
}
}
// If necessary, creates a 3-3-2 palette for the device context listed.
HPALETTE GetOpenGLPalette(HDC hDC)
{
HPALETTE hRetPal = NULL; // Handle to palette to be created
PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
LOGPALETTE *pPal; // Pointer to memory for logical palette
int nPixelFormat; // Pixel format index
int nColors; // Number of entries in palette
int i; // Counting variable
BYTE RedRange,GreenRange,BlueRange;
// Range for each color entry (7,7,and 3)
// Get the pixel format index and retrieve the pixel format description
nPixelFormat = GetPixelFormat(hDC);
DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
// Does this pixel format require a palette? If not, do not create a
// palette and just return NULL
if(!(pfd.dwFlags & PFD_NEED_PALETTE))
return NULL;
// palette and just return NULL
if(!(pfd.dwFlags & PFD_NEED_PALETTE))
return NULL;
// Number of entries in palette. 8 bits yeilds 256 entries
nColors = 1 << pfd.cColorBits;
nColors = 1 << pfd.cColorBits;
// Allocate space for a logical palette structure plus all the palette entries
pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));
pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) +nColors*sizeof(PALETTEENTRY));
// Fill in palette header
pPal->palVersion = 0x300; // Windows 3.0
pPal->palNumEntries = nColors; // table size
pPal->palVersion = 0x300; // Windows 3.0
pPal->palNumEntries = nColors; // table size
// Build mask of all 1's. This creates a number represented by having
// the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
// pfd.cBlueBits.
RedRange = (1 << pfd.cRedBits) -1;
GreenRange = (1 << pfd.cGreenBits) - 1;
BlueRange = (1 << pfd.cBlueBits) -1;
// the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
// pfd.cBlueBits.
RedRange = (1 << pfd.cRedBits) -1;
GreenRange = (1 << pfd.cGreenBits) - 1;
BlueRange = (1 << pfd.cBlueBits) -1;
// Loop through all the palette entries
for(i = 0; i < nColors; i++)
{
// Fill in the 8-bit equivalents for each component
pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
pPal->palPalEntry[i].peRed = (unsigned char)(
(double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);
for(i = 0; i < nColors; i++)
{
// Fill in the 8-bit equivalents for each component
pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
pPal->palPalEntry[i].peRed = (unsigned char)(
(double) pPal->palPalEntry[i].peRed * 255.0 / RedRange);
pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
pPal->palPalEntry[i].peGreen = (unsigned char)(
(double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
pPal->palPalEntry[i].peGreen = (unsigned char)(
(double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
pPal->palPalEntry[i].peBlue = (unsigned char)(
(double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
pPal->palPalEntry[i].peBlue = (unsigned char)(
(double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
pPal->palPalEntry[i].peFlags = (unsigned char) NULL;
}
}
// Create the palette
hRetPal = CreatePalette(pPal);
hRetPal = CreatePalette(pPal);
// Go ahead and select and realize the palette for this device context
SelectPalette(hDC,hRetPal,FALSE);
RealizePalette(hDC);
SelectPalette(hDC,hRetPal,FALSE);
RealizePalette(hDC);
// Free the memory used for the logical palette structure
free(pPal);
free(pPal);
// Return the handle to the new palette
return hRetPal;
}
return hRetPal;
}
// Select the pixel format for a given device context
void SetDCPixelFormat(HDC hDC)
{
int nPixelFormat;
static PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
1, // Version of this structure
PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
PFD_DOUBLEBUFFER, // Double buffered mode
PFD_TYPE_RGBA, // RGBA Color mode
24, // Want 8 bit color
0,0,0,0,0,0, // Not used to select mode
0,0, // Not used to select mode
0,0,0,0,0, // Not used to select mode
32, // Size of depth buffer
0, // Not used to select mode
0, // Not used to select mode
PFD_MAIN_PLANE, // Draw in main plane
0, // Not used to select mode
0,0,0 }; // Not used to select mode
sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
1, // Version of this structure
PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
PFD_DOUBLEBUFFER, // Double buffered mode
PFD_TYPE_RGBA, // RGBA Color mode
24, // Want 8 bit color
0,0,0,0,0,0, // Not used to select mode
0,0, // Not used to select mode
0,0,0,0,0, // Not used to select mode
32, // Size of depth buffer
0, // Not used to select mode
0, // Not used to select mode
PFD_MAIN_PLANE, // Draw in main plane
0, // Not used to select mode
0,0,0 }; // Not used to select mode
// Choose a pixel format that best matches that described in pfd
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
// Set the pixel format for the device context
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
SetPixelFormat(hDC, nPixelFormat, &pfd);
}
// WinMain: required for all Windows applications.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpszCmdLine, int nCmdShow)
{
char szApplicationName[] = "Shadow";
HWND hwnd;
MSG msg;
HWND hwnd;
MSG msg;
// Window class data structure
WNDCLASS wc;
WNDCLASS wc;
wc.style = CS_HREDRAW | CS_VREDRAW; // style: causes repaint when resizing
wc.lpfnWndProc = (WNDPROC) MyWndProc; // the window procedure
wc.cbClsExtra = 0; // number of extra bytes following the class structure
wc.cbWndExtra = 0; // number of extra bytes following the window instance
wc.hInstance = hInstance; // this instance that the window procedure is within
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // the class icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // the class cursor
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);// background brush
wc.lpszMenuName = NULL; // the menu resource name
wc.lpszClassName = szApplicationName; // the app name
wc.lpfnWndProc = (WNDPROC) MyWndProc; // the window procedure
wc.cbClsExtra = 0; // number of extra bytes following the class structure
wc.cbWndExtra = 0; // number of extra bytes following the window instance
wc.hInstance = hInstance; // this instance that the window procedure is within
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // the class icon
wc.hCursor = LoadCursor(NULL, IDC_ARROW); // the class cursor
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);// background brush
wc.lpszMenuName = NULL; // the menu resource name
wc.lpszClassName = szApplicationName; // the app name
// Register the window.
RegisterClass(&wc);
RegisterClass(&wc);
// Now create the window.
hwnd = CreateWindow(
szApplicationName, // registered class name
szApplicationName, // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // x position of window
CW_USEDEFAULT, // y position of window
300, // width of window
300, // height of window
HWND_DESKTOP, // handle to parent window
NULL, // handle to menu identifier
hInstance, // handle to application instance
NULL // pointer to window-creation data
);
hwnd = CreateWindow(
szApplicationName, // registered class name
szApplicationName, // window name
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // x position of window
CW_USEDEFAULT, // y position of window
300, // width of window
300, // height of window
HWND_DESKTOP, // handle to parent window
NULL, // handle to menu identifier
hInstance, // handle to application instance
NULL // pointer to window-creation data
);
// Display the window.
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Pump messages until a quit message is received.
while(GetMessage(&msg, NULL, 0,0))
{
TranslateMessage(&msg); // translates virtual-key messages into character messages
DispatchMessage(&msg); // dispatches a message to the window procedure
}
while(GetMessage(&msg, NULL, 0,0))
{
TranslateMessage(&msg); // translates virtual-key messages into character messages
DispatchMessage(&msg); // dispatches a message to the window procedure
}
return msg.wParam;
}
}
LRESULT CALLBACK MyWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
static HGLRC hRC; // Permenant Rendering context
static HDC hDC; // Private GDI Device context
{
static HGLRC hRC; // Permenant Rendering context
static HDC hDC; // Private GDI Device context
switch (message)
{
// This function is called by Windows to handle messages for this application.
case WM_CREATE:
// Store the device context
hDC = GetDC(hWnd);
{
// This function is called by Windows to handle messages for this application.
case WM_CREATE:
// Store the device context
hDC = GetDC(hWnd);
// Select the pixel format
SetDCPixelFormat(hDC);
SetDCPixelFormat(hDC);
// Create the rendering context and make it current
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
SetupRC();
// Create the palette
hPalette = GetOpenGLPalette(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);
SetupRC();
// Create the palette
hPalette = GetOpenGLPalette(hDC);
// Create a timer that fires every millisecond
SetTimer(hWnd,101,10,NULL);
break;
SetTimer(hWnd,101,10,NULL);
break;
// Window is being destroyed, cleanup
case WM_DESTROY:
// Kill the timer that we created
KillTimer(hWnd,101);
case WM_DESTROY:
// Kill the timer that we created
KillTimer(hWnd,101);
// Deselect the current rendering context and delete it
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
wglMakeCurrent(hDC,NULL);
wglDeleteContext(hRC);
// Delete the palette
if(hPalette != NULL)
DeleteObject(hPalette);
if(hPalette != NULL)
DeleteObject(hPalette);
// Tell the application to terminate after the window
// is gone.
PostQuitMessage(0);
break;
// is gone.
PostQuitMessage(0);
break;
// Window is resized.
case WM_SIZE:
// Call our function which modifies the clipping
// volume and viewport
ChangeSize(LOWORD(lParam), HIWORD(lParam));
break;
case WM_SIZE:
// Call our function which modifies the clipping
// volume and viewport
ChangeSize(LOWORD(lParam), HIWORD(lParam));
break;
// Timer, moves and bounces the rectangle, simply calls
// our previous OnIdle function, then invalidates the
// window so it will be redrawn.
case WM_TIMER:
{
IdleFunction();
InvalidateRect(hWnd,NULL,FALSE);
}
break;
// our previous OnIdle function, then invalidates the
// window so it will be redrawn.
case WM_TIMER:
{
IdleFunction();
InvalidateRect(hWnd,NULL,FALSE);
}
break;
// The painting function. This message sent by Windows
// whenever the screen needs updating.
case WM_PAINT:
{
// Call OpenGL drawing code
RenderScene();
// whenever the screen needs updating.
case WM_PAINT:
{
// Call OpenGL drawing code
RenderScene();
// Call function to swap the buffers
SwapBuffers(hDC);
SwapBuffers(hDC);
// Validate the newly painted client area
ValidateRect(hWnd,NULL);
}
break;
ValidateRect(hWnd,NULL);
}
break;
// Windows is telling the application that it may modify
// the system palette. This message in essance asks the
// application for a new palette.
case WM_QUERYNEWPALETTE:
// If the palette was created.
if(hPalette)
{
int nRet;
// Selects the palette into the current device context
SelectPalette(hDC, hPalette, FALSE);
SelectPalette(hDC, hPalette, FALSE);
// Map entries from the currently selected palette to
// the system palette. The return value is the number
// of palette entries modified.
nRet = RealizePalette(hDC);
// the system palette. The return value is the number
// of palette entries modified.
nRet = RealizePalette(hDC);
// Repaint, forces remap of palette in current window
InvalidateRect(hWnd,NULL,FALSE);
InvalidateRect(hWnd,NULL,FALSE);
return nRet;
}
break;
}
break;
// This window may set the palette, even though it is not the
// currently active window.
case WM_PALETTECHANGED:
// Don't do anything if the palette does not exist, or if
// this is the window that changed the palette.
if((hPalette != NULL) && ((HWND)wParam != hWnd))
{
// Select the palette into the device context
SelectPalette(hDC,hPalette,FALSE);
// Map entries to system palette
RealizePalette(hDC);
// Remap the current colors to the newly realized palette
UpdateColors(hDC);
return 0;
}
break;
RealizePalette(hDC);
// Remap the current colors to the newly realized palette
UpdateColors(hDC);
return 0;
}
break;
default: // Passes it on if unproccessed
return (DefWindowProc(hWnd, message, wParam, lParam));
return (DefWindowProc(hWnd, message, wParam, lParam));
}
return (0L);
}
}








덧글
2009/11/15 08:13 # 답글
비공개 덧글입니다.
JinuJJang 2009/12/10 16:18 #
openGL 환경세팅은 잘 하셨는지 확인해보세요. 저는 잘 되는데 @_@