Console menu updating OpenGL window - c++

I am making an application that does some custom image processing. The program will be driven by a simple menu in the console. The user will input the filename of an image, and that image will be displayed using openGL in a window. When the user selects some processing to be done to the image, the processing is done, and the openGL window should redraw the image.
My problem is that my image is never drawn to the window, instead the window is always black. I think it may have to do with the way I am organizing the threads in my program. The main execution thread handles the menu input/output and the image processing and makes calls to the Display method, while a second thread runs the openGL mainloop.
Here is my main code:
#include <iostream>
#include <GL/glut.h>
#include "ImageProcessor.h"
#include "BitmapImage.h"
using namespace std;
DWORD WINAPI openglThread( LPVOID param );
void InitGL();
void Reshape( GLint newWidth, GLint newHeight );
void Display( void );
BitmapImage* b;
ImageProcessor ip;
int main( int argc, char *argv[] ) {
DWORD threadID;
b = new BitmapImage();
CreateThread( 0, 0, openglThread, NULL, 0, &threadID );
while( true ) {
char choice;
string path = "TestImages\\";
string filename;
cout << "Enter filename: ";
cin >> filename;
path += filename;
b = new BitmapImage( path );
Display();
cout << "1) Invert" << endl;
cout << "2) Line Thin" << endl;
cout << "Enter choice: ";
cin >> choice;
if( choice == '1' ) {
ip.InvertColour( *b );
}
else {
ip.LineThinning( *b );
}
Display();
}
return 0;
}
void InitGL() {
int argc = 1;
char* argv[1];
argv[0] = new char[20];
strcpy( argv[0], "main" );
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowPosition( 0, 0 );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "ICIP Program - Character recognition using line thinning, Hilbert curve, and wavelet approximation" );
glutDisplayFunc( Display );
glutReshapeFunc( Reshape );
glClearColor(0.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
}
void Reshape( GLint newWidth, GLint newHeight ) {
/* Reset viewport and projection parameters */
glViewport( 0, 0, newWidth, newHeight );
}
void Display( void ) {
glClear (GL_COLOR_BUFFER_BIT); // Clear display window.
b->Draw();
glutSwapBuffers();
}
DWORD WINAPI openglThread( LPVOID param ) {
InitGL();
glutMainLoop();
return 0;
}
Here is my draw method for BitmapImage:
void BitmapImage::Draw() {
cout << "Drawing" << endl;
if( _loaded ) {
glBegin( GL_POINTS );
for( unsigned int i = 0; i < _height * _width; i++ ) {
glColor3f( _bitmap_image[i*3] / 255.0, _bitmap_image[i*3+1] / 255.0, _bitmap_image[i*3+2] / 255.0 );
// invert the y-axis while drawing
glVertex2i( i % _width, _height - (i / _width) );
}
glEnd();
}
}
Any ideas as to the problem?
Edit: The problem was technically solved by starting a glutTimer from the openglThread which calls glutPostRedisplay() every 500ms. This is OK for now, but I would prefer a solution in which I only have to redisplay every time I make changes to the bitmap (to save on processing time) and one in which I don't have to run another thread (the timer is another thread im assuming). This is mainly because the main processing thread is going to be doing a lot of intensive work and I would like to dedicate most of the resources to this thread rather than anything else.

I've had this problem before - it's pretty annoying. The problem is that all of your OpenGL calls must be done in the thread where you started the OpenGL context. So when you want your main (input) thread to change something in the OpenGL thread, you need to somehow signal to the thread that it needs to do stuff (set a flag or something).
Note: I don't know what your BitmapImage loading function (here, your constructor) does, but it probably has some OpenGL calls in it. The above applies to that too! So you'll need to signal to the other thread to create a BitmapImage for you, or at least to do the OpenGL-related part of creating the bitmap.

