Why isn't this opengl background working

Started by
5 comments, last by Choo Wagga Choo Choo 5 years ago

Why isn't this opengl background and lighting working?

 

#include<iostream>
#include<stdlib.h>

#ifdef __APPLE__
#include<OpenGL/OpenGL.h>
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<math.h>

// g++ rotateCube.cpp -lm -lglut -lGL -lGLU

using namespace std;

void handleResize(int w, int h);
void drawCube();
void update(int value);
void initRendering();

//The coordinates for vertices of the cube
double x=0.6;
double y=0.6;
double z=0.6;

float angle=0.0;

void drawCube()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
//Add ambient light
GLfloat ambientColor[]={0.2,0.2,0.2,1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add a positioned light
GLfloat lightColor0[]={0.5,0.5,0.5,1.0};
GLfloat lightPos0[]={4.0,0.0,8.0,1.0};
glLightfv(GL_LIGHT0,GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0,GL_DIFFUSE, lightPos0);
glTranslatef(0.5,1.0,0.0);
glRotatef(angle,1.0,1.0,1.0);
glRotatef(angle,1.0,0.0,1.0);
glRotatef(angle,0.0,1.0,1.0);
glTranslatef(-0.5,-1.0,0.0);
//Create the 3d cube
//BACK
glBegin(GL_POLYGON);
glColor3f(0.5,0.3,0.2);
glVertex3f(x,-y,z);
glVertex3f(x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,-y,z);
glEnd();
//FRONT
glBegin(GL_POLYGON);
glColor3f(0.0,0.5,0.0);
glVertex3f(-x,y,-z);
glVertex3f(-x,-y,-z);
glVertex3f(x,-y,-z);
glVertex3f(x,y,-z);
glEnd();
//LEFT
glBegin(GL_POLYGON);
glColor3f(0.5,0.5,0.5);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glEnd();
//RIGHT
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,-y,-z);
glVertex3f(x,-y,z);
glVertex3f(x,y,-z);
glVertex3f(x,y,-z);
glEnd();
//TOP
glBegin(GL_POLYGON);
glColor3f(0.6,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glVertex3f(x,y,-z);
glEnd();
//BOTTOM
glBegin(GL_POLYGON);
glColor3f(0.3,0.0,0.3);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(x,-y,z);
glVertex3f(x,-y,-z);
glEnd();

glFlush();
glutSwapBuffers();
}

// Function for increasing the angle variable smoothly,
// keeps it <=360
//It can also be implemented using the modulo operator
void update(int value)
{
angle+=1.0f;
if(angle>360)
{
angle-=360;
}

glutPostRedisplay();
glutTimerFunc(25, update, 0);
}

//Initializes 3D rendering
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//Set the color of the background
glClearColor(0.7f,0.8f,1.0f,1.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
}

// Called when the window is resized
void handleResize(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}

int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700,700);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL - Rotating a Cube");
glutDisplayFunc(drawCube);
glutReshapeFunc(handleResize);
// Add a timer for the update(...) function
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}

 

Advertisement

Could you edit your post and put the code in code tags? That will make it easier to read. (Obviously there's not much code there and I'm sure someone could diagnose the problem as it is currently, but it would be easier with proper formatting.)

Why isn't this opengl background and lighting working?

 


	#include<iostream>
#include<stdlib.h>
	#ifdef __APPLE__
#include<OpenGL/OpenGL.h>
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<math.h>
	// g++ rotateCube.cpp -lm -lglut -lGL -lGLU
	using namespace std;
	void handleResize(int w, int h);
void drawCube();
void update(int value);
void initRendering();
	//The coordinates for vertices of the cube
double x=0.6;
double y=0.6;
double z=0.6;
	float angle=0.0;
	void drawCube()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
//Add ambient light
GLfloat ambientColor[]={0.2,0.2,0.2,1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add a positioned light
GLfloat lightColor0[]={0.5,0.5,0.5,1.0};
GLfloat lightPos0[]={4.0,0.0,8.0,1.0};
glLightfv(GL_LIGHT0,GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0,GL_DIFFUSE, lightPos0);
glTranslatef(0.5,1.0,0.0);
glRotatef(angle,1.0,1.0,1.0);
glRotatef(angle,1.0,0.0,1.0);
glRotatef(angle,0.0,1.0,1.0);
glTranslatef(-0.5,-1.0,0.0);
//Create the 3d cube
//BACK
glBegin(GL_POLYGON);
glColor3f(0.5,0.3,0.2);
glVertex3f(x,-y,z);
glVertex3f(x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,-y,z);
glEnd();
//FRONT
glBegin(GL_POLYGON);
glColor3f(0.0,0.5,0.0);
glVertex3f(-x,y,-z);
glVertex3f(-x,-y,-z);
glVertex3f(x,-y,-z);
glVertex3f(x,y,-z);
glEnd();
//LEFT
glBegin(GL_POLYGON);
glColor3f(0.5,0.5,0.5);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glEnd();
//RIGHT
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,-y,-z);
glVertex3f(x,-y,z);
glVertex3f(x,y,-z);
glVertex3f(x,y,-z);
glEnd();
//TOP
glBegin(GL_POLYGON);
glColor3f(0.6,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glVertex3f(x,y,-z);
glEnd();
//BOTTOM
glBegin(GL_POLYGON);
glColor3f(0.3,0.0,0.3);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(x,-y,z);
glVertex3f(x,-y,-z);
glEnd();
	glFlush();
glutSwapBuffers();
}
	// Function for increasing the angle variable smoothly,
