PDA

View Full Version : Drawing a 3d rotating cylinder in opengl using visualstudio 2013



Uzairjavaid
03-16-2014, 10:17 AM
I have this assignment,
Q: Write OpenGL code that will draw a single cylinder in the center of a window, and should allow a user to resize, move and rotate this cylinder. The look of the cylinder should optionally change based on the point clicked.

Required Features:

• Draw a cylinder

• Allow the user to reposition the cylinder

• Allow the user to resize the cylinder

• Allow the user to rotate the cylinder

• Provide a means to toggle the look of the cylinder

o Changing the cylinder’s color is an option

• Properly handle window resizing

I'm a newbie so dont know what to do. Please help GUYS !!!

Brokenmind
03-16-2014, 12:36 PM
This assignment covers way too many features to be addressed in a single post, and I'm sure the person who gave you the assignment provided you with a basic openGL program. I'll just give you hints for what you have to look for to solve each point:

Draw a cylinder: gluCylinder (https://www.opengl.org/sdk/docs/man2/xhtml/gluCylinder.xml)
Allow the user to reposition the cylinder: glTranslatef (http://msdn.microsoft.com/en-us/library/windows/desktop/ee872066%28v=vs.85%29.aspx) with mouseEvents (are you using QT or WinAPI?)
Allow the user to resize the cylinder: Simply use the +/- keys and catch the keyEvents
Allow the user to rotate the cylinder: Use mouseEvents and save where the user first clicked the cylinder
Provide a means to toggle the look of the cylinder: Provide a key to save/load all of the settings you can modify the cylinder with
Changing the color: Another key to set the glColor (https://www.opengl.org/sdk/docs/man2/xhtml/glColor.xml) should do
Properly handle window resizing: You need something like a window resize event where you set the glViewport (http://www.khronos.org/opengles/sdk/docs/man/xhtml/glViewport.xml)


If you really never worked with openGL, you need some basic tutorials like the NEHE ones (http://nehe.gamedev.net/tutorial/lessons_01__05/22004/) (This one (http://www.opengl-tutorial.org/beginners-tutorials/) runs in VS). For anything more, you need at least a working program with an error that we could fix.

Uzairjavaid
03-16-2014, 08:50 PM
Thanks for the reply. We just started this course Computer Aided Design in our semester. And we've been through some basic programs like drawing a colorful window, a box in the center of the window with desired colors and an animation; moving box. With due respect, i've studied all the basic syntax u directed me to but the problem is im actually stuck here on how to use them in the program. If u could guide me with a program, i'll really be thankful to u...

Uzairjavaid
03-17-2014, 07:43 AM
#include <math.h> // For math routines (such as sqrt & trig).
#include <stdio.h>
#include <Windows.h>
#include <GL/glut.h> // OpenGL Graphics Utility Library

// The next global variable controls the animation's state and speed.
float RotateAngle = 0.0f; // Angle in degrees of rotation around y-axis
float Azimuth = 20.0; // Rotated up or down by this amount

float AngleStepSize = 3.0f; // Step three degrees at a time
const float AngleStepMax = 10.0f;
const float AngleStepMin = 0.1f;

int WireFrameOn = 1; // == 1 for wire frame mode

GLUquadricObj* myReusableQuadric = 0;



void drawGluSlantCylinder(double height, double radiusBase, double radiusTop, int slices, int stacks)
{
if (!myReusableQuadric) {
myReusableQuadric = gluNewQuadric();
// Should (but don't) check if pointer is still null --- to catch memory allocation errors.
gluQuadricNormals(myReusableQuadric, GL_TRUE);
}
// Draw the cylinder.
gluCylinder(myReusableQuadric, radiusBase, radiusTop, height, slices, stacks);
}

void drawGluCylinder(double height, double radius, int slices, int stacks) {
drawGluSlantCylinder(height, radius, radius, slices, stacks);
}



void drawGluSlantCylinderWithCaps(double height, double radiusBase, double radiusTop, int slices, int stacks)
{
// First draw the cylinder
drawGluSlantCylinder(height, radiusBase, radiusTop, slices, stacks);

// Draw the top disk cap
glPushMatrix();
glTranslated(0.0, 0.0, height);
gluDisk(myReusableQuadric, 0.0, radiusTop, slices, stacks);
glPopMatrix();

// Draw the bottom disk cap
glPushMatrix();
glRotated(180.0, 1.0, 0.0, 0.0);
gluDisk(myReusableQuadric, 0.0, radiusBase, slices, stacks);
glPopMatrix();

}
void drawGluCylinderWithCaps(double height, double radius, int slices, int stacks) {
drawGluSlantCylinderWithCaps(height, radius, radius, slices, stacks);
}

// glutKeyboardFunc is called below to set this function to handle
// all "normal" key presses.
void myKeyboardFunc(unsigned char key, int x, int y)
{
switch (key) {
case 'w':
WireFrameOn = 1 - WireFrameOn;
if (WireFrameOn) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Just show wireframes
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Show solid polygons
}
glutPostRedisplay();
break;
case 'R':
AngleStepSize *= 1.5;
if (AngleStepSize>AngleStepMax) {
AngleStepSize = AngleStepMax;
}
break;
case 'r':
AngleStepSize /= 1.5;
if (AngleStepSize<AngleStepMin) {
AngleStepSize = AngleStepMin;
}
break;
case 27: // Escape key
exit(1);
}
}

// glutSpecialFunc is called below to set this function to handle
// all "special" key presses. See glut.h for the names of
// special keys.
void mySpecialKeyFunc(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
Azimuth += AngleStepSize;
if (Azimuth>180.0f) {
Azimuth -= 360.0f;
}
break;
case GLUT_KEY_DOWN:
Azimuth -= AngleStepSize;
if (Azimuth < -360.0f) {
Azimuth += -360.0f;
}
break;
case GLUT_KEY_LEFT:
RotateAngle += AngleStepSize;
if (RotateAngle > 180.0f) {
RotateAngle -= 360.0f;
}
break;
case GLUT_KEY_RIGHT:
RotateAngle -= AngleStepSize;
if (RotateAngle < -180.0f) {
RotateAngle += 360.0f;
}
break;
}
glutPostRedisplay();

}

/*
* drawScene() handles the animation and the redrawing of the
* graphics window contents.
*/
void drawScene(void)
{
// Clear the rendering window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


// Rotate the image
glMatrixMode(GL_MODELVIEW); // Current matrix affects objects positions
glLoadIdentity(); // Initialize to the identity
glTranslatef(-0.5, 0.0, -35.0); // Translate from origin (in front of viewer)
glRotatef(RotateAngle, 0.0, 1.0, 0.0); // Rotate around y-axis
glRotatef(Azimuth, 1.0, 0.0, 0.0); // Set Azimuth angle

glDisable(GL_CULL_FACE);
glPushMatrix();
glTranslatef(1.5, 0.0, 0.0);
glRotatef(-90.0, 1.0, 0.0, 0.0);
glColor3f(1.0, 0.2, 0.2); // Reddish color
// Parameters: height, radius, slices, stacks
drawGluCylinder(3.0, 1.5, 15, 15);
glPopMatrix();

glEnable(GL_CULL_FACE);
glPushMatrix();
glTranslatef(-1.5, 0.0, 0.0);
glRotatef(-90.0, 1.0, 0.0, 0.0);
glColor3f(0.2, 1.0, 0.2); // Greenish color
// Parameters: height, base radius, top radius, slices, stacks
drawGluSlantCylinderWithCaps(3.0, 1.0, 1.0, 20, 20);
glPopMatrix();

// Flush the pipeline, swap the buffers
glFlush();
glutSwapBuffers();
}



// Initialize OpenGL's rendering modes
void initRendering()
{
glEnable(GL_DEPTH_TEST); // Depth testing must be turned on

glCullFace(GL_BACK);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Just show wireframes at first
}

// Called when the window is resized
// w, h - width and height of the window in pixels.
void resizeWindow(int w, int h)
{
double aspectRatio;

// Define the portion of the window used for OpenGL rendering.
glViewport(0, 0, w, h); // View port uses whole window

// Set up the projection view matrix: perspective projection
// Determine the min and max values for x and y that should appear in the window.
// The complication is that the aspect ratio of the window may not match the
// aspect ratio of the scene we want to view.
w = (w == 0) ? 1 : w;
h = (h == 0) ? 1 : h;
aspectRatio = (double)w / (double)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(15.0, aspectRatio, 25.0, 45.0);

}


// Main routine
// Set up OpenGL, define the callbacks and start the main loop
int main(int argc, char** argv)
{
glutInit(&argc, argv);

// We're going to animate it, so double buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

// Window position (from top corner), and size (width% and hieght)
glutInitWindowPosition(10, 60);
glutInitWindowSize(360, 360);
glutCreateWindow("GluCylinders");

// Initialize OpenGL as we like it..
initRendering();

// Set up callback functions for key presses
glutKeyboardFunc(myKeyboardFunc); // Handles "normal" ascii symbols
glutSpecialFunc(mySpecialKeyFunc); // Handles "special" keyboard keys

// Set up the callback function for resizing windows
glutReshapeFunc(resizeWindow);

// Call this for background processing
// glutIdleFunc( myIdleFunction );

// call this whenever window needs redrawing
glutDisplayFunc(drawScene);

fprintf(stdout, "Arrow keys control viewpoint.\n");
fprintf(stdout, "Press \"w\" to toggle wireframe mode.\n");
fprintf(stdout, "Press \"R\" or \"r\" to increase or decrease rate of movement (respectively).\n");

// Start the main loop. glutMainLoop never returns.
glutMainLoop();

return(0); // This line is never reached.
}

This is the code that i got through the internet and made some modifications. Now, i want to use the mouseEvents, toggling and resizing. Please help BrokenMind, i dont know what to do. And also i have like a day to submit it. Any help would be really appreciated. I ran this code in VisualStudio 2013 and it works fine. But it shows 2 cylinders and i just want the green one in the middle. Can u please make the necessary modifications !!!

Uzairjavaid
03-17-2014, 07:44 AM
#include <math.h> // For math routines (such as sqrt & trig).
#include <stdio.h>
#include <Windows.h>
#include <GL/glut.h> // OpenGL Graphics Utility Library

// The next global variable controls the animation's state and speed.
float RotateAngle = 0.0f; // Angle in degrees of rotation around y-axis
float Azimuth = 20.0; // Rotated up or down by this amount

float AngleStepSize = 3.0f; // Step three degrees at a time
const float AngleStepMax = 10.0f;
const float AngleStepMin = 0.1f;

int WireFrameOn = 1; // == 1 for wire frame mode

GLUquadricObj* myReusableQuadric = 0;



void drawGluSlantCylinder(double height, double radiusBase, double radiusTop, int slices, int stacks)
{
if (!myReusableQuadric) {
myReusableQuadric = gluNewQuadric();
// Should (but don't) check if pointer is still null --- to catch memory allocation errors.
gluQuadricNormals(myReusableQuadric, GL_TRUE);
}
// Draw the cylinder.
gluCylinder(myReusableQuadric, radiusBase, radiusTop, height, slices, stacks);
}

void drawGluCylinder(double height, double radius, int slices, int stacks) {
drawGluSlantCylinder(height, radius, radius, slices, stacks);
}



void drawGluSlantCylinderWithCaps(double height, double radiusBase, double radiusTop, int slices, int stacks)
{
// First draw the cylinder
drawGluSlantCylinder(height, radiusBase, radiusTop, slices, stacks);

// Draw the top disk cap
glPushMatrix();
glTranslated(0.0, 0.0, height);
gluDisk(myReusableQuadric, 0.0, radiusTop, slices, stacks);
glPopMatrix();

// Draw the bottom disk cap
glPushMatrix();
glRotated(180.0, 1.0, 0.0, 0.0);
gluDisk(myReusableQuadric, 0.0, radiusBase, slices, stacks);
glPopMatrix();

}
void drawGluCylinderWithCaps(double height, double radius, int slices, int stacks) {
drawGluSlantCylinderWithCaps(height, radius, radius, slices, stacks);
}

// glutKeyboardFunc is called below to set this function to handle
// all "normal" key presses.
void myKeyboardFunc(unsigned char key, int x, int y)
{
switch (key) {
case 'w':
WireFrameOn = 1 - WireFrameOn;
if (WireFrameOn) {
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Just show wireframes
}
else {
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); // Show solid polygons
}
glutPostRedisplay();
break;
case 'R':
AngleStepSize *= 1.5;
if (AngleStepSize>AngleStepMax) {
AngleStepSize = AngleStepMax;
}
break;
case 'r':
AngleStepSize /= 1.5;
if (AngleStepSize<AngleStepMin) {
AngleStepSize = AngleStepMin;
}
break;
case 27: // Escape key
exit(1);
}
}

// glutSpecialFunc is called below to set this function to handle
// all "special" key presses. See glut.h for the names of
// special keys.
void mySpecialKeyFunc(int key, int x, int y)
{
switch (key) {
case GLUT_KEY_UP:
Azimuth += AngleStepSize;
if (Azimuth>180.0f) {
Azimuth -= 360.0f;
}
break;
case GLUT_KEY_DOWN:
Azimuth -= AngleStepSize;
if (Azimuth < -360.0f) {
Azimuth += -360.0f;
}
break;
case GLUT_KEY_LEFT:
RotateAngle += AngleStepSize;
if (RotateAngle > 180.0f) {
RotateAngle -= 360.0f;
}
break;
case GLUT_KEY_RIGHT:
RotateAngle -= AngleStepSize;
if (RotateAngle < -180.0f) {
RotateAngle += 360.0f;
}
break;
}
glutPostRedisplay();

}

/*
* drawScene() handles the animation and the redrawing of the
* graphics window contents.
*/
void drawScene(void)
{
// Clear the rendering window
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


// Rotate the image
glMatrixMode(GL_MODELVIEW); // Current matrix affects objects positions
glLoadIdentity(); // Initialize to the identity
glTranslatef(-0.5, 0.0, -35.0); // Translate from origin (in front of viewer)
glRotatef(RotateAngle, 0.0, 1.0, 0.0); // Rotate around y-axis
glRotatef(Azimuth, 1.0, 0.0, 0.0); // Set Azimuth angle

glDisable(GL_CULL_FACE);
glPushMatrix();
glTranslatef(1.5, 0.0, 0.0);
glRotatef(-90.0, 1.0, 0.0, 0.0);
glColor3f(1.0, 0.2, 0.2); // Reddish color
// Parameters: height, radius, slices, stacks
drawGluCylinder(3.0, 1.5, 15, 15);
glPopMatrix();

glEnable(GL_CULL_FACE);
glPushMatrix();
glTranslatef(-1.5, 0.0, 0.0);
glRotatef(-90.0, 1.0, 0.0, 0.0);
glColor3f(0.2, 1.0, 0.2); // Greenish color
// Parameters: height, base radius, top radius, slices, stacks
drawGluSlantCylinderWithCaps(3.0, 1.0, 1.0, 20, 20);
glPopMatrix();

// Flush the pipeline, swap the buffers
glFlush();
glutSwapBuffers();
}



// Initialize OpenGL's rendering modes
void initRendering()
{
glEnable(GL_DEPTH_TEST); // Depth testing must be turned on

glCullFace(GL_BACK);

glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Just show wireframes at first
}

// Called when the window is resized
// w, h - width and height of the window in pixels.
void resizeWindow(int w, int h)
{
double aspectRatio;

// Define the portion of the window used for OpenGL rendering.
glViewport(0, 0, w, h); // View port uses whole window

// Set up the projection view matrix: perspective projection
// Determine the min and max values for x and y that should appear in the window.
// The complication is that the aspect ratio of the window may not match the
// aspect ratio of the scene we want to view.
w = (w == 0) ? 1 : w;
h = (h == 0) ? 1 : h;
aspectRatio = (double)w / (double)h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(15.0, aspectRatio, 25.0, 45.0);

}


// Main routine
// Set up OpenGL, define the callbacks and start the main loop
int main(int argc, char** argv)
{
glutInit(&argc, argv);

// We're going to animate it, so double buffer
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

// Window position (from top corner), and size (width% and hieght)
glutInitWindowPosition(10, 60);
glutInitWindowSize(360, 360);
glutCreateWindow("GluCylinders");

// Initialize OpenGL as we like it..
initRendering();

// Set up callback functions for key presses
glutKeyboardFunc(myKeyboardFunc); // Handles "normal" ascii symbols
glutSpecialFunc(mySpecialKeyFunc); // Handles "special" keyboard keys

// Set up the callback function for resizing windows
glutReshapeFunc(resizeWindow);

// Call this for background processing
// glutIdleFunc( myIdleFunction );

// call this whenever window needs redrawing
glutDisplayFunc(drawScene);

fprintf(stdout, "Arrow keys control viewpoint.\n");
fprintf(stdout, "Press \"w\" to toggle wireframe mode.\n");
fprintf(stdout, "Press \"R\" or \"r\" to increase or decrease rate of movement (respectively).\n");

// Start the main loop. glutMainLoop never returns.
glutMainLoop();

return(0); // This line is never reached.
}

This is the code that i got through the internet and made some modifications. Now, i want to use the mouseEvents, toggling and resizing. Please help BrokenMind, i dont know what to do. And also i have like a day to submit it. Any help would be really appreciated. I ran this code in VisualStudio 2013 and it works fine. But it shows 2 cylinders and i just want the green one in the middle. Can u please make the necessary modifications !!!

KSM888
05-16-2014, 09:23 AM
Hi Newbie here as well i noticed you were able to get visual studio 2013 and OpgenGl linked together and working can you tell me where you got the references to go about, or even better explain me the steps.
Thank you for time and help!

Regards,
Kay



I have this assignment,
Q: Write OpenGL code that will draw a single cylinder in the center of a window, and should allow a user to resize, move and rotate this cylinder. The look of the cylinder should optionally change based on the point clicked.

Required Features:

• Draw a cylinder

• Allow the user to reposition the cylinder

• Allow the user to resize the cylinder

• Allow the user to rotate the cylinder

• Provide a means to toggle the look of the cylinder

o Changing the cylinder’s color is an option

• Properly handle window resizing

I'm a newbie so dont know what to do. Please help GUYS !!!