A few points:
Generally, if you're going the multithreaded route, it's preferable if your main thread is your GUI thread i.e. it does minimal tasks keeping the GUI responsive. In your case, I would recommend moving the intensive image processing tasks into a thread and doing the OpenGL rendering in your main thread.
For drawing your image, you're using vertices instead of a textured quad. Unless you have a very good reason, it's much faster to use a single textured quad (the processed image being the texture). Check out glTexImage2D and glTexSubImage2D.
Rendering at a framerate of 2fps (500ms, as you mentioned) will have negligible impact on resources if you're using an OpenGL implementation that is accelerated, which is almost guaranteed on any modern system, and if you use a textured quad instead of a vertex per pixel.

Your problem may be in Display() at the line
b->Draw();
I don't see where b is passed into the scope of Display().

You need to make OpenGL calls on the thread in which context was created (glutInitDisplayMode). Hence glXX calls inside Display method which is on different thread will not be defined. You can see this easily by dumping the function address, hopefully it would be undefined or NULL.

It sounds like the 500ms timer is calling Display() regularly, after 2 calls it fills the back-buffer and the front-buffer with the same rendering. Display() continues to be called until the user enters something, which the OpenGL thread never knows about, but, since, global variable b is now different, the thread blindly uses that in Display().
So how about doing what Jesse Beder says and use a global int, call it flag, to flag when the user entered something. For example:
set flag = 1; after you do the b = new BitmapImage( path );
then set flag = 0; after you call Display() from the OpenGL thread.
You loop on the timer, but, now check if flag = 1. You only need call glutPostRedisplay() when flag = 1, i.e. the user entered something.
Seems like a good way without using a sleep/wake mechanism. Accessing global variables among more than one thread can also be unsafe. I think the worst that can happen here is the OpenGL thread miss-reads flag = 0 when it should read flag = 1. It should then catch it after no more than a few iterations. If you get strange behavior go to synchronization.
With the code you show, you call Display() twice in main(). Actually, main() doesn't even need to call Display(), the OpenGL thread does it.

Related

C++ opengl intersecting glScissor

A Project I am working on involves me using glScissor, in some cases i need to perform a scissor on an area twice (or more), with the goal of only rendering what is within both scissor boxes.
The issue im running into is that the second scissor box just overrides the previous one, meaning only the last box set is used instead of both.
I have tried existing solutions such as setting scissor1, push matrix, enable scissor_test, set scissor2, disable scissor_test, popmatrix, disable scissor_test. As proposed here: glScissor() call inside another glScissor()
I could not get these to produce any difference, I had also tried glPushAttrib instead of matrix but still no difference.
Here is an example program I wrote for scissor testing, its compiled by g++ and uses freeglut, the scissoring takes place in display():
/*
Compile: g++ .\scissor.cpp -lglu32 -lfreeglut -lopengl32
*/
#include <GL/gl.h>//standard from mingw, already in glut.h - header library
#include <GL/glu.h>//standard from mingw, already in glut.h - utility library
#include <GL/glut.h>//glut/freeglut - more utilities, utility tool kit
void display();
void reshape(int, int);
void timer(int);
void init(){
glClearColor(0, 0, 0, 1);
}
int main(int argc, char **argv){
glutInit(&argc, argv);//init glut
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);//init display mode, add double buffer mode
//init window
glutInitWindowPosition(200, 100);//if not specified, it will display in a random spot
glutInitWindowSize(500, 500);//size
//create window
glutCreateWindow("Window 1");
//give glut a function pointer so it can call that function later
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);//call certain function after a specified amount of time
init();
glutMainLoop();//once this loop runs your program has started running, when the loop ends the program terminates
}
float xPos = -10;
int state = 1;//1 = right, -1 = left
//our rendering happens here
void display(){
//clear previous frame
glClear(GL_COLOR_BUFFER_BIT);//pass in flag of frame buffer
//draw next frame below
glLoadIdentity();//reset rotations, transformations, ect. (resets coordinate system)
//we are using a model view matrix by default
//TEST
glEnable(GL_SCISSOR_TEST);
glScissor(0, 0, 100, 1000);
glPushMatrix();
glEnable(GL_SCISSOR_TEST);
glScissor(50, 0, 1000, 1000);
//assuming both scissors intersect, we should only see the square between 50 and 100 pixels
//draw
glBegin(GL_QUADS);//every set of 3 verticies is a triangle
//GL_TRIANGLES = 3 points
//GL_QUADS = 4 points
//GL_POLYGON = any amount of points
glVertex2f(xPos, 1);//the 2 is the amount of args we pass in, the f means theyr floats
glVertex2f(xPos, -1);
glVertex2f(xPos+2, -1);
glVertex2f(xPos+2, 1);
glEnd();//tell opengl your done drawing verticies
glDisable(GL_SCISSOR_TEST);
glPopMatrix();
glDisable(GL_SCISSOR_TEST);
//display frame buffer on screen
//glFlush();
glutSwapBuffers();//if double buffering, call swap buffers instead of flush
}
//gets called when window is reshaped
void reshape(int width, int hight){
//set viewport and projection
//viewport is a rectangle where everything is drawn, like its the window
glViewport(0, 0, width, hight);
//matrix modes: there is model view and projection, projection has depth
glMatrixMode(GL_PROJECTION);
glLoadIdentity();//reset current matrix after changing matrix mode
gluOrtho2D(-10, 10, -10, 10);//specify 2d projection, set opengl's coordinate system
glMatrixMode(GL_MODELVIEW);//change back to model view
}
//this like makes a loop
void timer(int a){
glutPostRedisplay();//opengl will call the display function the next time it gets the chance
glutTimerFunc(1000/60, timer, 0);
//update positions and stuff
//this can be done here or in the display function
switch(state){
case 1:
if(xPos < 8)
xPos += 0.15;
else
state = -1;
break;
case -1:
if(xPos > -10)
xPos -= 0.15;
else
state = 1;
break;
}
}
I tried following example solutions, such as push/pop matrix/attrib, but couldnt get anything to work
There is no first or second scissor box. There is just the scissor box. You can change the scissor box and that change will affect subsequent rendering. But at any one time, there is only one.
What you want is to use the stencil buffer to discard fragments outside of an area defined by rendering certain values into the stencil buffer.

