Rotation in X axis for object OpenGL - c++

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;
}

Related

How to change colors in OpenGL?

I want to color every part of the coordinate plane in different color (y in red, x in blue, z in green) but it seems that he remembers only the color of the lighting(red). So if someone can help me with that problem, i would be grateful.
#include <GL/glut.h>
//func
void setCamera();
void drawCP();
void drawPoints();
void drawCrtice();
GLfloat light_diffuse[] = {1.0, 0.0, .0, 0.0}; /* Red diffuse light. */
GLfloat light_position[] = {0.0, 0.0, 1.0, 0.0}; /* Infinite light location. */
GLfloat n[6][3] = {/* Normals for the 6 faces of a cube. */
{-1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{1.0, 0.0, 0.0},
{0.0, -1.0, 0.0},
{0.0, 0.0, 1.0},
{0.0, 0.0, -1.0}
};
GLint faces[6][4] = {/* Vertex indices for the 6 faces of a cube. */
{0, 1, 2, 3},
{3, 2, 6, 7},
{7, 6, 5, 4},
{4, 5, 1, 0},
{5, 6, 2, 1},
{7, 4, 0, 3}
};
GLfloat v[8][3]; /* Will be filled in with X,Y,Z vertexes. */
void
drawBox(void) {
int i;
for (i = 0; i < 6; i++) {
glBegin(GL_QUADS);
glNormal3fv(&n[i][0]);
glVertex3fv(&v[faces[i][0]][0]);
glVertex3fv(&v[faces[i][1]][0]);
glVertex3fv(&v[faces[i][2]][0]);
glVertex3fv(&v[faces[i][3]][0]);
glEnd();
}
}
void
display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
drawBox();
drawCP();
glClearColor(0.6f, 0.6f, 0.6f, 1.0f);
glRotatef(0.2, 0, 5, 0);
glutPostRedisplay();
glutSwapBuffers();}
void
init(void) {
/* Setup cube vertex data. */
v[0][0] = v[1][0] = v[2][0] = v[3][0] = -1;
v[4][0] = v[5][0] = v[6][0] = v[7][0] = 1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = -1;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = 1;
v[0][2] = v[3][2] = v[4][2] = v[7][2] = 1;
v[1][2] = v[2][2] = v[5][2] = v[6][2] = -1;
/* Enable a single OpenGL light. */
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
/* Use depth buffering for hidden surface elimination. */
glEnable(GL_DEPTH_TEST);
setCamera();}
void drawLines() {
glBegin(GL_LINES);
for (int i = 10; i != 0; i--) {
// po x osi
glVertex2f(-i, -0.1);
glVertex2f(-i, 0.1);
glVertex2f(i, -0.1);
glVertex2f(i, 0.1);
//po y osi
glVertex2f(-0.1, -i);
glVertex2f(0.1, -i);
glVertex2f(-0.1, i);
glVertex2f(0.1, i);
//po z osi
glColor3f(1.0f, 0.0f, 0.0f); //seems it doesnt register
glVertex3f(0, -0.3, -i);
glVertex3f(0, 0.3, -i);
glVertex3f(0, -0.3, i);
glVertex3f(0, 0.3, i);}
glEnd();}
void drawCP() {
//drawPoints();
drawLines();
glBegin(GL_LINES);
glLineWidth(100);
glVertex2f(-10.0f, 0);
glVertex2f(10.0f, 0);
glVertex2f(0.0f, 10);
glVertex2f(0.0f, -10);
glEnd();
}
void setCamera() {
/* Setup the view of the cube. */
glMatrixMode(GL_PROJECTION);
gluPerspective(/* field of view in degree */ 50.0, /* aspect ratio */ 1, /* Z near */ 1, /* Z far */ 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
/* Setup the view of the cube. */
gluLookAt(0, 0, 10 /*pozicija kamere*/, 0, 0, 0/* u koju tocku gleda ta kamera*/, 0, 1, 0 /*moran jos viti ca je to*/);
}
int
main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutCreateWindow("red 3D lighted cube");
glutDisplayFunc(display);
init();
// glutTimerFunc(10, rotate, 10);
glutMainLoop();
return 0; /* ANSI C requires main to return int. */
}
Try adding in: glEnable(GL_COLOR_MATERIAL);. That tells OpenGL your material properties are going to be defined with the glColor() function, it can be expanded on but that should get you what you expect.

Moving a 3D object with a mouse in OpenGL

I have been trying to make a whale in OpenGL. We have been trying to move the whale using a mouse input. The problem we are facing is with the syntax as well as the logic. We understand that the whale we are making is in a 3 dimensional world whereas the mouse input in in 2D for x and y on the screen.
If someone can be very specific with the syntax of using mouse input relevant to our code, that'd be great!
#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdlib.h>
#include "glut.h"
#include <math.h>
GLUquadric *qobja;
float movez = 0;
#define W_SCREEN 1366
#define H_SCREEN 768
#define TERR_D 75
#define TERR_W 750
#define pi 3.14
int mou_x = 0, mou_y = 0;
float alpha = 0;
float fahad = 0;
GLfloat ctrlpoints[4][4][3] = {
{ { -1.5, -1.5, 4.0 },
{ -0.5, -1.5, 2.0 },
{ 0.5, -1.5, -1.0 },
{ 1.5, -1.5, 2.0 } },
{ { -1.5, -0.5, 1.0 },
{ -0.5, -0.5, 3.0 },
{ 0.5, -0.5, 0.0 },
{ 1.5, -0.5, -1.0 } },
{ { -1.5, -1.5, 4.0 },
{ -0.5, -1.5, 2.0 },
{ 0.5, -1.5, -1.0 },
{ 1.5, -1.5, 2.0 } },
{ { -1.5, 1.5, -2.0 },
{ -0.5, 1.5, -2.0 },
{ 0.5, 1.5, 0.0 },
{ 1.5, 1.5, -1.0 } }
};
float cam_xrot = 180, cam_yrot = 180, cam_zrot = 0;
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat mat_ambient[] = { 0.7, 0.7, 0.7, 1.0 };
GLfloat mat_ambient_color[] = { 0.8, 0.8, 0.2, 1.0 };
GLfloat mat_diffuse[] = { 0.1, 0.5, 0.8, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat no_shininess[] = { 0.0 };
GLfloat low_shininess[] = { 5.0 };
GLfloat high_shininess[] = { 100.0 };
GLfloat mat_emission[] = { 0.3, 0.2, 0.2, 0.0 };
// Function Prototypes ////////////////////////////////////
void drawTerrain();
void drawAxes();
//drawing function decs
void draw_frust(float inner, float outer, float height, float m_z);
void cleanup();
void camera();
void pyramid();
void draw_closed_cyl(float a, float b, float c);
void draw_whale();
/* Initialize z-buffer, projection matrix, light source,
* and lighting model. Do not specify a material property here.
*/
void initLight(void)
{
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat position[] = { 0.0, 0.0, 2.0, 1.0 };
GLfloat mat_diffuse[] = { 0.6, 0.6, 0.6, 1.0 };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_DEPTH_TEST);
glMap2f(GL_MAP2_VERTEX_3, 0, 1, 3, 4,
0, 1, 12, 4, &ctrlpoints[0][0][0]);
glEnable(GL_MAP2_VERTEX_3);
glEnable(GL_AUTO_NORMAL);
glMapGrid2f(20, 0.0, 1.0, 20, 0.0, 1.0);
initLight();
}
///////////////////////////////////////////////////////////
void display(void)
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
camera();
drawAxes();
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(0, 0, movez);
draw_whale();
glPopMatrix();
}
void draw_whale()
{
glPushMatrix();
glTranslatef(0, 2, 90);
glRotatef(180, 0.0, 0.0, 1.0);
glScalef(1, 1, 2);
glEvalMesh2(GL_FILL, 0, 25, 0, 25);
glPopMatrix();
glPushMatrix();
float count = 5;
float a = 5;
for (float i = 0; i <= 16; i = i + 1)
{
draw_frust(a*sqrt(i), a*sqrt((i + 1)), 2, count * 2);
count++;
alpha = i + 1;
}
//a few smooth runs
for (int j = 0; j < 3; j++)
{
draw_frust(a*sqrt(alpha), a*sqrt(alpha), 2, count * 2);
count++;
}
float co = count;
for (float i = 17; i >= 3; i = i - 1)
{
draw_frust(a*sqrt(i), a*sqrt(i - 1), 2, co * 2);
co++;
fahad = a*sqrt(i - 1);
}
draw_frust(7.07, 5, 2, co * 2);
co++;
draw_frust(5, 3, 2, co * 2);
co++;
draw_frust(3, 1, 2, co * 2);
glPushMatrix();
//fin left
glPushMatrix();
glTranslatef(-25.5, 10, 25);
glRotatef(100, 1, 0, 0);
glRotatef(135, 0, 1, 0);
glRotatef(45, 0, 0, 1);
glScalef(1, 6, 1);
draw_closed_cyl(0, 0, 0);
glPopMatrix();
//fin right
glPushMatrix();
glTranslatef(25.5, 10, 25);
glRotatef(100, 1, 0, 0);
glRotatef(-135, 0, 1, 0);
glRotatef(-45, 0, 0, 1);
glScalef(1, 6, 1);
draw_closed_cyl(0, 0, 0);
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void draw_frust(float inner, float outer, float height, float m_z)
{
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(0, 0, m_z);
gluDisk(qobja, 0.0, inner, 200, 20);
gluCylinder(qobja, inner, outer, height, 200, 200);
glPopMatrix();
}
void draw_closed_cyl(float a, float b, float c)
{
qobja = gluNewQuadric();
gluQuadricNormals(qobja, GLU_SMOOTH);
glPushMatrix();
glTranslatef(a, b + 2, c);
glRotatef(120, 0, 0, 1);
gluCylinder(qobja, 3.0, 3.0, 2.0, 20, 20);
gluDisk(qobja, 0, 3.0, 20, 20);
glTranslatef(0, 0, 2.0);
gluDisk(qobja, 0, 3.0, 20, 20);
glPopMatrix();
}
void drawTerrain(){
GLfloat color[] = { 0.2, 0.8, 0.2 };
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glColor3f(0.2, 0.8, 0.2); // this line is not needed when lighting in enabled
glPushMatrix();
glTranslatef(-TERR_W / 2, 0.0, -TERR_D / 2);
glBegin(GL_POLYGON);
glVertex3f(0, 0, 0);
glVertex3f(TERR_W, 0, 0);
glVertex3f(TERR_W, 0, TERR_D);
glVertex3f(0, 0, TERR_D);
glVertex3f(0, 0, 0);
glEnd();
glPopMatrix();
}
void drawAxes(){
glColor3d(1, 0, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(3, 0, 0);
glEnd();
glColor3d(0, 1, 0);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 3, 0);
glEnd();
glColor3d(0, 0, 1);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(0, 0, 3);
glEnd();
}
///////////////////////////////////////////////////////////
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(1.5*60.0, (GLfloat)w / (GLfloat)h, 1, 1.5*120.0);
glMatrixMode(GL_MODELVIEW);
camera();
}
///////////////////////////////////////////////////////////
void camera(){
glLoadIdentity();
glTranslatef(0, 0, -110);
glRotatef(cam_xrot, 1, 0, 0);
glRotatef(cam_yrot, 0, 1, 0);
glRotatef(cam_zrot, 0, 0, 1);
}
///////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y)
{
// Camera controls - Rotation along principle axis
switch (key) {
case 'n':
if (movez <= -100)
movez = movez + 100;
else
movez = movez - 3;
break;
case 'q':
cam_xrot += 10;
if (cam_xrot >360) cam_xrot -= 360;
break;
case 'z':
cam_xrot -= 10;
if (cam_xrot < -360) cam_xrot += 360;
break;
case 'a':
cam_yrot += 10;
if (cam_yrot >360) cam_yrot -= 360;
break;
case 'd':
cam_yrot -= 10;
if (cam_yrot < -360) cam_yrot += 360;
break;
case 'w':
cam_zrot += 10;
if (cam_zrot >360) cam_zrot -= 360;
break;
case 'x':
cam_zrot -= 10;
if (cam_zrot < -360) cam_zrot += 360;
break;
case 27:
cleanup();
exit(0);
break;
default:
break;
}
glutPostRedisplay();
}
///////////////////////////////////////////////////////////
void cleanup() // call once when you exit program
{
}
///////////////////////////////////////////////////////////
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(W_SCREEN, H_SCREEN);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(25, update, 0);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}

