(1)Week01:WebGL,OpenGL專案,GLUT專案
(2)Week02:點、線、面,色彩
(3)Week03:移動、轉動、縮放、矩陣
範例:Transformation.exe
(4)Week04:mouse加入motion加入
(5)Week05:階層轉動
(6)Week06:T-R-T轉動
(7)Week07:貼圖、地球(地圖)
(8)Week08:打光
(9)Week10:期中考
(10)Week11:聲音PlaySound()、音樂CMP3_MCI.h、工作執行目錄working-dir
(11)Week12:3D模型
(12)Week13:擺pose
(13)Week14:讀檔、寫檔
(14)Week15:複習
示範階層性轉動
打光 關節轉動
用更BEST的音樂讀檔"CMP3_MCI.h"
(到moodle下載)
///只留下有陣列有light字樣的打光
#include <GL/glut.h>
#include <stdio.h>
///float angle=0,angle2=0,angle3=0,angle4=0, X=150, Y=150, oldX=0, oldY=0;
float angle[20], X=150, Y=150, oldX=0, oldY=0;
int ID=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[0], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[1], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[2], 0.0f, 0.0f, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[3], 0.0f, 0.0, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
}
#include "CMP3_MCI.h"
#include <windows.h>
#include <mmsystem.h>
CMP3_MCI myMP3;
void mouse(int button, int state, int x, int y)
{
oldX = x; oldY = y;
if(button == GLUT_RIGHT_BUTTON)
PlaySound("Shot.wav",NULL,SND_ASYNC);
}
void motion(int x, int y)
{
angle[ID]+= x-oldX;
oldX=x;
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
myMP3.Load("123.mp3");
myMP3.Play();
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
/// glutReshapeFunc(resize);
/// glutDisplayFunc(display);
/// glutKeyboardFunc(key);
/// glutIdleFunc(idle);
glClearColor(1,1,1,1);
/// glEnable(GL_CULL_FACE); /// glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
///float angle=0,angle2=0,angle3=0,angle4=0, X=150, Y=150, oldX=0, oldY=0;
float angle[20], X=150, Y=150, oldX=0, oldY=0;
int ID=0;
#include "glm.h"
GLMmodel * pmodel=NULL;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(pmodel==NULL){
pmodel=glmReadOBJ("123.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL );
glPushMatrix();
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[0], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[1], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[2], 0.0f, 0.0f, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[3], 0.0f, 0.0, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
if(key=='s'|| key=='S'||key=='w'|| key=='W'){
if(fout==NULL) fout=fopen("output.txt", "w+");
fprintf(fout, "%.2f %.2f %.2f %.2f\n",angle[0], angle[1], angle[2], angle[3]);
if(key=='r'|| key=='R'){
if(fin==NULL) fin=fopen("output.txt", "r");
fscanf(fin, "%f %f %f %f",&angle[0], &angle[1], &angle[2], &angle[3]);
glutPostRedisplay();
}
}
#include "CMP3_MCI.h"
#include <windows.h>
#include <mmsystem.h>
CMP3_MCI myMP3;
void mouse(int button, int state, int x, int y)
{
oldX = x; oldY = y;
if(button == GLUT_RIGHT_BUTTON)
PlaySound("Shot.wav",NULL,SND_ASYNC);
}
void motion(int x, int y)
{
angle[ID]+= x-oldX;
oldX=x;
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
myMP3.Load("123.mp3");
myMP3.Play();
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
/// glutReshapeFunc(resize);
/// glutDisplayFunc(display);
/// glutKeyboardFunc(key);
/// glutIdleFunc(idle);
glClearColor(1,1,1,1);
/// glEnable(GL_CULL_FACE); /// glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
打光 關節轉動
用更BEST的音樂讀檔"CMP3_MCI.h"
(到moodle下載)
///只留下有陣列有light字樣的打光
#include <GL/glut.h>
#include <stdio.h>
///float angle=0,angle2=0,angle3=0,angle4=0, X=150, Y=150, oldX=0, oldY=0;
float angle[20], X=150, Y=150, oldX=0, oldY=0;
int ID=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[0], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[1], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[2], 0.0f, 0.0f, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[3], 0.0f, 0.0, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
}
#include "CMP3_MCI.h"
#include <windows.h>
#include <mmsystem.h>
CMP3_MCI myMP3;
void mouse(int button, int state, int x, int y)
{
oldX = x; oldY = y;
if(button == GLUT_RIGHT_BUTTON)
PlaySound("Shot.wav",NULL,SND_ASYNC);
}
void motion(int x, int y)
{
angle[ID]+= x-oldX;
oldX=x;
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
myMP3.Load("123.mp3");
myMP3.Play();
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
/// glutReshapeFunc(resize);
/// glutDisplayFunc(display);
/// glutKeyboardFunc(key);
/// glutIdleFunc(idle);
glClearColor(1,1,1,1);
/// glEnable(GL_CULL_FACE); /// glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
#include <GL/glut.h>
#include <stdio.h>
FILE *fout=NULL;
FILE *fin=NULL;
///float angle=0,angle2=0,angle3=0,angle4=0, X=150, Y=150, oldX=0, oldY=0;
float angle[20], X=150, Y=150, oldX=0, oldY=0;
int ID=0;
#include "glm.h"
GLMmodel * pmodel=NULL;
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if(pmodel==NULL){
pmodel=glmReadOBJ("123.obj");
if (!pmodel) exit(0);
glmUnitize(pmodel);
glmFacetNormals(pmodel);
glmVertexNormals(pmodel, 90.0);
}
glmDraw(pmodel, GLM_SMOOTH | GLM_MATERIAL );
glPushMatrix();
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[0], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(0.2, 0, 0);
glRotatef(angle[1], 0.0f, 0.0f, 1.0f);
glTranslatef(0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[2], 0.0f, 0.0f, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glRotatef(angle[3], 0.0f, 0.0, 1.0f);
glTranslatef(-0.2, 0, 0);
glutSolidTeapot(0.2);
glPopMatrix();
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if(key=='1') ID=1;
if(key=='2') ID=2;
if(key=='3') ID=3;
if(key=='4') ID=4;
if(key=='5') ID=5;
if(key=='s'|| key=='S'||key=='w'|| key=='W'){
if(fout==NULL) fout=fopen("output.txt", "w+");
fprintf(fout, "%.2f %.2f %.2f %.2f\n",angle[0], angle[1], angle[2], angle[3]);
if(key=='r'|| key=='R'){
if(fin==NULL) fin=fopen("output.txt", "r");
fscanf(fin, "%f %f %f %f",&angle[0], &angle[1], &angle[2], &angle[3]);
glutPostRedisplay();
}
}
#include "CMP3_MCI.h"
#include <windows.h>
#include <mmsystem.h>
CMP3_MCI myMP3;
void mouse(int button, int state, int x, int y)
{
oldX = x; oldY = y;
if(button == GLUT_RIGHT_BUTTON)
PlaySound("Shot.wav",NULL,SND_ASYNC);
}
void motion(int x, int y)
{
angle[ID]+= x-oldX;
oldX=x;
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
myMP3.Load("123.mp3");
myMP3.Play();
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("GLUT Shapes");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
/// glutReshapeFunc(resize);
/// glutDisplayFunc(display);
/// glutKeyboardFunc(key);
/// glutIdleFunc(idle);
glClearColor(1,1,1,1);
/// glEnable(GL_CULL_FACE); /// glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
沒有留言:
張貼留言