I need help for X axis rotation of this object.
Currently if I press the mouse left/right button, then it will rotate only y axis. 1st
2nd
3rd
4th & so on... I am currently using Codeblockson Windows 10 with open source Glut packages
In which lines I have to change for X axis rotation of that cone that chanages the cones position for x axis.
#include<windows.h>
#include<bits/stdc++.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>
#include <sstream>
#include <vector>
static int slices = 16;
static int stacks = 16;
int rotation_lock=0,smotoh_color_transition_lock=0,mouse_rotation_zoom_lock=0,i;
GLuint colorCounter = 0, angle=0;
GLfloat lightXPos = 0.0f;
GLfloat lightYPos = 0.f;
static void resize(int width, int height)
{
const float ar = (float) width / (float) height; //windows size
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION); //projection
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0); //near, far value
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
}
static void display(void)
{
const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0; //time of camera/color change
const double a = t*90.0;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0,0,-6); //translate
glRotated(60,1,0,0); //rotate of angle, x,y
if (rotation_lock==1) //If auto rotate enabled
{
glRotated(a,0,0,1); //then it will rotate against
}
//glutSolidCube(1.0);
glutSolidCone(1,1,slices,stacks);
//glutSolidTeapot(1.0);
//glutSolidSphere(1.0,slices,stacks);
//glutSolidCube(1.5);
glPopMatrix();
//Color for the object
GLfloat diffColors[6][4] = {
{0.5, 0.5, 0.9, 1.0},
{0.9, 0.5, 0.5, 1.0},
{0.5, 0.9, 0.3, 1.0},
{0.9, 0.7, 0.2, 1.0},
{0.3, 0.8, 0.9, 1.0},
{1.0, 0.0, 0.0, 1.0}
}; //for c
GLfloat diffColors2[50][4] = {
{0, 0, 0.1, 1.0},
{0, 0, 0.2, 1.0},
{0, 0, 0.3, 1.0},
{0, 0, 0.4, 1.0},
{0, 0, 0.5, 1.0},
{0, 0, 0.6, 1.0},
{0, 0, 0.7, 1.0},
{0, 0, 0.8, 1.0},
{0, 0, 0.9, 1.0},
{0, 0, 1, 1.0},
{0.1, 0, 0, 1.0},
{0.2, 0, 0, 1.0},
{0.3, 0, 0, 1.0},
{0.4, 0, 0, 1.0},
{0.5, 0, 0, 1.0},
{0.6, 0, 0, 1.0},
{0.7, 0, 0, 1.0},
{0.8, 0, 0, 1.0},
{0.9, 0, 0, 1.0},
{1, 0, 0, 1.0},
{0, 0.1, 0, 1.0},
{0, 0.2, 0, 1.0},
{0, 0.3, 0, 1.0},
{0, 0.4, 0, 1.0},
{0, 0.5, 0, 1.0},
{0, 0.6, 0, 1.0},
{0, 0.7, 0, 1.0},
{0, 0.8, 0, 1.0},
{0, 0.9, 0, 1.0},
{0, 1, 0, 1.0},
{0.1, 0.1, 0, 1.0},
{0.2, 0.1, 0, 1.0},
{0.3, 0.1, 0, 1.0},
{0.4, 0.1, 0, 1.0},
{0.5, 0.1, 0, 1.0},
{0.6, 0.1, 0, 1.0},
{0.7, 0.1, 0, 1.0},
{0.8, 0.1, 0, 1.0},
{0.9, 0.1, 0, 1.0},
{1, 0.1, 0, 1.0},
{0, 0.1, 0, 1.0},
{0, 0.2, 0.1, 1.0},
{0, 0.3, 0.1, 1.0},
{0, 0.4, 0.1, 1.0},
{0, 0.5, 0.1, 1.0},
{0, 0.6, 0.1, 1.0},
{0, 0.7, 0.1, 1.0},
{0, 0.8, 0.1, 1.0},
{0, 0.9, 0.1, 1.0},
{0, 1, 0.1, 1.0}
}; //for transition
if(smotoh_color_transition_lock==1) //If auto color transition enabled
{
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffColors2[colorCounter]); //t count
}
else
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffColors[colorCounter]); // c count
// Define specular color and shininess
GLfloat specColor[] = {1.0, 1.0, 1.0, 1.0};
GLfloat shininess[] = {100.0};
// Note that the specular color and shininess can stay constant
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, shininess);
// Set light properties
// Light color (RGBA)
GLfloat Lt0diff[] = {1.0,1.0,1.0,1.0};
// Light position
GLfloat Lt0pos[] = {1.0f + lightXPos, 1.0f + lightYPos, 5.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, Lt0diff);
glLightfv(GL_LIGHT0, GL_POSITION, Lt0pos); //light position change
glutSwapBuffers();
}
static void key(unsigned char key, int x, int y)
{
switch (key)
{
case 'w': //Move up
glTranslatef(0,0.1,0);
glutPostRedisplay();
break;
case 's': //Move down
glTranslatef(0,-0.1,0);
glutPostRedisplay();
break;
case 'd'://Move right
glTranslatef(0.1,0,0);
glutPostRedisplay();
break;
case 'a': //Move left
glTranslatef(-0.1,0,0);
glutPostRedisplay();
break;
case 'c': //Color toggle
smotoh_color_transition_lock=0;
colorCounter += 1;
if(colorCounter>5)
colorCounter=0;
break;
case 't': //Enable/Disable smooth color transition
smotoh_color_transition_lock=1; //c enabled
colorCounter += 1;
if(colorCounter>49) //
colorCounter=0;
break;
case 'r': //Enable/Disable auto rotation
if(rotation_lock==0)
rotation_lock=1;
else
rotation_lock=0;
break;
case 'x': //Switch between rotation or zoom using mouse action
if(mouse_rotation_zoom_lock==0)
mouse_rotation_zoom_lock=1;
else
mouse_rotation_zoom_lock=0;
break;
case 27 : //exit when ESC is pressed
case 'q': //exit when q is pressed
exit(0);
break;
default:
printf("Unhandled key press %c\n",key);
}
glutPostRedisplay();
}
void specialFunc( int key, int x, int y )
{
switch ( key )
{
case GLUT_KEY_UP: //UP key to move the light source UP
lightYPos += 0.5f; //ligh post change
break;
case GLUT_KEY_DOWN: //DOWN key to move the light source DOWN
lightYPos += -0.5f;
break;
case GLUT_KEY_LEFT: //LEFT key to move the light source LEFT
lightXPos += -0.5f;
break;
case GLUT_KEY_RIGHT: //RIGHT key to move the light source RIGHT
lightXPos += 0.5f;
break;
}
// this will refresh the screen so that the user sees the light position
glutPostRedisplay();
}
void mouse(int button, int state, int mousex, int mousey) //mouse function
{
if (mouse_rotation_zoom_lock==1) // If zoom enabled //x chap dile
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
glScalef(1.2,1.2,1); //zoom in
}
else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
glScalef(0.7,0.7,1); //zoom out
}
}
else // If rotation enabled // x na chaple
{
if(button==GLUT_LEFT_BUTTON && state==GLUT_UP)
{
glRotatef(25,0,0,1); //rotate
}
else if(button==GLUT_RIGHT_BUTTON && state==GLUT_DOWN)
{
glRotatef(-25,0,0,1);
}
}
glutPostRedisplay();
}
static void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(500,500);
glutInitWindowPosition(250,250);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("OpenGL");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutSpecialFunc(specialFunc);
glutMouseFunc(mouse);
glutIdleFunc(idle);
//glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glutMainLoop();
return EXIT_SUCCESS;
}
I've been slowly learning how to work with 3d graphics and C++ and I've been working with an RGBA using an example from Ed angels interactive computer graphics. When trying to change it to have a color on each side, I hit a wall in either my understanding or I'm not seeing something obvious. A snippet of the original example I used that created a RGBA cube is this:
const int NumVertices = 36;
point4 points[NumVertices];
color4 colors[NumVertices];
//Vertices of a unit cube centered at origin, sides aligned with axes
point4 vertices[8] = {
point4( -1.0, -1.0, 1.0, 1.0),
point4( -1.0, 1.0, 1.0, 1.0),
point4( 1.0, 1.0, 1.0, 1.0),
point4( 1.0, -1.0, 1.0, 1.0),
point4( -1.0, -1.0, -1.0, 1.0),
point4( -1.0, 1.0, -1.0, 1.0),
point4( 1.0, 1.0, -1.0, 1.0),
point4( 1.0, -1.0, -1.0, 1.0)
};
//RGBA colors
color4 vertex_colors[8] = {
color4( 0.0, 0.0, 0.0, 1.0), //black
color4( 1.0, 0.0, 0.0, 1.0), //red
color4( 1.0, 1.0, 0.0, 1.0), //yellow
color4( 0.0, 1.0, 0.0, 1.0), //blue
color4( 0.0, 0.0, 1.0, 1.0), //green
color4( 1.0, 0.0, 1.0, 1.0), //magenta
color4( 1.0, 1.0, 1.0, 1.0), //white
color4( 0.0, 1.0, 1.0, 1.0) //cyan
};
//Array of rotation angles (in degrees) for each coordinate axis
enum { Xaxis = 0, Yaxis = 1, Zaxis = 2, NumAxes = 3 };
int Axis = Xaxis;
GLfloat Theta[NumAxes] = { 0.0, 0.0, 0.0};
GLuint theta; //The location of the "theta" shader uniform variable
//----------------------------------------------------------------------------
//quad generates two triangles for each face and assigns colors to the vertices
int Index = 0;
void
quad( int a, int b, int c, int d)
{
colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;
colors[Index] = vertex_colors[b]; points[Index] = vertices[b]; Index++;
colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++;
colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;
colors[Index] = vertex_colors[c]; points[Index] = vertices[c]; Index++;
colors[Index] = vertex_colors[d]; points[Index] = vertices[d]; Index++;
}
//----------------------------------------------------------------------------
//generate 12 triangles: 36 vertices and 36 colors
void
colorcube( void )
{
quad( 1, 0, 3, 2);
quad( 2, 3, 7, 6);
quad( 3, 0, 4, 7);
quad( 6, 5, 1, 2);
quad( 4, 5, 6, 7);
quad( 5, 4, 0, 1);
}
Now all i thought I had to do from what I read was just change the all the parameters of vertex_colors in quad to a making them all the same like this:
int Index = 0;
void
quad( int a, int b, int c, int d)
{
colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;
colors[Index] = vertex_colors[a]; points[Index] = vertices[b]; Index++;
colors[Index] = vertex_colors[a]; points[Index] = vertices[c]; Index++;
colors[Index] = vertex_colors[a]; points[Index] = vertices[a]; Index++;
colors[Index] = vertex_colors[a]; points[Index] = vertices[c]; Index++;
colors[Index] = vertex_colors[a]; points[Index] = vertices[d]; Index++;
}
But when I did this all the was generated was a blank white window. So what I'm confused on is there more then just changing the parameters or do I need to work with something else to get it working?
Basically, I have included the block of code which Compiles & appears to run for that matter, but the graphics does not display on the menus I have initialized. I know the code should display, because it compiles, runs & displays what it's suppose to on Linux. However, after transferring the code to Visual Studios, & going through a heavy amount of adjustments( glu, glut, & compatability adjustments ) it finally compiled, but now another problem has arised, which is no graphics that clearly displayed on another machine. :(
#include "stdafx.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include"SDL.h"
#include"SDL_opengl.h"
#include"glut.h"
#include < tchar.h >
#include < cstdlib >
#include < ctime >
#include < iostream >
#include < fstream >
#include < stdlib.h >
#include < stdio.h >
#include < string >
using namespace std;
#ifdef GL_VERSION_1_1
#define POINTER 1
#define INTERLEAVED 2
#define DRAWARRAY 1
#define ARRAYELEMENT 2
#define DRAWELEMENTS 3
int setupMethod = POINTER;
int derefMethod = DRAWARRAY;
static int legs = 0;
void setupPointers( void );
void init( void );
void display( void );
void reshape( int, int );
void keyboard( unsigned char, int, int );
int _tmain( int argc, char **argv ) {
glutInit( &argc, argv );
glutInitDisplayMode ( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize ( 500, 500 );
glutInitWindowPosition ( 100, 100 );
glutCreateWindow ( argv[0] );
init( );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
glutKeyboardFunc( keyboard );
glutMainLoop( );
return 0;
}
void setupPointers( void ) {
static GLint vertices[ ] = {
//spiderbody
-10, 35,
10, 35,
20, 55,
10, 75,
-10, 75,
-20, 55,
-10,35,
-30,25,
-30,-25,
-10,-35,
10,-35,
30,-25,
30,25,
10,35,
//legs
//leg1 position1
20,30, 40,42.5,32.5,77.5,47.5,42.5,30,25,
//leg2pos1
30,12.5,50,22.5,55,42.5,56,17.5,30,5,
//leg3pos1
30,-12.5,50,-22.5,55,-42.5,56,-17.5,30,-5,
//leg4pos1
20,-30, 40,-42.5,32.5,-77.5,47.5,-42.5,30,-25,
//leg5pos1
-20,30, -40,42.5,-32.5,77.5,-47.5,42.5,-30,25,
//leg6pos1
-30,12.5,-50,22.5,-55,42.5,-56,17.5,-30,5,
//leg7pos1
-30,-12.5,-50,-22.5,-55,-42.5,-56,-17.5,-30,-5,
//leg8pos1
-20,-30, -40,-42.5,-32.5,-77.5,-47.5,-42.5,-30,-25,
//leg1pos2
20,30,45,37.5,50,75,50,32.5,30,25,
//leg2pos2
30,12.5,51,12.5,47.5,22.5,60,5,30,5,
//leg3pos2
30,-12.5,51,-12.5,47.5,-22.5,60,-5,30,-5,
//leg4pos2
20,-30,45,-37.5,50,-75,50,-32.5,30,-25,
//leg5pos2
-20,30,-45,37.5,-50,75,-50,32.5,-30,25,
//leg6pos2
-30,12.5,-51,12.5,-47.5,22.5,-60,5,-30,5,
//leg7pos2
-30,-12.5,-51,-12.5,-47.5,-22.5,-60,-5,-30,-5,
//leg8pos2
-20,-30,-45,-37.5,-50,-75,-50,-32.5,-30,-25
};
static GLfloat colors[ ] = {
0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
1, 0, 0,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5,0.37, 0.35, 0.62,
0.5, 0.75, 0.25,
0.75, 0.75,0,
0.35, 0.35, 0.35,
0.5, 0.75, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.75,
0.75, 0.35, 0.35,
0.5, 0.28, 0.35,
0.5, 0.5, 0.5,
0.5, 0.5, 0.5, 0.75, 0.35, 0.35,
0.5, 0.28, 0.35
}; // COLORS
glEnableClientState ( GL_VERTEX_ARRAY );
glEnableClientState ( GL_COLOR_ARRAY );
glVertexPointer ( 2, GL_INT, 0, vertices );
glColorPointer ( 3, GL_FLOAT, 0, colors );
}
void init(void) {
glColor3f( 1,1,1 );
glutWireSphere( 100,100,100 );
glShadeModel( GL_SMOOTH );
setupPointers ( );
}
void display( void ) {
glClear (GL_COLOR_BUFFER_BIT);
glDrawArrays (GL_POLYGON, 0, 14);
if( legs % 5 == 0 || legs < 5 || legs%5==1 ) {
glDrawArrays (GL_POLYGON,15,4);
glDrawArrays (GL_POLYGON,20,4);
glDrawArrays (GL_POLYGON,25,4);
glDrawArrays (GL_POLYGON,30,4);
glDrawArrays (GL_POLYGON,35,4);
glDrawArrays (GL_POLYGON,40,4);
glDrawArrays (GL_POLYGON,45,4);
glDrawArrays (GL_POLYGON,50,4);
}
//second position of legs
else if( legs % 5 !=0 && legs%5!=1 ) {
glDrawArrays (GL_POLYGON,55,4);
glDrawArrays (GL_POLYGON,60,4);
glDrawArrays (GL_POLYGON,65,4);
glDrawArrays (GL_POLYGON,70,4);
glDrawArrays (GL_POLYGON,75,4);
glDrawArrays (GL_POLYGON,80,4);
glDrawArrays (GL_POLYGON,85,4);
glDrawArrays (GL_POLYGON,90,4);
}
}
void reshape ( int w, int h ) {
glViewport ( 0, 0, (GLsizei) w, (GLsizei) h );
gluOrtho2D ( -60.0, (GLdouble) w, -60.0, (GLdouble) h );
}
void keyboard ( unsigned char key, int x, int y ) {
switch ( key ) {
case 'd':
glTranslatef( 1,1,0 );
glRotatef( -5,0,0,1.0 );
legs++;
break;
case 'a':
glTranslatef( 1,1,0 );
glRotatef( 5,0,0,1.0 );legs++;
break;
case 'w':
glTranslatef( 0,5,0 );legs++;
break;
case 's':
glTranslatef( 0,-5,0 );legs++;
break;
case 27:
exit(0);
break;
default:
break;
}
glutPostRedisplay( );
}
#else
#endif