Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 spining a cube w/ lighting in C++
Index -> Programming, C++ -> C++ Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
tupac




PostPosted: Fri Nov 03, 2006 11:00 pm   Post subject: spining a cube w/ lighting in C++

k, so ive picked up again w/ openGL in c++ and learned some neat stuff i wana share w/ you.
today i'm going to show you how to create a window in c++ using GLUT, plus i'm going to show you how to create a cube that will spin on an axis and be lit by lighting provided with the GLUT library.
if you are using Dev-C++, download glut from here: http://www.nigels.com/glt/devpak/ click on: glut.3.7.6+.DevPak and install it. then open your Dev-C++, click on File->New->Project->Multimedia->glut. you will get a text file with stuff written on it, select it all and delete.

type the following in:
code:

//some simple header files that are needed
#include <GL/gl.h>
#include <GL/glut.h>

GLfloat angle = 0;

//for reshaping the window
void reshape(int w, int h){
     //Set a view port, and make w and h into floats that openGL understands
     glViewport(0,0,(GLsizei)w,(GLsizei)h);
     glMatrixMode(GL_PROJECTION);
     glLoadIdentity();
     gluPerspective(60,(GLsizei)w/(GLsizei)h,1.0,100.0);
     glMatrixMode(GL_MODELVIEW);
}

void InitLight(){
     glEnable(GL_DEPTH_TEST); //Tests the depth of space
     glEnable(GL_LIGHTING); //turns the "lights" on
     glEnable(GL_LIGHT0); //allows light #0 out of about 8 lights to shine
     glEnable(GL_COLOR_MATERIAL); //allows color to appear when object is lit
}

void cube(){
     glColor3f(1.0,0.0,0.0); //sets the color of the cube to red
     glRotatef(angle,1.0,1.0,1.0); //rotates the cube
     glutSolidCube(2); //draws the cube
}

//then we need to display something on the window we want to create
void display(void){
     glClearDepth(1); //clears the depth
     glClearColor(0.0,0.0,0.0,1.0);//clears the window color to black
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glLoadIdentity(); //loads matrix identity
     gluLookAt(0.0,0.0,5.0,0.0,0.0,0.0,0.0,1.0,0.0); //sets where camera looks
     cube(); //displays the cube function
     glutSwapBuffers(); //switches between 1st and 2nd buffers
     glFlush(); //flushes everything onto the screen
     angle++;
     if (angle>=360){ //this is checked in case that the float gets too big for c++ to handle, therefore preventing a crash
          angle = 0;
     }
}

int main(int argc, char *argv[]){ //type this if using Dev-C++
int main(int argc, char** argv){ // type this if using MSVC++
     glutInit(&argc,argv); //initializes argc and argv's
     glutInitDisplayMode (GL_DOUBLE | GL_RGBA | GL_DEPTH);//tells openGL to display double buffers, RedGreenBlueAlpha, and depth of light
     glutInitWindowSize(500,500);//sets window size to 500pixels by 500pixels
     glutInitWindowPosition(0,0);//sets the window to top left of screen
     glutCreateWindow("Spinning Cube");//sets a caption on the window
     InitLight();//turns on the "lights"
     //the next three are a little obvious
     glutDisplayFunc(display);
     glutIdleFunc(display);//since the cube is gonna be moving, openGL needs to change whats going on in the program, so it will use Idle
     glutReshapeFunc(reshape);
     glutMainLoop();
     return 0;
}


Now this code should work, i can't tell if i missed some small detail because i'm writing from a library computer (its the only place i can use the internet from so far) and it doesnt have c++ on it, so i cant compile my prog and see if something is wrong with it. well, if somethings wrong w/ it just reply with any questions.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Fri Nov 03, 2006 11:06 pm   Post subject: (No subject)

code:
int main(int argc, char *argv[]){ //type this if using Dev-C++
int main(int argc, char** argv){ // type this if using MSVC++


These are functionally identical.
tupac




PostPosted: Sat Nov 04, 2006 1:06 pm   Post subject: (No subject)

o... well iduno but for some reason Dev-C++ (may-b just mine) doesnt want to run "int main(int argc, char** argv)" but works when i put "int main(int argc, char *argv[])" Confused
but w/e i guess if it works then thats good Very Happy
wtd




PostPosted: Sat Nov 04, 2006 1:36 pm   Post subject: (No subject)

It has been my experience, that Dev-C++ is a darn poor environment. Just use GCC straight up.
Andy




PostPosted: Mon Nov 06, 2006 3:40 am   Post subject: (No subject)

wow, ogl is so much nicer than d3d..
Display posts from previous:   
   Index -> Programming, C++ -> C++ Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 5 Posts ]
Jump to:   


Style:  
Search: