Created
February 10, 2014 12:55
-
-
Save robbielynch/8915411 to your computer and use it in GitHub Desktop.
Cylinder in OpenGL with mesh.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Robbie Lynch | |
*/ | |
//////////////////////////////////////////////////////////// | |
// Headers | |
//////////////////////////////////////////////////////////// | |
#include "stdafx.h" | |
#ifdef _DEBUG | |
#pragma comment(lib,"sfml-graphics-d.lib") | |
#pragma comment(lib,"sfml-audio-d.lib") | |
#pragma comment(lib,"sfml-system-d.lib") | |
#pragma comment(lib,"sfml-window-d.lib") | |
#pragma comment(lib,"sfml-network-d.lib") | |
#else | |
#pragma comment(lib,"sfml-graphics.lib") | |
#pragma comment(lib,"sfml-audio.lib") | |
#pragma comment(lib,"sfml-system.lib") | |
#pragma comment(lib,"sfml-window.lib") | |
#pragma comment(lib,"sfml-network.lib") | |
#endif | |
#pragma comment(lib,"opengl32.lib") | |
#pragma comment(lib,"glu32.lib") | |
#include "SFML/Graphics.hpp" | |
#include "SFML/OpenGL.hpp" | |
#include <iostream> | |
//////////////////////////////////////////////////////////// | |
///Entrypoint of application | |
//////////////////////////////////////////////////////////// | |
void Draw_Cuboid(float,float,float); | |
float CylinderX(float,float,float[],float[]); | |
float CylinderY(float,float,float[],float[]); | |
float CylinderZ(float,float,float[],float[]); | |
int main() | |
{ | |
// Create the main window | |
sf::RenderWindow App(sf::VideoMode(600, 600, 32), "SFML OpenGL"); | |
int width=600,height=600; | |
// Create a clock for measuring time elapsed | |
sf::Clock Clock; | |
//prepare OpenGL surface for HSR | |
glClearDepth(1.f); | |
glClearColor(0.3f, 0.3f, 0.3f, 0.f); //background colour | |
glEnable(GL_DEPTH_TEST); | |
glDepthMask(GL_TRUE); | |
//// Setup a perspective projection & Camera position | |
glMatrixMode(GL_PROJECTION); | |
glLoadIdentity(); | |
//set up a 3D Perspective View volume | |
gluPerspective(90.f, 1.f, 1.f, 300.0f);//fov, aspect, zNear, zFar | |
//set up a orthographic projection same size as window | |
//this means the vertex coordinates are in pixel space | |
//glOrtho(0,800,0,600,0,100); // use pixel coordinates | |
//glMatrixMode(GL_PROJECTION); // reset projection matrix | |
//glLoadIdentity(); | |
//calculate new prespective and aspect ratio | |
//gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f); | |
glMatrixMode(GL_MODELVIEW); // reset modelview matrix | |
glLoadIdentity(); | |
// Start game loop | |
while (App.isOpen()) | |
{ | |
// Process events | |
sf::Event Event; | |
while (App.pollEvent(Event)) | |
{ | |
// Close window : exit | |
if (Event.type == sf::Event::Closed) | |
App.close(); | |
// Escape key : exit | |
if ((Event.type == sf::Event::KeyPressed) && (Event.key.code == sf::Keyboard::Escape)) | |
App.close(); | |
} | |
//Prepare for drawing | |
// Clear color and depth buffer | |
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); | |
// Apply some transformations | |
glMatrixMode(GL_MODELVIEW); | |
glLoadIdentity(); | |
glPointSize(2); | |
gluLookAt( 0,0,70,// camera position | |
0,0,0, //look at this point | |
0,1,1); //camera up | |
static float ang=0.0; | |
glRotatef(ang,1,0,0); //spin about x-axis | |
glRotatef(ang*2,0,1,0);//spin about y-axis | |
ang+=0.01f; | |
float p1[]={-15,20,15}; | |
float p2[]={+25,20,15}; | |
float TWO_PI=3.141*2; | |
glBegin(GL_TRIANGLES); | |
float dt=0.3; | |
float dphi=0.2; | |
//First set of triangles | |
for(float t=0;t<=1;t+=dt){ | |
//t=0; | |
for(float phi=0;phi<TWO_PI;phi+=dphi){ | |
glColor3d(1,0,0); | |
glVertex3f( | |
CylinderX(t,phi,p1,p2), | |
CylinderY(t,phi,p1,p2), | |
CylinderZ(t,phi,p1,p2)); | |
glVertex3f( | |
CylinderX(t,phi+dphi,p1,p2), | |
CylinderY(t,phi+dphi,p1,p2), | |
CylinderZ(t,phi+dphi,p1,p2)); | |
glVertex3f( | |
CylinderX(t+dt,phi,p1,p2), | |
CylinderY(t+dt,phi,p1,p2), | |
CylinderZ(t+dt,phi,p1,p2)); | |
} | |
} | |
glEnd(); | |
glBegin(GL_TRIANGLES); | |
dt=0.3; | |
dphi=0.2; | |
//This draws the second set of triangles adjacent to the first set of triangles. | |
//Done by incrementing/changing the first sets vertexs to adjacent positions | |
for(float t=0;t<=1;t+=dt){ | |
for(float phi=0;phi<TWO_PI;phi+=dphi){ | |
glColor3d(0,1,0); | |
glVertex3f( | |
CylinderX(t,phi+dphi,p1,p2), | |
CylinderY(t,phi+dphi,p1,p2), | |
CylinderZ(t,phi+dphi,p1,p2)); | |
glVertex3f( | |
CylinderX(t+dt,phi+dphi,p1,p2), | |
CylinderY(t+dt,phi+dphi,p1,p2), | |
CylinderZ(t+dt,phi+dphi,p1,p2)); | |
glVertex3f( | |
CylinderX(t+dt,phi,p1,p2), | |
CylinderY(t+dt,phi,p1,p2), | |
CylinderZ(t+dt,phi,p1,p2)); | |
} | |
} | |
glEnd(); | |
glTranslatef(0,40,0);//move everyting after this line by 40 units along y-axis | |
glRotatef(ang*5,0,0,1); //spin about z-axis | |
// Finally, display rendered frame on screen | |
App.display(); | |
} | |
return EXIT_SUCCESS; | |
} | |
float CylinderX(float t, float phi, float p1[3],float p2[3]){ | |
return p1[0]*(1-t)+p2[0]*t; | |
} | |
float CylinderY(float t, float phi, float p1[3],float p2[3]){ | |
float r=p1[1]*(1-t)+p2[1]*t; | |
return r*cos(phi); | |
} | |
float CylinderZ(float t, float phi, float p1[3],float p2[3]){ | |
float r=p1[1]*(1-t)+p2[1]*t; | |
return r*sin(phi); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment