About Opengl light
in
Core Library Questions
•
2 years ago
Hello I am Taiwan student
recently I start to use processing~
I use opnegl light to do this:
import javax.media.opengl.*;
import processing.opengl.*;
import java.nio.*;
import com.sun.opengl.util.*;
PGraphicsOpenGL pgl;
GL gl;
FloatBuffer lightKa;
FloatBuffer lightKd;
FloatBuffer lightKs;
FloatBuffer light_position;
void setup()
{
size(400,300,OPENGL);
pgl = (PGraphicsOpenGL) g; // g may change
gl = pgl.gl;
lightKa = BufferUtil.newFloatBuffer(4);
lightKa.put(0.2);
lightKa.put(0.2);
lightKa.put(0.2);
lightKa.put(1.0);
lightKa.rewind();
lightKd = BufferUtil.newFloatBuffer(4);
lightKd.put(0.7);
lightKd.put(0.7);
lightKd.put(0.7);
lightKd.put(1.0);
lightKd.rewind();
lightKs = BufferUtil.newFloatBuffer(4);
lightKs.put(1.0);
lightKs.put(1.0);
lightKs.put(1.0);
lightKs.put(1.0);
lightKs.rewind();
light_position = BufferUtil.newFloatBuffer(4);
light_position.put(0.0);
light_position.put(0.0);
light_position.put(100.0);
light_position.put(0.0);
light_position.rewind();
// set up light colors (ambient, diffuse, specular)
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightKa);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightKd);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightKs);
// position the light
float[] dir =new float[]{
0.0f, 0.0f, -1f,0.0f};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPOT_DIRECTION, dir,0);
gl.glEnable(GL.GL_LIGHT0);
}
void draw()
{
background(0);
camera(mouseX,mouseY,300,
0,0,0,
0,1,0);
Draw_Box(0,0,0,100,1,1,1,1);
}
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
void Draw_Box(float CenterX,float CenterY,float CenterZ, float R,
float Enter_H,float Enter_S, float Enter_B,float Enter_ColorA)
{
gl = pgl.beginGL();
gl.glBegin(GL.GL_QUADS);
// v0-v1-v6-v5
gl.glNormal3f(0,1,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ-R); // v6
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ-R); // v5
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ+R); // v0
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ+R); // v1
// v2-v3-v4-v7
gl.glNormal3f(0,-1,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ-R); // v7
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ-R); // v4
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ+R); // v3
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v2
// v0-v3-v4-v5
gl.glNormal3f(1,0,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ+R); // v0
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ+R); // v3
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ-R); // v4
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ-R); // v5
// v1-v2-v7-v6
gl.glNormal3f(-1,0,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v1
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ+R); // v2
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ-R); // v7
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ-R); // v6
// v1-v0-v2-v3
gl.glNormal3f(0,0,-1);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ+R); // v1
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ+R); // v0
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ+R); // v2
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v3
// v6-v5-v4-v7
gl.glNormal3f(0,0,-1);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ-R); // v6
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ-R); // v5
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ-R); // v4
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v7
gl.glEnd();
pgl.endGL();
}
but it has no shadow like this:
import processing.opengl.*;
void setup()
{
size(400, 300, OPENGL);
}
void draw()
{
camera(mouseX,mouseY,300,
0,0,0,0,1,0);
background(0);
directionalLight(126, 126, 126, 0, 0, -1);
ambientLight(64,64,64);
noStroke();
fill(0,255,0);
box(100);
}
I don't know why~~~~Please help me~~~
recently I start to use processing~
I use opnegl light to do this:
import javax.media.opengl.*;
import processing.opengl.*;
import java.nio.*;
import com.sun.opengl.util.*;
PGraphicsOpenGL pgl;
GL gl;
FloatBuffer lightKa;
FloatBuffer lightKd;
FloatBuffer lightKs;
FloatBuffer light_position;
void setup()
{
size(400,300,OPENGL);
pgl = (PGraphicsOpenGL) g; // g may change
gl = pgl.gl;
lightKa = BufferUtil.newFloatBuffer(4);
lightKa.put(0.2);
lightKa.put(0.2);
lightKa.put(0.2);
lightKa.put(1.0);
lightKa.rewind();
lightKd = BufferUtil.newFloatBuffer(4);
lightKd.put(0.7);
lightKd.put(0.7);
lightKd.put(0.7);
lightKd.put(1.0);
lightKd.rewind();
lightKs = BufferUtil.newFloatBuffer(4);
lightKs.put(1.0);
lightKs.put(1.0);
lightKs.put(1.0);
lightKs.put(1.0);
lightKs.rewind();
light_position = BufferUtil.newFloatBuffer(4);
light_position.put(0.0);
light_position.put(0.0);
light_position.put(100.0);
light_position.put(0.0);
light_position.rewind();
// set up light colors (ambient, diffuse, specular)
gl.glLightfv(GL.GL_LIGHT0, GL.GL_AMBIENT, lightKa);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_DIFFUSE, lightKd);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPECULAR, lightKs);
// position the light
float[] dir =new float[]{
0.0f, 0.0f, -1f,0.0f};
gl.glLightfv(GL.GL_LIGHT0, GL.GL_POSITION, light_position);
gl.glLightfv(GL.GL_LIGHT0, GL.GL_SPOT_DIRECTION, dir,0);
gl.glEnable(GL.GL_LIGHT0);
}
void draw()
{
background(0);
camera(mouseX,mouseY,300,
0,0,0,
0,1,0);
Draw_Box(0,0,0,100,1,1,1,1);
}
// v6----- v5
// /| /|
// v1------v0|
// | | | |
// | |v7---|-|v4
// |/ |/
// v2------v3
void Draw_Box(float CenterX,float CenterY,float CenterZ, float R,
float Enter_H,float Enter_S, float Enter_B,float Enter_ColorA)
{
gl = pgl.beginGL();
gl.glBegin(GL.GL_QUADS);
// v0-v1-v6-v5
gl.glNormal3f(0,1,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ-R); // v6
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ-R); // v5
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ+R); // v0
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ+R); // v1
// v2-v3-v4-v7
gl.glNormal3f(0,-1,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ-R); // v7
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ-R); // v4
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ+R); // v3
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v2
// v0-v3-v4-v5
gl.glNormal3f(1,0,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ+R); // v0
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ+R); // v3
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ-R); // v4
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ-R); // v5
// v1-v2-v7-v6
gl.glNormal3f(-1,0,0);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v1
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ+R); // v2
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ-R); // v7
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ-R); // v6
// v1-v0-v2-v3
gl.glNormal3f(0,0,-1);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ+R); // v1
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ+R); // v0
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ+R); // v2
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v3
// v6-v5-v4-v7
gl.glNormal3f(0,0,-1);
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY-R,CenterZ-R); // v6
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY-R,CenterZ-R); // v5
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX+R,CenterY+R,CenterZ-R); // v4
gl.glColor3f(0,1,0);
gl.glVertex3f(CenterX-R,CenterY+R,CenterZ+R); // v7
gl.glEnd();
pgl.endGL();
}
but it has no shadow like this:
import processing.opengl.*;
void setup()
{
size(400, 300, OPENGL);
}
void draw()
{
camera(mouseX,mouseY,300,
0,0,0,0,1,0);
background(0);
directionalLight(126, 126, 126, 0, 0, -1);
ambientLight(64,64,64);
noStroke();
fill(0,255,0);
box(100);
}
I don't know why~~~~Please help me~~~
1