# What am I doing wrong?

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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) {
glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
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);
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.

##### Share on other sites
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.

##### Share on other sites
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. :(

##### Share on other sites
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 <_< >_>

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
dont use glutTimerFunc(20, myTimer, 0);
just have your main loop get called as often as possible, most glut examples do this

##### Share on other sites
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?

##### Share on other sites
Quote:
 Original post by HalmaI'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.

##### Share on other sites
Why not try using SDL? :)

1. 1
2. 2
Rutin
19
3. 3
khawk
15
4. 4
5. 5
A4L
13

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633744
• Total Posts
3013653
×