I have two 3D objects, and I need to set material only on one of them using glMaterial function. How can I accomplish this?
#include <GLUT/glut.h>
#include <cmath>
const int width = 1200, height = 600;
float xValue = 0.0;
float position = 0.0;
float angle = 0.0;
float shininess[] = { 1.0 };
float color[] = { 0.2, 0.2, 0.8 };
void init(void);
void display(void);
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutInitWindowPosition(85, 75);
glutCreateWindow("3D объекты - освещение");
init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
void init(void) {
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_LIGHTING);
float lightAmbient[] = { 0.5, 0.5, 0.5, 0.0 };
float lightDiffuse[] = { 0.5, 0.5, 0.5, 0.0 };
float lightSpecular[] = { 0.5, 0.5, 0.5, 0.0 };
float lightPosition[] = { -10.0, 5.0, 0.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, lightSpecular);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glOrtho(-10.0, 10.0, -5.0, 5.0, -5.0, 5.0);
}
void display(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0.0, 0.0, width, height);
xValue += 0.01;
position = 5.0*cos(xValue);
angle = 270.0*sin(xValue);
glLineWidth(3);
glPushMatrix();
glTranslatef(position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glMaterialfv(GL_FRONT, GL_AMBIENT, color);
glMaterialfv(GL_FRONT, GL_DIFFUSE, color);
glMaterialfv(GL_FRONT, GL_SPECULAR, color);
glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
glutWireTorus(1, 2, 35, 35);
glPopMatrix();
glPushMatrix();
glTranslatef(-position, 0.0, 0.0);
glRotatef(angle, 0, 1, 0);
glutSolidTorus(0.5, 3.5, 35, 35);
glPopMatrix();
glutSwapBuffers();
glutPostRedisplay();
}
OpenGL is a state engine. Each state is kept until it is changed again, even beyond frames. For instance you can enable and disable lighting before drawing an object:
glEnable(GL_LIGHTING);
// darw object 1
glDisable(GL_LIGHTING);
// draw object 2
ALike the material settings can be set for each object individually, before drawing the object:
glMaterialfv(...);
// darw object 1
glMaterialfv(...);
// draw object 2
Related
I'm trying to move the perspective view, so I can see that the sun, earth, and moon is moving around on a screen.
I'm keep searching and trying to fix this but there's nothing on a screen.
I really need help to fix this...
#include <gl/glew.h>
#include <gl/freeglut.h>
#include <GL/GL.h>
#include <GL/GLU.h>
#include <GL/glut.h>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
using namespace glm;
static int Day = 0, Time = 0;
int windowWidth, windowHeight;
vec3 eye(0, 2.0, 4.0);
vec3 at(0.2, 0, 0);
vec3 up(normalize(cross(vec3(1, 0, 0), at - eye)));
void InitLight() {
// sun_light
GLfloat sun_light_amb[] = { 0.5, 0, 0, 1.0 };
GLfloat sun_light_diffuse[] = { 1, 0.5, 0.5, 1.0 };
GLfloat sun_light_specular[] = { 1, 1, 1, 1.0 };
// moon_light
GLfloat moon_light_amb[] = { 0.5, 0.5, 0, 1.0 };
GLfloat moon_light_diffuse[] = { 1, 1, 0.5, 1.0 };
GLfloat moon_light_specular[] = { 1, 1, 1, 1.0 };
glShadeModel(GL_SMOOTH); // using GL_SMOOTH
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
// sun - GL_LIGHT0
glLightfv(GL_LIGHT0, GL_AMBIENT, sun_light_amb);
glLightfv(GL_LIGHT0, GL_DIFFUSE, sun_light_diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, sun_light_specular);
// moon - GL_LIGHT1
glLightfv(GL_LIGHT1, GL_AMBIENT, moon_light_amb);
glLightfv(GL_LIGHT1, GL_DIFFUSE, moon_light_diffuse);
glLightfv(GL_LIGHT1, GL_SPECULAR, moon_light_specular);
}
void MyDisplay() {
// material
// sun
GLfloat sun_mat_amb[] = { 0.2, 0 , 0, 1.0 };
GLfloat sun_mat_diffuse[] = { 1, 0.5, 0.5, 1.0 };
GLfloat sun_mat_specular[] = { 0, 0, 0, 1 };
GLfloat sun_mat_emission[] = { 0.3, 0.1, 0.1, 0.0 };
// sun
GLfloat moon_mat_amb[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat moon_mat_diff[] = { 0.5, 0.5, 0.1, 1.0 };
GLfloat moon_mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat moon_shininess[] = { 100.0 };
GLfloat moon_mat_emission[] = { 0.3, 0.3, 0.1, 0.0 };
// earth
GLfloat earth_mat_amb[] = { 0.1, 0.1, 0.1, 1.0 };
GLfloat earth_mat_diff[] = { 0.1, 0.1, 0.8, 1.0 };
GLfloat earth_mat_specular[] = { 0.5, 0.5, 1.0, 1.0 };
GLfloat earth_shininess[] = { 80.0 };
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(20.f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.f, 20.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(eye[0], eye[1], eye[2], at[0], at[1], at[2], up[0], up[1], up[2]);
glEnable(GL_CULL_FACE);
glFrontFace(GL_CW);
glCullFace(GL_FRONT);
glColor3f(1.0, 0.3, 0.3); // sun
glMaterialfv(GL_FRONT, GL_AMBIENT, sun_mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, sun_mat_diffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, sun_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, sun_mat_emission);
glutSolidSphere(0.2, 20, 16);
glEnable(GL_LIGHT0); // sun - LIGHT0
glPushMatrix();
glRotatef((GLfloat)Day, 0.0, 1.0, 0.0); // earth
glTranslatef(0.7, 0.0, 0.0);
glRotatef((GLfloat)Time, 0.0, 1.0, 0.0);
glColor3f(0.5, 0.6, 0.7);
glMaterialfv(GL_FRONT, GL_AMBIENT, earth_mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, earth_mat_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, earth_mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, earth_shininess);
glutSolidSphere(0.1, 10, 8);
glPushMatrix();
glRotatef((GLfloat)Time, 0.0, 1.0, 0.0); // moon
glTranslatef(0.2, 0.0, 0.0);
glColor3f(0.9, 0.8, 0.2);
glMaterialfv(GL_FRONT, GL_AMBIENT, moon_mat_amb);
glMaterialfv(GL_FRONT, GL_DIFFUSE, moon_mat_diff);
glMaterialfv(GL_FRONT, GL_SPECULAR, moon_mat_specular);
glMaterialfv(GL_FRONT, GL_EMISSION, moon_mat_emission);
glutSolidSphere(0.04, 10, 8);
glEnable(GL_LIGHT1); // moon - LIGHT1
glPopMatrix();
glPopMatrix();
glutSwapBuffers();
}
void MyReshape(int w, int h) {
windowWidth = w;
windowHeight = h;
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
}
void MyTimer(int Value) {
Day = (Day + 2) % 360;
Time = (Time + 1) % 360;
mat3 rot = rotate(mat4(1), radians(1.f), vec3(0, 0, 1)); // rotate 1 degree
eye = rot * eye;
up = rot * up;
glutPostRedisplay();
glutTimerFunc(40, MyTimer, 1);
}
int main(int argc, char** argv) {
//Create window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("Test");
//get window size
windowWidth = glutGet(GLUT_WINDOW_WIDTH);
windowHeight = glutGet(GLUT_WINDOW_HEIGHT);
InitLight();
glutDisplayFunc(MyDisplay);
glutReshapeFunc(MyReshape);
glutTimerFunc(40, MyTimer, 1);
glutMainLoop();
return 0;
}
moving sun, earth, moon
using GL_SMOOTH
moving perspective when MyTimer callback (rotate eye and up => 1 degree)
The Perspective Projection matrix defines a Viewing Frustum. The near plane cannot be 0. The near plan must be grater than o and the far plan must be grater than the near plane.
0 < near < far
Change the near plane. e.g:
gluPerspective(20.f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.f, 20.f);
gluPerspective(20.f, (GLfloat)windowWidth / (GLfloat)windowHeight, 0.1f, 20.f);
my desired outcome(in red)
Hi, I'm trying to make my code in opengl c++ to produce this(with light coming from (1,1,1)), and it doesn't work and produces this. Can anyone teach me why?
I thought it was about matrices calculation or lighting usage at first, but not so sure anymore.
light function
void init_light(void) {
glLoadIdentity();
glGetFloatv(GL_PROJECTION_MATRIX, ProjectionMat);
ProjectionMatrix = glm::make_mat4(ProjectionMat);
glGetFloatv(GL_MODELVIEW_MATRIX, ViewMat);
ViewMatrix = glm::make_mat4(ViewMat);
glm::mat4 InverseProjectionMatrix = glm::inverse(ProjectionMatrix);
glm::mat4 InverseViewMatrix = glm::inverse(ViewMatrix);
glm::mat4 M = glm::inverse(ProjectionMatrix * ViewMatrix);
glm::mat4 Matr = ProjectionMatrix * ViewMatrix;
glClearColor(1.0, 1.0, 1.0, 0.0);
//glClearDepth(0.0);
GLfloat ambient[] = { 0.2, 0.2, 0.2, 1.0 };
GLfloat diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular[] = { 1.0, 1.0, 1.0, 1.0 };
//GLfloat position0[] = { 1., 1., 1., 0.0 };
//GLfloat position1[] = { 1., 1., 1., 1.0 };
GLfloat pos[] = { (GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[0],
(GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[1],
(GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[2],
(GLfloat)(InverseViewMatrix * glm::make_vec4(position0))[3] };
GLfloat spot_direction[] = { 1.0, 1.0, 1.0 };
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spot_direction);
glLightf(GL_LIGHT1, GL_SPOT_CUTOFF, 1.0);
glLightf(GL_LIGHT1, GL_SPOT_EXPONENT, 80.);
//glLightfv(GL_LIGHT0, GL_POSITION, position0);
//glLightfv(GL_LIGHT0, GL_POSITION, pos);
//glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, 2.0);
//glLightfv(GL_LIGHT1, GL_AMBIENT, ambient);
//glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuse);
//glLightfv(GL_LIGHT1, GL_POSITION, position1);
GLfloat mat_ambient[] = { 0.2, 0.2, 0.8, 0.0 };
GLfloat mat_diffuse[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat mat_specular[] = { 0.5, 0.5, 0.5, 0.0 };
//GLfloat mat_emissive[] = { 0.0, 0.0, 0.0, 0.0 };
GLfloat mat_shininess[] = { 120.0 };
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, mat_shininess);
//glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emissive);
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
glColorMaterial(GL_FRONT, GL_SHININESS);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glEnable(GL_NORMALIZE);
glEnable(GL_AUTO_NORMAL);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}
rendering scene
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
double ratio = window_size[0] * 1.0 / window_size[1];
if (rotationbool == 1) {
xRot = temp_xRot;
yRot = temp_yRot;
}
glPopMatrix();
gluPerspective(fovy, ratio, zNear + (zoom_factor)* ratio, zFar + (zoom_factor)* ratio);
glLightfv(GL_LIGHT0, GL_POSITION, position0);
glLightfv(GL_LIGHT1, GL_POSITION, position1);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glPopMatrix();
glRotatef(xRot, 1.0, .0, .0);
glRotatef(yRot, .0, 1.0, .0);
glTranslatef(xTrans, yTrans, zTrans);
glPushMatrix(); ...
}
main function
int main(int argc, char* argv[]) {
bool cad_draw = true;
if (cad_draw) {
glutInit(&argc, argv);
}
double L_base[3] = { 0.1, 0.1, 0.1 };
if (cad_draw) {
finemost_limit[0] = L_base[0];
finemost_limit[1] = L_base[1];
finemost_limit[2] = L_base[2];
window_params_init();
//glDepthRange(1.0, 0.);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(window_size[0], window_size[1]);
glutCreateWindow("Interactive boundary design");
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE, GLUT_ACTION_CONTINUE_EXECUTION);
//init_sfc();
//init_light();
// register callbacks
glDepthFunc(GL_LEQUAL);
//glDepthFunc(GL_GEQUAL);
glDepthRange(1.0, 0.); // left handed에서 right hanaded로 변경
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
init_light();
///glutMouseWheelFunc(render_wheel);
glutMouseFunc(mousePress);
glutMotionFunc(mouseDrag);
glutPassiveMotionFunc(passive_mouse);
glutKeyboardFunc(simple_keyboard_binding);
// enter GLUT event processing cycle
glutMainLoop();
}
return 0;
}
I want to see the movement of the Sphere on the screen.
but, If you run the code, you will not be able to update the screen.
How do I change the code to show a change in the location of the phrase I created?
void display() {
glLoadIdentity();
gluLookAt(0, 0, 10, 0, 0, 0, 0, 1, 0);
GLfloat diffuse1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular1[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat position1[] = { 0, 0, 0, 1.0 };
GLfloat diffuse2[] = { 0.5, 0.5, 0.5, 1 };
GLfloat ambient2[] = { 0.1, 0.1, 0.1, 1 };
GLfloat specular2[] = { 0.5, 0.5, 0.5, 1 };
GLfloat emission2[] = { 0, 0, 0, 1 };
GLfloat shine = 300;
glLightfv(GL_LIGHT0, GL_POSITION, position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient1);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse1);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular1);
glPushMatrix();
glMaterialfv(GL_FRONT, GL_AMBIENT, ambient2);
glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse2);
glMaterialfv(GL_FRONT, GL_SPECULAR, specular2);
glMaterialfv(GL_FRONT, GL_EMISSION, emission2);
glMaterialf(GL_FRONT, GL_SHININESS, shine);
glRotatef(mer_revolution, 0, 0, 1);
glTranslatef(0, 0.8, 0);
glRotatef(mer_rotate, 0, 0, 1);
glutSolidSphere(1, 300, 300);
glPopMatrix();
glFlush();
}
void main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,600);
glutCreateWindow("");
glClearColor(0, 0, 0, 0);
glColor3f(1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glMatrixMode(GL_MODELVIEW);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(move);
glutMainLoop();
}
Move 'gL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT' at the top of the display.
The screen must be initialized when performing a function. Only then will the screen continue to change.
I'm trying to illustrate orbits of Triton and Proteus around Neptune with a background of stars. I decided to sketch a template background by multisampling an array of white dots ( stars ) as the background. Could someone explain why my array isn't displaying?
*Background is called in Display() created in Init().
Full Code here:
int triton = 0;
int proteus = 0;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
// Material Specs
GLfloat mat_specular[] = { 0.8, 0.8, 0.9, 0.1 };
GLfloat mat_shininess[] = { 40.0 };
GLfloat lightDiffuse[] = { 1.0, 1.0, 1.0, 0.8 };
GLfloat lmodel_ambient[] = { 0.1, 0.2, 0.7, 0.0 };
// Light 0 Initialized.
GLfloat light0[] = { 1.0, 1.0, 1.0, 0.9 };
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
// Mat Specs Implmentations.
glMaterialfv(GL_FRONT, GL_DIFFUSE, lightDiffuse);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
// Light 0 implementation
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light0);
glLightfv(GL_LIGHT0, GL_SPECULAR, light0);
// Ambient surrounding light on object.
//glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
// Enable Lighting and Depth
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
// Background (Stars: In Progress)
glNewList(1, GL_COMPILE);
glBegin(GL_POINTS);
glColor3f(1.0, 1.0, 1.0);
for (int i = 0; i < 1000; i++)
{
for (int j = 0; j < 1000; j++)
{
if (((i + j) % 2) == 0)
{
glVertex2f(2*i, 2*j);
}
}
}
glEnd();
glEndList();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glEnable(GL_COLOR_MATERIAL);
// Neptune
glColor3f(0.1, 0.1, 0.3);
glutSolidSphere(2.0, 100, 100);
// Triton
glPushMatrix();
glColor3f(0.9, 0.7, 0.8);
glRotatef((GLfloat)triton, 1.0, 1.0, 1.0);
glTranslatef(2.5, 0.0, 0.0);
glRotatef((GLfloat)triton, 1.0, 0.0, 0.0);
glutSolidSphere(0.35, 100, 100);
glPopMatrix();
// Proteus
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glRotatef((GLfloat)proteus, 0.7, -0.4, 1.0);
glTranslatef(3.5, 0.0, 0.0);
glRotatef((GLfloat)proteus, 1.0, 0.0, 0.0);
glutSolidSphere(0.1, 100, 100);
glPopMatrix();
// Stars Background
glEnable(GL_MULTISAMPLE);
glPushMatrix();
glCallList(1); // Not Coding
glPopMatrix();
glDisable(GL_COLOR_MATERIAL);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0, (GLfloat)w / (GLfloat)h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
// Triton + Proteus Orbit.
case 'a':
triton = (triton - 2) % 360;
proteus = (proteus - 5) % 360;
glutPostRedisplay();
break;
case 'd':
triton = (triton + 2) % 360;
proteus = (proteus + 5) % 360;
glutPostRedisplay();
break;
case ',':
glTranslatef(- 0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case '.':
glTranslatef(0.3, 0.0, 0.0);
glutPostRedisplay();
break;
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH | GLUT_RGB | GLUT_MULTISAMPLE);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(100, 100);
glutCreateWindow("Neptune and Space");
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
You you draw the start, then the perspective projection matrix and the view matrix are stills set. The "stars" are not on the viewport.
Use a scale of about 1/500 to put the points into clip space:
glVertex2f(2*i / 500.0f, 2*j / 500.0f);
Or setup an orthographic projection, before you draw the stars:
// Stars Background
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 1000, 0, 1000 );
glCallList(1);
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
glPopMatrix();
I'm trying to draw a sphere using gluSphere. The sphere appears but certain patches are missing whether I draw it as a filled polygons or a wire frame.
int width=800, height=600;
void myinit(void)
{
// set up overall light data, including specular=ambient=light colors
GLfloat light_position[]={ 10.0, 10.0, -10.0, 1.0 };
GLfloat light_color[]={ 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient_color[]={ 0.0, 0.2, 0.2, 1.0 };
GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };
glClearColor( 0.0, 1.0, 1.0, 0.0 );
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
glLightfv(GL_LIGHT0, GL_POSITION, light_position );
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color );
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color );
/* attributes */
glEnable(GL_LIGHTING); // so lighting models are used
glEnable(GL_LIGHT0); // we'll use LIGHT0
glEnable(GL_DEPTH_TEST); // allow z-buffer display
//glClearDepth(1);
}
void drawHead(){
GLUquadric *sphere=gluNewQuadric();
gluQuadricDrawStyle( sphere, GLU_FILL);
gluQuadricNormals( sphere, GLU_SMOOTH);
gluQuadricOrientation( sphere, GLU_OUTSIDE);
gluQuadricTexture( sphere, GL_TRUE);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glPushMatrix();
glTranslated(0,0,-5);
glRotated(30,1,1,1);
gluSphere(sphere,2.0,15,15);
glPopMatrix();
}
void display(){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
drawHead();
glutSwapBuffers();
}
void main(int argc, char** argv){
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width,height);
glutInitWindowPosition(100,200);
glutCreateWindow("Automaton");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.0,1.0,0.0,20);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
myinit();
glutDisplayFunc(display);
glutMainLoop();
}
As per the docs, "...zNear must never be set to 0."
Bump your zNear out a bit, and your zFar a bit more:
gluPerspective( 90.0, 1.0, 1.0, 200.0 );
Complete:
#include <GL/glut.h>
int width=800, height=600;
void myinit(void)
{
// set up overall light data, including specular=ambient=light colors
GLfloat light_position[]={ 10.0, 10.0, -10.0, 1.0 };
GLfloat light_color[]={ 1.0, 1.0, 1.0, 1.0 };
GLfloat ambient_color[]={ 0.0, 0.2, 0.2, 1.0 };
GLfloat mat_specular[]={ 1.0, 1.0, 1.0, 1.0 };
glClearColor( 0.0, 1.0, 1.0, 0.0 );
glShadeModel(GL_SMOOTH);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular );
glLightfv(GL_LIGHT0, GL_POSITION, light_position );
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color );
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color );
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color );
/* attributes */
glEnable(GL_LIGHTING); // so lighting models are used
glEnable(GL_LIGHT0); // we'll use LIGHT0
glEnable(GL_DEPTH_TEST); // allow z-buffer display
}
void drawHead()
{
GLUquadric *sphere=gluNewQuadric();
gluQuadricDrawStyle( sphere, GLU_FILL);
gluQuadricNormals( sphere, GLU_SMOOTH);
gluQuadricOrientation( sphere, GLU_OUTSIDE);
gluQuadricTexture( sphere, GL_TRUE);
glPushMatrix();
glTranslated(0,0,-5);
glRotated(30,1,1,1);
gluSphere(sphere,2.0,15,15);
glPopMatrix();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1,1,0);
drawHead();
glutSwapBuffers();
}
void main(int argc, char** argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(width,height);
glutInitWindowPosition(100,200);
glutCreateWindow("Automaton");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective( 90.0, 1.0, 1.0, 200.0 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
myinit();
glutDisplayFunc(display);
glutMainLoop();
}