// keeps it <=360
//It can also be implemented using the modulo operator
void update(int value)
{
angle+=1.0f;
if(angle>360)
{
angle-=360;
}
	glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
	//Initializes 3D rendering
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//Set the color of the background
glClearColor(0.7f,0.8f,1.0f,1.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
}
	// Called when the window is resized
void handleResize(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
	int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700,700);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL - Rotating a Cube");
glutDisplayFunc(drawCube);
glutReshapeFunc(handleResize);
// Add a timer for the update(...) function
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}
	#include<iostream>
#include<stdlib.h>
	#ifdef __APPLE__
#include<OpenGL/OpenGL.h>
#include<GLUT/glut.h>
#else
#include<GL/glut.h>
#endif
#include<math.h>
	// g++ rotateCube.cpp -lm -lglut -lGL -lGLU
	using namespace std;
	void handleResize(int w, int h);
void drawCube();
void update(int value);
void initRendering();
	//The coordinates for vertices of the cube
double x=0.6;
double y=0.6;
double z=0.6;
	float angle=0.0;
	void drawCube()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Reset transformations
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
//Add ambient light
GLfloat ambientColor[]={0.2,0.2,0.2,1.0};
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
//Add a positioned light
GLfloat lightColor0[]={0.5,0.5,0.5,1.0};
GLfloat lightPos0[]={4.0,0.0,8.0,1.0};
glLightfv(GL_LIGHT0,GL_DIFFUSE, lightColor0);
glLightfv(GL_LIGHT0,GL_DIFFUSE, lightPos0);
glTranslatef(0.5,1.0,0.0);
glRotatef(angle,1.0,1.0,1.0);
glRotatef(angle,1.0,0.0,1.0);
glRotatef(angle,0.0,1.0,1.0);
glTranslatef(-0.5,-1.0,0.0);
//Create the 3d cube
//BACK
glBegin(GL_POLYGON);
glColor3f(0.5,0.3,0.2);
glVertex3f(x,-y,z);
glVertex3f(x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,-y,z);
glEnd();
//FRONT
glBegin(GL_POLYGON);
glColor3f(0.0,0.5,0.0);
glVertex3f(-x,y,-z);
glVertex3f(-x,-y,-z);
glVertex3f(x,-y,-z);
glVertex3f(x,y,-z);
glEnd();
//LEFT
glBegin(GL_POLYGON);
glColor3f(0.5,0.5,0.5);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glEnd();
//RIGHT
glBegin(GL_POLYGON);
glColor3f(0.0,0.0,0.0);
glVertex3f(x,-y,-z);
glVertex3f(x,-y,z);
glVertex3f(x,y,-z);
glVertex3f(x,y,-z);
glEnd();
//TOP
glBegin(GL_POLYGON);
glColor3f(0.6,0.0,0.0);
glVertex3f(x,y,z);
glVertex3f(-x,y,z);
glVertex3f(-x,y,-z);
glVertex3f(x,y,-z);
glEnd();
//BOTTOM
glBegin(GL_POLYGON);
glColor3f(0.3,0.0,0.3);
glVertex3f(-x,-y,-z);
glVertex3f(-x,-y,z);
glVertex3f(x,-y,z);
glVertex3f(x,-y,-z);
glEnd();
	glFlush();
glutSwapBuffers();
}
	// Function for increasing the angle variable smoothly,
// keeps it <=360
//It can also be implemented using the modulo operator
void update(int value)
{
angle+=1.0f;
if(angle>360)
{
angle-=360;
}
	glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
	//Initializes 3D rendering
void initRendering()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//Set the color of the background
glClearColor(0.7f,0.8f,1.0f,1.0f);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
}
	// Called when the window is resized
void handleResize(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,(double)w/(double)h,1.0,200.0);
}
	int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(700,700);
glutInitWindowPosition(100,100);
glutCreateWindow("OpenGL - Rotating a Cube");
glutDisplayFunc(drawCube);
glutReshapeFunc(handleResize);
// Add a timer for the update(...) function
glutTimerFunc(25, update, 0);
glutMainLoop();
return 0;
}

Hm, that didn't really help, I guess :)

Since someone is bound to ask eventually, I'll go ahead and do so: is there any particular reason you're using the fixed-function pipeline?

As for the specific problem, I'd recommend creating a minimal example (it looks like you could probably pare what you have down a bit), and then try reposting with more readable formatting (e.g. consistent indentation and so forth).

That said, unless you have a good reason for using legacy OpenGL features (and maybe you do have a good reason), you might be better off starting over with modern OpenGL.

My example shows how to draw a cube with texture using FreeGLUT and GLFW. Maybe it will be useful for you in the future. You can just download, open and run it in Visual Studio. All libraries are set up locally. Just select your "Platform Toolset" in the project settings if you do not have VS2015. This tutorial is good for start: https://learnopengl.com/

TexturedCube_GlfwStbImageOpenGL31Cpp.png.c899ddcbfd6cd9794a0cf2aff511aea2.png

To me it appears you translate your view into your world by -5.0 units(openGL) on the z axis early in DrawCube(). A 0.6 radius cube is rendered at the origin. I don't see a rotation on the view matrix so that's already behind your camera I believe, from a glance. Try translating your view the other direction to test.

This topic is closed to new replies.

Advertisement