2014-01-13 18 views
5

我正在嘗試在openGL中創建一個太陽系(只有太陽,地球和月亮)。我唯一的光源是太陽,它應該照亮其他行星。到目前爲止我的工作已經完成,但是遠離太陽的行星表面也被點亮了,我希望它們變黑。在opengl中創建一個太陽系 - 照明不能按計劃工作

enter image description here

的對象是glutSolidSpheres,太陽在原點,也有我的光源。 這裏是我的燈光設置的代碼:

GLfloat light_position[] = {0.0, 0.0, 0.0, 1.0}; 
    GLfloat light_ambient_color[] = {0.0, 0.0, 0.0, 0.0}; 
    GLfloat light_diffuse_color[] = {1.0, 1.0, 1.0, 1.0}; 

    glLightfv(GL_LIGHT0, GL_POSITION, light_position); 
    glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient_color); 
    glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse_color); 
    glEnable(GL_LIGHTING); 
    glEnable(GL_LIGHT0); 
    glColorMaterial (GL_FRONT, GL_DIFFUSE) ; 
    glEnable(GL_COLOR_MATERIAL); 

我到底做錯了什麼?你們能給我一個提示嗎?

+1

你可能有一些問題與你的法線。也許他們被顛倒了?如果你發佈了相關的代碼,我們可以看看它(**編輯**只是看到你正在使用'glutSolidSpheres'我個人不知道如何設置法線) – user1781290

+2

或者燈的位置沒有被轉換和放置與相機位於同一個地方(較暗的邊框暗示了這一點) –

+1

當我將Ambient_color設置爲1,1,1,1時,黑暗邊界消失。 此外,相機處於固定位置,燈光也固定在原點。 – stainless

回答

2

其實我剛剛意識到棘輪怪胎是正確的意見。我在設置燈光位置後將gluLookAt稱爲「旋轉」它。我現在打電話給它,它現在起作用了!

1

光源在太陽內部,與光源相比,光源的法線指向「錯誤」的方向。也就是說,沒有光線來自你的球體,但因爲你有漫反射光線,你仍然可以看到球體。 嘗試使用「GL_LIGHT_MODEL_TWO_SIDE」屬性爲您的燈光模型翻轉「太陽」的法線。爲了測試它是否會起作用,你可以簡單地不畫太陽,然後再添加它。

BTW這傢伙有完全相同的問題,也許它可以幫助: http://www.opengl.org/discussion_boards/showthread.php/122872-Reversing-normals-for-glutSphere

0
int i=0,j=260,k=30,l=150,m=90; 
int n=230,o=10,p=280,q=220; 
float pi=3.1424,a,b,c,d,e,f,g,h,z; 



int gd=DETECT,gm; 
initgraph(&gd,&gm,"C:\\TurboC3\\BGI"); 
outtextxy(0,10,"SOLAR SYSTEM -Mount Carmel"); 
outtextxy(400,10,"press the key to move"); 
circle(320,240,20);    
setfillstyle(1,4); 
floodfill(320,240,15); 
outtextxy(310,237,"sun"); 
circle(260,240,8); 
setfillstyle(1,2); 
floodfill(258,240,15); 
floodfill(262,240,15); 
outtextxy(240,220,"mercury"); 
circle(320,300,12); 
setfillstyle(1,1); 
floodfill(320,298,15); 
floodfill(320,302,15); 
outtextxy(335,300,"venus"); 
circle(320,160,10); 
setfillstyle(1,5); 
floodfill(320,161,15); 
floodfill(320,159,15); 
outtextxy(332,150, "earth"); 
circle(453,300,11); 
setfillstyle(1,6); 
floodfill(445,300,15); 
floodfill(448,309,15); 
outtextxy(458,280,"mars"); 
circle(520,240,14); 
setfillstyle(1,7); 
floodfill(519,240,15); 
floodfill(521,240,15); 
outtextxy(500,257,"jupiter"); 
circle(169,122,12); 
setfillstyle(1,12); 
floodfill(159,125,15); 
floodfill(175,125,15); 
outtextxy(130,137,"saturn"); 
circle(320,420,9); 
setfillstyle(1,13); 
floodfill(320,417,15); 
floodfill(320,423,15); 
outtextxy(310,400,"urenus"); 
circle(40,240,9); 
setfillstyle(1,10); 
floodfill(38,240,15); 
floodfill(42,240,15); 
outtextxy(25,220,"neptune"); 
circle(150,420,7); 
setfillstyle(1,14); 
floodfill(150,419,15); 
floodfill(149,422,15); 
outtextxy(120,430,"pluto"); 
a=(pi/180)*i; 
b=(pi/180)*j; 
c=(pi/180)*k; 
d=(pi/180)*l; 
e=(pi/180)*m; 
f=(pi/180)*n; 
g=(pi/180)*o; 
h=(pi/180)*p; 
z=(pi/180)*q; 
cleardevice(); 
circle(320,240,20); 
setfillstyle(1,4); 
floodfill(320,240,15); 
outtextxy(310,237,"sun"); 

circle(320+60*sin(a),240-35*cos(a),8); 
setfillstyle(1,2); 
pieslice(320+60*sin(a),240-35*cos(a),0,360,8); 
circle(320+100*sin(b),240-60*cos(b),12); 
setfillstyle(1,1); 
pieslice(320+100*sin(b),240-60*cos(b),0,360,12); 
circle(320+130*sin(c),240-80*cos(c),10); 
setfillstyle(1,5); 
pieslice(320+130*sin(c),240-80*cos(c),0,360,10); 
circle(320+170*sin(d),240-100*cos(d),11); 
setfillstyle(1,6); 
pieslice(320+170*sin(d),240-100*cos(d),0,360,11); 
circle(320+200*sin(e),240-130*cos(e),14); 
setfillstyle(1,7); 
pieslice(320+200*sin(e),240-130*cos(e),0,360,14); 
circle(320+230*sin(f),240-155*cos(f),12); 
setfillstyle(1,12); 
pieslice(320+230*sin(f),240-155*cos(f),0,360,12); 
circle(320+260*sin(g),240-180*cos(g),9); 
setfillstyle(1,13); 
pieslice(320+260*sin(g),240-180*cos(g),0,360,9); 
circle(320+280*sin(h),240-200*cos(h),9); 
setfillstyle(1,10); 
pieslice(320+280*sin(h),240-200*cos(h),0,360,9); 
circle(320+300*sin(z),240-220*cos(z),7); 
setfillstyle(1,14); 
pieslice(320+300*sin(z),240-220*cos(z),0,360,7); 
delay(20); 
i++; 
j++; 
k++; 
l++; 
m++; 
n++; 
o++; 
p++; 
q+=2;