Dark Bit Factory & Gravity

PROGRAMMING => Freebasic => Topic started by: Mynes on August 23, 2008

Title: Trying To Port Some C++ Tut's to Basic
Post by: Mynes on August 23, 2008
Hi, Hi, i've been looking around these forums for quite sometime but as of yet not posted D:

However i've been wanting to learn OpenGL for quite sometime, i tried my hand at C++ but found that it was a little to complex for me especially throwing OpenGL in there, this is why i turned back around and try learn basic, (not doing to bad at the moment) Only problem is i'm trying my hand at OpenGL now in basic, thanks to all the peep's that post source up has helped me out, however i'm still confused on alot of the funtions, when i tried my hand at C++ and OpenGL i found some really good tutorials at :

http://www.videotutorialsrock.com

i'm been trying to follow then using basic not don't to bad of a job however i came up stuck and could not get any further and some of the C++ functions used threw my off :S

This was the first tut's source which if anyone could help me port to basic would help loads on the rest of the tutorials, (i understand asking such a task maybe not the greatest first post)

Code: [Select]
/* Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above notice and this permission notice shall be included in all copies
 * or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
/* File for "Basic Shapes" lesson of the OpenGL tutorial on
 * www.videotutorialsrock.com
 */



#include <iostream>
#include <stdlib.h> //Needed for "exit" function

//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif

using namespace std;

//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) {    //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}

//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}

//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);

glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective

//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0,                  //The camera angle
   (double)w / (double)h, //The width-to-height ratio
   1.0,                   //The near z clipping coordinate
   200.0);                //The far z clipping coordinate
}

//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective

glBegin(GL_QUADS); //Begin quadrilateral coordinates

//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);

glEnd(); //End quadrilateral coordinates

glBegin(GL_TRIANGLES); //Begin triangle coordinates

//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);

glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);

//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);

glEnd(); //End triangle coordinates

glutSwapBuffers(); //Send the 3D scene to the screen
}

int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size

//Create the window
glutCreateWindow("Basic Shapes - videotutorialsrock.com");
initRendering(); //Initialize rendering

//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);

glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
return 0; //This line is never reached
}
Title: Re: Trying To Port Some C++ Tut's to Basic
Post by: Shockwave on August 23, 2008
Really the trick is getting the window set up, once you have that then you will be well on the way to making whatever you like :)

I'd suggest that you do a search in the general code forum here for "opengl", you'll come up with topics like this;

http://dbfinteractive.com/forum/index.php?topic=3062.0 (http://dbfinteractive.com/forum/index.php?topic=3062.0)

Which will hopefully show you how to get things working in opengl, essentially it's more or less exactly the same as you'd use it in C++ after you have opened the window.

Freebasic allows you to do this with a standard window btw, but I prefer to use my own setup because FB graphics is too big for what I make.

And welcome to the forum.  :) Your posts are most welcome.
 
Title: Re: Trying To Port Some C++ Tut's to Basic
Post by: Jim on August 24, 2008
Hi Mynes, welcome to the forum!

I think a really good place for you to start would be with Shockwave's or Rbz's code from this post
http://dbfinteractive.com/forum/index.php?topic=2821.0 (http://dbfinteractive.com/forum/index.php?topic=2821.0)
Shockwave's code relies on Freebasic to init OpenGL for you.  Rbz's contains instead a very nicely shortened OpenGL startup code.

The C++ code you've posted is more complicated than it needs to be, and it's using the glut library as well as OpenGL.

Jim
Title: Re: Trying To Port Some C++ Tut's to Basic
Post by: Mynes on August 24, 2008
Thanks Guys for the links you posted up i had a looked and downloaded the sources, and played around with them to get a bit more of an understanding, :D and i did to an extent, I also played around with the uFmod library, i create chiptunes (not saying there great) but its just handy to know the library for Basic, I didn't want to hurt your ears so included a nice chiptune from a VRL Cracktro not one of mine :D

how can i make this better  ???

