I am trying to create a 3d room in openGL. I am getting the right and bottom walls right but the back wall is not being displayed. I want to create a 3d room with camera view set to certain angle and place a table and teapot in the room.
Code:
#include<GL/glut.h>
void wall(double thickness) // function to create the walls with given thickness
{
glPushMatrix();
glTranslated(0.5,0.5*thickness,0.5);
glScaled(1.0,thickness,1.0);
glutSolidCube(1.0);
glPopMatrix();
}
void displaySolid(){ //function to create a 3d room
GLfloat mat_ambient[]={0.7f,0.7f,0.7f,0.1f};
GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f};
GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
GLfloat light_Intensity[]={0.7f,0.7f,0.7f,1.0f};
GLfloat light_Position[]={2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_Position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_Intensity);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winlet=1.0;
glOrtho(-winlet*64/48,winlet*64/48.0,-winlet*64/48,winlet*64/48,0.6,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3,1.38,2.0,0.0,0.25,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.08,0.08,0.08);
glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glPopMatrix();
glPushMatrix();
glTranslated(0.25,0.42,0.35);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0,0.4);
glPopMatrix();
wall(0.2);
glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();
glFlush();
}
void main(int argc,char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);
glutInitWindowSize(640,480);
glutInitWindowPosition(0,0);
glutCreateWindow("the pot");
glutDisplayFunc(displaySolid);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
glEnable(GL_NORMALIZE);
glClearColor(0.1,0.1,0.1,0.0);
glViewport(0,0,640,480);
glutMainLoop();
}
//end
This how ever is giving a black screen on the back side of the 3D cube room.I have set the lightning effect to LIGHT0, yet i am not able to see the side wall.
The wall function had been called only twice as pointed out.So Here's the fix.
void displaySolid(){
GLfloat mat_ambient[]={0.7f,0.7f,0.7f,0.1f};
GLfloat mat_diffuse[]={0.5f,0.5f,0.5f,1.0f};
GLfloat mat_specular[]={1.0f,1.0f,1.0f,1.0f};
GLfloat mat_shininess[]={50.0f};
glMaterialfv(GL_FRONT,GL_AMBIENT,mat_ambient);
glMaterialfv(GL_FRONT,GL_SPECULAR,mat_specular);
glMaterialfv(GL_FRONT,GL_SHININESS,mat_shininess);
GLfloat light_Intensity[]={0.7f,0.7f,0.7f,1.0f};
GLfloat light_Position[]={2.0f,6.0f,3.0f,0.0f};
glLightfv(GL_LIGHT0,GL_POSITION,light_Position);
glLightfv(GL_LIGHT0,GL_DIFFUSE,light_Intensity);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double winlet=1.0;
glOrtho(-winlet*64/48,winlet*64/48.0,-winlet*64/48,winlet*64/48,0.6,100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(2.3,1.38,2.0,0.0,0.25,0.0,0.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(0.08,0.08,0.08);
glPushMatrix();
glTranslated(0.6,0.38,0.5);
glRotated(30,0,1,0);
glutSolidTeapot(0.08);
glPopMatrix();
glPushMatrix();
glTranslated(0.25,0.42,0.35);
glPopMatrix();
glPushMatrix();
glTranslated(0.4,0,0.4);
glPopMatrix();
wall(0.2);
glPushMatrix();
glRotated(-90.0,1.0,0.0,0.0);
wall(0.02);
glPopMatrix();
glRotated(90.0,0.0,0.0,180.0);
wall(0.02);
glPopMatrix();
glFlush();
}
This is how the final 3d room looks like:
Related
I'm trying to draw a red sphere on a white background using OpenGL. Here's the part of my code that should draw the sphere.
Part of the main entry point:
initialise();
glutDisplayFunc(draw_Sphere);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
Initialisation:
void initialise()
{
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glDepthFunc(GL_LEQUAL);
glShadeModel(GL_SMOOTH);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
Drawing the sphere:
void draw_Sphere()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(200, 200, 0);
glColor3f(1.0, 0.0, 0.0);
glPushMatrix();
glutSolidSphere(0.25, 20, 20);
glPopMatrix();
glutSwapBuffers();
}
I have no idea what's going wrong, but this won't render the sphere (it will draw the white background, though). Any suggestions to fix it?
Using Matrices
Consider setting up a proper model, view and projection matrices for this. Then you will have more control on what you see.
Solution
Drop the glTranslatef(200, 200, 0); line as with this you are getting the sphere out of the frustum. And you should see the sphere.
My origin is not at top left corner, when I resize my window. And the 2d coordinates are not in pixel coordinates:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
rnd::initDraw();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, mainPanelx, mainPanely, 0.0, -1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
//glPushMatrix(); ----Not sure if I need this
glLoadIdentity();
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glClear(GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0);
glVertex2f(0.0, 0.0);
glVertex2f(10.0, 0.0);
glVertex2f(10.0, 10.0);
glVertex2f(0.0, 10.0);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glutSwapBuffers();
Resize function:
void resize(int width, int height)
{
if (height == 0) height = 1;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
/* note we divide our width by our height to get the aspect ratio */
gluPerspective(45.0, width / height, 1.0, 400.0);
glMatrixMode(GL_MODELVIEW);
mainPanelx = width;
mainPanely = height;
std::cout<<mainPanelx<<", "<<mainPanely<<"\n";
}
How do i get the origin constant at the top left corner of my window and how do i get the 2d coordinates in pixel coordinates?
You forgot to call glViewport in your resize function.
This means OpenGL will still render to the old sized viewport.
I am working on a background texture. What I want to do is that I want to set a background image. For that in my code I used switch to ortho, draw a square full of window size, texture it. Then switch back to 3d and draw 3d images. It draw the background texture and snowman but they all disappear in a sec. I have no idea where the error code is. gotta have something to do with pushing and popping the matrix I think. Below is the code of my InitGl, draw() and drawsnowman(), main and reshape(). I think the problem is in draw() function during the swithc between 3D to 2d. Advice?
int main (int argc, char **argv)
{
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("A basic OpenGL Window");
glutKeyboardFunc(key);
if( !initGL() ) { // NEW (16)
printf( "Unable to initialize graphics library!\n" );
return 1;
}
glutDisplayFunc (display);
// glutIdleFunc (display);
glutIdleFunc(idle);
glutReshapeFunc (reshape);
//Load our texture
texture = LoadTexture( "texture.bmp", 256, 256 );
glutMainLoop ();
//Free our texture
FreeTexture( texture );
return 0;
}
void drawSnowMan(void)
{
GLUquadricObj *pObj;
//glDisable(GL_TEXTURE_2D);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
// save the world matrix
glPushMatrix();
glTranslatef(xpos, -0.5, -5.0);
glRotated(rotX,1,0,0); // ******** NEW (11)
glRotated(rotY,0,1,0);
pObj = gluNewQuadric();
gluQuadricNormals(pObj, GLU_SMOOTH);
//glRotated(rotZ,0,0,1);
glPushMatrix();
//setting up light effect for base, mid and head spheres. all red!
ambient[0] = 1.0; ambient[1] = 0.0; ambient[2] = 0.0;
diffuse[0] = 1.0; diffuse[1] = 0.0; diffuse[2] = 0.0;
specular[0] = 0.7; specular[1] = 0.6; specular[2] = 0.5;
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_SHININESS, shiness);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
//glTranslatef(0.0 ,0.75f, 0.0f);
//bottom sphere. dont need to gltranslate again because it will use the previous gltranslate
//which is declared outside
glutSolidSphere(0.70f,20,20);
//mid sphere
glTranslatef(0.0f, 1.0f, 0.0f);
glutSolidSphere(0.45f,20,20);
//top sphere
glTranslatef(0.0f, 0.6f, 0.0f);
glutSolidSphere(0.30f,20,20);
//eyes
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glPopMatrix();
//drawing hat
glPushMatrix();
//black color. move it 1.85 in y position because thats where the head is
//rotate the cylinder and draw cylinder
glColor3f(0.0f, 0.0f, 0.0f);
glTranslatef(0.0f, 1.85f, 0.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
gluCylinder(pObj, 0.17f, 0.17f, 0.4f, 26, 13);
//drawing brim. disable cull_face. draw disk, so front part. disabl
glDisable(GL_CULL_FACE);
gluDisk(pObj, 0.17f, 0.28f, 26, 13);
glEnable(GL_CULL_FACE);
glTranslatef(0.0f, 0.0f, 0.40f);
gluDisk(pObj, 0.17f, 0.28f, 26, 13);
glPopMatrix();
glPushMatrix();
glPushMatrix();
glColor3f(1.0,1.0,1.0);
glTranslatef(2.0f, 1.0f, 0.0f);
glRotatef(90.0,0.0, 0.0,-5.0);
glScalef (0.01, 0.2, 0.06); /* modeling transformation */
//glutSolidCone(0.1, 0.1, 10.0, 14.0);
glutSolidCube(1.5);
glPopMatrix();
glPushMatrix();
glTranslatef(2.0f, 0.8f, 0.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
gluCylinder(pObj, 0.04f, 0.04f, 0.2f, 26, 13);
glPopMatrix();
//blade
glPushMatrix();
ambient[0] = 0.0; ambient[1] = 1.0; ambient[2] = 0.0;
diffuse[0] = 1.0; diffuse[1] = 0.0; diffuse[2] = 0.0;
specular[0] = 0.7; specular[1] = 0.6; specular[2] = 0.5;
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_SHININESS, shiness);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT1);
glTranslatef(2.0f, 1.0f, 0.0f);
glRotatef(-90.0f, 1.0f, 0.0f, 0.0f);
gluCylinder(pObj, 0.03f, 0.03f, 1.1f, 26, 13);
glDisable(GL_LIGHTING);
glDisable(GL_LIGHT0);
glPopMatrix();
glPopMatrix();
//big push matrix for eyes, and nose
glPushMatrix();
//eyes color = black light. set defuse all 0, ambient = black. turns the eyes black
/*ambient[0] = 0.0; ambient[1] = 0.0; ambient[2] = 0.0;
diffuse[0] = 0.0; diffuse[1] = 0.0; diffuse[2] = 0.0;
specular[0] = 0.7; specular[1] = 0.6; specular[2] = 0.5;
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular);
glMaterialfv(GL_FRONT, GL_SHININESS, shiness);*/
glColor3f(0.0, 0.0, 0.0);
//left eye
glPushMatrix();
glTranslatef(-0.17, 1.7, 0.25 );
glutSolidSphere(0.05, 10.0, 10.0);
glPopMatrix();
//right eye
glPushMatrix();
glTranslatef(0.17, 1.7, 0.25 );
glutSolidSphere(0.05, 10.0, 10.0);
glPopMatrix();
//drawing nose
glPushMatrix();
glTranslatef(0.0, 1.6, 0.25 );
glutSolidCone(0.08f,0.5f,10,2);
glPopMatrix();
glPopMatrix(); // end big push matrix for eyes and nose
glPopMatrix();
} // end of drawsnowman()
void display (void) {
glClearColor(0.25f, 0.25f, 0.50f, 1.0f ); // blueish color
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glLoadIdentity();
// MODEL VIEW is set up in IniitGL
//so save it
glPushMatrix();
//switch to projection matrix
//swithc to ortho.
//draw texture
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, -1.0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
// Draw a textured quad
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, 1, 0);
glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
glEnd();
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glPopMatrix();// pop the 3d model view
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
// You can ignore this. just a different of way drawing texture and still does not work
/* glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, 0.0, -1.0);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glDepthMask(GL_FALSE);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glDepthMask( false );
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, texture);
// Draw a textured quad
glBegin(GL_QUADS);
glTexCoord2f(0, 0); glVertex3f(0, 0, 0);
glTexCoord2f(0, 1); glVertex3f(0, 1, 0);
glTexCoord2f(1, 1); glVertex3f(1, 1, 0);
glTexCoord2f(1, 0); glVertex3f(1, 0, 0);
glEnd();
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
/*glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (60, 800 / 600, 1.0, 100.0);;
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt( 0.0, 0.0, 0.0, // Where would the camera be?
0.0, 0.0,-1.0, // Where would it be looking?
0.0, 1.0, 0.0); // What would be the "up" vector?fa*/
/* glDisable(GL_TEXTURE_2D);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
//gluPerspective (45, SCREEN_WIDTH / SCREEN_HEIGHT, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();*/
drawSnowMan(); // draw snow man
glutSwapBuffers();
angle ++;
}
void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective (45, (GLfloat)w / (GLfloat)h, 1.0, 100.0);
glMatrixMode (GL_MODELVIEW);
}
bool initGL (void) {
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
// Switch to the "camera" mode
glMatrixMode(GL_PROJECTION); // Camera
glLoadIdentity();
// Change the camera to a 3D view
glFrustum( -1 * (float) SCREEN_WIDTH / SCREEN_HEIGHT,
(float) SCREEN_WIDTH / SCREEN_HEIGHT,
-1.0,
1.0,
1.5,
1000.0);
glClearColor(0.25f, 0.25f, 0.50f, 1.0f );
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); // NEW (5)
glEnable(GL_LINE_SMOOTH); // NEW (6)
glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); // NEW (7)
glEnable(GL_POLYGON_SMOOTH); // NEW (8)
// this is a specular light
GLfloat mat_specular[] = { 1.0 , 1.0 , 1.0 , 1.0 }; // Color of a "shiny material
GLfloat mat_shininess[] = { 50.0 }; // How shiny is it?
// GLfloat mat_ambient_and_diffuse[] = { 0.0, 1.0, 0.0, 1.0 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; // Infinitely far away. Direction light
GLfloat light_position1[] = {2.0,1.5,0.0,1.0}; // saber light
// How to calculate the surface normal for pixels
glShadeModel(GL_SMOOTH); // Try this as well GL_FLAT
// Setup up some material reflective properties
// glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
// glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
//glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient_and_diffuse);
//glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_ambient_and_diffuse);
// Finally actually make the light
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT1, GL_POSITION, light_position1);
//Check for error
GLenum error = glGetError(); // NEW (9)
if( error != GL_NO_ERROR ) {
printf( "Error initializing OpenGL! %s\n", gluErrorString( error ) );
return false;
}
return true;
}
void idle(void) {
glutPostRedisplay();
}
Reoccuring newbie misconception: OpenGL "initialization".
OpenGL is not initialized! All the code you have in initGL and reshape belongs into display.
In the case of calls glLight… those must be placed in display, after setting the modelview matrix into the space you want the lights to be in. Also OpenGL is not a library; yes originally the 'L' in OpenGL did mean library, but I backronymed it to Layer, because that's what it is on most modern graphics systems: A layer between a end user program and the GPU and its drivers.
I think once you moved the code from initGL and reshape to display, the solution should become clear: You can change, reset and modify the projection and modelview matrix whenever you like. To have the window width and height available in display either store them in global variables in reshape, or, the preferred solution, use glutGet(GLUT_WINDOW_WIDTH) and glutGet(GLUT_WINDOW_HEIGHT) to query the window dimensions in the display function.
You want to draw things in a orthographic projection? Then setup projection and modelview appropriately. Switching to a perspective? Just do it.
I think I kinda figure out why my background texture keep disappearing. I had Cullface and depth something turned on. I turned it off befor popping the last matrix and it works.
However, there is one problem. The texture image is snowy background, but it turns into all red. So first I thought it was due to the reflection of the lighting from the snowgirl. I turned off all the light and still red. Can't figure out why. Could it be, the texture color blending in with the original color of the snowman?
How can I Create a 4 views (LEFT/TOP/PERSPECTIVE/FRONT) in Open GL ??
I Have Used this :
int main(int c,char ** argv)
{
glutInit(&c,argv);
glutInitDisplayMode(GLUT_DOUBLE |GLUT_DEPTH |GLUT_RGB );
glutInitWindowSize(w,h);
MainWin =glutCreateWindow("Teapot Window");
glutDisplayFunc(display);
LeftWin = glutCreateSubWindow(MainWin,0,0,s_window_w,s_window_h);
glutDisplayFunc(displayLeft);
glutReshapeFunc(reshapeLeft);
TopWin = glutCreateSubWindow(MainWin,s_window_w+3,0,s_window_w,s_window_h);
glutDisplayFunc(displayTop);
glutReshapeFunc(reshapeTop);
PerspectiveWin = glutCreateSubWindow(MainWin,0,s_window_h+3,s_window_w,s_window_h);
glutDisplayFunc(displayPerspective);
glutReshapeFunc(reshapePerspective);
FrontWin = glutCreateSubWindow(MainWin,s_window_w+3,s_window_h+3,s_window_w,s_window_h);
glutDisplayFunc(displayFront);
glutReshapeFunc(reshapeFront);
glutMainLoop();
return 0;
}
Then I have make the projection for top and left and like this :
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5f,0.5f,-0.35f,0.35f,1,500);
When I use the top lines for the front also , the output is something like this :
Where is my fault ?
thanks in advance
Why the Front view is empty and the perspective and left view are same ??
For perspective projection i had used glFrustum ....
So Am I going wrong ?
Please help on creating multiple views like 3ds max or maya ...
below is the code for display functions:
void displayLeft()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-1,0,0,0,0,1,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(teapot_angle,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayTop()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,-1,0,0,0,0,0,0,-1);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(teapot_angle,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayFront()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,0,0,0,0,1,0,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(teapot_angle,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayPerspective()
{
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0,0,-1,0,0,0,0,1,0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0,GL_POSITION,light_pos);
glShadeModel(GL_SMOOTH);
glClearColor(0.2f,0.3f,0.4f,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
glTranslatef(teapot_x,teapot_y,teapot_z);
glRotatef(0,0,1,0);
glColor3f(0,1,0);
glutSolidTeapot(0.2f);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
Use glViewport and glScissor to cut out panes of the window. Then for each pane perform the rendering as usual. Setting the projection matrix is actually a drawing state operation, so it belongs into the display function, not reshape.
Your display function should become something like
void ViewportScissor(int x, int y, int width, int height)
{
glViewport(x, y, width, height);
glScissor(x, y, width, height);
}
void display(void)
{
int const win_width = glutGet(GLUT_WINDOW_WIDTH);
int const win_height = glutGet(GLUT_WINDOW_HEIGHT);
glDisable(GL_SCISSOR);
glClear(…);
glEnable(GL_SCISSOR);
ViewportScissor(0, 0, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_frontview_projection();
glMatrixMode(GL_MODELVIEW);
setup_frontview();
draw_scene();
ViewportScissor(win_width/2, 0, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_rightview_projection();
glMatrixMode(GL_MODELVIEW);
setup_rightview();
draw_scene();
ViewportScissor(0, win_height/2, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_topview_projection();
glMatrixMode(GL_MODELVIEW);
setup_topview();
draw_scene();
ViewportScissor(win_width/2, win_height/2, win_width/2, win_height/2);
glMatrixMode(GL_PROJECTION);
setup_freecamview_projection();
glMatrixMode(GL_MODELVIEW);
setup_freecamview();
draw_scene();
glutSwapBuffers();
}
Adding splitter lines/frames is left as an exercise for the reader.
I want to draw several cubes using glutSolidCube in some points in space. The examples I have found just call glutSolidCube and it works, but the only way a cube gets drawn for me is if the line is enclosed in glBegin(GL_POLYGON), which isn't required in the examples I've seen, and I only get one cube instead of several. What I have is:
glColor3f(1, 0, 0);
glLoadIdentity();
glTranslatef(5,2,1);
glutSolidCube(1);
glLoadIdentity();
glTranslatef(10,8,0);
glutSolidCube(1);
glLoadIdentity();
glTranslatef(3,7,9);
glutSolidCube(1);
glLoadIdentity();
glTranslatef(1,4,6);
glutSolidCube(1);
When I run this nothing happens. I know there's not a problem with the points being outside my view because if I draw vertices at the same points, I can see them. As far as I can tell from the examples and documentation I've read, I'm not doing anything incorrect. Can someone tell me what I'm doing wrong or give me a snippet of code that draws multiple cubes?
Try this:
glColor3f(1, 0, 0);
glPushMatrix();
glTranslatef(5,2,1);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(10,8,0);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(3,7,9);
glutSolidCube(1);
glPopMatrix();
glPushMatrix();
glTranslatef(1,4,6);
glutSolidCube(1);
glPopMatrix();
Without re-setting the model view matrix with glLoadIdentity(). Note that to start with you need to call glOrtho() or glPerspective() to set the camera once.
#include <GL/glut.h>
void init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_COLOR_MATERIAL);
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
double aspect = (double)viewport[2] / (double)viewport[3];
gluPerspective(60, aspect, 1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// move back a bit
glTranslatef( 0, 0, -35 );
static float angle = 0;
angle += 1.0f;
glPushMatrix();
glTranslatef(0,0,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(255,0,255);
glutSolidCube(5);
glPopMatrix();
glPushMatrix();
glTranslatef(10,-10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(255,0,0);
glutSolidCube(5);
glPopMatrix();
glPushMatrix();
glTranslatef(10,10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(0,255,0);
glutSolidCube(5);
glPopMatrix();
glPushMatrix();
glTranslatef(-10,10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(0,0,255);
glutSolidCube(5);
glPopMatrix();
glPushMatrix();
glTranslatef(-10,-10,0);
glRotatef(angle, 0.1, 0.2, 0.5);
glColor3ub(255,255,0);
glutSolidCube(5);
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
}
void timer(int extra)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitWindowSize(640,480);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutCreateWindow("CUBES");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc(0, timer, 0);
init();
glutMainLoop();
return 0;
}