Opengl:what Is The Best Way To Get Color Of One Pixel From A 2D Texture?

Started by
12 comments, last by nickme 7 years, 9 months ago

hi

It just happened that either binding the png texture as 1d or 2d, it still worked. here are the codes that it generate a very interesting result after apply Nanoha's suggestions.

here is the codes:

void render()

{
GLdouble ds = 4.0f, x = 10.0;
if (once) {
once = false;
clear();
display_splash_screen(MenuID); glFlush();
Sleep(000);
clear();
glPointSize(ds);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, palID);
glBegin(GL_POINTS);
for (int c = 0; c < ss; x += ds, c++) {
glTexCoord2d((GLdouble)(c%ss) / (GLdouble)(ss-1), 0.0);
glVertex2d(x, 600.0);
}
glEnd();
glFlush();
x = 10.0;
glEnable(GL_TEXTURE_1D);
glBindTexture(GL_TEXTURE_1D, palID); // bind to 2d
glBegin(GL_POINTS);
for (GLint c = 0.0; c < ss; x += ds, c++) {
glTexCoord1d((GLdouble) (c%ss)/ (GLdouble) (ss-1));
glVertex2d(x, 700.0);
}
glEnd();
glFlush();
}
}
void init()
{
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_TEXTURE_2D);
buttonsID = LoadTexture2D("buttonsandwait.png");
MenuID = LoadTexture2D("fractals_screen.png");
glEnable(GL_TEXTURE_1D);
palID = LoadTexture2D("gradient1.png");
}

and here is the output:

the codes from the above generated the output in the left picture below.

why there are only one row of rainbow? seem like it read the png file row-wised, vertically. strange. when i rotated the png file in photoshop to 90 degree clockwise, i got this output from the right:

Advertisement

why there are only one row of rainbow?


Because of this bit:


glTexCoord1d((GLdouble) (c%ss)/ (GLdouble) (ss-1));

The value 'ss' is what causes the repetition. It works in such away that it is saying 'Repeat this texture every ss number of pixels.' I am assuming ss stands for screen size? In that case it's going to repeat the texture once every screen worth of pixels which is why you are only seeing one repetition. To change this behaviour you just need to change the value there but since you can't without changing ss just put a new variable that defines the repetition.


const int gradientLength = 16; // in number of pixels
glTexCoord1d((GLdouble) (c%gradientLength)/ (GLdouble) (gradientLength-1));

As long as the value you provide is smaller than the ss value you should see the gradient be repeated. You could possibly do this differently if you setup texture wrapping for that texture (repeat).


const int gradientLength = 16; // in number of pixels
glTexCoord1d(((GLdouble)c)/gradientLength ));

I checked the opengl docs and textures are set to repeat by default so that might just work straight away.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.

hi Nanoha

I finally realized how to make a 2d texture read:

void render()
{
GLdouble ds = 4.0f, x = 10.0;
if (once) {
once = false;
clear();
clear();
glPointSize(ds);
// the gradient1.png is 16x16 pixels file. so ss = 16x16 = 256
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, palID);
glBegin(GL_POINTS);
for (int c = 0; c < ss; x += ds, c++) {
glTexCoord2d((GLdouble)(c%16)/15.0, (GLdouble) c/ss); // ss = 16x16 = 256
glVertex2d(x, 600.0);
}
glEnd();
x = 10.0;
glEnable(GL_TEXTURE_1D);
glBindTexture(GL_TEXTURE_1D, palID); // bind to 2d
glBegin(GL_POINTS);
for (GLint c = 0.0; c < ss; x += ds, c++) {
glTexCoord1d((GLdouble) (c%ss)/ (GLdouble) (ss-1));
glVertex2d(x, 700.0);
}
glEnd();
glFlush();
}
}
it repeated 16 times.
I don't know whether it is vs 2015 or windows 10, it appeared that sometimes the program was unstable. sometime it worked sometimes it did not.

hi Nanoha:

I did not read you post until this morning. thanks for the reply.

This topic is closed to new replies.

Advertisement