GL_INVALID_OPERATION error 1218 (0x0502) when using simple OpenGL commands glvertex2i, glColor3ub

This is my first time using OpenGL, so this may be a stupid question, but my code is pulling a GL_INVALID_OPERATION error (1218 or 0x0502) between glBegin() and glEnd(). I'm only calling a couple simple commands but glGetError() returns 0 at the start of this function, and 1218 at the end.
This function is intended to be used to draw a single instance of any simple shape, and after stepping through in the debugger, the variables are what I intended them to be. Also note that, despite this being a stupid question, I spent several hours last night and several hours today searching Google and my textbook for possible solutions, and have found nothing helpful due to the vague nature of OpenGL errors.
void drawShape(GLenum type, int numPoints, GLubyte r, GLubyte g, GLubyte b, vertex vertices, ...) {
glBegin(type);
glColor3ub(r, g, b);
// iterate through vertices
va_list vertexList;
va_start(vertexList, vertices); // use vertexList for iteration
for (int i = 0; i < numPoints; ++i) {
vertex point = i == 0 ? vertices : va_arg(vertexList, vertex);
// add a new point at (x, y)
glVertex2i(std::get<0>(point), std::get<1>(point));
}
glEnd();
}
This function is called as follows:
drawShape(GL_LINE, 2, 0, 0, 0, vertex{ 10, 10 }, vertex{ 400, 10 });
And the context I'm using is FreeGLUT which is initialized as follows:
// gross workaround for calling init with no arguments
int argc = 1;
char *argv[1] = { (char*)"" };
glutInitContextVersion(4, 1);
glutInit(&argc, argv);
// RGB window, double buffer
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
// sets values necessary for window initialization
glutInitWindowPosition(INIT_X, INIT_Y);
glutInitWindowSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// draw the window and create the scene
glutCreateWindow(WORKING_WINDOW_TITLE);
glClearColor(1, 1, 1, 1); // set clear color to white
glutDisplayFunc(render);
glutMainLoop();
The problem lies in the call to glBegin itself. GL_LINE is not a valid parameter for this function. Due to this, all commands after that are not called inside a valid glBegin/glEnd block.
If you want to draw lines, the correct parameter is GL_LINES.
The first step when debugging an OpenGL application that doesn't support a debug context should always be to identify which function call produces the first error. This can, for example, be done by calling glGetError() after each OpenGL call until the problematic line is found. Also note, that glError returns errors in arbitrary order if more than one error happened.

