So I am able to draw a cube, but I want to draw multiple cubes, and I don't know how. Can you guys help me?
This is the code of how I draw the cube:
#include <GL/freeglut.h>
#include <stdlib.h>
void initGL(int width, int height)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,2.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
glEnable(GL_LIGHT0);glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,2.0f,100.0f);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_CULL_FACE);glCullFace(GL_BACK);
}
static void display(void)
{
glLoadIdentity();
glPushMatrix();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glPopMatrix();
glFlush();
}
static void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char *argv[])
{
int width = 640;
int height = 480;
glutInit(&argc, argv);
glutInitWindowSize(width,height);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Lab 1: Hello OpenGL World !");
glutDisplayFunc(display);
glutIdleFunc(idle);
initGL(width, height);
glutMainLoop();
return EXIT_SUCCESS;
}
I need to draw multiple cubes like the one that I am drawing. I tried with a for but I just started opengl today and don't know what I'm doing. I have tried multiple things and got very annoyed. Do you also recommend any book?
"I just started opengl today"
It will take a while to fully understand OpenGL but if you had consulted your study material, you should have spoted a few errors in you code.
You need to define a glutReshapeFunc function: without this your viewport is undefined. #KevinSpaghetti is also right your glutDisplayFunc has a bug: all cubes are drawn at the same position and a model-view matrix was not specified.
Here is a code that actually works.
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <iostream>
/*******************************************************************************
********************************************************************************
********************************************************************************/
struct { int width = 640; int height = 480; } window;
/*******************************************************************************
********************************************************************************
********************************************************************************/
void resize(int width, int height)
{
glViewport(0, 0, (window.width = width), (window.height = height));
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, ((GLdouble)window.width / window.height), 2, 100);
glMatrixMode(GL_MODELVIEW);
}
/*******************************************************************************
********************************************************************************
********************************************************************************/
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0, 30, 0, 0, 0, 0, 1, 0);
glRotatef(-30, 0, 1, 0); // <----- rotate entire scene by 30 degrees
glColor3f(0.5f, 0.0f, 0.0f);
for (int i = 0; i < 3; ++i) // <----- draw three cubes
{
glPushMatrix();
glRotatef(60, 1.0, 0.0, 0.0);
glRotatef(60, 0.0, 0.1, 0.0);
glutSolidCube(5);
glPopMatrix();
glTranslatef(0.0, 0.0, -10.0f);
}
glFlush();
}
/*******************************************************************************
********************************************************************************
********************************************************************************/
void initGL(void)
{
glClearColor(0.8f, 0.8f, 0.8f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glFrontFace(GL_CCW);
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glEnable(GL_COLOR_MATERIAL);
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, high_shininess);
}
/*******************************************************************************
********************************************************************************
********************************************************************************/
int main(int argc, char *argv[])
{
//.....................
glutInit(&argc, argv);
glutInitContextFlags(GLUT_FORWARD_COMPATIBLE);
glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE);
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_GLUTMAINLOOP_RETURNS);
glutInitWindowSize(window.width, window.height);
glutInitWindowPosition(10, 10);
glutInitDisplayMode(GLUT_DEPTH | GLUT_RGB | GLUT_SINGLE);
glutCreateWindow("Lab 1: Hello OpenGL World !");
if (glewInit() != GLEW_OK) exit(EXIT_FAILURE);
glutReshapeFunc(resize);
glutDisplayFunc(display);
initGL();
glutMainLoop();
std::cout << "...it worked!";
//.....................
return 0;
}
I would recommend: OpenGL Programming Guide. It is old OpenGl but still relevant.
glPushMatrix();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glPopMatrix();
Here it seems that you are drawing 2 cubes in the same position, so the second is drawn over the first, try changing the code to
glPushMatrix();
glTranslatef(2,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glLoadIdentity();
glTranslatef(0.0,0.0,-10);
glRotatef(60,1,0,0);
glRotatef(60,0,1,0);
glutSolidCube(2);
glPopMatrix();
Related
I am creating a sphere. The light source (ambient and diffuse lighting) works well. Only the specular light that doesn't show up in any part of the sphere.
The material code seems not working. I deleted the line and the result is also the same.
The result I want is the sphere rotates with white spot on it in order to look real.
Am I missing any required configuration for lighting?
Is there any solution?
Thanks.
float angle = 0.0f;
void initRendering(){
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_NORMALIZE);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
}
void draw(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3f(1.0f, 0.0f, 0.0f);
glPushMatrix();
glRotatef(angle, 0.0, 1.0, 0.0);
glutSolidSphere(0.5, 20, 20);
glPopMatrix();
GLfloat ambientColor[] = {0.0f, 0.0f, 0.0f, 1.0f};
GLfloat diffuseColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat specularColor[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat lightPosition[] = {0.0f, 5.0f, -3.0f, 0.0f};
GLfloat mat_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambientColor);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseColor);
glLightfv(GL_LIGHT0, GL_SPECULAR, specularColor);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMateriali(GL_FRONT, GL_SHININESS, 100);
angle+=0.1f;
glutPostRedisplay();
glutSwapBuffers();
}
main(){
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGBA);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Sphere");
glClearColor(1.0, 1.0, 1.0, 1.0);
initRendering();
glutDisplayFunc(draw);
glutMainLoop();
}
The default for specular light is to add to the colour evaulated in the diffuse illumination step. The specular intensity is modulated by the specular set. You've set this colour to "black", so it adds literally zero.
GLfloat mat_specular[] = { 0.8f, 0.8f, 0.8f, 1.0f };
// …
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
I'd start by actually not inhibiting the specular term.
I am trying to implement the bottom subwindow with three torus, but failed to display anything on the subwindow..
main function for subwindow:
instrument_window = glutCreateSubWindow(main_window, GAP, view_height + 3*GAP, 2*(view_width+GAP), INSTRUMENT_HEIGHT);
glutDisplayFunc(display);
//lighting
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 600, 1200, 1200, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
//glClearColor(0.33,0.33,0.33,0.33);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
//end of lighting
glutMainLoop();
for display function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
float width = 2*(GAP+view_width);
float height = INSTRUMENT_HEIGHT;
const float ar = (float) width / (float) height;
//glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(width/4.0, width*3.0/4.0, height*3.0/4.0, height/4.0, 2.0, 100.0);
//gluPerspective (160.0, 2*view_width/INSTRUMENT_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity() ;
/*
glMatrixMode(GL_PROJECTION);
// Reset transformations
glLoadIdentity();
gluPerspective (160.0, 2*view_width/INSTRUMENT_HEIGHT, 0.1, 1000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Draw first
draw (view_width+GAP-400, INSTRUMENT_HEIGHT/2);
// Draw second
draw (view_width+GAP-150, INSTRUMENT_HEIGHT/2);
// Draw third
draw (view_width+GAP+100, INSTRUMENT_HEIGHT/2);
for draw function:
void draw(double cx,double cf)
glColor3f(0.34f,0.34f,0.34f);
glPushMatrix();
glTranslated(0.0, cx, cy);
glRotated(-10, 1.0, 0.0, 0.0);
glutSolidTorus(OUTER_DIAL_RADIUS, OUTER_DIAL_RADIUS+30, 100, 200);
glPopMatrix();
end
really frustrating since it just display nothing and don't know how to debug. Any hints are highly appreciate!!!!
To be honest, its difficult to answer this question from the code example supplied. Half of the 'displayFunction' is commented out from the /* so nothing below it will get called. Even then its difficult to deduce the problem since there is little code relating to multiple viewports here. Since you are using fixed pipeline GL look at NeHe which has a good tutorial on employing multiple viewports.
http://nehe.gamedev.net/tutorial/multiple_viewports/20002/
Debugging is the single most important tool a developer has in their arsenal to resolve problems. It would be wise to learn how to do this as it would certainly save you time in the long run and you will find the answers to most (if not all) of your coding problems!
Problem: Objects aren't rendering correctly.
What I tried to do: Searched Stack Overflow. Read that I needed to enable Depth_Test and did so. Still doesn't work. Read that I possibly needed glDepthFunc(GL_LEQUAL);, did not work. Is there something in the way I wrote reshape/initscene/myDisplay that causes these common approaches not to work?
Code below.
void reshape (int w, int h)
{
viewport.w = w;
viewport.h = h;
glViewport(0,0,viewport.w,viewport.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();
}
void myDisplay() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the color buffer
glMatrixMode(GL_MODELVIEW); // indicate we are specifying camera transformations
glLoadIdentity(); // make sure transformation is "zero'd"
glScalef(zoom, zoom, zoom);
gluLookAt(0.0, -9.0, 0.0,
0.0, 0.0, 0.0,
0.0, 0.0, 1.0);
glRotatef(y_rotation, 0.0, 1.0, 0.0);
glRotatef(x_rotation, 1.0, 0.0, 0.0);
renderObjects();
glFlush();
glutSwapBuffers(); // swap buffers (we earlier set double buffer)
}
void initScene(){
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
reshape(viewport.w, viewport.h);
glShadeModel(GL_FLAT);
glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
GLfloat red[] = {1.0f, 0.2f, 0.2f, 1.0f};
GLfloat white[] = {1.0f, 1.0f, 1.0f, 1.0f};
GLfloat gray[] = {0.5f, 0.5f, 0.5f, 1.0f};
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, gray);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, gray);
GLfloat lightPos[] = {0.0f, 0.0f, -10.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
}
I'm trying to get all the quads filled. At the moment it colors only the right side of the box. Here is my code.
void showPictureW()
{
glDepthMask(true);
glColor3f(1,0,0);
//front
glBegin( GL_QUADS );
glVertex3f(-1.2,0.8,-4.0);
glVertex3f(2,0.8,-4.0);
glVertex3f(2,0.0,-4.0);
glVertex3f(-1.2,0.0,-4.0);
glEnd();
//back
glBegin( GL_QUADS );
glVertex3f(1.8,1.0,-4.0); //2
glVertex3f(1.8,1.8,-4.0);
glVertex3f(5.0,1.8,-4.0);
glVertex3f(5.0,1.0,-4.0);
glEnd();
//left
glBegin( GL_QUADS );
glVertex3f(1.8,1.8,-4.0);
glVertex3f(1.8,1.0,-4.0);
glVertex3f(-1.2,0.0,-4.0);
glVertex3f(-1.2,0.8,-4.0);
glEnd();
//right
glBegin( GL_QUADS );
glVertex3f(2.0,0.8,-4.0);
glVertex3f(2.0,0.0,-4.0);
glVertex3f(5.0,1.0,-4.0);
glVertex3f(5.0,1.8,-4.0);
glEnd();
}
static void resize(int width, int height)
{
const float ar = (float) width / (float) height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar, ar, -1.0, 1.0, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
static void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslated(-3.5,-3,0);
showPictureW();
glPopMatrix();
glutSwapBuffers();
}
static void idle(void)
{
glutPostRedisplay();
}
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
/* Program entry point */
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(800,600);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("FreeGLUT Shapes");
glutReshapeFunc(resize);
glutDisplayFunc(display);
glutKeyboardFunc(key);
glutIdleFunc(idle);
glClearColor(1,1,1,1);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
glutMainLoop();
return EXIT_SUCCESS;
}
When I use GL_LINE_LOOP the box displays fine. It's when I change to quads it doesn't output properly.
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
Make sure all your quads have the "correct" counter-clockwise winding.
Or disable culling.
I'd like to enable lighting in my openGL program, i already works for objects drawn with GLUT.
but when i draw my cuuboid
glBegin(5);
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_t,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_+(b/2)),y_t,(z_+(l/2)));
glEnd();
with
glColor3f(6,0,0);
lightning isn't applied on its surfaces. (the cuboid is drawn correct)
my lighning is set up like this:
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat light_position[] = { 2.0f, 5.0f, 1.0, 1.0f };
const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
const GLfloat high_shininess[] = { 100.0f };
glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
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, high_shininess);
does anyone know the flaw i make?
You didn't give it any normals. You need to use glNormal3f to specify the normals.
glBegin(GL_TRIANGLE_STRIP);
glNormal3f(0,1,0);
glVertex3f((x_+(b/2)),y_,(z_-(l/2)));
glVertex3f((x_+(b/2)),y_,(z_+(l/2)));
glVertex3f((x_-(b/2)),y_,(z_-(l/2)));
glVertex3f((x_-(b/2)),y_,(z_+(l/2)));
glNormal3f(-1,0,0);
// etc.