Jump to content
  • Advertisement
Sign in to follow this  
abt

OpenGL why this code is not working

This topic is 3802 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have written a code in opengl using glutKeyboardFunc function so that i can change the shape of the cylinder that i have created here from keyboard input. but this code is not working. here is the code #include<stdio.h> #include<GL\glut.h> #include<windows.h> void init() { glClearColor(.1,.10,.10,0); } void reshape(int w, int h) { glViewport(0,0,w,h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(50,1,0,15); glMatrixMode(GL_MODELVIEW); } void drawaxis() { glBegin(GL_LINES); glColor3f(0.0, 1.0, 0.0); glVertex2f(2.0, 0.0); glColor3f(1.0, 0.0, 0.0); glVertex2f(-2.0, 0.0); glColor3f(0.0, 0.0, 1.0); glVertex2f(0.0, 2.0); glColor3f(1.0, 1.0, 0.0); glVertex2f(0.0, -2.0); glEnd(); } void Cylinder (double height, double radius, int slices, int stacks) { GLUquadricObj* cyl; cyl = gluNewQuadric(); gluQuadricDrawstyle(cyl, GLU_LINE); gluCylinder(cyl, 1, 1, 3, 6, 6); } void square() { glBegin(GL_POLYGON); glVertex3d(-0.25,0.25,0); glVertex3d(0.25,0.25,0); glVertex3d(0.25,-0.25,0); glVertex3d(-0.25,-0.25,0); glEnd(); } //glTranslated(0,0,-5); //glRotated(10,0,1,0); void render() { glClear(GL_COLOR_BUFFER_BIT); glColor3d(1,0,1); glLoadIdentity(); gluLookAt(0,0,5,0,0,0,0,1,0); glTranslated(0,0,-2); glRotated(30,0,1,0); //glutWireCube(1); drawaxis(); Cylinder(100, 10, 2, 2); glFlush(); } void keyboard(unsigned char key, int x, int y) { switch (key) { case 'u': case 'U': glRotatef(3.0, 1.0, 0.0, 0.0); // rotate up break; case 'd': case 'D': glRotatef(-3.0, 1.0, 0.0, 0.0); // rotate down break; case 'l': case 'L': glRotatef(3.0, 0.0, 1.0, 0.0); // rotate left break; case 'r': case 'R': glRotatef(-3.0, 0.0, 1.0, 0.0); // rotate right } glutPostRedisplay(); } int main(int argc, char* argv[]) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(300, 300); glutInitWindowPosition(100,100); glutCreateWindow("hello window"); init(); glutDisplayFunc(render); glutReshapeFunc(reshape); glutKeyboardFunc(keyboard); glutMainLoop(); return 0; } can someone help me??

Share this post


Link to post
Share on other sites
Advertisement
Help number 1: you can use code tags. post your code between [code] and [/code] tags.

Help number 2: what actually goes wrong? is it
i) a compiler error?
ii) a crash bug?
iii) a strange behaviour?
iv) nothing is drawn
v) your cylinder is always drawn the same size despite you pressing buttons?
?
Given more info, I can probably help.

Share this post


Link to post
Share on other sites
in my code there is no compile error and the cylinder has been drawn here nicly.
but the problem is that here i want that when i press a key from the keyboard as mentained in the switch case in the code the shape of the cylinder will be changed.
but when i run my code after pressing key from the keyboard there is no change in the cylinder.
the cylinder remains same.
how can i solve this problem

Share this post


Link to post
Share on other sites
Don't expect the rotations in the keyboard callback to have any effect if one of the first things you do in the render function is resetting all transformations.

Share this post


Link to post
Share on other sites
As already mentioned by Brother Bob your not having any effect because your transformations are being reset in the render function. An alternative could be to do something like this:

enum DIRECTION {UP=0, DOWN=1, RIGHT=2, LEFT=3}
DIRECTION curDirection = 0;
void keyboard(unsigned char key, int x, int y)
{

switch (key)
{
case 'u': case 'U':
curDirection = UP;
break;
}
glutPostRedisplay();
}

void render()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(1,0,1);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
glTranslated(0,0,-2);
glRotated(30,0,1,0);
//glutWireCube(1);
drawaxis();
glPushMatrix();
if (curDirection == UP)
glRotatef(3.0, 1.0, 0.0, 0.0); // rotate up
else
glRotatef(-3.0, 1.0, 0.0, 0.0); // rotate up
Cylinder(100, 10, 2, 2);
glPopMatrix();
glFlush();
}


Its not complete nor the best method you could use but it should give you some ideas on how to solve the problem. I don't like giving complete source code but I'm happy to answer questions :)

Neutrinohunter

Share this post


Link to post
Share on other sites
Thank you all.
I have got a solution. I think the glRotated function should not be used in the keyboard function directly. here we can set a variable 'angle' and change it's value from the keyboard function and then pass it to the glRotated function in render.
here is the code
#define PI 3.1415926535898
GLdouble angle;

void render()
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3d(1,0,1);
glLoadIdentity();
gluLookAt(0,0,5,0,0,0,0,1,0);
//glTranslated(0,0,-2);
glRotated(angle,0,1,0);
//glutWireCube(1);
drawaxis();
Cylinder(100, 10, 2, 2);
glFlush();
}




void keyboard(unsigned char key, int x, int y)
{

switch (key)
{
case 'u':
case 'U':
angle=45*(180/PI);
break;
case 'd':
case 'D':
angle=90*(180/PI);
break;
case 'l':
case 'L':
angle=45*(180/PI);
break;
case 'r':
case 'R':
angle=(-45)*(180/PI);
}
glutPostRedisplay();
}

this code has better performance than the previous one.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!