Jump to content

  • Log In with Google      Sign In   
  • Create Account

nickme

Member Since 07 Aug 2011
Offline Last Active Yesterday, 09:30 PM

Posts I've Made

In Topic: Opengl:why Glcolor4Fv() Does Not Change Color?

21 July 2016 - 09:00 AM

hi

 

per your suggestion, I disabled the texturing in the init() and it works.

 

thanks everyone.


In Topic: Opengl:why Glcolor4Fv() Does Not Change Color?

21 July 2016 - 08:55 AM

hi everyone.

 

here is my new codes

 

 #include "source.h"

 
GLuint tex;
const int sizeX = 640, sizeY = 480;
const GLint iw = 839, ih = 665;
const int l = 0, r = sizeX, b = sizeY, t = 0;
const int texture_width = 16, texture_height = 16;
const int col = 3, row = 8;
const int bw = 280, bh = 82;
const int mx = 1910, my = 1100, maxbuttons = 18;
const int ss = texture_width * texture_height;
 
const GLfloat White[4] = { 1.0, 1.0, 1.0, 1.0 }; // solid White 
const GLfloat Transparent_White[4] = { 1.0, 1.0, 1.0, 0.65f }; // ~ half White
const GLfloat Black[4] = { 0.0, 0.0, 0.0, 1.0 }; // Black
const GLfloat Blue[4] = { 0.0, 0.0, 1.0, 1.0 }; // Blue
const GLfloat Yellow[4] = { 1.0, 1.0, 0.0, 1.0 }; // Yellow
const GLfloat Green[4] = { 0.0, 1.0, 0.0, 1.0 }; // Green
const GLfloat Red[4] = { 1.0, 0.0, 0.0, 1.0 }; // Red
const GLfloat Cyan[4] = { 1.0, 0.0, 1.0, 1.0 }; // Cyan
 
typedef struct { double su, sv, eu, ev; int x, y, ix, iy; } Coordv;
 
Coordv V[maxbuttons+1];
GLuint MenuID, buttonsID, palID, ID2, ID3;
bool once = true;
 
void Msg(GLint x, GLint y, char *c, const GLfloat fg[4], const GLfloat bg[4]);
 
void clear()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
}
 
void cleanup() {
glDeleteTextures(1, &palID);
glDeleteTextures(1, &buttonsID);
glDeleteTextures(1, &MenuID);
}
 
void show_error_msg(const char* filename)
{
char buffer[140] = "File \"";
strcat_s(buffer, filename);
strcat_s(buffer, "\" not found.");
clear();
Msg(sizeX/2-100, sizeY/2, buffer, White, Red);
glFlush();
cleanup();
printf(" error in downloading file : [%s] \n\n", filename);
system("pause");
exit(1);
}
 
void LoadTexture2D(const char *filename)
{
GLuint textureID;
 
textureID = SOIL_load_OGL_texture(filename, SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID, SOIL_FLAG_TEXTURE_REPEATS);
if (0 == textureID) {
show_error_msg(filename);
}
return textureID;
}
 
 
GLuint LoadTexture1D(char * filename, int size)
{
GLuint texture;
GLubyte * data = new unsigned char[size * 3]; // texture data for color bar file
 
FILE * texture_file;
fopen_s(&texture_file, filename, "rb");
if (texture_file == NULL) {
show_error_msg(filename);
}
fread(data, 1, size * 3, texture_file);
fclose(texture_file);
 
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_1D, texture);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB, size * 3, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
 
delete[] data;
 
return texture;
}
 
void winReshapeFcn(GLint newWidth, GLint newHeight)
{ // doesnot allow window size change
glutReshapeWindow(sizeX, sizeY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(l, r, b, t);
glMatrixMode(GL_MODELVIEW);
}
 
 
void draw_box(GLint l, GLint b, GLint r, GLint t, const GLfloat c[4])
{
glColor4fv©;
glRecti(l, b, r, t); // draw the foreground color box
}
 
void Msg(GLint x, GLint y, char *c, const GLfloat fg[4], const GLfloat bg[4])
{
int wl = 0, strlenght = strlen©;
for (int i = 0; i <= strlenght; i++)
wl += glutBitmapWidth(GLUT_BITMAP_HELVETICA_18, c[i]); //height = 18
glColor4fv(bg);
glRectd(x - 10, y + 10, x + wl + 10, y - 18 - 4); // draw background
glColor4fv(fg);
glRasterPos2i(x, y);
for (int i = 0; i <= strlenght; i++)
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, c[i]);
}
 
void render()
{
GLdouble ds = 4.0f, x = 10.0;
 
if (once) {
once = false;
clear();
Msg(100, 100, "  this is a test ", White, Green);
draw_box(200, 300, 250, 200, Yellow); // draw background
draw_box(300, 300, 350, 200, Cyan); // draw background
 
glColor4fv(White);
glRectd(400, 200, 500, 250);
glFlush();
}
}
 
void init()
{
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
 
glEnable(GL_TEXTURE_2D);
}
 
void processNormalKeys(unsigned char key, int x, int y)
{
if (true || key == ' ' || key == 27) { //any key will quit
cleanup();
exit(0);
}
}
 
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(sizeX, sizeY);
glutCreateWindow("Testing Program");
 
init();
glutIgnoreKeyRepeat(1);
glutReshapeFunc(winReshapeFcn);
glutKeyboardFunc(processNormalKeys);
glutDisplayFunc(render);
glutMainLoop();
 
cleanup();
 
return 0;
}

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

17 July 2016 - 11:30 AM

hi Nanoha:

 

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


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

17 July 2016 - 11:24 AM

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.

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

16 July 2016 - 03:49 PM

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:


PARTNERS