-
OpenGl help
Code:
#include "gl/freeglut.h"
#include "math.H"
bool forward;
float rotval = 0.0;
float angle = 0.0;
float scale = 0.0;
bool direction = false;
void drawaxis()
{
glBegin (GL_LINES);
glColor3f (1.0, 0.0, 0.0);
glVertex3f (-100.0, 0.0, 0.0);
glVertex3f (100.0, 0.0, 0.0);
glColor3f (0.0, 1.0, 0.0);
glVertex3f (0.0, -100.0, 0.0);
glVertex3f (0.0, 100.0, 0.0);
glColor3f (0.0, 0.0, 1.0);
glVertex3f (0.0, 0.0, -100.0);
glVertex3f (0.0, 0.0, 100.0);
glEnd ();
}
void drawLeftLegs()
{
glPushMatrix();
glColor3f(1, 0, 1);
glTranslatef(-35, -45, 0);
glScalef(5.0, 1.0, 3.5);
glutSolidSphere(5, 20, 20);
glPopMatrix();
}
void drawRightLegs()
{
glPushMatrix();
glColor3f(1, 0, 1);
glTranslatef(35, -45, 0);
glScalef(-5.0, 1.0, 3.5);
glutSolidSphere(5, 20, 20);
glPopMatrix();
}
void drawRightHands()
{
glPushMatrix();
glColor3f(0.5, 0.8, 1.0);
glTranslatef(-50, -10, 0);
glScalef(3.5, 1, 2);
glutSolidSphere(5, 20, 20);
glPopMatrix();
}
void drawLeftHands()
{
glPushMatrix();
glColor3f(0.5, 0.8, 1.0);
glTranslatef(50, -10, 0);
glScalef(3.5, 1, 2);
glutSolidSphere(5, 20, 20);
glPopMatrix();
}
void drawBody()
{
glPushMatrix();
glColor3f(0.0, 0.9, 0.9);
glTranslatef(0, -10, -10);
glScalef(1.8, 1.7, 1.5);
glutSolidSphere(23, 30, 30);
glPopMatrix();
}
void drawLeftEyes()
{
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(12, 10, 20);
glutSolidSphere(2, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(12,10, 22);
glutSolidSphere(1, 20, 20);
glPopMatrix();
}
void drawRightEyes()
{
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(-12, 10, 20);
glutSolidSphere(2, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(-12, 10, 22);
glutSolidSphere(1, 20, 20);
glPopMatrix();
}
/*
void drawSpot1()
{
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(40, 48, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot2()
{
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(30, 50, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot3()
{
glPushMatrix();
glColor3f(1, 1, 0);
glTranslatef(35, 45, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot4()
{
glPushMatrix();
glColor3f(1, 1, 0);
glTranslatef(35, 30, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot5()
{
glPushMatrix();
glColor3f(0, 1, 0);
glTranslatef(30, 40, 20);
glScalef(2, 1, 1);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot6()
{
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(30, 30, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot7()
{
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(-25, 30, 25);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot8()
{
glPushMatrix();
glColor3f(1, 1, 0);
glTranslatef(-30, 40, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot9()
{
glPushMatrix();
glColor3f(1, 1, 0);
glTranslatef(-25, 35, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot10()
{
glPushMatrix();
glColor3f(0, 1, 0);
glTranslatef(20, 35, 20);
glScalef(3, 1, 1);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot11()
{
glPushMatrix();
glColor3f(0, 1, 1);
glTranslatef(10, 35, 30);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot12()
{
glPushMatrix();
glColor3f(0, 1, 1);
glTranslatef(-5, 30, 20);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot13()
{
glPushMatrix();
glColor3f(0.5, 1, 1);
glTranslatef(-20, 45, 20);
glScalef(2, 1, 1);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot14()
{
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glTranslatef(-10, 45, 20);
glScalef(2, 1, 1);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
void drawSpot15()
{
glPushMatrix();
glColor3f(0.5, 0.5, 0.5);
glTranslatef(-5, 48, 20);
glScalef(2, 2, 1);
glutSolidSphere(1.5, 20, 20);
glPopMatrix();
}
*/
void drawHead1()
{
glPushMatrix();
glColor3f(1, 0, 0.5);
glTranslatef(0, 25, 10);
glScalef(2.5, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-10, 30, 20);
glScalef(1.0, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(5, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(15, 33, 10);
glScalef(0.8, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(5, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(10, 30, 20);
glScalef(0.8, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(5, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(20, 30, 20);
glScalef(1, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(5, 20, 20);
glPopMatrix();
}
void drawHead2()
{
glPushMatrix();
glColor3f(1, 0, 0.5);
glTranslatef(30, 25, -5);
glScalef(2.5, 1, 1 );
glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(45, 35, 3);
glScalef(0.2, 0.2, 0.3);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(37, 22, 3);
glScalef(0.3, 0.3, 0.3 );
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(45, 25, 3);
glScalef(0.3, 0.3, 0.3 );
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(30, 35, 3);
glScalef(0.3, 0.3, 0.3 );
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
}
void drawHead3()
{
glPushMatrix();
glColor3f(1, 0, 0.5);
glTranslatef(30, 35, -22);
glScalef(2.5, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(35, 40, -18);
glScalef(0.3, 0.2, 0.5);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(20, 38, -17);
glScalef(0.3, 0.2, 0.5);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(28, 45, -20);
glScalef(0.3, 0.2, 0.5);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(42, 40, -17);
glScalef(0.3, 0.2, 0.5);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
}
void drawHead5()
{
glPushMatrix();
glColor3f(1, 0, 0.5);
glTranslatef(-35, 25, -5);
glScalef(2.5, 1, 1);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-35, 35, 5);
glScalef(0.5, 0.2, 0.3);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-40, 25, 10);
glScalef(0.5, 0.2, 0.3);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
}
void drawHead4()
{
glPushMatrix();
glColor3f(1, 0, 0.5);
glTranslatef(-15, 35, -20);
glScalef(2.5, 1, 1);
glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-25, 43, -15);
glScalef(0.3, 0.3, 0.3);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-10, 45, -20);
glScalef(0.3, 0.3, 0.3);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(-20, 38, -5);
glScalef(0.3, 0.3, 0.3);
//glRotatef(-20, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glTranslatef(0, 43, -20);
glScalef(0.3, 0.3, 0.3);
//glRotatef(-35, 0, 0, 0);
glutSolidSphere(15, 20, 20);
glPopMatrix();
}
void drawMouth()
{
glPushMatrix();
glColor3f(1, 0, 0);
glTranslatef(0, 0, 30);
glScalef(1, -2, 1);
glutSolidSphere(5, 20, 20);
glPopMatrix();
}
void drawTorus()
{
glPushMatrix();
glColor3f(1, 1, 1);
glTranslatef(4, 36, -5.0);
glScalef(1.5, 0.5, 2.2);
glutSolidTorus(2.0, 5.0, 20, 20);
glPopMatrix();
}
void drawVileplume()
{
drawLeftLegs();
drawRightLegs();
drawLeftHands();
drawRightHands();
drawBody();
drawHead1();
drawHead2();
drawHead3();
drawHead4();
drawHead5();
drawLeftEyes();
drawRightEyes();
drawMouth();
drawTorus();
}
void MyDisplay()
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawaxis();
glPushMatrix();
glScalef(0.55, 0.55, 0.55);
//glRotatef(angle, 360, 0, 0);
drawVileplume();
glPopMatrix();
glutSwapBuffers ();
glutPostRedisplay ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0); // set background colour to black
glShadeModel (GL_FLAT);
glFrontFace(GL_CCW);
glEnable(GL_DEPTH_TEST);
}
void setupRC (void)
{
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (50.0, 1.45, 1.0, 1000.0);
//glOrtho(-50, 50, -50, 50, 1, 1000);
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
gluLookAt (20.0, 60.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
//gluLookAt(0.0, 0.0, 10.0, 0.0, 0.0, -100.0, 0.0, 1.0, 0.0);
//gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void rescale (GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
}
void Keyboard (unsigned char key, int x, int y)
{
switch (key)
{
glPushMatrix();
case 'r': glRotatef(1, 1, 0, 0); //rotate
drawVileplume();
break;
glPopMatrix();
glPushMatrix();
// bool checkjump() = false;
case 'j':glTranslatef(0, 1 ,0); //jump
scale++;
//checkjump = true;
drawVileplume();
break;
glPopMatrix();
glPushMatrix();
case 'd':
glTranslatef(1, 0, 0); //move right
drawVileplume();
break;
glPopMatrix();
glPushMatrix();
case 'a':glRotatef(-90, 1, 0, 0);
glTranslatef(1, 0, 0); //move left
drawVileplume();
break;
glPopMatrix();
glPushMatrix();
case 'f':glRotatef(-90, 0, 0, 0);
glTranslatef(1, 0, 0); //move front
drawVileplume();
break;
glPopMatrix();
}
}
void doMainLoop()
{
if (angle >= 180)
{
direction = true;
}
else if (angle <= 180)
{
direction = false;
}
if (direction) angle-= 5;
else angle+= 5;
if (scale >= 50)
{
direction = true;
}
else if (scale <= 50)
{
direction = false;
}
if (direction)
{
direction = false;
scale -= 5;
}
else
{
direction = true;
scale += 5;
}
}
void main (int argc, char * argv[])
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("My Assignment 1");
glutDisplayFunc (MyDisplay);
glutReshapeFunc (rescale); // called when window is resized
init ();
setupRC();
glutKeyboardFunc (Keyboard);
glutIdleFunc(doMainLoop);
glutMainLoop ();
}
I am basically building a vileplume in openGL. Ignore all of my code expect the doMainLoop and Keyboard function.Because my problem is when the user press 'j' to jump , the charcter would just keeps moving up without stopping. I have write a direction to stop after a certain limit but somehow could not work. Any1 tell me what is wrong with it.
-
Code:
case 'd':
glTranslatef(1, 0, 0); //move right
drawVileplume();
break;
glPopMatrix();
glPushMatrix();
When are you expecting these lines to execute?
-
Is there anything wrong with these lines?
-
Switch statements don't work quite the way you think they do, I believe.
The case blocks operate more like a goto than an if/else statement (given a small range of possible values, your compiler might use a jump table), and as such, stuff outside of a case/break block isn't going to get executed. That is, control flow jumps from the switch's condition (in this case, key) straight to the appropriate case statement, then executes until it hits a break, or the switch statement ends.
In the case of a break, the program jumps out of the switch statement immediately.
So your push/pop never executes.
-
There's something wrong with your knowledge of switch/case if you ever expect them to be executed.
Does your compiler complain about unreachable code?
-
My compiler work perfectly but not my animation. I think I miss out some points for the animation.
-
Seriously, i spend 2-3 hours trying to move the object from the top before going to bottom. But. somehow, the code could not work. I had completely no idea what should I do for the animation. Please help me.
Code:
glPushMatrix();
case 'j':
if (up <= 50 || down >= 50)
{
glTranslatef( 0, 1, 0);
up ++;
}
case 'J':
if ( up >= 50 || down <= 0 )
{
glTranslatef(0, -1, 0);
down++;
}
break;
glPopMatrix();
This is the part where I had problem.
-
Yes, and your push/pop matrix calls NEVER HAPPEN.
Is your compiler warning you about unreachable code?
-
then where should I placed the pop /push Matrix. My compiler all ok placing like this
-
Maybe this will convince you.
Code:
$ gcc -c -Wall -Wunreachable-code bar.c
bar.c: In function ‘main’:
bar.c:11: warning: will never be executed
bar.c:23: warning: will never be executed
$ cat bar.c
#include <stdio.h>
extern void glPushMatrix();
extern void glPopMatrix();
extern void glTranslatef(int, int, int);
int main()
{
int ch = 'j', up = 0, down = 1;
switch (ch) {
glPushMatrix();
case 'j':
if (up <= 50 || down >= 50) {
glTranslatef(0, 1, 0);
up++;
}
case 'J':
if (up >= 50 || down <= 0) {
glTranslatef(0, -1, 0);
down++;
}
break;
glPopMatrix();
}
return 0;
}
If not, put down the compiler until you learn how to use it.
-
Code:
glPushMatrix();
case 'j':
if (up <= 50 || down >= 50)
{
glTranslatef(0, 1, 0);
up++;
}
//case 'J':
if (up >= 50 || down <= 0)
{
glTranslatef(0, -1, 0);
down++;
}
break;
glPopMatrix();
Ok, nvm, i make it to move from top and back to middle. Nut the problem is I can copy press J once to move from top and back to the original postiion. I want to make it move from top and back to the original position if the user press J instead of just doing it once