📄 opengl cylinder.txt
字号:
//Cylinder and Parallelepiped by extruding Circle and Quadrilateral
#include <GL/glut.h>
#include <math.h>
#include <stdio.h>
void draw_pixel(GLint cx, GLint cy)
{ glColor3f(1.0,0.0,0.0);
glBegin(GL_POINTS);
glVertex2i(cx,cy);
glEnd();
}
void plotpixels(GLint h, GLint k, GLint x, GLint y)
{
draw_pixel(x+h,y+k);
draw_pixel(-x+h,y+k);
draw_pixel(x+h,-y+k);
draw_pixel(-x+h,-y+k);
draw_pixel(y+h,x+k);
draw_pixel(-y+h,x+k);
draw_pixel(y+h,-x+k);
draw_pixel(-y+h,-x+k);
}
void Circle_draw(GLint h, GLint k, GLint r) // Midpoint Circle Drawing Algorithm
{
GLint d = 1-r, x=0, y=r;
while(y > x)
{
plotpixels(h,k,x,y);
if(d < 0) d+=2*x+3;
else
{d+=2*(x-y)+5;
--y;
}
++x;
}
plotpixels(h,k,x,y);
}
void Cylinder_draw()
{
GLint xc=100, yc=100, r=50;
GLint i,n=50;
for(i=0;i<n;i+=3)
{
Circle_draw(xc,yc+i,r);
}
}
void parallelepiped(int x1, int x2,int y1, int y2, int y3, int y4)
{
glColor3f(0.0, 0.0, 1.0);
glPointSize(2.0);
glBegin(GL_LINE_LOOP);
glVertex2i(x1,y1);
glVertex2i(x2,y3);
glVertex2i(x2,y4);
glVertex2i(x1,y2);
glEnd();
}
void parallelepiped_draw()
{
int x1=200,x2=300,y1=100,y2=175,y3=100,y4=175;
GLint i,n=40;
for(i=0;i<n;i+=2)
{
parallelepiped(x1+i,x2+i,y1+i,y2+i,y3+i,y4+i);
}
}
void init(void)
{
glClearColor(1.0,1.0,1.0,0.0); // Set display window color to white
glMatrixMode(GL_PROJECTION); // Set Projection parameters
gluOrtho2D(0.0,400.0,0.0,300.0);
}
void display(void)
{ glClear(GL_COLOR_BUFFER_BIT); // Clear Display Window
glColor3f(1.0,0.0,0.0); // Set circle color to red (R G B)
glPointSize(2.0);
Cylinder_draw(); // Call cylinder
parallelepiped_draw();// call parallelepiped
glFlush(); // Process all OpenGL routines as quickly as possible
}
void main(int argc, char **argv)
{ glutInit(&argc,argv); // Initialize GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); // Set Display mode
glutInitWindowPosition(50,50); // Set top left window position
glutInitWindowSize(400,300); // Set Display window width and height
glutCreateWindow("Cylinder and parallelePiped Display by Extruding Circle and Quadrilaterl "); // Create Display Window
init();
glutDisplayFunc(display); // Send the graphics to Display Window
glutMainLoop();
}
// Template for Opengl 2D Application programming
// Presented by: Russel Ahmed Apu. TA@CPSC453
// University of Calgary
//C++ Standard library
#include <stdlib.h>
// This is the only library required to
#include <gl/glut.h>
#include <math.h>
// This is the user domain for class definition
// Visual.cpp is the part you'll be spending your time on...
// This is the global declaration area
// : AppTitle: Name your Window caption here
// : Window: Required handle of OpenGL window to destroy it when exiting
// : fullscreen: flag for fullscreen/window mode toggle
// : phasestep: speed. 1=lowest. no upper limit.
// : phase: The ticking clock of the application (in frame slices).
#define AppTitle "Face versus vertex normal"
int Window,fullscreen=1,phasestep=1;
unsigned long int phase=0;
int smooth=0;
#define MAX 60
double rpl[MAX][MAX][3];
double nrm[MAX][MAX][3];
double stp=1.0;
//-------------------------------------------------------------
// This function is hooked to OpenGL toolkit library
// OpenGL calls this procedure every time it wants to
// refresh it's drawing on the screen
void fnc()
{
int i,j;
double x,y,z,w;
w=-MAX*stp/2.0;
for (i=0;i<MAX;i++)
{
y=w+(double)i*stp;
for (j=0;j<MAX;j++)
{
x=w+(double)j*stp;
z=2.0*sin((x*x+y*y-phase)/100.0);
rpl[i][j][0]=x;
rpl[i][j][1]=y;
rpl[i][j][2]=z;
}
}
}
void normal()
{
int i,j;
double x,y,w,v;
w=-MAX*stp/2.0;
for (i=0;i<MAX;i++)
{
y=w+(double)i*stp;
for (j=0;j<MAX;j++)
{
x=w+(double)j*stp;
v=4.0*cos((x*x+y*y-phase)/100.0)/100.0;
nrm[i][j][0]=x*v;
nrm[i][j][1]=y*v;
nrm[i][j][2]=-1;
}
}
}
void draw1()
{
int i,j;
for (i=0;i<MAX-1;i++) for (j=0;j<MAX-1;j++)
{
glBegin(GL_QUADS);
glNormal3dv(nrm[i][j]);
glVertex3dv(rpl[i][j]);
glNormal3dv(nrm[i][j+1]);
glVertex3dv(rpl[i][j+1]);
glNormal3dv(nrm[i+1][j+1]);
glVertex3dv(rpl[i+1][j+1]);
glNormal3dv(nrm[i+1][j]);
glVertex3dv(rpl[i+1][j]);
glEnd();
}
}
void draw2()
{
int i,j;
for (i=0;i<MAX-1;i++) for (j=0;j<MAX-1;j++)
{
glBegin(GL_QUADS);
glNormal3dv(nrm[i][j]);
glVertex3dv(rpl[i][j]);
glVertex3dv(rpl[i][j+1]);
glVertex3dv(rpl[i+1][j+1]);
glVertex3dv(rpl[i+1][j]);
glEnd();
}
}
void display(void)
{
GLfloat Unlit[] = {0.0, 0.0, 0.0, 1.0};
GLfloat Material1Diffuse[] = {1.0, 0.6, 0.6, 1.0};
GLfloat Material1Specular[] = {1.0, 1.0, 1.0, 1.0};
GLfloat Material1Shininess[] = {20.0};
static GLfloat a=0.0;
a++;
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE,Material1Diffuse);
glMaterialfv(GL_FRONT_AND_BACK,GL_SPECULAR,Material1Specular);
glMaterialfv(GL_FRONT_AND_BACK,GL_SHININESS,Material1Shininess);
glPushMatrix();
glRotatef(60,1,1,1);
fnc();
normal();
if (smooth) draw1();
else draw2();
glPopMatrix();
// Swap the display buffer to show the new picture just drawn
glutSwapBuffers();
}
//-------------------------------------------------------------
// This function is hooked to OpenGL toolkit library
// openGL calls this function every time the window
// size is changed.
void reshape(int w,int h)
{
// Specify size of the window
glViewport(0,0,(GLsizei) w, (GLsizei) h);
// Select a display mode
// See the TA for more on Projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Select the viewport coordinate system
// The following is a 2D coordinate system
// X-Axis : -50 to 50
// Y-Axis : -50 to 50
glOrtho(1.33*(-50.0),1.33*50.0,50.0,-50.0,-100.0,100.0);
// This is another mode for matrix translation
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
//-------------------------------------------------------------------
// Mouse event handler
//-------------------------------------------------------------------
void mouse(int button,int state,int x,int y)
{
switch (button) {
case GLUT_RIGHT_BUTTON:
if (state==GLUT_DOWN)
{
// Right click to exit the application
glutDestroyWindow(Window);
exit(0);
}
break;
// Left click to toggle fullscreen mode
case GLUT_LEFT_BUTTON:
if (state==GLUT_DOWN && !fullscreen)
{
glutFullScreen();
fullscreen=1;
}
else if (state==GLUT_DOWN && fullscreen)
{
glutReshapeWindow(640,480);
fullscreen=0;
}
default:
break;
}
}
//-------------------------------------------------------------------
// Keyboard event handlers
//-------------------------------------------------------------------
void KeyInp(unsigned char key,int x,int y)
{
if (key=='=') phasestep++;
if (key=='-' && phasestep>0)phasestep--;
if (key==27)
{
glutDestroyWindow(Window);
exit(0);
}
if (key=='s') smooth=1-smooth;
}
void SpKeyInp(int key,int x,int y)
{
}
void Idle()
{
phase+=phasestep;
glutPostRedisplay();
}
//-------------------------------------------------------------------
// Function that initializes the OpenGL Environment
//-------------------------------------------------------------------
void init(void)
{
int a;
char *c2,c[]=".\\";
GLfloat Light1Position[] = {0, 0, 290.0, 1.0};
GLfloat Light2Position[] = {0, 0, -290.0, 1.0};
GLfloat LightAmbient[] = {0.1, 0.1, 0.1, 1.0};
GLfloat LightDiffuse[] = {1.0, 1.0, 1.0, 1.0};
GLfloat LightSpecular[] = {1, 1, 1, 1.0};
GLfloat Light1Shininess[] = {90.0};
GLfloat Light2Shininess[] = {90.0};
a=1;
c2=c;
glutInit(&a,&c2);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow(AppTitle);
glutSetCursor(GLUT_CURSOR_CROSSHAIR);
Window=glutGetWindow();
// Select black background
glClearColor(0.0,0.0,0.0,0.0);
// Shading mode is flat
glShadeModel(GL_SMOOTH);
// Now Enable Antialiasing
glEnable(GL_LINE_SMOOTH);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glHint(GL_LINE_SMOOTH_HINT,GL_DONT_CARE);
glLineWidth(1.1);
reshape(640,480);
glLightfv(GL_LIGHT0, GL_POSITION, Light1Position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT0, GL_SHININESS, Light1Shininess);
//glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION , 0.0001);
glLightfv(GL_LIGHT1, GL_POSITION, Light2Position);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular);
glLightfv(GL_LIGHT1, GL_SHININESS, Light2Shininess);
//glLightf(GL_LIGHT1, GL_QUADRATIC_ATTENUATION , 0.0001);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
// Hook the functions with OpenGL
glutDisplayFunc(display);
glutMouseFunc(mouse);
glutKeyboardFunc(KeyInp);
glutSpecialFunc(SpKeyInp);
glutIdleFunc(Idle);
// Goto Fullscreen Mode.
if (fullscreen) glutFullScreen();
glEnableClientState(GL_VERTEX_ARRAY);
}
//-------------------------------------------------------------------
// This is the application entry point of the main program
//-------------------------------------------------------------------
int main(int argc, char **argv)
{
// Initialize And switch to OpenGL Mode
init();
// Enter the main loop
glutMainLoop();
return 0;
}
// rect_meshp
// Rectangular Mesh using set of points f(i,j)=f(xi,yi) where xi=x0+i*dx, yi=y0+j*dy
#include <stdlib.h>
#include <GL/glut.h>
#define maxx 20
#define maxy 25
#define dx 15
#define dy 10
GLfloat x[maxx]={0.0},y[maxy]={0.0};
GLfloat x0=50,y0=50; // initial values for x, y
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -