week16
為防止當機,所以先下載git
把2023graphicsb放在桌面上
開心專案,命名為week16_all
撰寫程式
#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) {
//teapotX = (x-150)/150.0;
angle += x - oldX;//teapotY = (150-y)/150.0;
oldX = x;
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 <stdio.h>
#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) {
teapotX = (x-150)/150.0;
teapotY = (150-y)/150.0;
angle += x - oldX;
oldX = x;
glutPostRedisplay();
printf(" glTranslatef( %.2f, %.2f, 0 ); \n", teapotX, teapotY );
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(1,1,1);
//glTranslatef( 0.42, -0.06, 0 );
glRotatef(angle, 0, 0, 1);
glTranslatef( 0.42, -0.06, 0 );//glTranslatef(teapotX, teapotY, 0);
glutSolidTeapot( 0.3 );
glPopMatrix();
glColor3f(0,1,0);
glutSolidTeapot( 0.02 );
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 <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可
#include <opencv/cv.h>
#include <stdio.h>
#include <GL/glut.h>
#include "glm.h"
GLMmodel * head = NULL;
GLMmodel * body = NULL; ///GLMmodel * gundam = NULL;
float teapotX = 0, teapotY = 0, angle = 0, oldX = 0, oldY = 0;
int myTexture(char * filename)
{
IplImage * img = cvLoadImage(filename); ///OpenCV讀圖
cvCvtColor(img,img, CV_BGR2RGB); ///OpenCV轉色彩 (需要cv.h)
glEnable(GL_TEXTURE_2D); ///1. 開啟貼圖功能
GLuint id; ///準備一個 unsigned int 整數, 叫 貼圖ID
glGenTextures(1, &id); /// 產生Generate 貼圖ID
glBindTexture(GL_TEXTURE_2D, id); ///綁定bind 貼圖ID
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖T, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); /// 貼圖參數, 超過包裝的範圖S, 就重覆貼圖
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /// 貼圖參數, 放大時的內插, 用最近點
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); /// 貼圖參數, 縮小時的內插, 用最近點
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, img->width, img->height, 0, GL_RGB, GL_UNSIGNED_BYTE, img->imageData);
return id;
}
void mouse(int button, int state, int x, int y) {
oldX = x;
oldY = y;
}
void motion(int x, int y) {
teapotX += (x - oldX) / 150.0 * 10; ///teapotX = (x-150)/150.0;
teapotY += (oldY - y) / 150.0 * 10; ///teapotY = (150-y)/150.0;
angle += x - oldX;
oldX = x;
oldY = y;
glutPostRedisplay();
printf(" glTranslatef( %.2f, %.2f, 0 ); \n", teapotX, teapotY );
}
void display() {
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glScalef(1.6, 1.6, 1.6);
glTranslatef(0, -0.5, 0);///往下一半
glPushMatrix();
glColor3f(1,1,1);
glScalef(0.04, 0.04, 0.04);
///glRotatef(angle, 0, 1, 0);
glmDraw(body, GLM_MATERIAL|GLM_TEXTURE);///glmDraw(gundam, GLM_MATERIAL|GLM_TEXTURE);
glPushMatrix();
glTranslatef(teapotX, teapotY, 0);
glmDraw(head, GLM_MATERIAL|GLM_TEXTURE);
glPopMatrix();
glPopMatrix();
glColor3f(0,1,0);
glutSolidTeapot( 0.02 );
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_DEPTH);
glutCreateWindow("week16");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
head = glmReadOBJ("model/head.obj");
body = glmReadOBJ("model/body.obj"); ///gundam = glmReadOBJ("model/Gundam.obj");
myTexture("model/Diffuse.jpg");
glEnable(GL_DEPTH_TEST);
glutMainLoop();
}
沒有留言:
張貼留言