So i am writing a program using openGl on C++.I want to load an image and then display it in a NxN grid form. I loaded the image and stored its data in an array and then proceeded to use the following method to achieve my goal:
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);
}
}
This function opens the file containing the image data, loads the image in the pixels array at first and then at the paper array that i use in glTexImage2D. MyAtof() is a function i built to convert a string into a float. The data is properly passed from the file to the array, i've tested it. After ther fillGrid function the following function is called t do the repaint:
void drawGrid2(void)
{
for(int i=-240;i<240;i+=40){
for(int j=-300;j<=300;j+=40){
drawSquare2(i,j);
}
}
}
And also:
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 ();
}
Main:
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;
}
Other functions:
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();
}
}
Is this the correct way to create a texture and display an image with openGl?
The file compiles but the result isn't what i want. I wanted an 15x15 grid of squares, with each square containing the image. Before the repaint the result was this .
After the repaint the result is this .
I used different functions for the first repaint and the the second one. Since the first one is working i didnt post it.
OpenGL's default texture minification filter is GL_NEAREST_MIPMAP_LINEAR
. Your texture is not mipmap complete, so texturing will not work in this mode. You should set glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
(or GL_LINEAR
).
You also seem to try to set the texture maginification filter here:
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_REPEAT);
butt GL_REPEAT
is not a valid filter mode at all.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.