I am working on a practice OpenGL program. I am basically trying to display a pyramid. I pasted the code I have so far bellow. The issue I am having is that when I call gluLookAt my screen becomes completely black. This does not make sense to me. My pyramid has triangles with dimensions bounded within a box of size (1, 1, 1) and it is at the origin. So therefore if I position the eye at something far away if (10, 10, 10) and look at (0, 0, 0) why do I not see the pyramid. When I comment out gluLookAt I can see it clearly.
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
struct
{
GLuint trifan;
GLuint tribase;
GLuint colors;
} b;
void display();
void init_buffer();
void check_errors();
void reshape(int w, int h);
void keyboard(unsigned char k, int x, int y);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitWindowSize(500, 500);
glutCreateWindow("Pyramid");
init_buffer();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
}
void init_buffer()
{
glGenBuffers(1, &(b.trifan));
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
GLfloat trifan[6][3] =
{
{0.0, 1.0, 0.0},
{1.0, 0.0, 1.0},
{-1.0, 0.0, 1.0},
{-1.0, 0.0, -1.0},
{1.0, 0.0, -1.0},
{1.0, 0.0, 1.0}
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, trifan, GL_STATIC_COPY);
glGenBuffers(1, &b.colors);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
GLfloat colors[6][3] =
{
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 0.0},
{1.0, 0.0, 1.0},
{0.5, 0.0, 1.0},
{1.0, 0.0, 0.5},
};
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 6, colors, GL_STATIC_COPY);
check_errors();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (float)w / (float)h, 1.0, 100.0);
}
float angle = 0.0;
float zoom = 1.0;
void keyboard(unsigned char k, int x, int y)
{
if(k == 'x')
{
angle += 10;
}
else if(k == 'y')
{
angle -= 10;
}
else if(k == 'w')
{
zoom += 0.1;
}
else if(k == 's')
{
zoom -= 0.1;
}
glutPostRedisplay();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotated(angle, 1.0, 0.0, 0.0);
gluLookAt(10.0, 10.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
{
glBindBuffer(GL_ARRAY_BUFFER, b.trifan);
glVertexPointer(3, GL_FLOAT, 0, 0);
glBindBuffer(GL_ARRAY_BUFFER, b.colors);
glColorPointer(3, GL_FLOAT, 0, 0);
glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
}
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
check_errors();
glFlush();
}
void check_errors()
{
GLint err = glGetError();
if(err != GL_NO_ERROR)
{
fputs((char*)gluErrorString(err), stderr);
exit(1);
}
}
Related
I would like to use the polar coordinates to manipulate the upper, lower, left, and right camera points with my keyboard.
#include <stdlib.h>
#include <glut.h>
GLint TopLeftX, TopLeftY, BottomRightX, BottomRightY ;
static int HourOfDay = 0;
static int DayOfYear = 10;
void init(void) {
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void myDisplay(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix(); // Origin
glClear(GL_DEPTH_BUFFER_BIT);
/* Sun */
glColor3f(1.0, 1.0, 0.0);
glutSolidSphere(1.0, 20, 10);
/* Mercury */
glPushMatrix();
glRotatef((GLfloat)DayOfYear, 0.0, 0.0, 1.0);
glTranslatef(3.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.87, 0.53, 0.25);
glutSolidSphere(0.3, 10, 2);
glPopMatrix();
/* Venus */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 195, 0.0, 0.0, 1.0);
glTranslatef(5.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.99, 0.91, 0.66);
glutSolidSphere(0.5, 10, 2);
glPopMatrix();
/* Earth */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 80, 0.0, 0.0, 1.0);
glTranslatef(7.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.47, 0.82, 0.98);
glutSolidSphere(0.5, 10, 2);
/* Earth's Moon 1*/
glPushMatrix();
glRotatef((GLfloat)DayOfYear, 0.0, 0.0, 1.0);
glTranslatef(0.7, 0.0, 0.0);
glColor3f(0.89, 0.93, 0.95);
glutSolidSphere(0.1, 5, 5);
glPopMatrix();
glPopMatrix();
/* Mars */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 275, 0.0, 0.0, 1.0);
glTranslatef(9.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.84, 0.16, 0.15);
glutSolidSphere(0.35, 10, 2);
glPopMatrix();
/* Jupiter */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 33, 0.0, 0.0, 1.0);
glTranslatef(11.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.93, 0.64, 0.27);
glutSolidSphere(1.0, 10, 2);
glPopMatrix();
/* Saturn */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 180, 0.0, 0.0, 1.0);
glTranslatef(15.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.92, 0.82, 0.45);
glutSolidSphere(0.9, 10, 2);
glPopMatrix();
/* Uranus */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 90, 0.0, 0.0, 1.0);
glTranslatef(17.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.64, 0.84, 0.78);
glutSolidSphere(0.8, 10, 2);
glPopMatrix();
/* Neptune */
glPushMatrix();
glRotatef((GLfloat)DayOfYear + 150, 0.0, 0.0, 1.0);
glTranslatef(20.0, 0.0, 0.0);
glRotatef((GLfloat)HourOfDay, 0.0, 0.0, 1.0);
glColor3f(0.29, 0.74, 0.95);
glutSolidSphere(0.25, 10, 2);
glPopMatrix();
glutSwapBuffers();
// Animation State
DayOfYear = (DayOfYear + 1) % 360;
HourOfDay = (HourOfDay + 5) % 360;
}
void Timer(int iUnused) {
glutPostRedisplay();
glutTimerFunc(30, Timer, 0);
}
static float cam_axis_x = 0.0;
static float cam_axis_y = 0.0;
static float cam_axis_z = -50.0;
void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w / (GLfloat)h, 1.0, 90.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cam_axis_x, cam_axis_y, cam_axis_z);
}
/*void reshape(int w, int h) {
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLsizei)w / (GLsizei)h, 0.0, 90.0);
}*/
void keyboard(unsigned char key, int x, int y) {
switch (key) {
case 'q': // Esc
exit(0);
break;
case '75': // left
break;
case '77': // right
break;
case '72': // up
break;
case '80': // down
break;
}
}
void MyMouseClick(GLint Button, GLint State, GLint X, GLint Y) {
if (Button == GLUT_LEFT_BUTTON && State == GLUT_DOWN) {
TopLeftX = X;
TopLeftY = Y;
}
}
void MyMouseMove(GLint X, GLint Y) {
BottomRightX = X;
BottomRightY = Y;
glutPostRedisplay();
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(myDisplay);
glutReshapeFunc(reshape);
Timer(0);
glutKeyboardFunc(keyboard);
glutMouseFunc(MyMouseClick);
glutMotionFunc(MyMouseMove);
glutMainLoop();
return 0;
}
I want to move the camera around the sun
Can you tell me about gluLookAt?
I don't know what you mean by polar coordinates, normally it's for 2D. maybe you mean Spherical coordinates.
The method would be quite identical:
Assuming your sun is at the origin of the world (which it currently does)
You have 2 methods
Assuming Theta is the angle you want your camera to orbit around the sun
and Phi is some "pitching" around your xAxis frame (you may or may not want it).
1) Compute the x,y,z position of your camera using the basic formula:
https://en.wikipedia.org/wiki/Spherical_coordinate_system
Spherical Coordinates:
// r is the distance to the sun
cam_axis_x = r * sin(theta) * cos(phi);
cam_axis_y = r * r * cos(theta);
cam_axis_z = r * sin(theta) * sin(phi);
"Polar coordinates":
cam_axis_x = r * sin(theta);
cam_axis_z = r * cos(theta);
then apply the translation as you did before
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cam_axis_x, cam_axis_y, cam_axis_z);
2) Apply a rotation on your camera after translating it to its original position:
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(cam_axis_x, cam_axis_y, cam_axis_z);
glRotatef(0, theta, phi);
I want to create two objects with different light position. But the light position of second object (Gold sphere) is always the same as the first object (Silver sphere) even I have set different position parameter.
I have enable both light one after another object. But the second one is always the same as the first/above one.
// light from top right
GLfloat light_position_top_right[] = { 1.0, 1.0, 1.0, 0.0 };
// light from bottom left
GLfloat light_position_bottom_left[] = { -1.0, -1.0, 1.0, 0.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLightfv(GL_LIGHT0, GL_POSITION, light_position_top_right);
glLightfv(GL_LIGHT1, GL_POSITION, light_position_bottom_left);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_DEPTH_TEST);
You can find complete code in this gist.
I expect the second object to have light position from bottom left. But it shows from top right as the first object.
The fourth coordinate of your light positions must be 1.0 instead of 0.0, otherwise due to how projective transformations work the lights will behave as if they are infinitely far away in the specified direction.
With a fourth coordinate of 0.0:
With a fourth coordinate of 1.0:
As for why there are two highlights instead of just one, you forgot to set the proper glLight* settings for your lights. By default, GL_LIGHT0 begins with white diffuse and specular color, but all other lights have black diffuse and specular color by default.
EDIT:
If you want separate spheres to use separate lights, you'll need to initialize the lights during init(). But instead of glEnableing the lights right then, do the following:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(1.0, 0.0, -1.0);
glEnable(GL_LIGHT0);
drawGraySphere();
glutSolidSphere(0.5, 50, 50);
glDisable(GL_LIGHT0);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.75, -0.8, 0.0);
glEnable(GL_LIGHT1);
drawGoldSphere();
glutSolidSphere(0.5, 50, 50);
glDisable(GL_LIGHT1);
glPopMatrix();
glFlush();
}
Alternatively, you have the option to just use a single light, and change its parameters using glLight* each frame, before rendering each sphere. This is the better option if you plan on having a bunch of objects, each with their own light.
Here's your code, modified to do what you want it to do:
#include<windows.h>
#include <GL/glut.h>
#include <GL/glu.h>
#include <GL/gl.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
void drawGoldSphere() {
GLfloat mat_specular[] = { 0.628281, 0.555802, 0.366065, 1.0 };
GLfloat mat_shininess[] = { 51.2 };
GLfloat mat_ambient[] = { 0.24725, 0.1995, 0.0745, 1.0 };
GLfloat mat_diffuse[] = { 0.75164, 0.60648, 0.22648, 1.0 };
//Material Properties
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void drawGraySphere() {
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void init(void)
{
// light from middle left
//GLfloat light_position[] = { 0.0, 0.0, 0.0, 1.0 };
// light from top right
GLfloat light_position_top_right[] = { 1.0, 1.0, 1.0, 0.0 };
// light from bottom left
GLfloat light_position_bottom_left[] = { -1.0, -1.0, 1.0, 0.0 };
// light from bottom left
//GLfloat light_position[] = { -0.1, -0.1, -0.1, 0.0 };
// light from middle top
//GLfloat light_position[] = { 0.0, 1.0, 0.0, 0.0 };
// light from middle right
//GLfloat light_position[] = { 1.0, 0.0, 0.0, 0.0 };
// light from middle middle
//GLfloat light_position[] = { 0.0, 0.0, 1.0, 0.0 };
// light from middle bottom
//GLfloat light_position[] = { 0.0, -1.0, 1.0, 0.0 };
GLfloat no_mat[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat high_shininess[] = { 100.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position_top_right);
glLightfv(GL_LIGHT1, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT1, GL_POSITION, light_position_bottom_left);
glEnable(GL_LIGHTING);
glEnable(GL_DEPTH_TEST);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(1.0, 0.0, -1.0);
glEnable(GL_LIGHT0);
drawGraySphere();
glutSolidSphere(0.5, 50, 50);
glDisable(GL_LIGHT0);
glPopMatrix();
glPushMatrix();
glTranslatef(-0.75, -0.8, 0.0);
glEnable(GL_LIGHT1);
drawGoldSphere();
glutSolidSphere(0.5, 50, 50);
glDisable(GL_LIGHT1);
glPopMatrix();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-1.5, 1.5, -1.5*(GLfloat)h/(GLfloat)w,
1.5*(GLfloat)h/(GLfloat)w, -10.0, 10.0);
else
glOrtho(-1.5*(GLfloat)w/(GLfloat)h,
1.5*(GLfloat)w/(GLfloat)h, -1.5, 1.5, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Render result:
I have to create a room using openGL and glut. From what I see in the output it looks like the left wall gets drawn but I can't see the front wall at all. I only have to draw 2 walls and have the camera pointing towards the corner where the 2 walls meet. Any idea what I'm doing wrong?
My code:
#include <gl/gl.h>
#include <gl/glut.h>
/*defining colors used in the room*/
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
GLfloat brown[] = {165.0/255.0, 42.0/255.0, 42.0/255.0, 1.0};
void drawFloor()
{
glBegin(GL_POLYGON);
glColor4fv(black);
glVertex3f(0.0, 0.0, -3.0);
glVertex3f(1.0, 0.0, -3.0);
glVertex3f(2.0, 0.0, -3.0);
glEnd();
}
void drawWalls()
{
glBegin(GL_QUAD_STRIP);
glColor4fv(white);
glPolygonMode(GL_FRONT, GL_FILL);
int i;
//draw left wall
for (i = 0; i <= 5; i++)
{
glVertex3f(0.0, 0.0, i);
glVertex3f(0.0, 1.0, i);
}
//draw front wall
for(i = 1; i <= 5; i++)
{
glVertex3f(i, 0, 5);
}
glEnd();
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, -1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0);
drawFloor();
drawWalls();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat) w / (GLfloat) h, 1, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idleFunc()
{
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("My room");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idleFunc);
glutMainLoop();
}
I'm trying to combine lighting with 3D graphics.There's sphere that is on depth 100, and it has 100 as radius.I use translatef to make it be far from the eye position.
But nothing is displayed on the screen:
#import <OpenGL/OpenGL.h>
#import <GLUT/GLUT.h>
GLfloat width=500, height=500;
GLfloat angle=0.0;
void makeRound (GLfloat* angle)
{
if(*angle>360.0)
{
*angle-=360.0;
}
else if(*angle<0.0)
{
*angle+=360.0;
}
}
void init(void)
{
GLfloat mat_specular[] = { 1.0, 0.0, 0.0, 0.0 };
GLfloat mat_diffuse[] = { 0.9, 0.0, 0.0, 0.0 };
GLfloat mat_shininess[] = { 50.0 };
GLfloat light_position[] = { 5.0, 5.0, 5.0, 1.0 };
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat lmodel_ambient[] = { 0.1, 0.5, 0.1, 1.0 };
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 0, 0, 0, 100, 0, 1, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 1, 1, 1000);
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_SMOOTH);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR,white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT,lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glRotatef(angle, 0, 1, 0);
glTranslatef(0, 0, 100);
glutSolidSphere (100.0, 80, 64);
glPopMatrix();
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
case '+':
angle+=5.0;
makeRound(&angle);
glutPostRedisplay();
break;
case '-':
angle-=5.0;
makeRound(&angle);
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (width, height);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init ();
glutMainLoop();
return 0;
}
UPDATE
This is what I see with :
glTranslatef(0,0,-200);
As a result of your translate you are actually positioned on the edge of the sphere. That means the polys actually go though "you". You won't see anything as a result of your near clip plane and thanks to back-face culling you can't see the tris on the other side of the sphere.
Set either:
glCullFace( GL_FRONT );
Or set the transform to push the sphere further away:
glTranslatef(0, 0, -200);
I'm trying to change the sphere position after a mouse click but it doesnt work when using the x and y from glutMouseFunc ,, here is the code :
//
#include "stdafx.h"
#include <stdlib.h>
#include <GL/glut.h>
bool Cone=false , ConeSelected=false,
Cube=false, CubeSelected=false,
Sphere=false, SphereSelected=false,
Teapot=false, TeapotSelected =false,
Torus=false, TorusSelected=false;
static float XSphere=0, YSphere=-1.5 ,ZSphere=0;
void init(void)
{
GLfloat blankMaterial[] = {1.0, 0.0, 0.0};
GLfloat whiteDiffuseLight[] = {30};
glClearColor (0.0, 0.1, 0.2, 0.0);
glClearDepth(1.0);
glShadeModel (GL_FLAT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, blankMaterial);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blankMaterial);
glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, whiteDiffuseLight);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_DEPTH_TEST);
//Drawing Cube
if(Cube)
{
glPushMatrix();
glColor3f(0, 1, 0);
glTranslatef(-3,0,-3);
glRotatef(20, 1,0,0);
glutSolidCube(2);
glPopMatrix() ;
}
//drawing cone
if(Cone)
{
glPushMatrix();
glColor3f (1.0, 0.0, 1.0);
glTranslatef(0,2,0);
glRotatef(50, 1,0,0);
glutSolidCone(.5,1,10,10);
glPopMatrix() ;
}
//Drawing Solid Sphere
if(Sphere)
{
glPushMatrix();
glTranslatef(XSphere,YSphere,ZSphere);
glutSolidSphere(.5, 20, 20);
glPopMatrix();
glDisable(GL_DEPTH_TEST);
}
glColor3f (1.0f, 1.0f, 1.0f);
//drawing Torus
if(Torus)
{
glPushMatrix();
glColor3f (1.0, 1.0, 1.0);
glTranslatef(2,2,0);
glRotatef(60, 1,0,0);
glutSolidTorus(.2,.5,40,30);
glPopMatrix() ;
}
//Drawing teapot
if(Teapot)
{
glPushMatrix ();
glTranslatef (2.0, 0.0, 0.0);
glutSolidTeapot(.5);
glPopMatrix ();
}
glFlush();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void keyboard (unsigned char key, int x, int y)
{int modifiers = glutGetModifiers();
switch (key) {
case 'A':
Teapot=Cone=Sphere=Cube=Torus=true;
glutPostRedisplay();
break;
case 'D':
Teapot=Cone=Sphere=Cube=Torus=false;
glutPostRedisplay();
break;
case 'S':
Teapot=Cone=Cube=Torus=false;
Sphere=true;
TeapotSelected=ConeSelected=CubeSelected=TorusSelected=false;
SphereSelected=true;
break;
case 27:
exit(0);
break;
default:
break;
}
}
Here in the mouse section I tried to set the gltranslation in the above drawing sphere but when I checked the results for x and y they were large and that's why I couldnt see the sphere after clicking the mouse , how can i fix this ??
void MyMouse(int button, int state, int x, int y)
{
switch (button)
{
case GLUT_LEFT_BUTTON:
if(state == GLUT_UP)
{
if (SphereSelected)
{
Cone=Cube=Torus=Teapot=false;
Sphere=true;
XSphere=x;
YSphere=y;
ZSphere=1;
glutPostRedisplay();
}
}
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize (900, 900);
glutInitWindowPosition (0, 100);
glutCreateWindow ("Scene Modeling and Interaction");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMouseFunc(MyMouse);
glutMainLoop();
return 0;
}
The x and y that the mouse function gives you are in screen pixel coordinates, not in the coordinates that your scene uses. In order to do this, you have to center your coordinates (by subtracting by half your screen size) and scaling them to about your screen size (by dividing them by something on the order of your screen size).
Thus, you probably want to do something like
XSphere = (x - 450) * 3.0 / 900;
YSphere = (450 - y) * 3.0 / 900;
and play around with the 3.0 until it gives you something reasonable.