c++ program and scope variables [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I compiled this c++ program with dev-c++ and give "was not declared in this scope" for all variables.
#include <cstdlib> // standard definitions
#include <iostream> // C++ I/O
#include <cstdio> // C I/O (for sprintf)
#include <cmath> // standard definitions
#include <GL/glut.h> // GLUT
#include <GL/glu.h> // GLU
#include <GL/gl.h> // OpenGL
using namespace std; // make std accessible
//-----------------------------------------------------------------------
// Global data
//-----------------------------------------------------------------------
GLint TIMER_DELAY = 10000; // timer delay (10 seconds)
GLfloat RED_RGB[] = {1.0, 0.0, 0.0}; // drawing colors
GLfloat BLUE_RGB[] = {0.0, 0.0, 1.0};
//-----------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------
static bool isReversed = false; // draw reversed colors?
//-----------------------------------------------------------------------
// Callbacks
// The global variable "isReversed" describes the drawing state.
// When false, a blue rectangle is drawn on top of red diamond.
// When true the colors are reversed. The "isReversed" variable is
// complemented whenever the left mouse button is clicked or the
// timer goes off (every 10 seconds).
//-----------------------------------------------------------------------
void myReshape(int w, int h) {
cout << "MyReshape called width=" << w << " height=" << h << endl;
glViewport (0, 0, w, h); // update the viewport
glMatrixMode(GL_PROJECTION); // update projection
glLoadIdentity();
gluOrtho2D(0.0, 1.0, 0.0, 1.0); // map unit square to viewport
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay(); // request redisplay
}
// draw diamond and rectangle
void drawObjects(GLfloat* diamColor, GLfloat* rectColor) {
glColor3fv(diamColor); // set diamond color
glBegin(GL_POLYGON); // draw the diamond
glVertex2f(0.90, 0.50);
glVertex2f(0.50, 0.90);
glVertex2f(0.10, 0.50);
glVertex2f(0.50, 0.10);
glEnd();
glColor3fv(rectColor); // set rectangle color
glRectf(0.25, 0.25, 0.75, 0.75); // draw the rectangle
}
void myDisplay(void) { // display callback
cout << "MyDisplay called" << endl;
glClearColor(0.5, 0.5, 0.5, 1.0); // background is gray
glClear(GL_COLOR_BUFFER_BIT); // clear the window
if (isReversed) // draw the objects
drawObjects(BLUE_RGB, RED_RGB);
else
drawObjects(RED_RGB, BLUE_RGB);
glutSwapBuffers(); // swap buffers
}
void myTimer(int id) { // timer callback
cout << "Timer just went off. Reversing colors." << endl;
isReversed = !isReversed; // reverse drawing colors
glutPostRedisplay(); // request redraw
glutTimerFunc(TIMER_DELAY, myTimer, 0); // reset timer for 10 seconds
}
void myMouse(int b, int s, int x, int y) { // mouse click callback
if (s == GLUT_DOWN) {
cout << "Mouse click detected at coordinates x="
<< x << " and y=" << y << endl;
if (b == GLUT_LEFT_BUTTON) {
isReversed = !isReversed;
cout << "Left mouse click. Reversing colors." << endl;
glutPostRedisplay();
}
}
}
// keyboard callback
void myKeyboard(unsigned char c, int x, int y) {
switch (c) { // c is the key that is hit
case 'q': // 'q' means quit
exit(0);
break;
default:
cout << "Hit q to quit. All other characters ignored" << endl;
break;
}
}
//-----------------------------------------------------------------------
// Main program
// This does all the set up for the program. It creates the game
// and then passes control to glut.
//-----------------------------------------------------------------------
int main(int argc, char** argv)
{
cout <<
"Colors swap every 10 seconds.\n"
"Click left mouse button to swap colors.\n" <<
"Try resizing and covering/uncovering the window.\n" <<
"Hit q to quit." << endl;
glutInit(&argc, argv); // OpenGL initializations
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);// double buffering and RGB
glutInitWindowSize(400, 400); // create a 400x400 window
glutInitWindowPosition(0, 0); // ...in the upper left
glutCreateWindow(argv[0]); // create the window
glutDisplayFunc(myDisplay); // setup callbacks
glutReshapeFunc(myReshape);
glutMouseFunc(myMouse);
glutKeyboardFunc(myKeyboard);
glutTimerFunc(TIMER_DELAY, myTimer, 0);
glutMainLoop(); // start it running
return 0; // ANSI C expects this
}
Where is the problem?
[Error] 'glutPostRedisplay' was not declared in this scope
[Error] 'glutSwapBuffers' was not declared in this scope
[Error] 'glutPostRedisplay' was not declared in this scope
[Error] 'glutTimerFunc' was not declared in this scope
[Error] 'GLUT_DOWN' was not declared in this scope
[Error] 'GLUT_LEFT_BUTTON' was not declared in this scope
[Error] 'glutPostRedisplay' was not declared in this scope
etc.
From the GLUT docs here:
The header files for GLUT should be included in GLUT programs with the following include directive (which you have):
#include <GL/glut.h>
Because a very large window system software vendor (who will remain nameless) has an apparent inability to appreciate that OpenGL's API is independent of their window system API, portable ANSI C GLUT programs should not directly include <GL/gl.h> or <GL/glu.h>. Instead, ANSI C GLUT programs should rely on <GL/glut.h> to include the necessary OpenGL and GLU related header files.
If you are not on windows, this may be causing an issue.
It does look like this file is not being included correctly since even the symbolic constants (e.g. for GLUT_DOWN) are not being resolved.
You should look for the header files that declare these functions and variables and include them in this source file.
A good place to start would be this GL directory you seem to be including glut.h from, assuming it exists.
Check building without calling functions in main(). Then try one by one. I think the error is coming from one of your include files.
Are there any classes defined in those files. Check there header files are ok or not. Check for the existence of header files in specified paths.
Then check the namespaces. If you defined a class with a namespace, don't forget to use using namespace namespace_name or specify full qualified name for variables.

