![]() |
Draw Cylinder (with GRID lines) in OpenGL - Printable Version +- Computer Aided Automation (https://computeraidedautomation.com) +-- Forum: CAA Discussions (https://computeraidedautomation.com/forum-7.html) +--- Forum: Visualization (https://computeraidedautomation.com/forum-13.html) +--- Thread: Draw Cylinder (with GRID lines) in OpenGL (/thread-16.html) |
Draw Cylinder (with GRID lines) in OpenGL - techberth - 23-11-2019 Press space bar to ON/OFF grid appearance ![]() Sample code - -- #include <iostream> #include<conio.h> #include <GL/freeglut.h> //available at www.computeraidedautomation.com using namespace std; GLuint jijmodel; float jijmodelrot; //int x,y,z; bool bUsePredefinedCamera = true; bool grid = true; bool bFullsreen = false; int nWindowID; bool init = false; // camera attributes float viewerPosition[3] = { 0.0, 0.0, -20.0 }; float viewerDirection[3] = { 0.0, 0.0, 0.0 }; float viewerUp[3] = { 0.0, 1.0, 0.0 }; // rotation values for the navigation float navigationRotation[3] = { 0.0, 0.0, 0.0 }; int mousePressedX = 0, mousePressedY = 0; float lastXOffset = 0.0, lastYOffset = 0.0, lastZOffset = 0.0; // mouse button states int leftMouseButtonActive = 0, middleMouseButtonActive = 0, rightMouseButtonActive = 0; // modifier state int shiftActive = 0, altActive = 0, ctrlActive = 0; #ifndef M_PI #define M_PI 3.14159265 #endif int win, win2; int drawObj = 0, maxObj = 2; GLfloat lightRotX, lightRotY; GLfloat objectRotX, objectRotY; int curx, cury, width, height; void drawobjects1(void); void drawGrid() { int i; for(i=0;i<20;i++) { glPushMatrix(); if(i<10){glTranslatef(0,0,i);} if(i>=10){glTranslatef(i-10,0,0); glRotatef(-90,0,1,0);} glBegin(GL_LINES); glColor3f(.5,.5,0); glLineWidth(1); glVertex3f(0,-0.1,0); glVertex3f(9,-0.1,0); glEnd(); glPopMatrix(); } } void drawCylinder(int nmax, int nmin, float height, float radius) { double MAX = height / nmax; double MIN = 2.0 * M_PI / nmin; int i, j; for (i = 0; i < nmax; ++i) { GLfloat z0 = 0.5 * height - i * MAX; GLfloat z1 = z0 - MAX; glBegin(GL_TRIANGLE_STRIP); for (j = 0; j <= nmin; ++j) { double a = j * MIN; GLfloat x = radius * cos(a); GLfloat y = radius * sin(a); glNormal3f(x / radius, y / radius, 0.0); glTexCoord2f(j / (GLfloat) nmin, i / (GLfloat) nmax); glVertex3f(x, y, z0); glNormal3f(x / radius, y / radius, 0.0); glTexCoord2f(j / (GLfloat) nmin, (i + 1) / (GLfloat) nmax); glVertex3f(x, y, z1); } glEnd(); } } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef( viewerPosition[0], viewerPosition[1], viewerPosition[2] ); glRotatef( navigationRotation[0], 1.0f, 0.0f, 0.0f ); glRotatef( navigationRotation[1], 0.0f, 1.0f, 0.0f ); //glRotatef(40,2,2,0); if (grid==true){ drawGrid();} drawobjects1(); glutSwapBuffers(); } void drawobjects1() { drawCylinder(10, 36,0.8, 0.5); } void keyboard(unsigned char key,int x,int y) { if(key==32){ if(grid == true) { grid = false; cout<<"GRID OFF \n"; } else { grid = true; cout<<"GRID ON \n"; } } glutPostRedisplay(); } void mouseMotionFunc(int x, int y) { float xOffset = 0.0, yOffset = 0.0, zOffset = 0.0; // navigation // if (altActive) { // rotatation if (leftMouseButtonActive) { navigationRotation[0] -= ((mousePressedY - y) * 180.0f) / 200.0f; navigationRotation[1] -= ((mousePressedX - x) * 180.0f) / 200.0f; mousePressedY = y; mousePressedX = x; } // panning else if (middleMouseButtonActive) { xOffset = (mousePressedX + x); if (!lastXOffset == 0.0) { viewerPosition[0] -= (xOffset - lastXOffset) / 8.0; viewerDirection[0] -= (xOffset - lastXOffset) / 8.0; } lastXOffset = xOffset; yOffset = (mousePressedY + y); if (!lastYOffset == 0.0) { viewerPosition[1] += (yOffset - lastYOffset) / 8.0; viewerDirection[1] += (yOffset - lastYOffset) / 8.0; } lastYOffset = yOffset; } // depth movement else if (rightMouseButtonActive) { zOffset = (mousePressedX + x); if (!lastZOffset == 0.0) { viewerPosition[2] -= (zOffset - lastZOffset) / 5.0; viewerDirection[2] -= (zOffset - lastZOffset) / 5.0; } lastZOffset = zOffset; } // } } void mouseFunc(int button, int state, int x, int y) { // get the modifiers switch (glutGetModifiers()) { case GLUT_ACTIVE_SHIFT: shiftActive = 1; break; case GLUT_ACTIVE_ALT: altActive = 1; break; case GLUT_ACTIVE_CTRL: ctrlActive = 1; break; default: shiftActive = 0; altActive = 0; ctrlActive = 0; break; } // get the mouse buttons if (button == GLUT_LEFT_BUTTON) if (state == GLUT_DOWN) { leftMouseButtonActive += 1; // myselection(mousePressedX, mousePressedY); // myselection(x, y); } else leftMouseButtonActive -= 1; else if (button == GLUT_MIDDLE_BUTTON) if (state == GLUT_DOWN) { middleMouseButtonActive += 1; lastXOffset = 0.0; lastYOffset = 0.0; } else middleMouseButtonActive -= 1; else if (button == GLUT_RIGHT_BUTTON) if (state == GLUT_DOWN) { rightMouseButtonActive += 1; lastZOffset = 0.0; } else rightMouseButtonActive -= 1; // if (altActive) { mousePressedX = x; mousePressedY = y; // } } void idleFunc(void) { glutPostRedisplay(); } void reshape(int w,int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective (60, (GLfloat)w / (GLfloat)h, 0.1, 1000.0); //glOrtho(-25,25,-2,2,0.1,100); glMatrixMode(GL_MODELVIEW); } int main(int argc, char **argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB|GLUT_DEPTH); glutInitWindowSize(800,450); glutInitWindowPosition(20,20); glutCreateWindow("draw cylinder with mouse nav"); glutDisplayFunc(display); glutReshapeFunc(reshape); glutMouseFunc( mouseFunc); glutMotionFunc( mouseMotionFunc); glutKeyboardFunc(keyboard); glutIdleFunc(idleFunc); glutMainLoop(); return 0; } |