OpenGL Perspective

I'm trying to depict a cube using a perspective projection, but all I get is the corner of a square. The face of the square is set at the origin and expands in the positive direction. Using glOrtho I can set the coordinate system, but I'm having trouble doing the same thing using glPerspective.
#include <gl/glut.h>
void mesh(void) {
float v[8][3] = { /* Vertices for 8 corners of a cube. */
{0.0, 0.0, 0.0}, {100.0, 0.0, 0.0}, {100.0, 100.0, 0.0}, {0.0, 100.0, 0.0},
{0.0, 0.0, -100.0}, {100.0, 0.0, -100.0}, {100.0, 100.0, -100.0}, {0.0, 100.0, -100.0} };
float n[6][3] = { /* Normals for the 6 faces of a cube. */
{0.0, 0.0, 1.0}, {1.0, 0.0, 0.0}, {0.0, 1.0, 0.0},
{-1.0, 0.0, 0.0}, {0.0, -1.0, 0.0}, {0.0, 0.0, -1.0} };
int f[6][4] = { /* Indexes of the vertices in v that make the 6 faces of a cube. */
{0, 1, 2, 3}, {1, 5, 6, 2}, {3, 2, 6, 7},
{0, 4, 7, 3}, {0, 1, 5, 4}, {4, 5, 6, 7} };
for (int j = 0; j < 6; j++) {
glBegin(GL_QUADS);
glNormal3fv(&n[j][0]);
glVertex3fv(&v[f[j][0]][0]);
glVertex3fv(&v[f[j][1]][0]);
glVertex3fv(&v[f[j][2]][0]);
glVertex3fv(&v[f[j][3]][0]);
glEnd();
glFlush();
}
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 0.0, 0.0);
mesh();
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB |GLUT_DEPTH |GLUT_SINGLE);
glutInitWindowSize(400, 300);
glutInitWindowPosition(200, 200);
glutCreateWindow("Mesh");
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glRotatef(15, 0.0, 0.0, 1.0);
//glOrtho(-400.0, 400.0, -300.0, 300.0, 200.0, -200.0);
gluPerspective(120,1,0,600);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glutDisplayFunc(display);
glutMainLoop();
}
You say you only see corners of the cube? Then your Field of view is too wide.. you are using gluPerspective() and providing your calculations are correct.. the values are a bit off imo, the function parameters are:
void gluPerspective(GLdouble fovy,
GLdouble aspect_ratio,
GLdouble zNear,
GLdouble zFar);
i propose changing that to something like
gluPerspective(45.0f,
width_of_window / height_of_window, //aspect ratio
0.1f,
500.0f);

