invalid use of member ‘xx::x’ in static member function - c++

I want to apply selection operation in a 3d viewer (using qt-creator)
any.h file:
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include <stdio.h>
#include <GL/glut.h>
class any
{
public:
any();
int board[3][3];
void init(void);
static void drawSquares(GLenum mode);
static void processHits (GLint hits, GLuint buffer[]);
static void pickSquares(int button, int state, int x, int y);
static void display(void);
static void reshape(int w, int h);
};
any.ccp file :
#include "any.h"
any::any()
{
}
void any:: init(void)
{
int i, j;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j ++)
board[i][j] = 0;
glClearColor (0.0, 0.0, 0.0, 0.0);
}
void any:: drawSquares(GLenum mode)
{
GLuint i, j;
for (i = 0; i < 3; i++) {
if (mode == GL_SELECT)
glLoadName (i);
for (j = 0; j < 3; j ++) {
if (mode == GL_SELECT)
glPushName (j);
glColor3f ((GLfloat) i/3.0, (GLfloat) j/3.0,(GLfloat) board[i][j]/3.0);
glRecti (i, j, i+1, j+1);
if (mode == GL_SELECT)
glPopName ();
}
}
}
/* processHits prints out the contents of the
* selection array.
*/
void any:: processHits (GLint hits, GLuint buffer[])
{
unsigned int i, j;
GLuint ii, jj, names, *ptr;
printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;
for (i = 0; i < hits; i++) { /* for each hit */
names = *ptr;
printf (" number of names for this hit = %d\n", names);
ptr++;
printf(" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf(" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
printf (" names are ");
for (j = 0; j < names; j++) { /* for each name */
printf ("%d ", *ptr);
if (j == 0) /* set row and column */
ii = *ptr;
else if (j == 1)
jj = *ptr;
ptr++;
}
printf ("\n");
board[ii][jj] = (board[ii][jj] + 1) % 3;
}
}
#define BUFSIZE 512
void any:: pickSquares(int button, int state, int x, int y)
{
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];
if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)
return;
glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);
(void) glRenderMode (GL_SELECT);
glInitNames();
glPushName(0);
glMatrixMode (GL_PROJECTION);
glPushMatrix ();
glLoadIdentity ();
/* create 5x5 pixel picking region near cursor location */
gluPickMatrix ((GLdouble) x, (GLdouble) (viewport[3] - y),
5.0, 5.0, viewport);
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
drawSquares (GL_SELECT);
glMatrixMode (GL_PROJECTION);
glPopMatrix ();
glFlush ();
hits = glRenderMode (GL_RENDER);
processHits (hits, selectBuf);
glutPostRedisplay();
}
void any:: display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
drawSquares (GL_RENDER);
glFlush();
}
void any:: reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (0.0, 3.0, 0.0, 3.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
main file:
#include <any.h>
int main(int argc, char *argv[])
{
QGuiApplication app(argc, argv);
// glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (100, 100);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
any* an=new any();
an->init();
glutMouseFunc (an->pickSquares);
glutReshapeFunc (an->reshape);
glutDisplayFunc(an->display);
glutMainLoop();
return app.exec();
}
the error :
../untitled/any.cpp:60:24: error: invalid use of member ‘any::board’ in static member function
when I try to make board static it shows a lot of errors about :
26: undefined reference to `any::board'

Static function cannot access non-static variables
In your source code, compiler will flash an error i.e. illegal reference to non-static member any::board.
Hence, accessing non-static members from static method in C++ programming is not possible.
static method memories will be create once without creating an instance of class. so you cannot use of a non-static class members in a static method, because non-static members has no memory already in static methods.
Please define your methods as non-static or use of static members in your class.
Please check out this link.

board is an instance member. You can't use it in a static method like drawSquares. You either need to make board a static member too, or, more likely, make drawSquares non-static.

Related

C++ OpenGL Draw Pixel

I'm learning how to use OpenGL by doing an online course and the practice problem asks to create a function to draw pixels:
void SetPixel(int x, int y, char r, char g, char b){}
I know I could do something like this to draw one:
glBegin(GL_POINTS);
glColor3f(1.0,1.0,1.0);
glVertex2i(100,100);
glEnd();
However given the context of the problem it's not a valid solution.
The problem also asks to use the SetPixel function by calling it within a loop to randomly place pixels across the screen. I understand how to implement that, but it's the drawing the actual pixel I don't understand, especially within the context of the program given.
The program (using GLUT):
#include <stdlib.h> //- for exit()
#include <stdio.h> //- for sprintf()
#include <string.h> //- for memset()
#ifdef _WIN32
#include "libs/glut.h"
#include <windows.h>
#pragma comment(lib, "winmm.lib") //- not required but have it in anyway
#pragma comment(lib, "libs/glut32.lib")
#elif __APPLE__
#include <GLUT/glut.h>
#elif __unix__
#include <GL/glut.h>
#endif
#define FRAME_WIDE 1000
#define FRAME_HIGH 600
//====== Structs & typedefs =========
typedef unsigned char BYTE;
struct POINT2D {int x, y;};
//====== Global Variables ==========
BYTE pFrameL[FRAME_WIDE * FRAME_HIGH * 3];
BYTE pFrameR[FRAME_WIDE * FRAME_HIGH * 3];
int shade = 0;
POINT2D xypos = {0,0};
int stereo = 0;
int eyes = 10;
//===== Forward Declarations ========
void ClearScreen();
void DrawFrame();
void Interlace(BYTE* pL, BYTE* pR);
void BuildFrame(BYTE *pFrame, int view);
void OnIdle(void);
void OnDisplay(void);
void reshape(int w, int h);
////////////////////////////////////////////////////////
// Program Entry Poinr
////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
//-- setup GLUT --
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); //GLUT_3_2_CORE_PROFILE |
glutInitWindowSize(FRAME_WIDE, FRAME_HIGH);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
//--- set openGL state --
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_FLAT);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
//-- register call back functions --
glutIdleFunc(OnIdle);
glutDisplayFunc(OnDisplay);
glutReshapeFunc(reshape);
//-- run the program
glutMainLoop();
return 0;
}
////////////////////////////////////////////////////////
// Event Handers
////////////////////////////////////////////////////////
void OnIdle(void)
{
DrawFrame();
glutPostRedisplay();
}
void OnDisplay(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(0, 0);
glDrawPixels(FRAME_WIDE, FRAME_HIGH, GL_RGB,GL_UNSIGNED_BYTE, (GLubyte*)pFrameR);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble) w, 0.0, (GLdouble) h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
////////////////////////////////////////////////////////
// Utility Functions
////////////////////////////////////////////////////////
void ClearScreen()
{
memset(pFrameL, 0, FRAME_WIDE * FRAME_HIGH * 3);
memset(pFrameR, 0, FRAME_WIDE * FRAME_HIGH * 3);
}
void Interlace(BYTE* pL, BYTE* pR)
{
int rowlen = 3 * FRAME_WIDE;
for (int y = 0; y < FRAME_HIGH; y+=2)
{
for (int x = 0; x < rowlen; x++) *pR++ = *pL++;
pL += rowlen;
pR += rowlen;
}
}
void DrawFrame()
{
ClearScreen();
if (!stereo) BuildFrame(pFrameR, 0);
else {
BuildFrame(pFrameL, -eyes);
BuildFrame(pFrameR, +eyes);
Interlace((BYTE*)pFrameL, (BYTE*)pFrameR);
}
}
////////////////////////////////////////////////////////
// Drawing Function
////////////////////////////////////////////////////////
void BuildFrame(BYTE *pFrame, int view)
{
// create a loop in here that uses SetPixel function to randomly place 10,000 pixels
}
void SetPixel(int x, int y, char r, char g, char b)
{
}
Can someone please help me work this out? How would I implement the SetPixel function elegantly?
Based on your configurations for the pixel format, the red component of the pixel at (x, y) is at index 3 * (y * width + x), and +1 / 2 for green / blue:
void SetPixel(int x, int y, char r, char g, char b)
{
if (x < 0 || x >= FRAME_WIDE || y < 0 || y >= FRAME_HIGH)
return; // out of bounds
int index = 3 * (y * FRAME_WIDE + x);
pFrame[index] = r;
pFrame[index+1] = g;
pFrame[index+2] = b;
}
Note that it is unclear whether SetPixel should write both buffers or there should be a flag indicating which one, so I just wrote pFrame as a placeholder.

I am trying to use structure in c++ but its not working

in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.

OpenGL Glut Drawing Points Not Working

Can someone help me out in checking my code? I'm trying to draw a simple dot by clicking in the window, but I can't see any points drawn. The point recording system is copied as an example from one of the stack overflow posts.
#include <stdlib.h>
#include <stdio.h>
//#include <GL\glew.h>
#include <gl/glut.h>
//#include <GL\freeglut.h>
#include <math.h>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include "draw.h"
//DRAW draw2;
class Point
{
int Xvalue, Yvalue;
std::string Text;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
void text(std::string text)
{
Text = text;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
//return text value
std::string rtext() { return Text; }
};
Point point[30];
int count = 0;
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
//glColor3f(0.0, 0.0, 0.0);
//glPointSize(5);
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}
void Display()
{
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glColor3f(1.0, 1.0, 1.0);
//glLoadIdentity();
/*Drawing here*/
//redraw
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
//draw2.Dot(x, y);
std::cout << "drawing dot " << x << " " << y << std::endl;
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f);
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
}
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
//draw2.Dot(x, y);
count++;
}
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv); //initialize toolkit
//Request double buffered true color window with Z-buffer
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB ); //display mode
glutInitWindowSize(1200, 800); //set window size
glutCreateWindow("NURBS Curve"); //open screen window
Init(); //additional initializations
//CALLBACK FUNCTIONS
glutMouseFunc(mouse);
glutDisplayFunc(Display); //send graphics to display window
/*
GLenum err = glewInit();
if (GLEW_OK != err)
{
fprintf(stderr, "GLEW error");
return 1;
}
*/
//pass control to glut for events, loops
glutMainLoop();
return 0;
}
glBegin(GL_POINTS);
glColor3f(0.3, 0.3, 0.3);
glPointSize(5.0f); // wat
glVertex2i(x, glutGet(GLUT_WINDOW_HEIGHT) - y);
glEnd();
There's a short list of GL functions you can call inside a glBegin()/glEnd() pair and glPointSize() is not on it:
Only a subset of GL commands can be used between glBegin and glEnd.
The commands are
glVertex,
glColor,
glSecondaryColor,
glIndex,
glNormal,
glFogCoord,
glTexCoord,
glMultiTexCoord,
glVertexAttrib,
glEvalCoord,
glEvalPoint,
glArrayElement,
glMaterial, and
glEdgeFlag.
Also,
it is acceptable to use
glCallList or
glCallLists to execute
display lists that include only the preceding commands.
If any other GL command is executed between glBegin and glEnd,
the error flag is set and the command is ignored.
Move the glPointSize() outside the glBegin()/glEnd() pair:
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
All together:
#include <gl/glut.h>
class Point
{
int Xvalue, Yvalue;
public:
void xy(int x, int y)
{
Xvalue = x;
Yvalue = y;
}
//return individual x y value
int x() { return Xvalue; }
int y() { return Yvalue; }
};
Point point[30];
int count = 0;
void Display()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); // clear display window
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
const double w = glutGet( GLUT_WINDOW_WIDTH );
const double h = glutGet( GLUT_WINDOW_HEIGHT );
gluOrtho2D(0.0, w, 0.0, h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPointSize(5.0f);
glBegin(GL_POINTS);
for (int i = 0; i < count; i++)
{
int x = point[i].x();
int y = point[i].y();
glColor3f(0.3, 0.3, 0.3);
glVertex2i(x, h - y);
}
glEnd();
glFlush();
}
void mouse(int button, int state, int x, int y)
{
if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[count].xy(x, y);
count++;
}
glutPostRedisplay();
}
int main( int argc, char *argv[] )
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize(1200, 800);
glutCreateWindow("NURBS Curve");
glutMouseFunc(mouse);
glutDisplayFunc(Display);
glutMainLoop();
return 0;
}
Found out that glcolor3f and glpointsize had to be initialized first in my Init() function.
void Init()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glColor3f(0.0, 0.0, 0.0); //this one
glPointSize(5); //and this one
glMatrixMode(GL_PROJECTION); //coordinate system
//glLoadIdentity();
gluOrtho2D(0.0, 1200.0, 0.0, 800.0);
}

opengl not drawing anything

Just trying to draw a point using glut and glew for opengl version 4.3
my code
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
is not rendering anything, can someone tell me what did i miss? here is full code:
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOS };
#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY 10 //ms
//general
long frameCount = 0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void timerEvent(int value)
{
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}
void init (void)
{
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, WIDTH, HEIGHT);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("The Abyss");
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);
if (glewInit()) //i guess this is true on failure
{
cerr << "Error initializing glew, Program aborted." << endl;
exit(EXIT_FAILURE);
}
//Init First
init();
//Init callback for Rendering
glutDisplayFunc(Renderer);
//Main Loop
glutMainLoop();
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27) :
exit(EXIT_SUCCESS);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
mouse_buttons |= 1<<button;
}
else if (state == GLUT_UP)
{
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
void motion(int x, int y)
{
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1)
{
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
{
translate_z += dy * 0.01f;
}
mouse_old_x = x;
mouse_old_y = y;
}
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
You have requested a Core context. You need to:
Specify a vertex and fragment shader. There are no freebies in Core.
Stop using deprecated functionality like glBegin() and glMatrixMode().
Start using VBOs to submit your geometry.
Start using glDrawArrays() and friends to draw your geometry.

potential bug openGL

I am attempting to tile an image on the screen using menus. My menu works-tiling in the appropriate way-properly until I move my mouse then the tiling design disappears?
#include <GL/glut.h>
#include <fstream>
#include <cstdlib>
#include "TUGL.h"
#include <glui.h>
//Global variables
class GLintPoint
{
public:
GLint x;
GLint y;
};
GLint numpoints;
GLintPoint P[10];
window Win(800,600);
frame Frame;
GLUI *glui;
int mainwindow;
//Function prototypes
void readinpoints();
void drawline(GLint from, GLint to);
void display(void);
void init (void);
void keyboard(unsigned char key, int x, int y);
void tile1(void);
void tile4(void);
void tile9(void);
void tile16(void);
void myReshape(GLsizei W, GLsizei H);
void define_glui_window();
void choice_selected(int value);
void drawImage(void);
//Main
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (Win.width, Win.height);
glutInitWindowPosition (100, 100);
mainwindow = glutCreateWindow ("*star*");
init();
define_glui_window();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutReshapeFunc(myReshape);
readinpoints();
glutMainLoop();
return 0;
}
//Functions
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); //set background color
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 200, 0.0, 249, -1.0, 1.0);
}
void readinpoints()
{
ifstream infile("polyline.pts");
numpoints=0;
infile>>P[numpoints].x>>P[numpoints].y;
while (!infile.eof())
{
numpoints++;
infile>>P[numpoints].x>>P[numpoints].y;
}
numpoints=numpoints-1;
}
void drawline(GLint from, GLint to)
{
glBegin(GL_LINES);
{
glVertex2i(P[from].x, P[from].y);
glVertex2i(P[to].x, P[to].y);
}
glEnd();
}
void drawImage(void);
{
glClear (GL_COLOR_BUFFER_BIT)
glColor3f (1.0, 0.0, 1.0);
glLineWidth(3);
drawline(0,1);
drawline(1,2);
drawline(2,3);
drawline(3,4);
drawline(4,5);
drawline(5,6);
drawline(6,7);
drawline(7,8);
drawline(8,9);
drawline(9,0);
glFlush();
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
//autoViewPort(Frame, Win);
drawImage();
}
void tile1(void)
{ glClear (GL_COLOR_BUFFER_BIT);
for(int i=0; i < 1; i++)
for(int j=0; j < 1; j++)
{
//autoViewPort(Frame, Win);
glViewport(i * 800, j * 600, 800, 600);
drawImage();
glFlush();
}
}
void tile4(void)
{ glClear (GL_COLOR_BUFFER_BIT);
for(int i=0; i < 2; i++)
for(int j=0; j < 2; j++)
{
// autoViewPort(400, 300);
glViewport(i * 400, j * 300, 400, 300);
drawImage();
glFlush();
}
}
void tile9(void)
{
glClear (GL_COLOR_BUFFER_BIT);
for(int i=0; i < 3; i++)
for(int j=0; j < 3; j++)
{
// autoViewPort(267, 200);
glViewport(i * 267, j * 200, 267, 200);
drawImage();
glFlush();
}
}
void tile16(void)
{
glClear (GL_COLOR_BUFFER_BIT);
for(int i=0; i < 4; i++)
for(int j=0; j < 4; j++)
{
// autoViewPort(200, 150);
glViewport(i * 200, j * 150, 200, 150);
drawImage();
glFlush();
}
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27:
exit(0);
break;
case 'f':
tile4();
break;
case 'n':
tile9();
break;
case 's':
tile16();
break;
case 'o':
tile1();
break;
}
}
void myReshape(GLsizei W, GLsizei H)
{
Win.set(W,H);
autoViewPort(Frame, Win);
}
void choice_selected(int value)
{
switch (value)
{
case 1:
tile1();
break;
case 2:
tile4();
break;
case 3:
tile9();
break;
case 4:
tile16();
break;
}
}
void define_glui_window()
{
glui = GLUI_Master.create_glui("OPTIONS", 0, 560, 50);
GLUI_StaticText *message1 = glui->add_statictext(" BACKGROUND COLOR ");
glui->add_button("Tile-1", 1, choice_selected);
glui->add_button("Tile-4", 2, choice_selected);
glui->add_button("Tile-9", 3, choice_selected);
glui->add_button("Tile-16", 4, choice_selected);
glui->set_main_gfx_window(mainwindow);
GLUI_Master.sync_live_all();
}
Check for errors with glError() after each OpenGL call. The point which is failing will return an error. Then you can also look up the error code for extra information on what went wrong.
Try
glutPostRedisplay();
after glFlush();