potential bug openGL - c++

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();

Related

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

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.

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.

where do i put in my rotate function

#include "std_lib_facilities.h"
#include <GL/glut.h>
static float dx=0.0;
static float dz=10;
static float points[2];
static float cx=0.0;
static float cy=0.0;
static float cz=0.0;
static float spin=0.0;
static float rotation=1.0;
int readObject()
{
ifstream inputFile("spikeBall.ogl");
if(!inputFile)
{
cerr << "cannot open file spikeBall.ogl" << endl;
return -1;
}
int count = 0;
float x,y,z;
do{
inputFile >> count;
glBegin(GL_POLYGON);
for(int i=0;i<count;i++)
{
inputFile >> x >> y >> z;
glVertex3f(((x/5)+cx),((y/5)+cy),((z/5)+cz));
}
glEnd();
}while(count!=0);
inputFile.close();
}
void init(void)
{
glClearColor(0.0,0.0,0.0,0.0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
glLoadIdentity();
gluLookAt (0,0,20, 0.0,0.0,0.0, 0.0,1.0,0.0);
glPushMatrix();
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
GLfloat aMaterial[]={0.1745,0.01175,0.01175,1.0};
GLfloat bMaterial[]={0.61424,0.04136,0.04136,1.0};
GLfloat cMaterial[]={0.727811,0.626959,0.626959,1.0};
GLfloat dMaterial=40;
glMaterialfv(GL_FRONT,GL_AMBIENT,aMaterial);
glMaterialfv(GL_FRONT,GL_DIFFUSE,bMaterial);
glMaterialfv(GL_FRONT,GL_SPECULAR,cMaterial);
glMaterialf(GL_FRONT,GL_SHININESS,dMaterial);
readObject();
glRotatef(spin,0.0,1.0,0.0);
glPopMatrix();
glFlush();
}
void idleFunc(void)
{
spin+=rotation;
if(spin>360.0)
{
spin-=360.0;
}
cz+=0.1;
if(cz==20)
{
srand((unsigned int) time(NULL));
for(int i=0;i<2;i++)
{
points[i] = (rand()%20)-10;
}
cx = points[0];
cy = points[1];
cz = 0;
GLfloat lightPos[] = {cx,cy,(cz+2),1.0};
glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
std::cout << "cx:" << cx;
std::cout << "cy:" << cy;
std::cout << "cz:" << cz;
}
else
{
}
glutPostRedisplay();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0,20.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(300,300);
glutInitWindowPosition(0,0);
glutCreateWindow("3ds Max loader");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
init();
glutIdleFunc(idleFunc);
glutMainLoop();
return(0);
}
iat the moment it's just moving forward, i want it to spin too, but everywhere i put it, it's either not spinning or it makes the sphere look like it just blew up ( like when i put it after glEnd())
I would create an update loop where you could call all of your logic functions, and create a new seperate Rotate() function then call it there. The general format for games programming I usually follow is:
main()
{
init();
while(gameOn)
{
update();
draw();
}
}
init()
{
//create textures, etc.
}
update()
{
//do logic
}
draw()
{
//display on screen after update
}

Why isn't glTranslate doing anything in a double buffered GLUT window?

I'm trying to make it so that when I press W, A, S, or D, it moves a bunch of lines around on the screen. I read in all the lines from a file and display them, and that works fine.
So I have a keyboard function that has a switch statement that increments an X and Y variable which I use in glTranslate inside of my display function, but my lines don't move. Could anyone help me out with this?
#include <GL/glut.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
int height = 640, width = 640;
int X = 0, Y = 0;
void drawPolyLineFile(char * fileName) {
std::fstream inStream;
inStream.open(fileName, std::ios::in);
if (inStream.fail()) {
std::cerr<< "Error opening file";
return;
}
glClear(GL_COLOR_BUFFER_BIT);
GLint numpolys, numLines, x, y;
inStream >> numpolys;
for ( int j =0; j < numpolys; j++) {
inStream >> numLines;
glBegin(GL_LINE_STRIP);
for (int i = 0; i < numLines; i++) {
inStream >> x >> y;
glVertex2i(x, y);
}
glEnd();
}
//glutSwapBuffers();
inStream.close();
}
void display(void)
{
/* clear all pixels */
glClear (GL_COLOR_BUFFER_BIT);
glTranslatef(X, Y, 0);
drawPolyLineFile("dino.dat");
/* don't wait!
* start processing buffered OpenGL routines
*/
glutSwapBuffers();
}
void init (void)
{
/* select clearing color */
glClearColor (0.0, 0.0, 0.0, 0.0);
/* initialize viewing values */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 640.0, 0.0, 640.0, -1.0, 1.0);
}
void keyboard(unsigned char key, int x, int y) {
float speed = 5.0f;
switch ( key ) {
case 'a':
X -= speed;
std::cerr<< X << std::endl;
break;
case 'd':
X += speed;
std::cerr<< X << std::endl;
break;
case 's':
Y -= speed;
std::cerr<< Y << std::endl;
break;
case 'w':
Y += speed;
std::cerr<< Y << std::endl;
break;
default:
break;
}
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (640, 640);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
I haven't read too carefully, but you're most likely missing a
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
before the call to glTranslate. glTranslate composes a translation. From your code it seems like you're expecting it to set a translation.
You'll probably also want to avoid reading from disk at every frame. Load your model into a data structure at startup and render from that instead.
You were missing a glutPostRedisplay in your keyboard handler. Without that no redraw of the window would have been initiated. Settings display as idle func does the trick as well, but works differently: The window is constantly redrawn, almost all CPU time used up for drawing. Your code also had some other fallacies. I fixed that.
#include <GL/glut.h>
#include <stdlib.h>
#include <fstream>
#include <iostream>
#include <stdexcept>
int X = 0, Y = 0;
void drawPolyLineFile(char * fileName) throw(std::runtime_error)
{
std::fstream inStream;
inStream.open(fileName, std::ios::in);
if (inStream.fail()) {
throw std::runtime_error("Error opening file")
}
GLint numpolys, numLines, x, y;
inStream >> numpolys;
for ( int j =0; j < numpolys; j++) {
inStream >> numLines;
glBegin(GL_LINE_STRIP);
for (int i = 0; i < numLines; i++) {
inStream >> x >> y;
glVertex2i(x, y);
}
glEnd();
}
inStream.close();
}
void display(void)
{
int window_width, window_height;
window_width = glutGet(WINDOW_WIDTH);
window_height = glutGet(WINDOW_HEIGHT);
glViewport(0, 0, window_width, window_height);
/* clear all pixels */
glClearColor (0.0, 0.0, 0.0, 0.0);
glClear (GL_COLOR_BUFFER_BIT);
/* always set all projection parameters a new
* each rendering pass
*/
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, window_width, 0.0, window_height, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(X, Y, 0);
drawPolyLineFile("dino.dat");
/*
* SwapBuffers will flush the OpenGL queue indeed,
* but more importantly it brings the content from
* the back buffer to the front
*/
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y) {
float speed = 5.0f;
switch ( key ) {
case 'a':
X -= speed;
std::cerr<< X << std::endl;
break;
case 'd':
X += speed;
std::cerr<< X << std::endl;
break;
case 's':
Y -= speed;
std::cerr<< Y << std::endl;
break;
case 'w':
Y += speed;
std::cerr<< Y << std::endl;
break;
default:
break;
}
glutPostRedisplay();
}
/*
* Declare initial window size, position, and display mode
* (single buffer and RGBA). Open window with "hello"
* in its title bar. Call initialization routines.
* Register callback function to display graphics.
* Enter main loop and process events.
*/
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (640, 640);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
try {
glutMainLoop();
} catch (std::runtime_error &err) {
std::cerr << err.what();
}
return 0;
}