Login
Username:

Password:

Remember me



Lost Password?

Register now!

Sections

Who's Online
191 user(s) are online (124 user(s) are browsing Forums)

Members: 1
Guests: 190

DrProton, more...

Headlines

 
  Register To Post  

(1) 2 »
OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
Just 4 fun,
I start learning C/C++ and OpenGL and I wanted to share my 1st small test as some sort of free tutorial for C/C++ OpenGL ...
(I'm a true beginner in both C/C++ and OpenGL)

/* OpenGL Sample #1
    SStar scrolling with 4 parallaxs
*/

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

float PosXY[256][2];

/*  WHAT WE WANT TO DISPLAY ON SCREEN MUST BE ADDED IN THIS FUNCTION */
void My_glRenderingvoid )
{
  
int i;
  
glClearColor0.00.00.00.0);    /* Set backdrop as black */
  
glClearGL_COLOR_BUFFER_BIT );   /* Clear the backdrop with glClearColor color */
  
glColor3f1.01.01.0 );

  for( 
0257 i++ ){
    
PosXY[i][1] -= 0.0001f ;
    if ( 
64 )
      
PosXY[i][1] -= 0.0001f ;
    if ( 
128 )
      
PosXY[i][1] -= 0.0001f ;
    if ( 
192 )
      
PosXY[i][1] -= 0.0001f ;
    if ( 
PosXY[i][1] <=0.0 )
      
PosXY[i][1] = 1.0;
    
glBegin(GL_POINTS) ;
      
glVertex3fPosXY[i][1], PosXY[i][2], 0.0f );
     
glEnd() ;
   }
  
glFlush();
 }

/* SOME INITIALIZATIONS HERE */
void My_glInitvoid )
{
  
glMatrixModeGL_PROJECTION );
  
glLoadIdentity() ;
  
glOrtho0.01.00.01.0, -1.01.0 );
 }

/* MAIN C PROCEDURE STARTUP-SEQUENCE IS HERE */
int main(int argcchar** argv)
{
  
int i;
  for( 
1257 i++ ){                                                                                                                           
    
PosXY[i][1] = rand() % 100000 100000.0 ;
    
PosXY[i][2] = rand() % 100000 100000.0 ;
   }

  
glutInit(&argcargv);    /* Initialize Glut */
  
glutInitDisplayMode(GLUT_SINGLE GLUT_RGB);   /* Define display mode properties such as single buffer, color mode and  */
  
glutInitWindowSize640480 );         /* define the sizes of the window to create */
  
glutInitWindowPosition300300 );
  
glutCreateWindow("AmiDARK Test Window");       /* Create the final rendering window */


  
My_glInit();
  
glutDisplayFuncMy_glRendering );
  
glutIdleFuncMy_glRendering );
  
mglEnableSync(GL_TRUE );
  
glutMainLoop();

  return 
0;
 }


Here is a small link (Rar file) that contain both source code and executable (and CodeBENCH project) to test :
http://files.odyssey-creators.com/os4opengl/OpenGLStarsv1.rar

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@freddix

Very well done.


But may I suggest the you move glBegin and glEnd outside the loop.

glBegin(GL_POINTS);
for(
0points; ++i)
{
    
glVertex(x,y,z);
}
glEnd();

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Shadow
Yes,

After releasing the code, I've optimised it a bit and it becomes this :

/* OpenGL Sample #1
    SStar scrolling with 4 parallaxs
*/

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

float PosXY[256][2];

/*  WHAT WE WANT TO DISPLAY ON SCREEN MUST BE ADDED IN THIS FUNCTION */
void My_glRenderingvoid )
{
  
int imultiplier;
  
glClearColor0.00.00.00.0);    /* Set backdrop as black */
  
glClearGL_COLOR_BUFFER_BIT );   /* Clear the backdrop with glClearColor color */
  
glColor3f1.01.01.0 );

  for( 
0257 i++ ){
    
multiplier 4.0;
    
PosXY[i][1] -= ( 0.00001f multiplier ) ;
    if ( 
PosXY[i][1] < 0.0 )
      
PosXY[i][1] = 1.0;
   }
  
glBegin(GL_POINTS) ;
    for( 
00257 i++ ){
      
glVertex3fPosXY[i][1], PosXY[i][2], 0.0f );
     }
   
glEnd() ;
  
glFlush();
 }

/* SOME INITIALIZATIONS HERE */
void My_glInitvoid )
{
  
glMatrixModeGL_PROJECTION );
  
glLoadIdentity() ;
  
