What is wrong with my lighting? It seems everything is fine, but in fact not what you need.

Here is the code:

#include "stdafx.h" #include <gl/glut.h> #include <malloc.h> #include <math.h> #ifndef M_PI #define M_PI 3.1415 #endif float winWid = 600.0; float winHeig = 600.0; float angleX = 90, angleY = 0; float scale= 1.0; int button = -1; float mouseX, mouseY; float mat1_dif[]={0.8f,0.8f,0.0f}; float mat1_amb[] = {0.2f,0.2f,0.2f}; float mat1_spec[] = {0.6f,0.6f,0.6f}; float mat1_shininess=0.5f*128; float mat4_dif[] = {0.9f,0.2f,0.9f}; float mat4_amb[] = {0.2f,0.2f,0.2f}; float mat4_spec[] = {0.6f,0.6f,0.6f}; float mat4_shininess = 0.1f*128; GLuint texture; GLuint LoadTextureRAW( const char * filename, int wrap ) { GLuint texture; int width, height; unsigned char * data; FILE * file; // open texture data file = fopen( filename, "rb" ); if ( file == NULL ) return 0; // allocate buffer width = 256; height = 256; data = (unsigned char*) malloc( width * height * 3 ); // read texture data fread( data, width * height * 3, 1, file ); fclose( file ); // allocate a texture name glGenTextures( 1, &texture ); // select our current texture glBindTexture( GL_TEXTURE_2D, texture ); // select modulate to mix texture with color for shading glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE ); // when texture area is small, bilinear filter the closest mipmap glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST ); // when texture area is large, bilinear filter the first mipmap glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); // if wrap is true, the texture wraps over at the edges (repeat) // ... false, the texture ends at the edges (clamp) glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap ? GL_REPEAT : GL_CLAMP ); glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap ? GL_REPEAT : GL_CLAMP ); // build our texture mipmaps gluBuild2DMipmaps( GL_TEXTURE_2D, 3, width, height, GL_RGB, GL_UNSIGNED_BYTE, data ); // free buffer free( data ); return texture; } void cube(float size){ glMaterialfv(GL_FRONT,GL_AMBIENT,mat4_amb); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat4_dif); glMaterialfv(GL_FRONT,GL_SPECULAR,mat4_spec); glMaterialf(GL_FRONT,GL_SHININESS,mat4_shininess); glutSolidCube(size); } void torus(float innerRadius, float outerRadius, GLint nsides, GLint rings){ glMaterialfv(GL_FRONT,GL_AMBIENT,mat4_amb); glMaterialfv(GL_FRONT,GL_DIFFUSE,mat4_dif); glMaterialfv(GL_FRONT,GL_SPECULAR,mat4_spec); glMaterialf(GL_FRONT,GL_SHININESS,mat4_shininess); glPushMatrix();// запис ΠΏΠΎΡ‚ΠΎΡ‡Π½ΠΎΡ— ΠΌΠ°Ρ‚Ρ€ΠΈΡ†Ρ– Π² стСк glutSolidTorus (innerRadius, outerRadius, 40, 40); glPopMatrix(); } void lines(double x, double y, double z){ glColor3f(1.0, 1.0, 1.0); glBegin(GL_LINES); for(int i=x-winWid/2; i<winWid/2;i+=10){ glVertex2f(i, -winHeig/2); glVertex2f(i, winHeig/2); } glEnd(); glBegin(GL_LINES); for(int i=y-winHeig/2; i<winHeig/2;i+=10){ glVertex2f(-winWid/2, i); glVertex2f(winWid/2, i); } glEnd(); } void on_keyboard(unsigned char key, int x, int y){ switch(key){ case 'r': angleX=90; angleY=0; scale=1; break; case 'a' : if(angleX==360){ angleX=0; } angleX++; break; case 'd' : angleX--; break; case 'w' : if(angleY==360){ angleY=0; } angleY++; break; case 's' : angleY--; break; case 'q' : if(scale<5) scale+=0.1; break; case 'e' : if(scale>1) scale-=0.1; break; } } void on_specKeyboard(int key, int x, int y){ switch(key){ case GLUT_KEY_F1: angleX=90; angleY=0; scale=1; break; case GLUT_KEY_LEFT : angleX++; break; case GLUT_KEY_RIGHT : angleX--; break; case GLUT_KEY_UP : angleY++; break; case GLUT_KEY_DOWN : angleY--; break; } } void on_paint(){ glClear(GL_COLOR_BUFFER_BIT); glClearColor(0.3, 0.5, 0.5, 0); glPushMatrix(); glRotatef(angleX, 0.0, 0.0, 1.0); glRotatef(angleY, 0.0, 1.0, 0.0); glScalef(scale, scale,1.0); glPushMatrix(); glNormal3f(0.0, 0.0, 1.0); glRotatef(90,0,1,0); glTranslatef(0,0,-150); glBegin(GL_QUADS); glTexCoord2d(0.0,0.0); glVertex2d(-200.0, -200.0); glTexCoord2d(1.0,0.0); glVertex2d(200.0, -200.0); glTexCoord2d(1.0,1.0); glVertex2d(200.0, 200.0); glTexCoord2d(0.0,1.0); glVertex2d(-200.0, 200.0); glEnd(); glPopMatrix(); glPushMatrix(); glTranslatef(-120, -75, -80); glRotatef(90,1,0,0); torus(15,10,20,20); glPopMatrix(); glPushMatrix(); glTranslatef(-120,-75,80); glRotatef(90,1,0,0); torus(15,10,20,20); glPopMatrix(); glPushMatrix(); glTranslatef(-120,75,-80); glRotatef(90,1,0,0); torus(15,10,20,20); glPopMatrix(); glPushMatrix(); glTranslatef(-120, 75,80); glRotatef(90,1,0,0); torus(15,10,20,20); glPopMatrix(); glPopMatrix(); glutSwapBuffers(); } void initialize(){ glEnable( GL_TEXTURE_2D ); glBindTexture( GL_TEXTURE_2D, texture ); glClearColor(0.5, 0.5, 0.8, 1); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho( -winWid/2, winWid/2, -winHeig/2, winHeig/2, -200, 200); } void on_mousePressed(int _button, int state, int x, int y){ if (state == 1) { // 0 - натиснули Π½Π° ΠΊΠ½ΠΎΠΏΠΊΡƒ, 1 - відпустили ΠΊΠ½ΠΎΠΏΠΊΡƒ button = -1; // ніяка ΠΊΠ½ΠΎΠΏΠΊΠ° Π½Π΅ натиснута return; } switch(button = _button){ case GLUT_MIDDLE_BUTTON: angleX=90; angleY=0; scale=1; break; case GLUT_LEFT_BUTTON: if(scale<5) scale+=0.1; break; case GLUT_RIGHT_BUTTON: if(scale>1) scale-=0.1; break; } } void on_mousePressedMove(int x, int y){ switch (button) { case 0: angleX += x - mouseX; angleY += y - mouseY; mouseX = x; mouseY = y; break; } } void on_timer(int value) { on_paint(); // ΠΏΠ΅Ρ€Π΅ΠΌΠ°Π»ΡŽΡ”ΠΌΠΎ Π΅ΠΊΡ€Π°Π½ glutTimerFunc(75, on_timer, 0); // Ρ‡Π΅Ρ€Π΅Π· 33мс Π²ΠΈΠΊΠ»ΠΈΡ‡Π΅Ρ‚ΡŒΡΡ ця функція } float ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f }; float diffuse[] = {0.3,0.9,0.67,1.0}; float specular[] = {0.45,0.50,1.0,1.0}; float position[] = {50.0,50.0,50.0,0.0}; int main(int argc, char** argv){ //Π˜Π½ΠΈΡ†ΠΈΠ°Π»ΠΈΠ·Π°Ρ†ΠΈΡ glutInit(&argc,argv); glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB); glutInitWindowSize(winWid, winHeig); glutCreateWindow("Test 1"); //РСгистрация initialize(); glutDisplayFunc(on_paint); glutKeyboardFunc(on_keyboard); glutSpecialFunc(on_specKeyboard); glutMotionFunc(on_mousePressedMove); glutMouseFunc(on_mousePressed); glutTimerFunc(75, on_timer, 0); glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glLightfv(GL_LIGHT0, GL_AMBIENT, ambient); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse); glLightfv(GL_LIGHT0, GL_POSITION, position); glEnable(GL_COLOR_MATERIAL); glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE); glutMainLoop(); return 0; } 

Closed due to the fact that the essence of the question is not clear to the participants of Kromster , Denis Bubnov , user194374, αλΡχολυτ , Ρ„ 18 Feb '17 at 19:10 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • Lighting is fine. There is a source of light, it illuminates. - Vanyamba Electronics
  • 3
    Show, than "everything is normal" differs from "not that that is necessary"? - Kromster
  • The functions glMaterialfv expect a vector of 4 float, and you transfer only 3. Maybe you have garbage in transparency? - Unick

0