For a few hours I tought I would make something for fun in 3d so I desided as a first project it would have to be easy.
So I desided to go for a rotating 3d triangle with 3 different colors.
I made it and the code for it looks like this:
It worked! My first 3d app worked.Code:#include <SDL/SDL.h> #include <gl/gl.h> int main(int argc, char *argv[]){ SDL_Event event; float RotSpeed = 0.0f; SDL_Init(SDL_INIT_VIDEO); SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME); glViewport(0, 0, 600, 300); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glMatrixMode(GL_PROJECTION); glMatrixMode(GL_MODELVIEW); int done; for(done = 0; !done;){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,0.0f); glRotatef(RotSpeed, 0.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.87f, -0.5f); glEnd(); RotSpeed +=1.0f; SDL_GL_SwapBuffers(); SDL_PollEvent(&event); if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; } } SDL_Quit(); return(0); }
Then I wanted to add more features to it. I wanted to somehow make it go faster if you clicked "F" and slower if you clicked "S".
The code:
I've marked the places where the compiler gives an error.Code:#include <SDL/SDL.h> #include <gl/gl.h> int main(int argc, char *argv[]){ SDL_Event event; SDL_Event event2; SDL_Event event3; float RotSpeed = 0.0f; SDL_Init(SDL_INIT_VIDEO); SDL_SetVideoMode(600, 300, 0, SDL_OPENGL | SDL_HWSURFACE | SDL_NOFRAME); glViewport(0, 0, 600, 300); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0); glDepthFunc(GL_LESS); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glMatrixMode(GL_PROJECTION); glMatrixMode(GL_MODELVIEW); int done; for(done = 0; !done;){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0f,0.0f,0.0f); glRotatef(RotSpeed, 0.0f, 1.0f, 0.0f); glBegin(GL_TRIANGLES); glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.0f, 1.0f); glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.87f, -0.5f); glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(-0.87f, -0.5f); glEnd(); RotSpeed +=1.0f; SDL_GL_SwapBuffers(); SDL_PollEvent(&event); if(event.key.keysym.sym == SDLK_ESCAPE) { done = 1; } SDL_PollEvent (&event2); if(event2.key.keysym.sym == SDLK_F) { RotSpeed +=10.0f; } SDL_PollEvent (&event3); if(event3.key.keysym.sym == SDLK_S) { RotSpeed +=1.0f; } } SDL_Quit(); return(0); }
I'm using dev-c++, btw.
Thanks,
//Arangol



LinkBack URL
About LinkBacks



CornedBee