how can we make spot lights

i need to draw street lights, but it is illuminating the entire environment, i couldnt make it seem like a spot light. The code below is the one i tried
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0) ;
GLfloat color[4] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position[4] = {SCREEN_W/2,SCREEN_H/2,0.0,0.0};
float LightDir[3] = {0.0f, 0.0f, -1.0f}; // towards the viewer
glLighti(GL_LIGHT0, GL_SPOT_CUTOFF, 30);
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, LightDir);
glLightfv(GL_LIGHT0, GL_DIFFUSE, color);
Like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
/* Some <math.h> files do not define M_PI... */
#ifndef M_PI
#define M_PI 3.14159265
#endif
#define TWO_PI (2*M_PI)
typedef struct lightRec {
float amb[4];
float diff[4];
float spec[4];
float pos[4];
float spotDir[3];
float spotExp;
float spotCutoff;
float atten[3];
float trans[3];
float rot[3];
float swing[3];
float arc[3];
float arcIncr[3];
} Light;
static int useSAME_AMB_SPEC = 1;
/* *INDENT-OFF* */
static float modelAmb[4] = {0.2, 0.2, 0.2, 1.0};
static float matAmb[4] = {0.2, 0.2, 0.2, 1.0};
static float matDiff[4] = {0.8, 0.8, 0.8, 1.0};
static float matSpec[4] = {0.4, 0.4, 0.4, 1.0};
static float matEmission[4] = {0.0, 0.0, 0.0, 1.0};
/* *INDENT-ON* */
#define NUM_LIGHTS 3
static Light spots[] =
{
{
{0.2, 0.0, 0.0, 1.0}, /* ambient */
{0.8, 0.0, 0.0, 1.0}, /* diffuse */
{0.4, 0.0, 0.0, 1.0}, /* specular */
{0.0, 0.0, 0.0, 1.0}, /* position */
{0.0, -1.0, 0.0}, /* direction */
{20.0},
{60.0}, /* exponent, cutoff */
{1.0, 0.0, 0.0}, /* attenuation */
{0.0, 1.25, 0.0}, /* translation */
{0.0, 0.0, 0.0}, /* rotation */
{20.0, 0.0, 40.0}, /* swing */
{0.0, 0.0, 0.0}, /* arc */
{TWO_PI / 70.0, 0.0, TWO_PI / 140.0} /* arc increment */
},
{
{0.0, 0.2, 0.0, 1.0}, /* ambient */
{0.0, 0.8, 0.0, 1.0}, /* diffuse */
{0.0, 0.4, 0.0, 1.0}, /* specular */
{0.0, 0.0, 0.0, 1.0}, /* position */
{0.0, -1.0, 0.0}, /* direction */
{20.0},
{60.0}, /* exponent, cutoff */
{1.0, 0.0, 0.0}, /* attenuation */
{0.0, 1.25, 0.0}, /* translation */
{0.0, 0.0, 0.0}, /* rotation */
{20.0, 0.0, 40.0}, /* swing */
{0.0, 0.0, 0.0}, /* arc */
{TWO_PI / 120.0, 0.0, TWO_PI / 60.0} /* arc increment */
},
{
{0.0, 0.0, 0.2, 1.0}, /* ambient */
{0.0, 0.0, 0.8, 1.0}, /* diffuse */
{0.0, 0.0, 0.4, 1.0}, /* specular */
{0.0, 0.0, 0.0, 1.0}, /* position */
{0.0, -1.0, 0.0}, /* direction */
{20.0},
{60.0}, /* exponent, cutoff */
{1.0, 0.0, 0.0}, /* attenuation */
{0.0, 1.25, 0.0}, /* translation */
{0.0, 0.0, 0.0}, /* rotation */
{20.0, 0.0, 40.0}, /* swing */
{0.0, 0.0, 0.0}, /* arc */
{TWO_PI / 50.0, 0.0, TWO_PI / 100.0} /* arc increment */
}
};
static void
usage(char *name)
{
printf("\n");
printf("usage: %s [options]\n", name);
printf("\n");
printf(" Options:\n");
printf(" -geometry Specify size and position WxH+X+Y\n");
printf(" -lm Toggle lighting(SPECULAR and AMBIENT are/not same\n");
printf("\n");
#ifndef EXIT_FAILURE /* should be defined by ANSI C <stdlib.h> */
#define EXIT_FAILURE 1
#endif
exit(EXIT_FAILURE);
}
static void
initLights(void)
{
int k;
for (k = 0; k < NUM_LIGHTS; ++k) {
int lt = GL_LIGHT0 + k;
Light *light = &spots[k];
glEnable(lt);
glLightfv(lt, GL_AMBIENT, light->amb);
glLightfv(lt, GL_DIFFUSE, light->diff);
if (useSAME_AMB_SPEC)
glLightfv(lt, GL_SPECULAR, light->amb);
else
glLightfv(lt, GL_SPECULAR, light->spec);
glLightf(lt, GL_SPOT_EXPONENT, light->spotExp);
glLightf(lt, GL_SPOT_CUTOFF, light->spotCutoff);
glLightf(lt, GL_CONSTANT_ATTENUATION, light->atten[0]);
glLightf(lt, GL_LINEAR_ATTENUATION, light->atten[1]);
glLightf(lt, GL_QUADRATIC_ATTENUATION, light->atten[2]);
}
}
static void
aimLights(void)
{
int k;
for (k = 0; k < NUM_LIGHTS; ++k) {
Light *light = &spots[k];
light->rot[0] = light->swing[0] * sin(light->arc[0]);
light->arc[0] += light->arcIncr[0];
if (light->arc[0] > TWO_PI)
light->arc[0] -= TWO_PI;
light->rot[1] = light->swing[1] * sin(light->arc[1]);
light->arc[1] += light->arcIncr[1];
if (light->arc[1] > TWO_PI)
light->arc[1] -= TWO_PI;
light->rot[2] = light->swing[2] * sin(light->arc[2]);
light->arc[2] += light->arcIncr[2];
if (light->arc[2] > TWO_PI)
light->arc[2] -= TWO_PI;
}
}
static void
setLights(void)
{
int k;
for (k = 0; k < NUM_LIGHTS; ++k) {
int lt = GL_LIGHT0 + k;
Light *light = &spots[k];
glPushMatrix();
glTranslatef(light->trans[0], light->trans[1], light->trans[2]);
glRotatef(light->rot[0], 1, 0, 0);
glRotatef(light->rot[1], 0, 1, 0);
glRotatef(light->rot[2], 0, 0, 1);
glLightfv(lt, GL_POSITION, light->pos);
glLightfv(lt, GL_SPOT_DIRECTION, light->spotDir);
glPopMatrix();
}
}
static void
drawLights(void)
{
int k;
glDisable(GL_LIGHTING);
for (k = 0; k < NUM_LIGHTS; ++k) {
Light *light = &spots[k];
glColor4fv(light->diff);
glPushMatrix();
glTranslatef(light->trans[0], light->trans[1], light->trans[2]);
glRotatef(light->rot[0], 1, 0, 0);
glRotatef(light->rot[1], 0, 1, 0);
glRotatef(light->rot[2], 0, 0, 1);
glBegin(GL_LINES);
glVertex3f(light->pos[0], light->pos[1], light->pos[2]);
glVertex3f(light->spotDir[0], light->spotDir[1], light->spotDir[2]);
glEnd();
glPopMatrix();
}
glEnable(GL_LIGHTING);
}
static void
drawPlane(int w, int h)
{
int i, j;
float dw = 1.0 / w;
float dh = 1.0 / h;
glNormal3f(0.0, 0.0, 1.0);
for (j = 0; j < h; ++j) {
glBegin(GL_TRIANGLE_STRIP);
for (i = 0; i <= w; ++i) {
glVertex2f(dw * i, dh * (j + 1));
glVertex2f(dw * i, dh * j);
}
glEnd();
}
}
int spin = 0;
void
display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0, 1, 0);
aimLights();
setLights();
glPushMatrix();
glRotatef(-90.0, 1, 0, 0);
glScalef(1.9, 1.9, 1.0);
glTranslatef(-0.5, -0.5, 0.0);
drawPlane(16, 16);
glPopMatrix();
drawLights();
glPopMatrix();
glutSwapBuffers();
}
void
animate(void)
{
spin += 0.5;
if (spin > 360.0)
spin -= 360.0;
glutPostRedisplay();
}
void
visibility(int state)
{
if (state == GLUT_VISIBLE) {
glutIdleFunc(animate);
} else {
glutIdleFunc(NULL);
}
}
int
main(int argc, char **argv)
{
int i;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
/* process commmand line args */
for (i = 1; i < argc; ++i) {
if (!strcmp("-lm", argv[i])) {
useSAME_AMB_SPEC = !useSAME_AMB_SPEC;
} else {
usage(argv[0]);
}
}
glutCreateWindow("GLUT spotlight swing");
glutDisplayFunc(display);
glutVisibilityFunc(visibility);
glMatrixMode(GL_PROJECTION);
glFrustum(-1, 1, -1, 1, 2, 6);
glMatrixMode(GL_MODELVIEW);
glTranslatef(0.0, 0.0, -3.0);
glRotatef(45.0, 1, 0, 0);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, modelAmb);
glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
glMaterialfv(GL_FRONT, GL_AMBIENT, matAmb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, matDiff);
glMaterialfv(GL_FRONT, GL_SPECULAR, matSpec);
glMaterialfv(GL_FRONT, GL_EMISSION, matEmission);
glMaterialf(GL_FRONT, GL_SHININESS, 10.0);
initLights();
glutMainLoop();
return 0;
}

