WEEK12
week12-1
hello world
*fout = file out
讀檔案fscanf
把file.txt讀進來
*fin = file input
*fscanf是read
fscanf和fprintf的結合,加了fclose(fout)、fclose(fin)
=>fprintf完就結束->fscanf要在前面加 &
PRINTF_FPRINTF.CPP
#include <stdio.h>
int main(){
FILE*fout=fopen("file.txt","w"); ("檔名" "模式") 檔案指標fout
printf("Hello World\n");
fprintf(fout,"Hello World在檔案裡\n");
}
week12-2
#include <stdio.h>
int main(){
FILE*fin=fopen("file.txt","r");
char line[100];
fscanf(fin,"%s",line);
printf("從file.txt讀到了:%s\n",line);
fscanf(fin,"%s",line);
printf("從file.txt讀到了:%s\n",line);
}
week12-3
#include <stdio.h>
int main(){
int a[10]={10,20,30,40,50,60,70,80,90,100};
int b[10]={};
FILE*fout=fopen("file3.txt","w");
for(int i=0;i<10;i++){
printf("%d ",a[i]);
fprintf(fout, "%d ",a[i]);
}
printf("\n");
fprintf(fout, "\n");
fclose(fout);
FILE*fin=fopen("file3.txt","r");
for(int i=0;i<10;i++){
fscanf(fin,"%d",&b[i]);
printf("b%d:%d ",i,b[i]);
}
fclose(fin);
}
week12-4
#include <stdio.h>
#include <GL/glut.h>
float teapotX=0, teapot Y=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void mouse(int button,int state, int x, int y);
{
if(stage==GLUT_DOWN){
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
}
display();
}
int main(int argc,char**argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DPUBLE |GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutMainLoop();
}
week12-5
#include <stdio.h>
#include <GL/glut.h>
FILE* fout=NULL;
FILE* fin==NULL;
float teapotX=0, teapot Y=0;
void display(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glutSolidTeapot(0.3);
glutSwapBuffers();
}
void mouse(int button,int state, int x, int y);
{
if(stage==GLUT_DOWN){
teapotX=(x-150)/150.0;
teapotY=(150-y)/150.0;
if(fout==NULL)fout=fopen("file4.txt","w");
fclose(fout);
fin=fopen("file4.txt","r");
}
fscanf(fin,"%f %f", &teapotX,&teapotY);
display();
}
int main(int argc,char**argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_DPUBLE |GLUT_DEPTH);
glutCreateWindow("week12");
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFnction(keyboard);
glutMainLoop();
}
總程式:
| #include <GL/glut.h> | |
| #include "glm.h" | |
| GLMmodel * pmodel = NULL;///有一個模型的指標,先是空的 | |
| #include <opencv/highgui.h> ///使用 OpenCV 2.1 比較簡單, 只要用 High GUI 即可 | |
| #include <opencv/cv.h> | |
| #include <GL/glut.h> | |
| 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 display() | |
| { | |
| glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); | |
| if(pmodel == NULL){ | |
| pmodel = glmReadOBJ("Gundam.obj"); | |
| glmUnitize(pmodel); | |
| glmFacetNormals(pmodel); | |
| glmVertexNormals(pmodel,90); | |
| } | |
| glmDraw(pmodel,GLM_MATERIAL | GLM_TEXTURE); | |
| ///glutSolidTeapot(0.3); | |
| glutSwapBuffers(); | |
| } | |
| int main(int argc,char* argv[]) | |
| { | |
| glutInit(&argc,argv); | |
| glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH); | |
| glutCreateWindow("week10"); | |
| myTexture("Diffuse.jpg"); | |
| glutDisplayFunc(display); | |
| glutMainLoop(); | |
| } |
下禮拜
FINAL PROJECT
上傳git





沒有留言:
張貼留言