C++ OpenGL Pong ball moving too fast

Im trying to create a simple pong game in C++ using opengl. I have the borders displaying on the screen, the paddles, the ball, and all of them move, so that's great! The problem is that the ball moves lightning fast even at one pixel of speed.
Im updating it's position in a call-back function called init which I then pass into glutIdleFunc like so: glutIdleFunc(idle);
the idle function is as follows:
void idle(){
ball.moveLeft();
glutPostRedisplay();
}
essentially im just having it move left by one pixel but, I guess that idle gets called a lot so it moves lightning fast. How do I fix this error?
If there's more information you need just ask!
Use a GLUT timer to kick your display() callback every 16 milliseconds:
void timer( int extra )
{
glutPostRedisplay();
glutTimerFunc( 16, timer, 0 );
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( ... );
glutInitWindowSize( ... );
glutCreateWindow( ... );
...
glutTimerFunc( 0, timer, 0 );
...
glutMainLoop();
return 0;
}
Here's a link I found to a blog that talks about how to get the time in glut to display frames per second.
http://www.lighthouse3d.com/tutorials/glut-tutorial/frames-per-second/
You need to decide what the velocity of your ball is in pixels/second. Then multiply that velocity by the number of seconds that have elapsed between your last update & the current update. According to the blog, you can get this via glutGet(GLUT_ELAPSED_TIME). If that doesn't work, google for how to find the current time in milliseconds on your platform.
Hope this helps.

OpenGL: always same color

