What am I doing wrong?

Started by
13 comments, last by GameDev.net 18 years, 6 months ago
Hello everyone. I have a problem here. Why does glut not work right for me even in as simple program as this one:

#include <windows.h>
#include<gl/glut.h>
#include<gl/glu.h>
#include<gl/gl.h>
#include "bmp.h"
#include "ext.h"

float xtrans=0.0;
float ytrans=0.0;

GLubyte *Bits=NULL;
BITMAPINFO *Info=NULL;
GLuint texture[1];

void myInit(void) {
	Bits=LoadDIBitmap("tex.bmp", &Info);
	glGenTextures(1, &texture[0]);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	glTexImage2D(GL_TEXTURE_2D, 0, 3, Info->bmiHeader.biWidth, Info->bmiHeader.biHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, Bits);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClear(GL_COLOR_BUFFER_BIT);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, 640.0, 0.0, 480.0);
	glEnable(GL_TEXTURE_2D);
	}

void renderScene(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	glBindTexture(GL_TEXTURE_2D, texture[0]);
	
	glPushMatrix();
	glTranslatef(xtrans, ytrans, 0);
	glBegin(GL_QUADS);
		glTexCoord2f(0.0, 0.0); glVertex2f(20, 20);
		glTexCoord2f(1.0, 0.0); glVertex2f(84, 20);
		glTexCoord2f(1.0, 1.0); glVertex2f(84, 84);
		glTexCoord2f(0.0, 1.0); glVertex2f(20, 84);
	glEnd();
	
	glPopMatrix();
	glutSwapBuffers();
}

void myTimer(int z) {
	xtrans+=1;
	glutPostRedisplay();
	glutTimerFunc(20, myTimer, z);
	}

void main(int argc, char **argv) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
	glutInitWindowPosition(100,100);
	glutInitWindowSize(640,480);
	glutCreateWindow("Stuff");
	myInit();
	glutDisplayFunc(renderScene);
	glutTimerFunc(20, myTimer, 0);
	glutMainLoop();
}

Nothing special here. Everything is as simple as possible. I'm using glutTimerFunc for animation. But it still manages to make everything go crappy (I mean all the choppiness and flickering as the object moves). And it's not because of vsync coz it would flicker even when the object is still and it doesn't. Must be something I have missed here. It is so discouraging because I have seen programs that claimed they were using glutTimerFunc and they run very smooth. Hope somebody helps me overcome this problem.
Advertisement
is main exectued repeatedly..?

if that's the case, you're rebuilding and creating new render stuff all the time...

do something like:

LoadtexturesEtc();Init();     while(!GameOver)        {          tickCount = GetTickCount ();          UpdateStuff(tickCount-lastTickCount);          lastTickCount = tickCount;                    RenderStuff();        }



I am not experienced in GLUT however, but the above is my main loop that I use, (pseudo code, that is...) in my project.
"Game Maker For Life, probably never professional thou." =)
I believe main is not executed repeatedly. There is such function as glutMainLoop() which asks different registered in main() callbacks such as rendering and idle callbacks every tick. And it kinda should work properly, coz it is written in every glut tutorial. But somehow it works like crap. :(
I bet many of those who looked at the post knew the answer but didn't bother to answer. Please, answer. Or they will kill me <_< >_>
It works OK for me. (Note that I disabled texturing, since I didn't have your texture!) It could be smoother, but that's because you're not rendering as fast as it needs to be.

Do you have the latest drivers installed for your video card?

By the way, I don't recommend that you do animation this way. I recommend you do it this way: Render as fast as you can (for example, call glutPostRedisplay in a glutIdleFunc callback). Each time you render, get the system time, using either timeGetTime or QueryPerformanceCounter. Then you can decide where to draw your objects, based the amount of time elapsed since you rendered the last frame. Just remember that if you use timeGetTime, set its precision to 1 millisecond.
First I must say I have latest drivers available for my Radeon 9800 pro. Then about rendering as often as posssible, adding line glutIdleFunc(renderScene) doesn't really help. Also tried to make framerate independent animation like it is written here (http://www.gaffer.org/articles/Timestep.html) and here (http://www.sacredsoftware.net/tutorials/Animation/TimeBasedAnimation.xhtml
). Tried everything and still it behaves awfully. What the hell? I compile with MS Visual Studio 2003. Maybe something wrong with it?
dont use glutTimerFunc(20, myTimer, 0);
just have your main loop get called as often as possible, most glut examples do this
I'm not sure if glutIdleFunc(renderScene) will work.. Have you tried using a function containing a call to glutPostRedisplay()?

I'm on a GeForce 6600 and it works fine for me. Could you explain a bit more, what it looks like for you?
Quote:Original post by Halma
I'm not sure if glutIdleFunc(renderScene) will work.. Have you tried using a function containing a call to glutPostRedisplay()?

I'm on a GeForce 6600 and it works fine for me. Could you explain a bit more, what it looks like for you?


Well, I believe there's no actual difference in making renderScene an idle function or making a separate idle function which calls for redisplaying through glutPostRedisplay() (btw I tried it, nothing changes). Well what this prog behaves for me is like a jerkily moving quad which also occasionally flickers as it moves, nothing similar to normal application that runs at low fps, believe me.
Why not try using SDL? :)

This topic is closed to new replies.

Advertisement