Week16-1(不知道為甚麼這週上傳不了圖片)
這週要複習這學期交的程式
先開啟一個新的glut專案
輸入下列程式碼
#include <GL/glut.h>
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_POLYGON);
glVertex2f(0.5, 0.5);
glVertex2f(-0.5, 0.5);
glVertex2f(-0.5, -0.5);
glVertex2f(0.5, -0.5);
glEnd();
glColor3f(1,1,0);
glutSolidTeapot( 0.3 );
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week16");
glutDisplayFunc(display);
glutMainLoop();
}
會出現一個茶壺和黃色背景
然後第二個程式
#include <GL/glut.h>
float teapotX=0, teapotY=0;
void motion(int x, int y) {
teapotX = (x-150) / 150.0;
teapotY = (150-y) / 150.0;
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week16");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMainLoop();
}
就可以用滑鼠移動茶壺
第三個程式
#include <GL/glut.h>
float teapotX=0, teapotY=0, angle=0, oldX=0, oldY=0;
void mouse(int button, int state, int x, int y) {
oldX = x;
oldY = y;
}
void motion(int x, int y) {
angle += x - oldX; ///teapotX = (x-150) / 150.0;
oldX = x; ///teapotY = (150-y) / 150.0;
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(angle, 0, 0, 1); ///glTranslatef(teapotX, teapotY, 0);
glutSolidTeapot( 0.3 );
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week16");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
第五個程式
#include <GL/glut.h>
#include <stdio.h>
float teapotX=0, teapotY=0, angle=0, oldX=0, oldY=0;
void mouse(int button, int state, int x, int y) {
oldX = x;
oldY = y;
}
void motion(int x, int y) {
teapotX = (x-150) / 150.0;
teapotY = (150-y) / 150.0;
angle += x - oldX;
oldX = x;
printf("glTranslatef(%.3f , %.3f , 0 );\n", teapotX, teapotY);
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1,1,1);
glTranslatef(-0.420 , +0.047 , 0 );///glTranslatef(....);
glRotatef(angle, 0, 0, 1);
glTranslatef(0.420 , -0.047 , 0 );///glTranslatef(....);
glutSolidTeapot( 0.3 );
glPopMatrix();
glColor3f(0,1,0);
glutSolidTeapot( 0.01 );
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week16");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutMainLoop();
}
這個程式會在你轉動茶壺角度時在小黑視窗給你茶壺的座標位置
第六個程式
#include <GL/glut.h>
#include <stdio.h>
#include "glm.h"
GLMmodel * gundam = NULL;
float teapotX=0, teapotY=0, angle=0, oldX=0, oldY=0;
void mouse(int button, int state, int x, int y) {
oldX = x;
oldY = y;
}
void motion(int x, int y) {
teapotX = (x-150) / 150.0;
teapotY = (150-y) / 150.0;
angle += x - oldX;
oldX = x;
printf("glTranslatef(%.3f , %.3f , 0 );\n", teapotX, teapotY);
glutPostRedisplay();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1,0,0);
glScalef(0.03, 0.03, 0.03);
glmDraw(gundam, GLM_MATERIAL);
glPopMatrix();
glColor3f(0,1,0);
glutSolidTeapot( 0.01 );
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week16");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
gundam = glmReadOBJ("model/gundam.obj");
glutMainLoop();
}
沒有留言:
張貼留言