glOrtho0.01.00.01.0, -1.01.0 );
 }

/* MAIN C PROCEDURE STARTUP-SEQUENCE IS HERE */
int main(int argcchar** argv)
{
  
int i;
  for( 
1257 i++ ){                                                                                                                           
    
PosXY[i][1] = rand() % 100000 100000.0 ;
    
PosXY[i][2] = rand() % 100000 100000.0 ;
   }

  
glutInit(&argcargv);    /* Initialize Glut */
  
glutInitDisplayMode(GLUT_SINGLE GLUT_RGBA);   /* Define display mode properties such as single buffer, color mode and  */
  
glutInitWindowSize640480 );         /* define the sizes of the window to create */
  
glutInitWindowPosition300300 );
  
glutCreateWindow("AmiDARK Test Window");       /* Create the final rendering window */


  
My_glInit();
  
glutDisplayFuncMy_glRendering );
  
glutIdleFuncMy_glRendering );
  
mglEnableSync(GL_TRUE );
  
glutMainLoop();

  return 
0;
 }

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@freddix

Great, but since your are in the optimizing mood you should change:

multiplier 4.0;


to:

multiplier 0.25f;





Oh and by the way.

You are looping to far, and accessing illegal memory.

Your array has 256 element but you are accessing 257 elements (0-256), your break condition should be i < 256.

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Shadow
ok

I'm new into C/C++ so I'm not perfect in understand the way the loops works with their limits :p

Thank you.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@Shadow

An other thing you should do was to change your idle function

/* OpenGL Sample #1
    SStar scrolling with 4 parallaxs
*/

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

float PosXY[256][2];

/*  WHAT WE WANT TO DISPLAY ON SCREEN MUST BE ADDED IN THIS FUNCTION */
void My_glRenderingvoid )
{
  
int i;
  
glClearColor0.00.00.00.0);    /* Set backdrop as black */
  
glClearGL_COLOR_BUFFER_BIT );   /* Clear the backdrop with glClearColor color */
  
glColor3f1.01.01.0 );

  
glBegin(GL_POINTS) ;
    for( 
00256 i++ ){
      
glVertex3fPosXY[i][1], PosXY[i][2], 0.0f );
     }
   
glEnd() ;
  
glFlush();
 }

void My_IdleFuncvoid
{
  
int imultiplier;
  for( 
0256 i++ ){
    
multiplier 0.25f;
    
PosXY[i][1] -= ( 0.00001f multiplier ) ;
    if ( 
PosXY[i][1] < 0.0 )
      
PosXY[i][1] = 1.0;
   }

  
glutPostRedisplay();
}

/* SOME INITIALIZATIONS HERE */
void My_glInitvoid )
{
  
glMatrixModeGL_PROJECTION );
  
glLoadIdentity() ;
  
glOrtho0.01.00.01.0, -1.01.0 );
 }

/* MAIN C PROCEDURE STARTUP-SEQUENCE IS HERE */
int main(int argcchar** argv)
{
  
int i;
  for( 
1256 i++ ){                                                                                                                           
    
PosXY[i][1] = rand() % 100000 100000.0 ;
    
PosXY[i][2] = rand() % 100000 100000.0 ;
   }

  
glutInit(&argcargv);    /* Initialize Glut */
  
glutInitDisplayMode(GLUT_SINGLE GLUT_RGBA);   /* Define display mode properties such as single buffer, color mode and  */
  
glutInitWindowSize640480 );         /* define the sizes of the window to create */
  
glutInitWindowPosition300300 );
  
glutCreateWindow("AmiDARK Test Window");       /* Create the final rendering window */


  
My_glInit();
  
glutDisplayFuncMy_glRendering );
  
glutIdleFuncMy_IdleFunc );
  
mglEnableSync(GL_TRUE );
  
glutMainLoop();

  return 
0;
 }

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Shadow
Ok

What is the difference between glutPostRedisplay() and glFlush() ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@freddix

glutPostRedisplay tells glut to start rendering. glFlush empties the OpenGL command queue.

Idle function is always being called by GLUT while the rendering function is actually only called when something happens to the window eg. it is moved. I am not sure this is true for the amiga port of GLUT though.

Anyway because the rendering function is rarely called you should use the idle function to update your geometry and other stuff, then when that is done you use glutPostRedisplay to tell GLUT that now would be a good time to do some rendering.

Does that make sence?

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Shadow:

I've understood but,
I noticed something,
if I don't define a function for glutIdleFunc(). Nothing is updated on screen until I move or resize the window.

Strange... Issue ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@freddix

