เขียนโปรแกรม ภาพ 3d ค่ะใช้ภาษา c++
ตอนปรับมุมมองด้วย วิธีเปลี่ยนตัวเลข ใน lookAt โดยตรงก็ไม่มีปัญหานะคะ
แต่พอเพิ่มฟังก์ชันให้หมุนมุมกล้อง ด้วยการกดปุ่มคีย์บอด ขึ้น ลง ซ้ายขวา วัตถุ เลื่อน มาทับกันบ้าง เลื่อนลงไปข้างล่างพื้นห้องบ้าง
เละเทะเลยค่ะ T_T ตอนแรกที่ไม่ได้หมุนก็ปกติดี
บางทีไม่ต้องหมุนมุมกล้องก็เละค่ะ ถ้าไปคลิกที่บนวินโดว แล้วเลื่อนไปมา ๆ ซักพัก ของจะเละ T_T
เป็นโปรเจคส่งครู ช่วยหน่อยนะคะ นี่โค้ด
#include<GL\glut.h>
float xx = 3,yy = 1,zz = 4;
void color(float rd,float gd,float bd,float ad,float ra,float ga,float ba,float sh)
{
GLfloat mat_diffuse[] = {rd,gd,bd,ad};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_ambient[] = {ra,ga,ba,1.0f};
GLfloat mat_shininess[] = {sh};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}
void roundChair()
{
//rounded chair
GLUquadricObj *theObj;
theObj = gluNewQuadric();
gluQuadricDrawStyle(theObj,GLU_FILL);
//top
glPushMatrix();
glTranslated(0.3,0.45,0.2);
glRotated(90,1,0,0);
gluCylinder(theObj,0.21,0.21,0.025,30,30);
glPopMatrix();
//close top
glPushMatrix();
glTranslated(0.3,0.45,0.2);
glScaled(1,0.1,1);
glutSolidSphere(0.21,30,30);
glPopMatrix();
//leg
glPushMatrix();
glTranslated(0.3,0.45,0.2);
glRotated(90,1,0,0);
gluCylinder(theObj,0.02,0.02,0.5,30,30);
glPopMatrix();
gluDeleteQuadric(theObj);
}
void wall(double thickness)
{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.5,thickness,1.5);
glutSolidCube(1);
glPopMatrix();
}
void slot(int rS,int gS,int bS,int rC,int gC,int bC)
{
////slot machine
color(rS,gS,bS,1,0,0,0,10);
GLUquadricObj *theObj;
theObj = gluNewQuadric();
gluQuadricDrawStyle(theObj,GLU_FILL);
//cylinder and close end
glPushMatrix();
glTranslated(0,-.53,0);
glPushMatrix();
glScaled(1,.7,.7);
glTranslated(-0.004,.85,-.05);
color(1, 1, 0,1,0,0,0,200);
glPushMatrix();
glTranslated(0.3,2,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.31,0.31,0.15,30,30);
glPopMatrix();
color(1, 0.501, 0.752,1,0,0,0,200);
glPushMatrix();
glTranslated(0.5,2,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.31,0.31,0.15,30,30);
glPopMatrix();
color(0.909, 0.313, 0.141,1,0,0,0,200);
glPushMatrix();
glTranslated(0.7,2,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.31,0.31,0.15,30,30);
glPopMatrix();
//lid cylinder
glPushMatrix();
glTranslated(0.85,2,0.2);
glRotated(90,0,0,1);
glScaled(1,0.1,1);
gluSphere(theObj,0.31,31,30);
glPopMatrix();
glPushMatrix();
glTranslated(0.3,2,0.2);
glRotated(90,0,0,1);
glScaled(1,0.1,1);
gluSphere(theObj,0.31,31,30);
glPopMatrix();
glPopMatrix();
color(rS,gS,bS,1,1,1,0,100);
//upper cube
glPushMatrix();
glTranslated(0.57,1.6,0.11);
glScaled(1,0.5,.7);
glutSolidCube(.62);
glPopMatrix();
//lower cube
glPushMatrix();
glTranslated(0.57,1,0.2);
glScaled(1,1.5,1);
glutSolidCube(.62);
glPopMatrix();
//intervention
glPushMatrix();
glTranslated(0.29,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
glPushMatrix();
glTranslated(0.476,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
glPushMatrix();
glTranslated(0.662,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
glPushMatrix();
glTranslated(0.85,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
//surface lid
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
color(0, 1, 1,.25,1,1,1,500);
glPushMatrix();
glTranslated(.58,2,0.35);
glScaled(1,.8,.05);
glutSolidCube(.62);
glPopMatrix();
//beyond lid
color(rS,gS,bS,1,1,1,0,100);
glPushMatrix();
glTranslated(0.57,2.3,0.09);
glScaled(1,.2,1);
glutSolidCube(.62);
glPopMatrix();
color(1, 0, 1,1,0,0,0,5);
//button
glPushMatrix();
glTranslated(0.78,1.45,0.42);
glutSolidCube(.08);
glPopMatrix();
color(0.501, 1, 0.501,1,0,0,0,5);
glPushMatrix();
glTranslated(0.58,1.45,0.42);
glutSolidCube(.08);
glPopMatrix();
color(0, 0, 1,1,0,0,0,5);
glPushMatrix();
glTranslated(0.38,1.45,0.42);
glutSolidCube(.08);
glPopMatrix();
//side bar
color(0, 0, 0,1,0,0,0,5);
glPushMatrix();
glTranslated(.88,1.6,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.1,0.1,0.05,30,30);
glPopMatrix();
glPushMatrix();
glTranslated(.93,1.6,0.2);
glRotated(90,0,0,1);
glScaled(1,0.3,1);
gluSphere(theObj,0.1,31,30);
glPopMatrix();
glPushMatrix();
glTranslated(.95,1.6,0.2);
glRotated(250,1,0,0);
gluCylinder(theObj,0.03,0.03,0.5,30,30);
glPopMatrix();
glPushMatrix();
glTranslated(0.95,2.11,0.03);
gluSphere(theObj,0.05,31,30);
glPopMatrix();
color(0, 0, 0,1,0,0,0,200);
//drawer
glPushMatrix();
glRotated(-10,1,0,0);
glTranslated(.58,1,0.75);
glScaled(1,.8,.05);
glutSolidCube(.62);
glPopMatrix();
color(rC,gC,bC,1,0,0,0,200);
glPushMatrix();
glScaled(1.5,1.5,1.5);
glTranslated(0.09,.4,0.5);
roundChair();
glPopMatrix();
gluDeleteQuadric(theObj);
}
//end slot
Opengl 3 มิติ พอหมุนมุมกล้อง วัตถุในฉากเคลื่อนเละเทะเลยค่ะ ช่วยด้วยยยย
ตอนปรับมุมมองด้วย วิธีเปลี่ยนตัวเลข ใน lookAt โดยตรงก็ไม่มีปัญหานะคะ
แต่พอเพิ่มฟังก์ชันให้หมุนมุมกล้อง ด้วยการกดปุ่มคีย์บอด ขึ้น ลง ซ้ายขวา วัตถุ เลื่อน มาทับกันบ้าง เลื่อนลงไปข้างล่างพื้นห้องบ้าง
เละเทะเลยค่ะ T_T ตอนแรกที่ไม่ได้หมุนก็ปกติดี
บางทีไม่ต้องหมุนมุมกล้องก็เละค่ะ ถ้าไปคลิกที่บนวินโดว แล้วเลื่อนไปมา ๆ ซักพัก ของจะเละ T_T
เป็นโปรเจคส่งครู ช่วยหน่อยนะคะ นี่โค้ด
#include<GL\glut.h>
float xx = 3,yy = 1,zz = 4;
void color(float rd,float gd,float bd,float ad,float ra,float ga,float ba,float sh)
{
GLfloat mat_diffuse[] = {rd,gd,bd,ad};
GLfloat mat_specular[] = {1.0f,1.0f,1.0f,1.0f};
GLfloat mat_ambient[] = {ra,ga,ba,1.0f};
GLfloat mat_shininess[] = {sh};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_DIFFUSE,mat_diffuse);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
}
void roundChair()
{
//rounded chair
GLUquadricObj *theObj;
theObj = gluNewQuadric();
gluQuadricDrawStyle(theObj,GLU_FILL);
//top
glPushMatrix();
glTranslated(0.3,0.45,0.2);
glRotated(90,1,0,0);
gluCylinder(theObj,0.21,0.21,0.025,30,30);
glPopMatrix();
//close top
glPushMatrix();
glTranslated(0.3,0.45,0.2);
glScaled(1,0.1,1);
glutSolidSphere(0.21,30,30);
glPopMatrix();
//leg
glPushMatrix();
glTranslated(0.3,0.45,0.2);
glRotated(90,1,0,0);
gluCylinder(theObj,0.02,0.02,0.5,30,30);
glPopMatrix();
gluDeleteQuadric(theObj);
}
void wall(double thickness)
{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.5,thickness,1.5);
glutSolidCube(1);
glPopMatrix();
}
void slot(int rS,int gS,int bS,int rC,int gC,int bC)
{
////slot machine
color(rS,gS,bS,1,0,0,0,10);
GLUquadricObj *theObj;
theObj = gluNewQuadric();
gluQuadricDrawStyle(theObj,GLU_FILL);
//cylinder and close end
glPushMatrix();
glTranslated(0,-.53,0);
glPushMatrix();
glScaled(1,.7,.7);
glTranslated(-0.004,.85,-.05);
color(1, 1, 0,1,0,0,0,200);
glPushMatrix();
glTranslated(0.3,2,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.31,0.31,0.15,30,30);
glPopMatrix();
color(1, 0.501, 0.752,1,0,0,0,200);
glPushMatrix();
glTranslated(0.5,2,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.31,0.31,0.15,30,30);
glPopMatrix();
color(0.909, 0.313, 0.141,1,0,0,0,200);
glPushMatrix();
glTranslated(0.7,2,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.31,0.31,0.15,30,30);
glPopMatrix();
//lid cylinder
glPushMatrix();
glTranslated(0.85,2,0.2);
glRotated(90,0,0,1);
glScaled(1,0.1,1);
gluSphere(theObj,0.31,31,30);
glPopMatrix();
glPushMatrix();
glTranslated(0.3,2,0.2);
glRotated(90,0,0,1);
glScaled(1,0.1,1);
gluSphere(theObj,0.31,31,30);
glPopMatrix();
glPopMatrix();
color(rS,gS,bS,1,1,1,0,100);
//upper cube
glPushMatrix();
glTranslated(0.57,1.6,0.11);
glScaled(1,0.5,.7);
glutSolidCube(.62);
glPopMatrix();
//lower cube
glPushMatrix();
glTranslated(0.57,1,0.2);
glScaled(1,1.5,1);
glutSolidCube(.62);
glPopMatrix();
//intervention
glPushMatrix();
glTranslated(0.29,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
glPushMatrix();
glTranslated(0.476,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
glPushMatrix();
glTranslated(0.662,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
glPushMatrix();
glTranslated(0.85,2,0.11);
glScaled(.1,1,.7);
glutSolidCube(.62);
glPopMatrix();
//surface lid
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
color(0, 1, 1,.25,1,1,1,500);
glPushMatrix();
glTranslated(.58,2,0.35);
glScaled(1,.8,.05);
glutSolidCube(.62);
glPopMatrix();
//beyond lid
color(rS,gS,bS,1,1,1,0,100);
glPushMatrix();
glTranslated(0.57,2.3,0.09);
glScaled(1,.2,1);
glutSolidCube(.62);
glPopMatrix();
color(1, 0, 1,1,0,0,0,5);
//button
glPushMatrix();
glTranslated(0.78,1.45,0.42);
glutSolidCube(.08);
glPopMatrix();
color(0.501, 1, 0.501,1,0,0,0,5);
glPushMatrix();
glTranslated(0.58,1.45,0.42);
glutSolidCube(.08);
glPopMatrix();
color(0, 0, 1,1,0,0,0,5);
glPushMatrix();
glTranslated(0.38,1.45,0.42);
glutSolidCube(.08);
glPopMatrix();
//side bar
color(0, 0, 0,1,0,0,0,5);
glPushMatrix();
glTranslated(.88,1.6,0.2);
glRotated(90,0,1,0);
gluCylinder(theObj,0.1,0.1,0.05,30,30);
glPopMatrix();
glPushMatrix();
glTranslated(.93,1.6,0.2);
glRotated(90,0,0,1);
glScaled(1,0.3,1);
gluSphere(theObj,0.1,31,30);
glPopMatrix();
glPushMatrix();
glTranslated(.95,1.6,0.2);
glRotated(250,1,0,0);
gluCylinder(theObj,0.03,0.03,0.5,30,30);
glPopMatrix();
glPushMatrix();
glTranslated(0.95,2.11,0.03);
gluSphere(theObj,0.05,31,30);
glPopMatrix();
color(0, 0, 0,1,0,0,0,200);
//drawer
glPushMatrix();
glRotated(-10,1,0,0);
glTranslated(.58,1,0.75);
glScaled(1,.8,.05);
glutSolidCube(.62);
glPopMatrix();
color(rC,gC,bC,1,0,0,0,200);
glPushMatrix();
glScaled(1.5,1.5,1.5);
glTranslated(0.09,.4,0.5);
roundChair();
glPopMatrix();
gluDeleteQuadric(theObj);
}
//end slot