Glut Open GL object wont animate - c++

My polygon wont move, I tried many things and i think glClear(GL_COLOR_BUFFER_BIT);or glutMainLoop(); wont do their job. So first picture stay the same. There is no animation.
float x=0;
float y=0;
float b=0;
void displayCB(void)
{
glClear(GL_COLOR_BUFFER_BIT);
kvadrat();
}
void kvadrat()
{
glBegin(GL_POLYGON);
glColor3f(1, 0, 0); glVertex2d(0.5-x, 0.5-y);
glColor3f(1, 0, 0); glVertex2d(0.5-x, -0.5-y);
glColor3f(1, 0, 0); glVertex2d(-0.5-x, -0.5-y);
glColor3f(1, 0, 0); glVertex2d(-0.5-x, 0.5-y);
glEnd();
Sleep(1999);
glFlush();
x=x+0.01; // I modified this value so it will always be between 0.5 and -0.5,
//this is just example
}
int main(int argc, char *argv[])
{
int win;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(800,600);
win = glutCreateWindow("Elementi");
glClearColor(0.0,0.0,0.0,0.0);
glutDisplayFunc(displayCB);
glutKeyboardFunc(keyCB);
glutMainLoop();
return 0;
}

I think you may be moving the position only a very small increment every two seconds, so the image appears to be static, but it's not, it's just changing very slowly. Try taking out the Sleep(1999) and see whether your animation works any better.
If you need to animate over time, it is better to instead use glutGet(GLUT_ELAPSED_TIME) to figure out how much time has elapsed since the last frame and use that to scale your deltas.

I fixed problem with glutPostRedisplay(); // Inform GLUT that the display has changed

Related

GLUT individual pixel manipulation produces unexpected result

For the sake of learning, I'm accessing individual pixel data using GLUT and manually setting pixel color by going through all pixels in the window, like this (some non-related code omitted):
void init() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, WIDTH, 0.0, HEIGHT);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < HEIGHT; j++) {
glPointSize(1.0f);
glColor3f(255, 0, 0);
glBegin(GL_POINTS);
glVertex2i(i, j);
glEnd();
}
}
glutSwapBuffers();
}
void timer(int obsolete) {
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowPosition(100, 100);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow("GLUT Test");
init();
glutDisplayFunc(display);
timer(0);
glutMainLoop();
return 0;
}
I'm expecting to get a fully red pixels window, but I'm getting something different - a window with black vertical stripes, as if horizontal for loop suddenly skipped some lines.
Any ideas what am I doing wrong here? I have a suspicion it might be related to float to int conversion somewhere "inside", but I'm not sure what to search for.
Edit: I've found out that if I resize the window in runtime to be one pixel less in width, then these black stripe tears disappear.
You set up the projection such that the left edge is at 0, and the right one at WIDTH. Note that your pixels are small squares with an area, and this means that 0.0 maps to the left edge ot the left-most pixel, and WIDTH maps to the right edge of the right-most pixel. Integer coordinates will lie exactly in the middle between two pixels. And with some numerical precision loss during transformation, you might end up with two neighboring points beeing rounded to the same pixel.
You can either add 0.5 to x and y when drawing your points, or just shift your orth projection by half a pixel so that integers are mapped to pixel centers:
Ortho(-0.5f, WIDTH-0.5f, -0.5f, HEIGHT-0.5f, ...);

Using GLUT library in Visual Studio, no error, but output console doesn't work