No then it works as expected, rendering is only done when you tell it to render with glutPostRedisplay.

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Shadow:
Do you think it should be the best to ask
flush() to force emptying the queue and during this time, make other calculations that are not related to graphics ? and then use glutPostRedisplay() to update the display when everything is done ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Home away from home
Home away from home


See User information
@freddix

Quote:

freddix wrote:
@Shadow:

I've understood but,
I noticed something,
if I don't define a function for glutIdleFunc(). Nothing is updated on screen until I move or resize the window.

Strange... Issue ?

That's what it's supposed to do, although there was a bug that resulted in a newly opened window remaining gray until the window was moved/resized. I fixed that a few days ago.

Have a look at my frame-rate independent animation template to see how to do animation (if you're not animating, you don't need to update the display).

Hans

http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more.
https://keasigmadelta.com/ - more of my work
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Hans
Ok.
Thank you.

I think I've understood the principle of refreshing the display now.
I'll take a look at animations ...

As I'm beginner on C/C++, I didn't found how to convert an integer into a string, because I'd like to display debug informations during my dev tests such as frame rate, etc ...
Do you have any info ?

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Home away from home
Home away from home


See User information
@freddix

Quote:

freddix wrote:
@Shadow:
Do you think it should be the best to ask
flush() to force emptying the queue and during this time, make other calculations that are not related to graphics ? and then use glutPostRedisplay() to update the display when everything is done ?


GlFlush() causes your program to wait until the graphics card/driver has finished processing all the commands that you just gave it. It's usually not needed. You would use it only if you want to read back the image that you've just drawn in order to use it for something (e.g., as a reflection map).

GlutPostRedisplay() tells GLUT to call your render function. It does not actually update the screen. This is usually called from the idle/timer function. Basically, perform all calculations to update the position of objects and then call glutPostRedisplay() in order to render them at their new locations.

Normally glutSwapBuffers() is put at the end of the render function. If a screen is double-buffered, this will update the screen to display the new image. If double-buffering isn't enabled, glutSwapBuffers() still doesn't hurt.

Hans

http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more.
https://keasigmadelta.com/ - more of my work
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@freddix

char buf[256];
int i 23043;

sprintf(buf"%d"i);

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Home away from home
Home away from home


See User information
@freddix

Quote:

freddix wrote:
@Hans
Ok.
Thank you.

I think I've understood the principle of refreshing the display now.
I'll take a look at animations ...

As I'm beginner on C/C++, I didn't found how to convert an integer into a string, because I'd like to display debug informations during my dev tests such as frame rate, etc ...
Do you have any info ?


You could use snprintf. If you're using C++, you could also have a look at the sstream and string classes. Snprintf() is probably easier to get started with, but using sstream is less fiddly.

Hans

http://hdrlab.org.nz/ - Amiga OS 4 projects, programming articles and more.
https://keasigmadelta.com/ - more of my work
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
Ok Hans and Shadow.
Thank you.

@Shadow : I tried this without success ... nothing appear on screen ...
Is there a precise moment to display the text ?


Edited by freddix on 2009/2/16 23:53:40
All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just popping in
Just popping in


See User information
@freddix

Shadow's example converts an int into a C string. I second Hans' advice and use stringstreams instead if you're in C++. There is a brief description of stringstreams on CppReference.com.

Also, if you do use C, snprintf instead of sprintf because sprintf allows buffer overruns (writing past the end of allocated memory) while snprintf keeps track of the length of the buffer it is writing to.

Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Quite a regular
Quite a regular


See User information
@Hans :
Quote:
GlFlush() causes your program to wait until the graphics card/driver has finished processing all the commands that you just gave it. It's usually not needed. You would use it only if you want to read back the image that you've just drawn in order to use it for something (e.g., as a reflection map).

Can we render the 3D in something else than a window ? (a texture for example, this could be better for future shader use)

@Samurai_Crow:
Thank you for this link, now I understand how the function work. It work now how I expected.

All we have to decide is what to do with the time that is given to us.
Go to top
Re: OpenGL - Small Stars scroll 4 layers.
Just can't stay away
Just can't stay away


See User information
@freddix

If you just want to output numbers to the stdout stream (normally the console) you can use printf().

#include <stdio.h>

int main () {
int number = 42;
printf("the value of number is %d\n", number);
return 0;
}

Go to top

  Register To Post
(1) 2 »

 




Currently Active Users Viewing This Thread: 1 ( 0 members and 1 Anonymous Users )




Powered by XOOPS 2.0 © 2001-2023 The XOOPS Project