OpenGL antialiasing not working

I'v been trying to anti alias with OGL. I found a code chunk that is supposed to do this but I see no antialiasing. I also reset my settings in Nvidia Control Panel but no luck.
Does this code in fact antialias the cube?
GLboolean polySmooth = GL_TRUE;
static void init(void)
{
glCullFace (GL_BACK);
glEnable (GL_CULL_FACE);
glBlendFunc (GL_SRC_ALPHA_SATURATE, GL_ONE);
glClearColor (0.0, 0.0, 0.0, 0.0);
}
#define NFACE 6
#define NVERT 8
void drawCube(GLdouble x0, GLdouble x1, GLdouble y0,
GLdouble y1, GLdouble z0, GLdouble z1)
{
static GLfloat v[8][3];
static GLfloat c[8][4] = {
{0.0, 0.0, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0},
{0.0, 1.0, 0.0, 1.0}, {1.0, 1.0, 0.0, 1.0},
{0.0, 0.0, 1.0, 1.0}, {1.0, 0.0, 1.0, 1.0},
{0.0, 1.0, 1.0, 1.0}, {1.0, 1.0, 1.0, 1.0}
};
/* indices of front, top, left, bottom, right, back faces */
static GLubyte indices[NFACE][4] = {
{4, 5, 6, 7}, {2, 3, 7, 6}, {0, 4, 7, 3},
{0, 1, 5, 4}, {1, 5, 6, 2}, {0, 3, 2, 1}
};
v[0][0] = v[3][0] = v[4][0] = v[7][0] = x0;
v[1][0] = v[2][0] = v[5][0] = v[6][0] = x1;
v[0][1] = v[1][1] = v[4][1] = v[5][1] = y0;
v[2][1] = v[3][1] = v[6][1] = v[7][1] = y1;
v[0][2] = v[1][2] = v[2][2] = v[3][2] = z0;
v[4][2] = v[5][2] = v[6][2] = v[7][2] = z1;
#ifdef GL_VERSION_1_1
glEnableClientState (GL_VERTEX_ARRAY);
glEnableClientState (GL_COLOR_ARRAY);
glVertexPointer (3, GL_FLOAT, 0, v);
glColorPointer (4, GL_FLOAT, 0, c);
glDrawElements(GL_QUADS, NFACE*4, GL_UNSIGNED_BYTE, indices);
glDisableClientState (GL_VERTEX_ARRAY);
glDisableClientState (GL_COLOR_ARRAY);
#else
printf ("If this is GL Version 1.0, ");
printf ("vertex arrays are not supported.\n");
exit(1);
#endif
}
/* Note: polygons must be drawn from front to back
* for proper blending.
*/
void display(void)
{
if (polySmooth) {
glClear (GL_COLOR_BUFFER_BIT);
glEnable (GL_BLEND);
glEnable (GL_POLYGON_SMOOTH);
glDisable (GL_DEPTH_TEST);
}
else {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable (GL_BLEND);
glDisable (GL_POLYGON_SMOOTH);
glEnable (GL_DEPTH_TEST);
}
glPushMatrix ();
glTranslatef (0.0, 0.0, -8.0);
glRotatef (30.0, 1.0, 0.0, 0.0);
glRotatef (60.0, 0.0, 1.0, 0.0);
drawCube(-0.5, 0.5, -0.5, 0.5, -0.5, 0.5);
glPopMatrix ();
glFlush ();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 't':
case 'T':
polySmooth = !polySmooth;
glutPostRedisplay();
break;
case 27:
exit(0); /* Escape key */
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB
| GLUT_ALPHA | GLUT_DEPTH);
glutInitWindowSize(200, 200);
glutCreateWindow(argv[0]);
init ();
glutReshapeFunc (reshape);
glutKeyboardFunc (keyboard);
glutDisplayFunc (display);
glutMainLoop();
return 0;
}
Thanks
You should really consider to
find a better (read "up to date") source of information than the redbook
use multisampling/supersampling instead of the almost-deprecated GL_POLYGON_SMOOTH
On your problem:
What is your graphic card ?
Which version of OpenGL are you using ?
Are you depth sorting all your polygons ?
Have you tried to supply GLUT_MULTISAMPLE to the glutInitDisplayMode(..) call? It's not sure your glut implementation supports it though.