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

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;
}

Related

How do I move a polygon's vertices in the direction of the front face based on its present rotation?

I want the user to have the ability to use the arrow keys to manipulate the polygon. The top arrow key is supposed to move all of the vertices in the direction of the top face.
The left arrow key does this - > angleOfRotation += 5. The right arrow key does this - > angleOfRotation -= 5.
In this image you will find my polygon. There is a box and arrow pointing out the top face. Also, you will notice an arrow pointing away from the polygon illustrating the direction I want it to move with its current angleOfRotation.
game.cpp
#include <iostream>
#include <GL/freeglut.h>
int i;
static int r = 25;
static int screen[] = {640, 480};
static float angleOfRotation = 0.0;
int pos[] = {15, 1};
struct Box {
GLint x = -1; GLint y = -1;
int rx = 2.5, ry = 5;
GLfloat pos[4][2] = {
{x, y}, {x, y-ry}, {x+rx, y-ry}, {x+rx, y}
};
};
Box box;
void reshape(GLint w, GLint h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-r,r,-r,r);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef((box.pos[0][0]+box.pos[1][0])/2, (box.pos[0][1]+box.pos[1][1])/2, 0.0);
glRotatef(angleOfRotation, 0.0, 0.0, 1.0);
glTranslatef((-box.pos[0][0]+-box.pos[1][0])/2, (-box.pos[0][1]+-box.pos[1][1])/2, 0.0);
glBegin(GL_POLYGON);
for (i=0;i<4;i++)
glVertex2f(box.pos[i][0], box.pos[i][1]);
glEnd();
glutSwapBuffers();
}
void timer(int v)
{
glutTimerFunc(1000/60, timer, v);
glutPostRedisplay();
}
void special(int key, int, int)
{
switch (key)
{
case GLUT_KEY_LEFT:
angleOfRotation += 5;
break;
case GLUT_KEY_RIGHT:
angleOfRotation -= 5;
break;
case GLUT_KEY_UP:
for (i=0;i<4;i++)
box.pos[i][1] += 0.36;
break;
case GLUT_KEY_DOWN:
for (i=0;i<4;i++)
box.pos[i][1] -= 0.36;
break;
default: return;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(screen[0], screen[1]);
glutCreateWindow("game");
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutSpecialFunc(special);
glutTimerFunc(0,timer,0);
glutMainLoop();
}

glutKeyboardUpFunc get's called without key release

Im having a problem with glutKeyboardUpFunc. Everytime I press a key and don't release it, the callback of glutKeyboardUpFunc stills gets called, the longer I hold the key the more times the callback executes.
Here is a quick example I put up:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#include <time.h>
#include <GL/glut.h>
#define DEBUG 1
/* VARI�VEIS GLOBAIS */
typedef struct {
GLboolean doubleBuffer;
GLint delay;
} Estado;
typedef struct {
GLfloat x;
GLfloat y;
GLfloat height;
GLfloat width;
GLfloat y_accelaration;
} Plataforma;
//save stage coordinates
typedef struct {
GLfloat top;
GLfloat bottom;
GLfloat right;
GLfloat left;
} Screen;
Estado estado;
Screen ecra;
Plataforma plataforma;
GLfloat tab_speed = 2.0 / 20.0;
GLfloat y_accelaration = 0.001;
void Init(void) {
struct tm *current_time;
time_t timer = time(0);
estado.delay = 42;
estado.doubleBuffer = GL_TRUE;
plataforma.height = 0.3;
plataforma.width = 0.05;
plataforma.y_accelaration = 0;
plataforma.y = 0;
// L� hora do Sistema
current_time = localtime(&timer);
glClearColor(0.3, 0.3, 0.3, 0.0);
glEnable(GL_POINT_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_POLYGON_SMOOTH);
}
void Reshape(int width, int height) {
GLint size;
GLfloat ratio = (GLfloat) width / height;
GLfloat ratio1 = (GLfloat) height / width;
if (width < height)
size = width;
else
size = height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (width < height) {
ecra.left = -1;
ecra.right = 1;
ecra.bottom = -1 * ratio1;
ecra.top = 1 * ratio1;
gluOrtho2D(-1, 1, -1 * ratio1, 1 * ratio1);
} else {
ecra.left = -1 * ratio;
ecra.right = 1 * ratio;
ecra.bottom = -1;
ecra.top = 1;
gluOrtho2D(-1 * ratio, 1 * ratio, -1, 1);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void desenhar_plataforma(Plataforma pf) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(pf.x - pf.width / 2, pf.y - pf.height / 2);
glVertex2f(pf.x + pf.width / 2, pf.y - pf.height / 2);
glVertex2f(pf.x + pf.width / 2, pf.y + pf.height / 2);
glVertex2f(pf.x - pf.width / 2, pf.y + pf.height / 2);
glEnd();
}
void Draw(void) {
glClear(GL_COLOR_BUFFER_BIT);
plataforma.x = ecra.left + 0.1;
desenhar_plataforma(plataforma);
glFlush();
if (estado.doubleBuffer)
glutSwapBuffers();
}
void Key(unsigned char key, int x, int y) {
switch (key) {
case 'a':
plataforma.y += tab_speed;
glutPostRedisplay();
break;
case 's':
plataforma.y -= tab_speed;
glutPostRedisplay();
break;
}
}
void KeyUp(unsigned char key, int x, int y) {
if (DEBUG)
printf("Key Up %c\n", key);
}
int main(int argc, char **argv) {
estado.doubleBuffer = GL_TRUE;
glutInit(&argc, argv);
glutInitWindowPosition(0, 0);
glutInitWindowSize(800, 600);
glutInitDisplayMode(((estado.doubleBuffer) ? GLUT_DOUBLE : GLUT_SINGLE) | GLUT_RGB);
if (glutCreateWindow("No Man's Pong") == GL_FALSE)
exit(1);
Init();
glutReshapeFunc(Reshape);
glutDisplayFunc(Draw);
// Callbacks de teclado
glutKeyboardFunc(Key);
glutKeyboardUpFunc(KeyUp);
glutMainLoop();
return 0;
}
Here's the ouput when pressing 'a':
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
Key Up a
......
Needless to say im not releasing 'a'.
Im using linux mint 17.3 and g++, shutting off key repeat or setting glutIgnoreKeyRepeat(true), stops this issue, but also makes glutKeyboardFunc execute only once per key press, and I realy don't want that.
Does anyone know how to fix this, or whats causing this?
I also compiled the same code under windows 8.1 and the keyUp only fires when there's an actual key release.

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
}