一、色彩系統
程式碼如下:
RGB模式:
size(255,255);colorMode(RGB,255);
for(int x=0;x<255;x++)
{
for(int y=0;y<255;y++)
{
stroke(x,y,255);
point(x,y);
}
}
HSB模式:
size(255,255);
colorMode(HSB,255);for(int x=0;x<255;x++)
{
for(int y=0;y<255;y++)
{
stroke(x,y,255);
point(x,y);
}
}
二、足球軌跡、等速茶壺
程式碼如下:
#include <opencv/highgui.h> ///for cvLoadImage()
#include <opencv/cv.h> ///for cvCvtColor()
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
float mat[]={1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1};
GLUquadric * quad;
GLuint idBall, idCanvas, idTeapot;
float ballSize=0.2;
float posX=0.5, posY=0.5, posWin=0.1;
int oldX=0, oldY=0;
void mouse(int button, int state, int x, int y)
{
oldX=x; oldY=y;
}
double dist(float x, float y){
return sqrt(x*x+y*y);
}
void motion(int x, int y){
int dx=x-oldX, dy=oldY-y;
oldX=x; oldY=y;
glPushMatrix();
glRotatef( dist(dx,dy)/2, dy, -dx, 0);
glMultMatrixf(mat);
glGetFloatv(GL_MODELVIEW_MATRIX, mat);
glPopMatrix();
glutPostRedisplay();
posX+=dx/500.0*posWin;
posY-=dy/500.0*posWin;
static FILE * fout=NULL;
if(fout==NULL)fout=fopen("trace.txt", "w+");
fprintf(fout, "%d %d\n", dx, dy);
static IplImage * img = NULL;
if(img==NULL){
img=cvLoadImage("imgCanvas.png"); ///OpenCV讀圖
}
int px=posX*img->width, py=posY*img->height, pdx=-(dx/200.0*posWin)*img->width, pdy=(dy/500.0*posWin)*img->height;
cvLine(img, cvPoint(px,py), cvPoint(px+pdx, py+pdy), cvScalar(255,0,0) );
glBindTexture(GL_TEXTURE_2D, idCanvas); ///綁定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);
}
void display()
{
glEnable(GL_DEPTH_TEST); ///要啟動 Detph Test 深度值的測試,3D顯示才正確
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glTranslatef(0, 0.5, 0);
glBindTexture(GL_TEXTURE_2D, idTeapot);
glutSolidTeapot(0.3);
glPopMatrix();
glPushMatrix();
glTranslatef(0,0, ballSize);
glNormal3f(0,0,-1);
glColor3f(1,1,1);
glBindTexture(GL_TEXTURE_2D, idCanvas);
glBegin(GL_POLYGON);
glTexCoord2f(posX-posWin,posY-posWin);glVertex2f(-1,+1);
glTexCoord2f(posX-posWin,posY+posWin);glVertex2f(-1,-1);
glTexCoord2f(posX+posWin,posY+posWin);glVertex2f(+1,-1);
glTexCoord2f(posX+posWin,posY-posWin);glVertex2f(+1,+1);
glEnd();
glPopMatrix();
glPushMatrix();
glMultMatrixf(mat);
glBindTexture(GL_TEXTURE_2D, idBall);
gluQuadricTexture(quad, 1);
gluQuadricOrientation(quad, GLU_OUTSIDE);
gluSphere(quad, ballSize, 30, 30);///glutSolidTeapot(0.3);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y){
if(key==27)exit(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 myLighting(){
GLfloat light_position[] = {0,0,-1,0};
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0,GL_POSITION, light_position);
}
int main(int argc, char**argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutCreateWindow("roach");
glutDisplayFunc(display);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
printf("Use mouse to roll the ball and to draw trace\n");
quad = gluNewQuadric();
idBall = myTexture("imgBall.png");///重點:一定不可在display()讀圖檔
idCanvas=myTexture("imgCanvas.png");///重點: 你要在main()只讀一次
idTeapot=myTexture("idTeapot.jpg");
myLighting();///時機點要在 glutMainLoop()之前, glutCreateWindow()之後
glutMainLoop();
}
沒有留言:
張貼留言