Related
I try to code two object move independent which mean both are moving two different direction it's worked but it can't move continuous. when i put glTranslatef() at outside the glPushMatrix() ... glPopMatrix() it work normally.
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
// if put gltransate() at here the object will moving continuous
glPushMatrix();
glTranslatef(.5, 0, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
glTranslatef(-.5, 0, 0);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
i expect the for the first square objects will go to the right constantly but it seem like just translate once then stop at the position.
Note, in general I recommend to use a math library like OpenGL Mathematics to do the matrix calculations and glLoadMatrix() to load a matrix of type glm::mat4 to the current matrix.
Anyway, an operation like glTranslatef() creates a new matrix and multiplies the current matrix by the new matrix. That's why consecutive calls to glTranslatef cause progressive "movement".
glPushMatrix / glPopMatrix store and restore the matrix on the matrix stack. So the consecutive movement can't work, because the current matrix stays the same at the begin of each frame.
One solution would be to store the translation to a variable and increment the variable:
GLfloat trans_a = 0.0f;
GLflaot trans_b = 0.0f;
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
trans_a += 0.5f;
glTranslatef(trans_a, 0, 0);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
trans_b += 0.5f;
glTranslatef(trans_b, 0, 0);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
A more generalized solution is to get and store the current matrix (after the translation) by glGetFloatv(GL_MODELVIEW_MATRIX, ...) and to reload it by glLoadMatrix():
// init identity matrices
GLfloat mat_a[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
GLfloat mat_b[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 };
void display()
{
glClearColor(0.356, 0.701, 0.0, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glLoadMatrixf(mat_a)
glTranslatef(0.5f, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat_a);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.8, 0.8);
glVertex2f(-0.2, 0.8);
glVertex2f(-0.2, 0.5);
glEnd();
glPopMatrix();
glPushMatrix();
glLoadMatrixf(mat_b)
glTranslatef(0.5f, 0, 0);
glGetFloatv(GL_MODELVIEW_MATRIX, mat_b);
glBegin(GL_QUADS);
glColor3f(.0, .0, .0);
glVertex2f(-0.8, 0.2);
glVertex2f(-0.8, 0.5);
glVertex2f(-0.2, 0.5);
glVertex2f(-0.2, 0.2);
glEnd();
glPopMatrix();
}
Note, in this case mat_a and mat_b should be loaded after the initialization of the view matrix and when the view matrix changes, then the matrices would not consider that.
I'm trying to figure out how to write my own primitives like gluSolidCube()..
In the following code I'm drawing 2 quads. One with the help of this method (the red one) and another one - by my own with the help of glBegin()/glEnd() (the blue one).
glPushMatrix();
glRotatef(-angleX, 0, 0, 1);
glRotatef(angleY, 0, 1, 0);
glColor3f(1, 0, 0);
glTranslatef(0.6, 0, 0);
glutSolidCube(1);
glColor3f(0, 0, 1);
glTranslatef(-1.2, 0, 0);
glPolygonMode(GL_FRONT, GL_FILL);
glShadeModel(GL_SMOOTH);
glBegin(GL_QUADS);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, -0.5);
glVertex3f(-0.5, 0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(0.5, 0.5, 0.5);
glVertex3f(-0.5, 0.5, 0.5);
glVertex3f(-0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, 0.5);
glVertex3f(0.5, -0.5, -0.5);
glVertex3f(-0.5, -0.5, -0.5);
glVertex3f(-0.5, -0.5, 0.5);
glEnd();
glPopMatrix();
So as you can see at the following screenshots the lightnings on the red quad is correct unlike the blue one. How to solve this?
OpenGL fixed-function lighting won't work correctly without sensible vertex/face normals.
You need to supply some, perhaps via glNormal().
I create a cube as room and set camera inside it. I want to draw a clock on one of the walls. but when I draw a circle for showing clock, it doesn't work correctly. can anyone help me to draw a clock on the wall? here is my codes:
void room(){
if(flag){
glRotatef(rt,0.0f,1.0f,0.0f);
}
glEnable(GL_NORMALIZE);
glColor3f(1.0, 1.0 ,1.0); //roof
glNormal3f(0,-1,0);
glVertex3f( 3.0, 3.0,-3.0);
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f( 3.0, 3.0, 3.0);
glColor3f(1.0, 1.0 ,0.0); //floor
glNormal3f(0,1,0);
glVertex3f( 3.0,-3.0, 3.0);
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f( 3.0,-3.0,-3.0);
glColor3f(1.0, 0.0 ,0.0); //behind wall
glNormal3f(0,0,-1);
glVertex3f( 3.0, 3.0, 3.0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f(-3.0,-3.0, 3.0);
glVertex3f( 3.0,-3.0, 3.0);
glColor3f( 0.0, 1.0,1.0); // front wall
glNormal3f(0,0,1);
glVertex3f( 3.0,-3.0,-3.0);
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f( 3.0, 3.0,-3.0);
glColor3f(0.0 ,0.0,1.0); //left wall
glNormal3f(1,0,0);
glVertex3f(-3.0, 3.0, 3.0);
glVertex3f(-3.0, 3.0,-3.0);
glVertex3f(-3.0,-3.0,-3.0);
glVertex3f(-3.0,-3.0, 3.0);
glColor3f( 0.2, 0.4, 0.0); // right wall
glNormal3f(-1,0,0);
glVertex3f( 3.0, 3.0,-3.0);
glVertex3f( 3.0, 3.0, 3.0);
glVertex3f( 3.0,-3.0, 3.0);
glVertex3f( 3.0,-3.0,-3.0);
}
void drawClock() {
char buff[100];
GLfloat x1=2.0;
GLfloat y1=2.0;
GLfloat z1=-3.0;
GLfloat radius = 1.0; // Radius
int angle;
glLoadIdentity();
//glClearColor(1.0, 1.0, 1.0, 1.0);
//glClear(GL_COLOR_BUFFER_BIT);
//glOrtho(0.0, 20.0, 0.0, 20.0, -10.0, 10.0);
// Draw circle
glPushMatrix();
glTranslatef(3.0,3.0,-3.0);
glBegin(GL_TRIANGLE_FAN);
glColor3f(0.5,1.0,0.0);
glVertex3f(x1, y1, z1);
for(angle=0; angle <= 360; angle +=1)
glVertex3f(x1 + cos(angle * PI/180.0f)* radius, y1 + sin(angle *PI/180.0f)* radius, z1);
glEnd();
glPopMatrix();
}
The center of the wall should be (0, 0, -3) not (2, 2, -3). Probably it's better to use however (0, 0, -2.9) so that the clock is slightly inside and not exactly on the wall.
Also I'd say that you need to add
glBegin(GL_QUADS);
at the start of room function and
glEnd();
at the end of it.
Also why are you calling glTranslatef before drawing the clock if it uses the same coordinate system as the walls?
been following a few tutorials and other questions people have been asking on here. Essentially trying to make it when you press the escape key the background colour changes into another colour.
Edited the post with the whole code.
#include "stdafx.h"
#include <glut.h>
void render(void);
void keyboard(int key, int x, int y);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100, 100); //Position of the window
glutInitWindowSize(620, 440); //Screen Size
glClearColor (0.0, 1.0, 0.0, 0.0 );
glutCreateWindow("Greeting Card"); //Creates the window and names it
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Enables Alpha channel
glutDisplayFunc(render);
glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
glutMainLoop(); //Finished, now render
}
void keyboard (unsigned char key, int x, int y)
{
GLfloat colors[][3] = { { 0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f } };
static int back;
switch (key) {
case 27:
exit(0);
default:
back ^= 1;
glClearColor(colors[back][0], colors[back][1], colors[back][2], 1.0f);
glutPostRedisplay();
}
}
void render(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// World Snow //
glPushMatrix();
glTranslatef(0, -0.35, 0); //Position of the shape
glBegin(GL_POLYGON); //Defines the type of shape
glColor3f(1, 1,1); //Colour of the shape 'RED, GREEN, BLUE'
glVertex2f(-1.5,-0.7); //Vertex 2F Gives the vertex some coords
glColor3f(1, 1, 1);
glVertex2f(-1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5,-0.7);
glEnd();
glPopMatrix();
glFlush();
// Grey gradient world
glPushMatrix();
glTranslatef(0, -0.35, 0);
glBegin(GL_POLYGON);
glColor3f(1, 1, 1);
glVertex2f(-1.5,-0.7);
glColor3f(1, 1, 1);
glVertex2f(-1.5, 0.7);
glColor3f(0.658824, 0.658824, 0.658824);
glVertex2f( 1.5, 0.7);
glColor3f(1, 1, 1);
glVertex2f( 1.5,-1.7);
glEnd();
glPopMatrix();
glFlush();
// Top of the first Tree //
glPushMatrix();
glTranslatef(-0.6, 0.5, 0);
glBegin(GL_TRIANGLES); //Defines the shape as being a triangle
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
// Middle of the first tree
glPushMatrix();
glTranslatef(-0.6, 0.4, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
// Bottom of the first tree
glPushMatrix();
glTranslatef(-0.6, 0.3, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.1, -0.1);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.1);
glEnd();
glPopMatrix();
glFlush();
//Stump of first tree
glPushMatrix();
glTranslatef(-0.6, 0.16, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.04);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.04);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.02, 0.04);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.04);
glEnd();
glPopMatrix();
// Large Tree TOP
glPushMatrix();
glTranslatef(-0.2, 0, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Large Tree MIDDLE
glPushMatrix();
glTranslatef(-0.2, -0.15, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);;
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Large Tree Bottom
glPushMatrix();
glTranslatef(-0.2, -0.30, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.15, -0.15);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.15);
glEnd();
glPopMatrix();
glFlush();
//Smaller tree Top
glPushMatrix();
glTranslatef(0.05, 0.45, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//Smaller tree MIDDLE
glPushMatrix();
glTranslatef(0.05, 0.40, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//smaller tree bottom
glPushMatrix();
glTranslatef(0.05, 0.50, 0);
glBegin(GL_TRIANGLES);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(-0.05, -0.05);
glColor3f( 0.137255, 0.556863,0.137255);
glVertex2f(0.05, -0.05);
glColor3f(0.32, 0.49, 0.46);
glVertex2f(0.0, 0.05);
glEnd();
glPopMatrix();
glFlush();
//Stump of smaller tree
glPushMatrix();
glTranslatef(0.05, 0.32, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.01,-0.03);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.01, 0.03);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.01, 0.03);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.01,-0.03);
glEnd();
glPopMatrix();
//Stump of MAIN tree
glPushMatrix();
glTranslatef(-0.2, -0.50, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.05);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.05);
glColor3f( 0.647059, 0.164706, 0.164706);
glVertex2f( 0.02, 0.05);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.05);
glEnd();
glPopMatrix();
// Red Present
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 1, 0, 1);
glVertex2f(-0.04,-0.05);
glColor3f( 1, 0, 0);
glVertex2f(-0.04, 0.05);
glColor3f( 1, 0, 0);
glVertex2f( 0.04, 0.05);
glColor3f( 1, 0, 0);
glVertex2f( 0.04,-0.05);
glEnd();
glPopMatrix();
//Blue Present
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.07,-0.06);
glColor3f( 0, 0, 1);
glVertex2f(-0.07, 0.06);
glColor3f( 0, 0, 1);
glVertex2f( 0.07, 0.06);
glColor3f( 0.196078, 0.6, 0.8);
glVertex2f( 0.07,-0.06);
glEnd();
glPopMatrix();
// BLUE Ribbon RED present VERT
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.04,-0.01);
glColor3f( 0, 0, 1);
glVertex2f(-0.04, 0.01);
glColor3f( 0, 0, 1);
glVertex2f( 0.04, 0.01);
glColor3f( 0, 0, 1);
glVertex2f( 0.04,-0.01);
glEnd();
glPopMatrix();
//BLUE ribbon RED present HORIZ
glPushMatrix();
glTranslatef(0, -0.5, 0);
glBegin(GL_POLYGON);
glColor3f( 0, 0, 1);
glVertex2f(-0.01,-0.05);
glColor3f( 0, 0, 1);
glVertex2f(-0.01, 0.05);
glColor3f( 0, 0, 1);
glVertex2f( 0.01, 0.05);
glColor3f( 0, 0, 1);
glVertex2f( 0.01,-0.05);
glEnd();
glPopMatrix();
//Yellow Ribbon Blue Present VERT
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.07,-0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.07, 0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.07, 0.01);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.07,-0.01);
glEnd();
glPopMatrix();
// BLUE present YELLOW ribbon VERT
glPushMatrix();
glTranslatef(-0.2, -0.7, 0);
glBegin(GL_POLYGON);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.01,-0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f(-0.01, 0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.01, 0.06);
glColor3f( 0.6, 0.8, 0.196078);
glVertex2f( 0.01,-0.06);
glEnd();
glPopMatrix();
//Sign Post
glPushMatrix();
glTranslatef(0.5, -0.1, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02,-0.25);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.02, 0.25);
glColor3f( 0.35, 0.16, 0.14);
glVertex2f( 0.02, 0.25);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.02,-0.25);
glEnd();
glPopMatrix();
//Sign, Attatched to the post
glPushMatrix();
glTranslatef(0.5, -0.001, 0);
glBegin(GL_POLYGON);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.15,-0.10);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f(-0.15, 0.10);
glColor3f( 0.35, 0.16, 0.14);
glVertex2f( 0.15, 0.10);
glColor3f( 0.36, 0.25, 0.20);
glVertex2f( 0.15,-0.10);
glEnd();
glPopMatrix();
//Moon
glPushMatrix();
glTranslatef(-0.9, 0.90, 0);
glBegin(GL_POLYGON);
glColor4f( 0.90, 0.91, 0.98, 1); //RGBA
glVertex2f(-0.10,-0.2);
glColor4f( 0.329412, 0.329412, 0.329412, 1);
glVertex2f(-0.10, 0.2);
glColor4f( 0.90, 0.91, 0.98, 1);
glVertex2f( 0.10, 0.2);
glColor4f( 0.90, 0.91, 0.98, 1);
glVertex2f( 0.10,-0.2);
glEnd();
glPopMatrix();
//MAIN PRESENT UNDER SIGN
glPushMatrix();
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f(-0.20,-0.20);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f(-0.20, 0.20);
glColor3f( 0.89, 0.47, 0.20);
glVertex2f( 0.20, 0.20);
glColor3f( 1.0, 0.25, 0);
glVertex2f( 0.20,-0.20);
glEnd();
glPopMatrix();
//Orange Present Purple Ribbon VERT
glPushMatrix();
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f(-0.20,-0.06);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f(-0.20, 0.06);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f( 0.20, 0.06);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f( 0.20,-0.06);
glEnd();
glPopMatrix();
//Orange Present Purple Ribbon HORIZ
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06,-0.20);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06,-0.20);
glEnd();
glPopMatrix();
glFlush();
//'North Pole' TEXT sign
glPushMatrix();
glLoadIdentity();
glTranslatef(0.360, -0.010, 0);
glRotatef(90,0.0f,0.0f,0.0f);
glColor3f( 0.0, 0.0, 0.0 ); //Colour is black
glRasterPos3i(10,100,1);
char text[50]="North Pole"; //Text
for(int i=0; i<50; i++)
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18,(int)text[i]);
}
glPopMatrix();
glutSwapBuffers();
}
trying to make it when you press the escape key the background colour changes into another colour.
Then why are you exit()ing when you press it? Swap your case 27: and default: logic.
Reset your projection/modelview matrices each time through render():
void render()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
...
You also have an un-matched glPopMatrix() in "Orange Present Purple Ribbon HORIZ":
//Orange Present Purple Ribbon HORIZ
glTranslatef(0.5, -0.6, 0);
glBegin(GL_POLYGON);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06,-0.20);
glColor3f( 0.87, 0.58, 0.98);
glVertex2f(-0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06, 0.20);
glColor3f( 0.73, 0.16, 0.96);
glVertex2f( 0.06,-0.20);
glEnd();
glPopMatrix();
^^^^^^^^^^^^^ wha-hey?
Other than those things the background toggle seems to work.
I am lighting my scene in opengl and there are 4 walls, only 2 of them are being lit.
I think it is something wrong with the normals but I am not sure.
This is the lighting code:
glShadeModel(GL_SMOOTH);
//glEnable(GL_CULL_FACE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
GLfloat lightpos0[] = {-5, 1, 0, 0.};
GLfloat AmbientLight0[] = {0.2, 0.2, 0.2,1.0};
GLfloat DiffuseLight0[] = {0.8, 0.8, 0.8,1.0};
GLfloat SpecularLight0[] = {1.0, 1.0, 1.0,1.0};
glLightfv(GL_LIGHT0, GL_POSITION, lightpos0);
glLightfv(GL_LIGHT0, GL_AMBIENT, AmbientLight0);
glLightfv(GL_LIGHT0, GL_DIFFUSE, DiffuseLight0);
glLightfv(GL_LIGHT0, GL_SPECULAR, SpecularLight0);
GLfloat lightpos1[] = {5, 1, 0, 0};
GLfloat AmbientLight1[] = {0.2, 0.2, 0.2,1.0};
GLfloat DiffuseLight1[] = {0.8, 0.8, 0.8,1.0};
GLfloat SpecularLight1[] = {1.0, 1.0, 1.0,1.0};
glLightfv(GL_LIGHT1, GL_POSITION, lightpos1);
glLightfv(GL_LIGHT1, GL_AMBIENT, AmbientLight1);
glLightfv(GL_LIGHT1, GL_DIFFUSE, DiffuseLight1);
glLightfv(GL_LIGHT1, GL_SPECULAR, SpecularLight1);
This is the code for drawing the 4 walls.
//north
glBegin(GL_QUADS);
glNormal3f(1.0,0,0);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//south
glBegin(GL_QUADS);
glNormal3f(-1.0, 0, 0);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//east
glBegin(GL_QUADS);
glNormal3f(0,0, -1.0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//west
glBegin(GL_QUADS);
glNormal3f(0,0,1.0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();
If it helps, here's and image of whats happening: http://i.stack.imgur.com/xRgVL.png
As pointed out by Vladislav Khorev, the normals must be directed out of the coordinate that is common to all points (given this special orthogonal arrangement).
But also one should consider the winding of the polygons:
// Polygon A -- north Polygon B -- south
glVertex3f(-10,0,-10); glVertex3f(-10,0, 10);
glVertex3f( 10,0,-10); glVertex3f( 10,0, 10);
glVertex3f( 10,5,-10); glVertex3f( 10,5, 10);
glVertex3f(-10,5,-10); glVertex3f(-10,5, 10);
Normal(0,0,1), Normal(0,0,-1);
The two polygons (north and south), are of different winding. The first is ClockWise observed from point 0,0,0 and the other of CCW. One should be consistent with these.
It seems that you pass wrong normal vector to walls.
If your wall got Z = -10 for all points (wall is parallel to X), then it must have normal vector directed in positive direction of Z: (0,0,1).
Same is true for all directions.
Try this:
glBegin(GL_QUADS);
glNormal3f(0,0,1);
glVertex3f(-10,0,-10);
glVertex3f( 10,0,-10);
glVertex3f( 10,5,-10);
glVertex3f(-10,5,-10);
glEnd();
//south
glBegin(GL_QUADS);
glNormal3f(0, 0, -1);
glVertex3f(-10,0, 10);
glVertex3f( 10,0, 10);
glVertex3f( 10,5, 10);
glVertex3f(-10,5, 10);
glEnd();
//east
glBegin(GL_QUADS);
glNormal3f(-1,0, -0);
glVertex3f( 10,0,-10);
glVertex3f( 10,0,10);
glVertex3f( 10,5,10);
glVertex3f( 10,5,-10);
glEnd();
//west
glBegin(GL_QUADS);
glNormal3f(1, 0, 0);
glVertex3f( -10,0,-10);
glVertex3f( -10,0,10);
glVertex3f( -10,5,10);
glVertex3f( -10,5,-10);
glEnd();