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);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///在main中glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
///可能會導致GL_DEPTH變動,因此一定要寫GL_DEPTH_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();
}
✤注意:如果在display函式中開檔讀檔,則每次loop執行都會浪費時間
所以盡量不要在display中開檔讀檔!
#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);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
///在main中glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE| GLUT_DEPTH);
///可能會導致GL_DEPTH變動,因此一定要寫GL_DEPTH_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();
}
✤注意:如果在display函式中開檔讀檔,則每次loop執行都會浪費時間
所以盡量不要在display中開檔讀檔!



沒有留言:
張貼留言