Hello,
I am trying to solve my OpenGL pyramid project. If anyone can please help me with a code as soon as possible.
I have to do when I build the code it will come black window After that perform the following task:
- F1+r = 60’ rotation(static)
- F2+r =45’ rotation(continuous x-axis)
- CTRL+w = White
- B = Black
Thanks for your concern and help.
#include <stdlib.h>
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// all variables initialized to 1.0, meaning
// the triangle will initially be white
float red=1.0f, blue=1.0f, white=1.0f;
// angle for rotating triangle
float angle = 0.0f;
void changeSize(int w, int h) {
// Prevent a divide by zero, when window is too short
// (you cant make a window of zero width).
if (h == 0)
h = 1;
float ratio = w * 1.0 / h;
// Use the Projection Matrix
glMatrixMode(GL_PROJECTION);
// Reset Matrix
glLoadIdentity();
// Set the viewport to be the entire window
glViewport(0, 0, w, h);
// Set the correct perspective.
gluPerspective(45.0f, ratio, 0.1f, 100.0f);
// Get Back to the Modelview
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void) {
// Clear Color and Depth Buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glLoadIdentity();
// Set the camera
gluLookAt( 0.0f, 0.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glRotatef(angle, 1.0f, 1.0f, 0.0f);
glColor3f(red,white,blue);
glBegin(GL_TRIANGLES);
//glColor3f(1.0, 1.0, 1.0); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
// glColor3f(1.0, 1.0, 1.0); // Green
glVertex3f(-1.0f, -1.0f, 1.0f);
//glColor3f(1.0, 1.0, 1.0); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
// Right
// glColor3f(0.0, 1.0, 1.0); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
//glColor3f(0.0, 1.0, 1.0); // Blue
glVertex3f(1.0f, -1.0f, 1.0f);
//glColor3f(0.0, 1.0, 1.0); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// Back
//glColor3f(0.0, 0.5, 0.0); // Red
glVertex3f(0.0f, 1.0f, 0.0f);
// glColor3f(0.0, 0.5, 0.0); // Green
glVertex3f(1.0f, -1.0f, -1.0f);
// glColor3f(0.0, 0.5, 0.0); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f);
// Left
// glColor3f(0.6, 0.6, 0.6); // Red
glVertex3f( 0.0f, 1.0f, 0.0f);
// glColor3f(0.663, 0.663, 0.663); // Blue
glVertex3f(-1.0f,-1.0f,-1.0f);
// glColor3f(0.663, 0.663, 0.663); // Green
glVertex3f(-1.0f,-1.0f, 1.0f);
glEnd();
angle+=0.1f;
glutSwapBuffers();
}
void processNormalKeys(unsigned char key, int x, int y) {
if (key == 27)
exit(0);
}
void processSpecialKeys(int key, int x, int y) {
switch(key) {
if(key=='1')
{
angle+=10;
}
if(key=='0')
{
angle+=1;
}
case GLUT_KEY_F1 :
red = 1.0;
white = 0.0;
blue = 0.0;
; break;
case GLUT_KEY_F2 :
red = 0.0;
white = 1.0;
blue = 0.0; break;
case GLUT_KEY_F3 :
red = 0.0;
white = 0.0;
blue = 1.0; break;
}
}
int main(int argc, char **argv) {
// init GLUT and create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(640,480);
glutCreateWindow("Lighthouse3D- GLUT Tutorial");
// register callbacks
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
glutIdleFunc(renderScene);
// here are the new entries
glutKeyboardFunc(processNormalKeys);
glutSpecialFunc(processSpecialKeys);
// enter GLUT event processing cycle
glutMainLoop();
return 1;
}