I'm trying to use GLUT for a specific openGL project. I've put glut32.dll/glut.h/glut32.lib in their required directories. After adding a source file to the project in Visual Studio, when I hit debug/run, it doesn't show any error what so ever. The source code I'm using is that of a colored cube that rotates. Now after hitting debug, the output console does show up with the colored cube, but only for a split second, which was not supposed to happen.
the code I'm using:
#include <GL/glut.h>
#define window_width 640
#define window_height 480
// Main loop
void main_loop_function() {
// Z angle
static float angle;
// Clear color (screen)
// And depth (used internally to block obstructed objects)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Load identity matrix
glLoadIdentity();
// Multiply in translation matrix
glTranslatef(0, 0, -10);
// Multiply in rotation matrix
glRotatef(angle, 0, 0, 1);
// Render colored quad
glBegin(GL_QUADS);
glColor3ub(255, 000, 000);
glVertex2f(-1, 1);
glColor3ub(000, 255, 000);
glVertex2f(1, 1);
glColor3ub(000, 000, 255);
glVertex2f(1, -1);
glColor3ub(255, 255, 000);
glVertex2f(-1, -1);
glEnd();
// Swap buffers (color buffers, makes previous render visible)
glutSwapBuffers();
// Increase angle to rotate
angle += 0.25;
}
// Initialze OpenGL perspective matrix
void GL_Setup(int width, int height) {
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glEnable(GL_DEPTH_TEST);
gluPerspective(45, (float) width / height, .1, 100);
glMatrixMode(GL_MODELVIEW);
}
// Initialize GLUT and start main loop
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("GLUT Example!!!");
glutIdleFunc(main_loop_function);
GL_Setup(window_width, window_height);
glutMainLoop();
}
Can someone tell me what might be causing this? The code doesn't have any error. And since the output is indeed showing for only half of a second, I'm assuming the GLUT files have been placed correctly. Then what might be causing the console to go away within a second?
With glut you should not draw things from the function attached to glutIdleFunc but rather glutDisplayFunc.
Use glutDisplayFunc(main_loop_function); and create a new timer function doing the angle += 0.25; and connect the callback with glutTimerFunc(...) to rotate in a timed fashion rather than on every redraw, which may not happen in regular intervals.

Translate a triangle using animation(one by one after some interval) with opengl and c++

I want to make a space invader game in opengl. So I thought of creating the enemies using triangles. Before making the game, I want to try out my hand in animation. I have triangle. i want to translate it left upto some point with animation(i.e, triangle is translated after some interval of time. It should look as if it is moving).After reaching some point in the left, I want to translate it back to the same position or some distances right. The process should go on till the screen is open. I used sleep function. But it is not working. No animation is shown. Only the translated triangle is drawn at different translated position. Help me.
Here is my code-
#include "windows.h"
#include <gl/glut.h>
#include<stdio.h>
void init( void )
{
printf( "OpenGL version: %s\n", (char*)glGetString(GL_VERSION));
printf( "OpenGL renderer: %s\n", (char*)glGetString(GL_RENDERER));
//Configure basic OpenGL settings
glClearColor(1.0, 1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,640.0,0.0,480.0);
glColor3f(1.0,0.0,0.0);
glPointSize(3);
}
void house(int x, int y,int z)
{
glPushMatrix();
glTranslatef(x, y,z);
glBegin (GL_LINES);
glVertex2i (0,30);
glVertex2i (15,60);
glVertex2i (15,60);
glVertex2i (30,30);
glVertex2i (30,30);
glVertex2i (0,30);
glEnd();
//Sleep(200);
glPopMatrix();
//glutSwapBuffers();
}
// Main drawing routine. Called repeatedly by GLUT's main loop
void display( void )
{
//Clear the screen and set our initial view matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
int i;
for(i = 10; i < 350; i = i + 50)
{
house(i,20,0);
Sleep(200);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}
// Entry point - GLUT setup and initialization
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode (GLUT_DEPTH | GLUT_SINGLE| GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow( "OpenGL Test" );
glutDisplayFunc( display );
init();
glutMainLoop();
return 0;
}
In main() you have declared your display() as display callback function. GLUT will call this function either when it determines that the window need to be redrawn or when it is told to redraw it for example by the function glutPostRedisplay().
The display function is expected to call redraw the windows at a specific point in time. The glFlush() will force the execution of the GL commands.
The problem is that your animation loop is inside the redraw function and glFlush() is called at the end, showing the result at once. And you don't tell GLUT to redraw the windows. This is why you don't seee the animation.
For the purpose of the tutorial, I propose you to define a global variable for the initial position of the house drawing. Of course, you'll have to improve this as soon as you understood how all this works.
static int pos = 10; // temporary work around, just for the demo
Then define a timer function, that gets called after a time interval. This will be the core of your animation, organizing the moving, and the redrawing of the window by calling glutPostRedisplay() :
void timerfunc(int value) // handle animation
{
pos += 50; // update the postion
if (pos<350) // as in your originial loop
glutTimerFunc(200, timerfunc, 0); // plan next occurence
glutPostRedisplay(); // redraw the window
}
Activate your timer function, in main() just before launching glutMainLoop():
glutTimerFunc(200, timerfunc, 0); // call a timer function
glutMainLoop(); // this call is already in your code
Your display function can then be changed into:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
house(pos, 20, 0); // draw the house at its last calculated position
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}
Then it works in animated modus !

