使用openGl显示pgm图像 所以我用C++上的OpenGL编写了一个程序,我想加载一个图像,然后以NXN网格的形式显示它。 我加载了图像并将其数据存储在一个数组中,然后继续使用以下方法来实现我的目标: void fillGrid(){ ifstream myfile("paper.pgm"); ofstream otherfile; otherfile.open("test.txt"); string line; string buffer; fstream afile; if (myfile.is_open()) { int counter=0; while (getline(myfile, line)) { if(counter>2){ buffer=buffer+line; } counter++; } pixels=new float[1600]; int i=0; string delimiters = " ,"; size_t current; size_t next = -1; do { current = next + 1; next = buffer.find_first_of( delimiters, current ); if(i<=1600){ pixels[i]=myAtof (buffer.substr(current)); paper[i]=pixels[i]; } i++; } while (next != string::npos); for(int j=0;j<=1600;j++){ otherfile<<paper[j]<<" "<< j<<endl; } glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 140, 140, 0, GL_RGB, GL_FLOAT, paper); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT); } }
主要内容: 其他职能:使用openGl显示pgm图像 所以我用C++上的OpenGL编写了一个程序,我想加载一个图像,然后以NXN网格的形式显示它。 我加载了图像并将其数据存储在一个数组中,然后继续使用以下方法来实现我的目标: void fillGrid(){ ifstream myfile("paper.pgm"); ofstream otherfile; otherfile.open("test.txt"); string line; string buffer; fstream afile; if (myfile.is_open()) { int counter=0; while (getline(myfile, line)) { if(counter>2){ buffer=buffer+line; } counter++; } pixels=new float[1600]; int i=0; string delimiters = " ,"; size_t current; size_t next = -1; do { current = next + 1; next = buffer.find_first_of( delimiters, current ); if(i<=1600){ pixels[i]=myAtof (buffer.substr(current)); paper[i]=pixels[i]; } i++; } while (next != string::npos); for(int j=0;j<=1600;j++){ otherfile<<paper[j]<<" "<< j<<endl; } glEnable(GL_TEXTURE_2D); glEnable(GL_DEPTH_TEST); glBindTexture(GL_TEXTURE_2D, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 140, 140, 0, GL_RGB, GL_FLOAT, paper); glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT); } },c++,arrays,opengl,C++,Arrays,Opengl,主要内容: 其他职能: void init (void) { // glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white. glClearColor (0.0, 0.0, 0.0, 1.0);//black glClear (GL_COLOR_BUFFER_BIT); glLoadIdentity(); glMatrixMode (GL_PROJECT
void init (void)
{
// glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glClearColor (0.0, 0.0, 0.0, 1.0);//black
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D(-300,300,-300,300);//0,width,0,height
}
void processEscKey(unsigned char key, int x, int y)
{
if(key==27){
exit(0);
}
else if(key==98){
fillGrid();
drawGrid2();
}
}
这是使用openGl创建纹理和显示图像的正确方法吗
文件可以编译,但结果不是我想要的。
我想要一个15x15的正方形网格,每个正方形都包含图像。
在重新油漆之前,结果是
重新绘制后,结果为
我在第一次重绘和第二次重绘时使用了不同的函数。
由于第一个正在工作,我没有发布它。OpenGL的默认纹理缩小过滤器是
GL\u NEAREST\u MIPMAP\u LINEAR
。您的纹理未完成mipmap,因此在此模式下纹理将无法工作。您应该设置glTexParameteri(GL\u TEXTURE\u 2D、GL\u TEXTURE\u MIN\u FILTER、GL\u NEAREST)代码>(或GL\u LINEAR
)
您似乎还尝试在此处设置纹理maginification过滤器:
但是GL\u REPEAT
根本不是有效的过滤模式。是否可能是我在发送到glTexImage2d的数据中使用了错误的格式?首先感谢您的回答。我已经尝试了您所说的GL\u REPEAT、GL\u NEAREST和GL\u LINEAR,但它不起作用。我仍然在屏幕上得到相同的结果。问题:是否可能需要对float*纸张中的数据进行一些修改,以便GLTEXAGE2D正确使用?对于我来说,完全不清楚您正在使用float*纸张
数组做什么。像素阵列只有1600个条目,但140乘以140 RGB像素的imag需要19600*3个条目。
void drawSquare2(int x,int y)
{
glBindTexture(GL_TEXTURE_2D, 1);
glBegin(GL_QUADS); //Start drawing quad
glVertex2f(x,y); //first coordinate x y
glVertex2f(x+40,y); //second coordinate
glVertex2f(x+40,y+40); //third coordinate
glVertex2f(x,y+40); //last coordinate
glEnd(); //Stop drawing quads
glFlush ();
}
int main (int argc, char** argv)
{
glutInit (&argc, argv); // Initialize GLUT.
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // Set display mode.
glutInitWindowPosition (0, 0); // Set top-left display-window position.
glutInitWindowSize (600, 500); // Set display-window width and height.
glutCreateWindow ("Main"); // Create display window.
init (); // Execute initialization procedure.
glutDisplayFunc (display); // Send graphics to display window.
glutKeyboardFunc(processEscKey);
glutMainLoop (); // Display everything and wait.
return 0;
}
void init (void)
{
// glClearColor (1.0, 1.0, 1.0, 0.0); // Set display-window color to white.
glClearColor (0.0, 0.0, 0.0, 1.0);//black
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glMatrixMode (GL_PROJECTION); // Set projection parameters.
gluOrtho2D(-300,300,-300,300);//0,width,0,height
}
void processEscKey(unsigned char key, int x, int y)
{
if(key==27){
exit(0);
}
else if(key==98){
fillGrid();
drawGrid2();
}
}
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);