week 03 補充程式範例&Vertex Buffer/Array
step 1.學姊的小小兵程式範例
#include <GL/glut.h>
#include <stdio.h>
//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 | GL_DEPTH_BUFFER_BIT);
//Read pixel file //讀檔
//注意:minion.txt須放到freeglut\bin裡
f = fopen("minion.txt", "r");
//Read width, height, channel
fscanf(f, "%f %f %f", &w, &h, &c);
//Set point size
glPointSize(10.0f); //放大10倍
//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); //是b,g,r喔!
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;
}
step 2.足球的程式範例
1.下載3D exploration
2.到http://www.cmlab.csie.ntu.edu.tw/~jsyeh/3dcg10/網站下載圖並拉進3D exploration
3.選SampleAPP
4.建立OpenGL專案並把原本的main從.c檔改成.cpp檔
5.貼上3D足球的程式碼(把會當的地方改掉)
6.完成並執行






沒有留言:
張貼留言