學習學姐的程式
程式碼:
//Drawing funciton
void draw(void)
{
FILE *f;
float w, h, c;
float r, g, b;
float i, j, x, y;
//Background color
glClearColor(0,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
//Read pixel file
f = fopen("minion.txt", "r");
//Read width, height, channel
fscanf(f, "%f %f %f", &w, &h, &c);
//Set point size
glPointSize(10.0f);
//Begin drawing
glBegin(GL_POINTS);
for(i=0; i<w; i++){
for(j=0; j<h; j++){
//Read r, g, b
fscanf(f, "%f %f %f", &r, &g, &b);
//Geometric transformation
x = -(i-(w/2.0))/(w/2.0);
y = (j-(h/2.0))/(h/2.0);
//Draw(in python, read as bgr)
glColor3f(b/255.0, g/255.0, r/255.0);
glVertex3f(y, x, 0);
}
}
//End drawing
glEnd();
fclose(f);
//Draw order
glutSwapBuffers();
}
//Main program
int main(int argc, char **argv)
{
glutInit(&argc, argv);
//Simple buffer
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
glutInitWindowPosition(50,25);
glutInitWindowSize(300, 300);
glutCreateWindow("minion");
//Call to the drawing function
glutDisplayFunc(draw);
glutMainLoop();
return 0;
}
*將minion.txt放進freeglut\bin中
建立3D圖案的程式
1. 下載3D Exploration並安裝
2. 上網任意下載一個3D圖形 (https://free3d.com/),並在3D Exploration中開啟
3. 再另存新檔為cpp Open GL code類型,下一部的Export Type要設成Sample APP
4. 開啟CodeBlocks建立OpenGL project,再將cpp檔加入
5. 嘗試執行程式,會發生錯誤
6. 刪掉老舊的程式
7. 再次執行就會成功了
沒有留言:
張貼留言