Code: [Select]
'-------------------------------------------------------------------------------
'                                   /\/otes
'-------------------------------------------------------------------------------
'add to the settings on the compiler
'"<$fbc>" "<$file>" ufmod.o  -s gui
'
' [Some Original Source By Shockwave][edited of course by Mynes :P]
'
'-------------------------------------------------------------------------------


    Option Static
    Option Explicit

    #INCLUDE "GL/gl.bi"
    #INCLUDE "GL/glu.bi"
   
   
'   INCLUDE WINDOWS.BI AFTER GL, GLU.   

    #INCLUDE "WINDOWS.BI"
    #include "uFMOD/ufmod.bi"

    'Include our music
    #include "Music.bas"
   
    Dim hWave As HWAVEOUT
   
    hWave = uFMOD_PlaySong(@Music(0),30231,XM_MEMORY)
   
    uFMOD_SetVolume(60)

'===============================================================================   
' MODE 20 = 1024*768 - 24 BIT - DEFAULT PAGES -
' FLAG = 3 (2 for gl drawing + 1 for full screen) FOR GL DRAWING
'===============================================================================

    'Set the window title
    Windowtitle "Well This Sucks, !_!"

    Screen 19,24,,2
   
    glViewport 0, 0, 800, 600
   
    Dim angle As Single            '' X Rotation
    Dim angle1 As Single
    Dim angle2 As Single


While((GETASYNCKEYSTATE(VK_ESCAPE)<> -32767) )

glClear GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT

glEnable GL_DEPTH_TEST
glEnable GL_NORMALIZE

glMatrixMode GL_PROJECTION       
glLoadIdentity                 

glMatrixMode GL_MODELVIEW       
glLoadIdentity                 


        glRotatef(angle, 3.0, 0.0, 0.0)
        glRotatef(angle1, 0.0, 2.0, 0.0)
        glRotatef(angle2, 0.0, 0.0, 1.0)
       

       

GLBEGIN GL_TRIANGLES

'the TOP, RIGHT, LEFT, are so ballsed up
   
    GLCOLOR3F   1.0, 1.0, 1.0           '
    GLVERTEX3F  0.0, 1.0, 0.0           ' TOP
    GLCOLOR3F   0.0, 1.0, 0.0           '
    GLVERTEX3F  1.0, -1.0, 0.0          ' RIGHT
    GLCOLOR3F   0.0, 1.0, 0.0           '    
    GLVERTEX3F -1.0, -1.0, 0.0          ' LEFT
   
    GLCOLOR3F   1.0, 0.0, 0.0           '
    GLVERTEX3F  0.0, -1.0, 1.0           ' TOP
    GLCOLOR3F   1.0, 1.0, 1.0           '
    GLVERTEX3F  0.0, 1.0, 0.0          ' RIGHT
    GLCOLOR3F   1.0, 0.0, 0.0           '    
    GLVERTEX3F -1.0, -1.0, 0.0          ' LEFT
   
     GLCOLOR3F  0.0, 0.0, 1.0           '
    GLVERTEX3F  0.0, -1.0, 1.0           ' TOP
    GLCOLOR3F   0.0, 0.0, 1.0           '
    GLVERTEX3F  1.0, -1.0, 0.0          ' RIGHT
    GLCOLOR3F   1.0, 1.0, 1.0           '    
    GLVERTEX3F  0.0, 1.0, 0.0           ' LEFT
   

   
   
   

GLEND

Flip
   
        angle = angle + 0.1
        angle1 = angle1 + 0.2
        angle2 = angle1 + 0.6
   
Wend
End

Title: Re: Trying To Port Some C++ Tut's to Basic
Post by: Shockwave on August 25, 2008
Well done for getting somewhere with it :)

They say from humble beginnings.. and you chose a nice tune too :D

How to make it better?
Well I think that it runs kind of fast here, so maybe slow it down a little so that we can see what's going on and adding lots more geometary.

Maybe have the pyramid in some kind of vortex or starfield or something?

Whatever you do with this it will stand you in good stead for the future.
I also reccomend that you get hold of a copy of "the red book" if you don't already have one, it's essential for GL programmers :) Well, I think so at least.

You might want to get hold of opengl superbible too if you want more of a tutorial-like way of learning this api.

Of course you have the forum here too :)