I'm writting a program on windows using c++, opengl 2.1 and SDL and am having some issues with the vertex colors.
I'm using glColor3f to set the color for each vertex set, but it seems to not be working. I get every vertex drawn red no matter what color I pick. I checked the values being passed to glColor3f and they are indeed not 1.f,0.f,0.f...
Has anyone ever encountered such a problem, or knows what might be causing it? Am I maybe not including some lib I should? Or do you reckon it might be some issue with the SDL initialization code (it shouldn't be, as I've used it before correctly)?
EDIT4: Solved.. it was indeed a gpu issue, I got the drivers from the manufacturer and it is now working properly... go figure
EDIT: I'm also not using any lighting, textures or anything of the sorts. Graphically I just set up the window and tell opengl some vertices and colors to draw..
EDIT2: Sure, but I highly doubt there's any issue there:
int GLmain::redraw(GLvoid)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_LINE_STRIP);
for(int i=0; i<s->divs.size(); i++){
vec3 v = s->divs.at(i).getPosition();
vec3 c = s->divs.at(i).getColor();
glColor3f(c.get(0),c.get(1),c.get(2));
glVertex3f(v.get(0),v.get(1),v.get(2));
}
glEnd();
return TRUE;
}
As you can see, pretty standard stuff.. c holds values between 0.0-1.0.
I just tried running some other work I had done with OpenGL and everything is showing up red as well (it wasn't before), so I'm guessing it has something to do with the libs I'm using:
opengl32.lib
sdl.lib
sdlmain.lib
glu32.lib
SDL is version 1.2.14. Also, could it be a problem with my gpu? Everything else shows up with normal colors though.. web browser, videos, games, etc.
EDIT3: SDL initialization code:
int done = 0;
int w = 800;
int h = 600;
GLenum gl_error;
char* sdl_error;
SDL_Event event;
Init_OpenGL(argc, argv, w, h); // start window and OpenGL
SDL_WM_SetCaption( "MySlinky", "" ); /* Set the window manager title bar */
void Init_OpenGL(int argc, char* argv[], int w, int h)
{
int rgb_size[3];
int bpp = 0;
Uint32 video_flags = SDL_OPENGL;
if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
fprintf(stderr,"Couldn't initialize SDL: %s\n",SDL_GetError());
exit( 1 );
}
/* See if we should detect the display depth */
if ( bpp == 0 ) {
if ( SDL_GetVideoInfo()->vfmt->BitsPerPixel <= 8 ) {
bpp = 8;
} else {
bpp = 16; /* More doesn't seem to work */
}
}
/* Initialize the display */
switch (bpp) {
case 8:
rgb_size[0] = 3;
rgb_size[1] = 3;
rgb_size[2] = 2;
break;
case 15:
case 16:
rgb_size[0] = 5;
rgb_size[1] = 5;
rgb_size[2] = 5;
break;
default:
rgb_size[0] = 8;
rgb_size[1] = 8;
rgb_size[2] = 8;
break;
}
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, rgb_size[0] ); // Sets bits per channel
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, rgb_size[1] );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, rgb_size[2] );
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 24 ); // 3 channels per pixel (R, G and B)
SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
SDL_GL_SetAttribute( SDL_GL_ACCELERATED_VISUAL, 1 );
if ( SDL_SetVideoMode( w, h, bpp, video_flags ) == NULL ) {
fprintf(stderr, "Couldn't set GL mode: %s\n", SDL_GetError());
SDL_Quit();
exit(1);
}
}
There are two possible causes:
Either you have enabled some render state that will eventually result in red vertices. E.g. textures, materials (!?), lighting and so on.
The code "around" your OpenGL calls has bugs (e.g.: are you really sure, that c.get(1) does not return 0?).
EDIT: Setup of the OpenGL rendering context failed or the context does not work as expected/intended! E.g. it does not have the expected properties, bit depths and so on.
To eliminate any doubt, please add the following line, and check its results: printf("c=(%f,%f,%f)",(float)c.get(0),(float)c.get(1),(float)c.get(2));
Can you try this :
glShadeModel( GL_SMOOTH );
and tell if the problem is fixed?
Put the call in the Init_OpenGL function.
Not sure if this applies to you, but all I needed was one call to glUseProgram(NULL);, to tell opengl to use the fixed function pipeline, and the problem was fixed.