2012-10-21 4 views
0

나는 건물을 그렸을 뿐인 매우 간단한 코드를 가지고 건물의 그림자를 추가하고 싶습니다. 나는 많은 코드 샘플을 시도했지만, 너무 복잡하여 조명 된 오브젝트가 보이지 않습니다. 또는 너무 막연합니다. 내 건물의 그림자를 어떻게 얻을 수 있습니까? OpenGL은 렌더링 된 장면에서OpenGL에서 그림자 추가

#include "GLee/GLee.h" //GL header file, including extensions 
#include "glut.h" 
#include <stdio.h> 
#include <conio.h> 
#include <math.h> 
#include "tga.h" 
#define WIN32_LEAN_AND_MEAN 
#include <windows.h> 
#include <stdio.h> 
char g_SelectedColor = 'w'; 
int g_Width; 
int g_Height; 
int a=0.0 ,b=0.0, c=500, d=0.0, e=0.0, f=0.0,g=0.0,h=1.0,i=0.0; 

static int rotationAngle=0; 
void init(); 
void myMouseFunction(int button, int state, int mouseX, int mouseY); 
void myKeyboardFunction(unsigned char key, int mouseX, int mouseY); 
void Reshape(int width, int height); 
void timer(int val); 
void display(); 
void drawBuilding(); 
void menu(int); 
// Assign a default value 
float light_diffuse[] = { 0.8, 0.8, 0.8, 1.0 }; 
float light_ambient[] = { 0.1, 1.1, 0.0, 0.0 }; 
float light_specular[] = { 0.5, 0.5, 0.9, 1.0 }; 
float light_position[] = { 0.0, 10.0, 0.0, 1.0 }; 
void selectMessage(int val) 
{ 
    if(val==2) 
    { 
    glBegin(GL_POLYGON); 
     glVertex3f(0.0,0.0,0.0); 
     glVertex3f(0.0,200.0,0.0); 
     glVertex3f(200.0,200.0,0.0); 
     glVertex3f(200.0,0.0,0.0); 
     glVertex3f(0.0,0.0,0.0); 
    glEnd(); 
    } 
} 
int main(int argc, char** argv) 
{ 
    glutCreateMenu(menu); 
     glutAddMenuEntry("View1", 1); 
     glutAddMenuEntry("View2", 2); 
     glutAttachMenu(GLUT_RIGHT_BUTTON); 


    g_Width =1200; g_Height = 600; 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 
    // glutInitWindowSize(g_Width, g_Height); 
    //glutFullScreen(); 
    glutInitWindowPosition(50, 50); 
    glutCreateWindow("CHECK"); 
    init(); 
    glutMouseFunc(myMouseFunction); 
    glutKeyboardFunc(myKeyboardFunction); 

    glutReshapeFunc(Reshape); 
    glutDisplayFunc(display); 
    glutMainLoop(); 
    return 0; 
} 
void init(void) 
{ 

    glutAttachMenu(GLUT_RIGHT_BUTTON); 
    glClearColor(1.0, 1.0, 1.0, 1.0); 
    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(50.0, 1.0, 200, 1000); 
    //glOrtho(-5.0, +5.0, -5.0, +5.0, +5.0, -5.0); 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 
    glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); 
    glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
    glShadeModel(GL_SMOOTH); 
    //glShadeModel(GL_FLAT); 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glEnable(GL_DEPTH_TEST); 
    //glCullFace(GL_FRONT_AND_BACK); 
    glDisable(GL_CULL_FACE); 
    glMatrixMode(GL_MODELVIEW); 
    if (loadTGA ("im1.tga", 10) == false) 
     printf ("\nError: File myQuakeTexture.tga not found!"); 

    if (loadTGA ("im2.tga", 11) == false) 
     printf ("\nError: File myQuakeTexture.tga not found!"); 
} 
void myMouseFunction(int button, int state, int mouseX, int mouseY) 
{ 
} 
void myKeyboardFunction(unsigned char key, int mouseX, int mouseY) 
{ 

    switch(key) 
    { 
    case 'r': 
     { 

     glClearColor(0.0, 0.0, 1.0, 1.0); 
     glRotatef(rotationAngle++, 0.0, 1.0, 0.0); 
    // drawBuilding(); 
     //display(); 

     } 
    case 'R': 
    case 'g': 

    case 'G': 
    case 'b': 

    case 'B': 
    case 'w': 
    case 'W': 
     g_SelectedColor = key; 
     break; 
    case '1': 

    case '2': 
    case '3': 
    case '4': 
    case '5': 
     break; 
    case 27: // Esc key 
     exit(0); 
     break; // redundant 
    default: 
     break; 
    } 
} 
void Reshape(int width, int height) 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    g_Width = width; 
    g_Height = height; 
    glViewport (0, 0, g_Width, g_Height); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(50.0, 1.0, 200, 1000); 
    //glOrtho(-5.0, +5.0, -5.0, +5.0, +5.0, -5.0);; 
} 
void timer(int val) 
{ 
    display(); 
} 
static float firstAngle=0; 
void drawBuilding() 
{ 
    float DoorMaterial[4] = { 0.5, 0.2, 1.3, 1.0 }; 
    // 

    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D ,10); 
    glTranslatef(30,-180,0); 
    //Building 1 
    //Front 
    glBegin(GL_POLYGON); 
     glVertex3f(0.0,0.0,0.0); 
     glTexCoord2d(1,1); 
     glVertex3f(0.0,400.0,0.0); 
     glTexCoord2d(0,1); 
     glVertex3f(70.0,400.0,0.0); 
     glTexCoord2d(0,0); 
     glVertex3f(70.0,0.0,0.0); 
     glTexCoord2d(1,0); 
     glVertex3f(0.0,0.0,0.0); 
    glEnd(); 
    //Back 
     glBegin(GL_POLYGON); 
     glVertex3f(0.0,0.0,-50.0); 
     glTexCoord2d(1,1); 
     glVertex3f(0.0,400.0,-50.0); 
     glTexCoord2d(0,1); 
     glVertex3f(70.0,400.0,-50.0); 
     glTexCoord2d(0,0); 
     glVertex3f(70.0,0.0,-50.0); 
     glTexCoord2d(1,0); 
     glVertex3f(0.0,0.0,-50.0); 
    glEnd(); 
    //Left 
     glBegin(GL_POLYGON); 
     glVertex3f(0.0,0.0,0.0); 
     glTexCoord2d(1,1); 
     glVertex3f(0.0,400.0,0.0); 
     glTexCoord2d(0,1); 
     glVertex3f(0.0,400.0,-50.0); 
     glTexCoord2d(0,0); 
     glVertex3f(0.0,0.0,-50.0); 
     glTexCoord2d(1,0); 
     glVertex3f(0.0,0.0,0.0); 
    glEnd(); 
    //Right 
     glBegin(GL_POLYGON); 
     glVertex3f(70.0,0.0,0.0); 
     glTexCoord2d(1,1); 
     glVertex3f(70.0,400.0,0.0); 
     glTexCoord2d(0,1); 
     glVertex3f(70.0,400.0,-50.0); 
     glTexCoord2d(0,0); 
     glVertex3f(70.0,0.0,-50.0); 
     glTexCoord2d(1,0); 
     glVertex3f(70.0,0.0,0.0); 
    glEnd(); 
    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D ,11); 

} 
void menu(int item) 
{ 
     switch (item) 
     { 
     case 1: 
      { 

       a=-500, b=0, c=300,d=0,e=0,f=0,g=0,h=1,i=0; 


      } 

       break; 

     case 2: 
      { 
       a=0, b=300, c=500,d=0,e=0,f=0,g=0,h=1,i=0; 
      drawBuilding(); 
      } 
     } 
} 
void display() 
{ 
    glRotatef(rotationAngle++, 0.0, 1.0, 0.0); 
    glutFullScreen(); 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    gluLookAt(a, b, c, 
       d, e, f, 
       g, h, i); 

    glRotatef(rotationAngle++, 0.0, 1.0, 0.0); 
    // 
    drawBuilding(); 
    glDisable(GL_TEXTURE_2D); 



    // this tells glut to call the 'timer' function in 33 milliseconds 
    // i.e. this way we will draw 1000/33 = 30 times a second 
    glutTimerFunc(33, timer, 0); 
    glutSwapBuffers(); 
    printf("."); 
} 

답변

7

갖는 그림자는 단순히 일부 기능을 사용의 문제가 아니다. OpenGL 자체만으로 점, 선 및 삼각형을 그립니다. 한 번에 하나씩 그들 사이의 컨텍스트없이. 컨텍스트를 제공하는 것은 사용자의 몫입니다.

그리기 그림자는 여러 가지 방법으로 구현 될 수 있지만, 가장 많이 사용되는 그것들을 설명하면서

  • 스텐실 볼륨 그림자

  • 그림자 매핑

있습니다 여기에 크게 surpa 것입니다 StackOverflow 질문의 한계입니다. 나는 때문에 (사람들에 의해 연결되어 자원) 그들에 위키 백과 기사

에 당신을 참조