First try - graphical program

I used a book called Computer Graphices using OpenGL.
at page number 51 i found this code
#include <windows.h>
#include "glut.h"
//<<<<<<<<<<<<<<<<< method(s) >>>>>>>>>>>>>>>>>>>
void My_Display(void);
void My_Inti(void);
//<<<<<<<<<<<<<<<<< main method >>>>>>>>>>>>>>>>>>
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(100, 150);
glutCreateWindow("my second try ");
glutDisplayFunc(My_Display);
My_Inti();
glutMainLoop();
return 0;
}
//<<<<<<<<<<<<<<<<<<<<<<<< IMPLEMENTING METHOD(S) >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>.
//<<<<<<<<<<<<<<<< My_Inti >>>>>>>>>>>>>>>>>>>>>>>>>
void My_Inti(void)
{
glClearColor(1, 1, 1, 0); // white color
glColor3f(0, 0, 0); // Black color
glPointSize(10); // point size is 10 pixel this is big .
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, 640, 0, 480);
}
//<<<<<<<<<<<<<<<< My_Display >>>>>>>>>>>>>>>>>>>>>>
void My_Display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINT);
glVertex2i(100, 50);
glVertex2i(100, 130);
glVertex2i(150, 130);
glEnd();
glFlush();
}
all what i add to this code is the comments and i change a little at the variable ; nothing more .
When we come to the problem this code work fine but it didn't creat the three points at the display method ?
The problem is just one missing letter. Instead of this:
glBegin(GL_POINT);
The correct value is:
glBegin(GL_POINTS);
The first thing I would do any time you get no rendering, or not the expected rendering, is to call glGetError(), and see if it returns an error. I admit that I didn't see this problem initially, but calling glGetError() would have pointed it out quickly.
BTW: In case anybody else is surprised that there are both GL_POINT and GL_POINTS enums in OpenGL. GL_POINT is one of the possible arguments to glPolygonMode(), as opposed to GL_POINTS which denotes one of the possible primitive types for draw calls.
It does not work because there is an error in the source code (unlikely) or you mis-copied the argument for glBegin(GL_POINT); (likely).
Using
glBegin(GL_POINTS);
I got this image:

What's going on with my program (openGL in c++)?

I am starting with openGL and c++, and I was wondering why I don't see anything on the window. Here is my code:
#include <GLUT/GLUT.h>
#include <stdlib.h>
void init() {
glClearColor(0, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -10);
}
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3i(-0.5, -0.5, 0);
glVertex3i(0, 0.5, 0);
glVertex3i(0.5, -0.5, 0);
glEnd();
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE| GLUT_RGBA);
glutInitWindowPosition(200, 200);
glutInitWindowSize(400, 400);
glutCreateWindow("Window");
init();
glutDisplayFunc(display);
glutMainLoop();
}
I have a few questions:
If I run the program like this all I see is a white window... Didn't I set the color to blue?
When I do glutSwapBuffers() at the end of display function and run the program, I see the blue window without the triangle. So, I thought glutSwapBuffers() function only worked with double buffering.
And the most important, where the hell is my triangle? O.o Didn't I translate the camera with glTranslatf() function to -10 in the z-axes? If you are wondering why I used gluPerspective, I have to say that I am trying out new things, but neither works if I use gluOrtho2D().
I don't know if I am missing something or what. Maybe I need to search more information about this, but I think most of the code is correct.
1 & 2) Well you don't have to call glutSwapBuffers() when using single buffer. But you have to call glFlush(), so the draw commands are executed on the GPU.
3) I noticed that you are creating vertices with double coordinates, but you are calling integer version of glVertex** function (decimal part will be truncated) - it means that you will be drawing triangle with zero size.
Use glVertex3d() or glVertex3f() instead of glVertex3i().
Small note: intermediate mode is deprecated in the latest OpenGL.