I am trying to draw some random points as the star in the window,
but points are not showing. But others objects are showing correctly.
My source code:
#include<windows.h>
#include <GL\glut.h>
#include <math.h> // For math routines (such as sqrt & trig).
GLfloat xRotated, yRotated, zRotated;
GLdouble radius=3;
GLfloat qaBlack[] = {0.0, 0.0, 0.0, 1.0}; //Black Color
GLfloat qaGreen[] = {0.0, 1.0, 0.0, 1.0}; //Green Color
GLfloat qaWhite[] = {1.0, 1.0, 1.0, 1.0}; //White Color
GLfloat qaRed[] = {1.0, 0.0, 0.0, 1.0}; //Red Color
// Set lighting intensity and color
GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0};
GLfloat emitLight[] = {0.9, 0.9, 0.9, 0.9};
GLfloat Noemit[] = {0.0, 0.0, 0.0, 1.0};
// Light source position
GLfloat qaLightPosition[] = {1, 1, 1, 1};
void display(void);
void reshape(int x, int y);
void idleFunc(void)
{
if ( zRotated > 360.0 ) {
zRotated -= 360.0*floor(zRotated/360.0); // Don't allow overflow
}
if ( yRotated > 360.0 ) {
yRotated -= 360.0*floor(yRotated/360.0); // Don't allow overflow
}
zRotated += 0.05;
yRotated +=0.01;
display();
}
void initLighting()
{
// Enable lighting
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_SPECULAR, qaSpecularLight);
}
void display(void){
glMatrixMode(GL_MODELVIEW);
// clear the drawing buffer.
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
// clear the identity matrix.
glLoadIdentity();
glTranslatef(0.0,0.0,-40.0);
glPushMatrix();
glutSolidSphere(radius,25,25);
glPopMatrix();
glPopMatrix();
glPushMatrix();
glRotatef(yRotated,0.0,2.0,0.0);
glTranslatef(5.0,0.0,0.0);
// Set the light position
glLightfv(GL_LIGHT0, GL_POSITION, qaLightPosition);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, emitLight); // Make sphere glow (emissive)
glutSolidSphere(radius/6,25,25);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, Noemit);
glPopMatrix();
glTranslatef(0.0,0.0,0.0);
glPushMatrix();
glColor3f(1.0, 1.0, 1.0);
glPointSize(3);
for(int i=1;i<100;i++){
int x = rand()%640 ;
int y = rand()%480;
glBegin(GL_POINTS);
glVertex2i (x,y);
glEnd();
}
glPopMatrix();
glLoadIdentity();
glColor3f(1.0, 1.0, 1.0);
glPointSize(3);
for(int i=1;i<100;i++){
int x = rand()%640 ;
int y = rand()%480;
glBegin(GL_POINTS);
glVertex2i (x,y);
glEnd();
}
glFlush(); //FOR RENDERING
glutSwapBuffers();
}
void reshape(int x, int y){
if(y == 0 || x == 0) return;
glMatrixMode(GL_PROJECTION);
gluPerspective(20.0,(GLdouble)x/(GLdouble)y,0.6,40.0);
glMatrixMode(GL_MODELVIEW);
glViewport(0,0,x,y); //Use the whole window for rendering
}
int main (int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize(1000,600);
glutCreateWindow("Project_KD");
initLighting();
xRotated = yRotated = zRotated = 0.0;
glutIdleFunc(idleFunc);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
Lots of wonkiness:
Couple instances of unmatched glPushMatrix()/glPopMatrix() calls; avoid over-/under-flowing the matrix stack; I like to use extra scopes to visually indicate matrix stack nesting.
Your point drawing was assuming an ortho projection while you only set a perspective one.
You left lighting enabled while trying to draw your points, resulting in very dark points everywhere except the bottom-left.
Your point drawing loop was duplicated for some reason; if you want double the stars adjust the for-loop end value instead of copy-pasting the loop.
You should use glutPostRedisplay() in your idle callback instead of calling display() directly.
Set your projection/modelview matrices each time through display() instead of setting them in a resize callback; helps reduce a source of mysterious matrix errors. The default resize callback calls glViewport() for you so you don't have to worry about doing that.
You're drawing the points ("stars"?) after the 3D spheres; I think the intent was to draw them before so they're "underneath".
Unholy mishmash of code formatting; recommend something like clang-format to keep that in check.
If you're using FreeGLUT on Windows (which you ought to be; it's really the only maintained GLUT implementation left) you don't need the #include <Windows.h>.
Recommend using a timer callback instead of an idle callback to update your simulation/animation. Without vsync that idle callback will be called incredibly often. With a timer callback you can simulate the even ~16 millisecond frames a vsync'd system will give you.
All together:
#include <GL/glut.h>
#include <cmath>
GLfloat xRotated, yRotated, zRotated;
GLdouble radius = 3;
void timer( int value )
{
if( zRotated > 360.0 )
{
zRotated -= 360.0 * floor( zRotated / 360.0 ); // Don't allow overflow
}
if( yRotated > 360.0 )
{
yRotated -= 360.0 * floor( yRotated / 360.0 ); // Don't allow overflow
}
zRotated += 5.0;
yRotated += 1.0;
glutTimerFunc( 16, timer, 0 );
glutPostRedisplay();
}
void display()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glDepthMask( GL_FALSE );
glDisable( GL_DEPTH_TEST );
glDisable( GL_LIGHTING );
// 2D rendering
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 640, 0, 480, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
{
glColor3f( 1.0, 1.0, 1.0 );
glPointSize( 3 );
glBegin( GL_POINTS );
for( int i = 1; i < 100; i++ )
{
int x = rand() % 640;
int y = rand() % 480;
glVertex2i( x, y );
}
glEnd();
}
glPopMatrix();
glDepthMask( GL_TRUE );
glEnable( GL_DEPTH_TEST );
// Enable lighting
glEnable( GL_LIGHTING );
glEnable( GL_LIGHT0 );
GLfloat qaSpecularLight[] = {1.0, 1.0, 1.0, 1.0};
glLightfv( GL_LIGHT0, GL_SPECULAR, qaSpecularLight );
// 3D rendering
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
gluPerspective( 20.0, w / h, 0.1, 80.0 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glTranslatef( 0.0, 0.0, -40.0 );
glPushMatrix();
{
glutSolidSphere( radius, 25, 25 );
}
glPopMatrix();
glPushMatrix();
{
glRotatef( yRotated, 0.0, 2.0, 0.0 );
glTranslatef( 5.0, 0.0, 0.0 );
GLfloat qaLightPosition[] = {1, 1, 1, 1};
glLightfv( GL_LIGHT0, GL_POSITION, qaLightPosition );
GLfloat emitLight[] = {0.9, 0.9, 0.9, 0.9};
glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, emitLight ); // Make sphere glow (emissive)
glutSolidSphere( radius / 6, 25, 25 );
GLfloat Noemit[] = {0.0, 0.0, 0.0, 1.0};
glMaterialfv( GL_FRONT_AND_BACK, GL_EMISSION, Noemit );
}
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize( 1000, 600 );
glutCreateWindow( "Project_KD" );
xRotated = yRotated = zRotated = 0.0;
glutDisplayFunc( display );
glutTimerFunc( 0, timer, 0 );
glutMainLoop();
return 0;
}
Related
#include <stdio.h> // this library is for standard input and output
#include "glut.h"// this library is for glut the OpenGL Utility Toolkit
#include <math.h>
float squareX = 162.0f;
float squareY = 0.0f;
float squareZ = 0.0f;
// background color
void drawBackground() {
float width = 400;
float height = 100;
// color up
glBegin(GL_POLYGON);
glColor3f(0.0, 0.0, 1.0);
glVertex2f(width, height);
glVertex2f(width, width);
glVertex2f(0, width);
glVertex2f(0, height);
glVertex2f(width, height);
glEnd();
// color down
glBegin(GL_POLYGON);
glColor3f(0.0, 1.0, 0.0);
glVertex2f(width, height);
glVertex2f(width, 0);
glVertex2f(0, 0);
glVertex2f(0, height);
glVertex2f(width, height);
glEnd();
}
void drawShape(void) {
float width = 58.0f;
float height = 40.0f;
glTranslatef(squareX, squareY, squareZ);
glBegin(GL_POLYGON);
glColor3f(1.0, 0.0, 0.0);
glVertex2f(0, 0);
glVertex2f(width, 0);
glVertex2f(width, height);
glVertex2f(0, height);
glVertex2f(0, 0);
glEnd();
}
// called when the window is resized
void handleResize(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f);
}
void drawScene() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawBackground();
glPushMatrix();
drawShape();
glPushMatrix();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
int state = 1;
// make the square go up
void update(int value) {
// 1 : move up
if (state == 1) {
squareY += 1.0f;
if (squareY > 400.0) {
state = 2;
squareX = 0.0f;
squareY = 180.0f;
}
}
// 2 : move right
else if (state == 2) {
squareX += 1.0f;
if (squareX > 400.0) {
state = 3;
squareX = 180.0f;
squareY = 400.0f;
}
}
// 3 : move down
else if (state == 3) {
squareY -= 1.0f;
if (squareY < 0.0) {
state = 0;
}
}
glutPostRedisplay();
glutTimerFunc(25, update, 0);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400);
glutCreateWindow("Moving Square");
glutDisplayFunc(drawScene);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);
glutMainLoop();
return(0);
}
The code above moves a square from bottom to top, left to right, and top to bottom. The background color is blue, but how do I change the background color after each collision detection? The color order I am trying to achieve after each collision detection is blue, red, and green. I've added a comment to where the background is.
A switch() based on state with appropriate glClearColor()/glColor3f() calls would work:
void drawScene()
{
switch( state )
{
case 0: glClearColor( 0.0, 0.0, 0.0, 1.0 ); break;
case 1: glClearColor( 0.0, 1.0, 0.0, 1.0 ); break;
case 2: glClearColor( 0.2, 0.0, 0.0, 1.0 ); break;
case 3: glClearColor( 0.0, 0.0, 1.0, 1.0 ); break;
}
glClear( GL_COLOR_BUFFER_BIT );
...
Demo:
All together:
#include <cstdio>
#include <GL/glut.h>
#include <cmath>
float squareX = 162.0f;
float squareY = 0.0f;
float squareZ = 0.0f;
void drawShape( void )
{
float width = 58.0f;
float height = 40.0f;
glTranslatef( squareX, squareY, squareZ );
glBegin( GL_POLYGON );
glColor3f( 1.0, 0.0, 0.0 );
glVertex2f( 0, 0 );
glVertex2f( width, 0 );
glVertex2f( width, height );
glVertex2f( 0, height );
glVertex2f( 0, 0 );
glEnd();
}
// called when the window is resized
void handleResize( int w, int h )
{
glViewport( 0, 0, w, h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0f, (float)w, 0.0f, (float)h, -1.0f, 1.0f );
}
int state = 1;
void drawScene()
{
switch( state )
{
case 0: glClearColor( 0.0, 0.0, 0.0, 1.0 ); break;
case 1: glClearColor( 0.0, 1.0, 0.0, 1.0 ); break;
case 2: glClearColor( 0.2, 0.0, 0.0, 1.0 ); break;
case 3: glClearColor( 0.0, 0.0, 1.0, 1.0 ); break;
}
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glPushMatrix();
drawShape();
glPushMatrix();
glFlush();
glutSwapBuffers();
glutPostRedisplay();
}
// make the square go up
void update( int value )
{
// 1 : move up
if( state == 1 )
{
squareY += 1.0f;
if( squareY > 400.0 )
{
state = 2;
squareX = 0.0f;
squareY = 180.0f;
}
}
// 2 : move right
else if( state == 2 )
{
squareX += 1.0f;
if( squareX > 400.0 )
{
state = 3;
squareX = 180.0f;
squareY = 400.0f;
}
}
// 3 : move down
else if( state == 3 )
{
squareY -= 1.0f;
if( squareY < 0.0 )
{
state = 0;
}
}
glutPostRedisplay();
glutTimerFunc( 25, update, 0 );
}
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize( 400, 400 );
glutCreateWindow( "Moving Square" );
glutDisplayFunc( drawScene );
glutReshapeFunc( handleResize );
glutTimerFunc( 25, update, 0 );
glutMainLoop();
return( 0 );
}
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 am trying to create a model using OpenGL, and I tried to enable depth testing.
I use these commands in my main:
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glEnable(GL_DEPTH_TEST);
And this in my display:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
I even tried adding:
glDepthFunc(GL_LEQUAL);
And it doesn't work. I can still see what I think are depth problems.
Here is a video showing the problem: https://www.youtube.com/watch?v=OafrRH4Mzjc
Note: In that video, the board is build right to left, top to bottom, so the first angle is OK, but any other angle is bad.
What am I missing?
Edit: Minimal example file of reproduction:
#define _CRT_SECURE_NO_WARNINGS
#define SIZE_MOVES 17
#include <stdio.h>
/* Include the GLUT library. This file (glut.h) contains gl.h and glu.h */
#include <GL\glew.h>
#include <GL\freeglut.h>
static int left_click = GLUT_UP;
static int right_click = GLUT_UP;
static int xold;
static int yold;
static float rotate_x = 146;
static float rotate_y = -26;
int width, height;
GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 };
GLfloat light_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_specular[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat light_position[] = { 0, 5, -10, 0 };
GLfloat mat_specular[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat mat_shininess[] = { 1 };
// colors
const GLfloat colors[2][4] = {
{ 1.0, 1.0, 1.0, 1.0 }, //white
{ 0.0, 0.0, 0.0, 1.0 } //black
};
// rgb
const GLfloat rgb[3][4] = {
{ 1.0, 0.0, 0.0, 1.0 },
{ 0.0, 1.0, 0.0, 1.0 },
{ 0.0, 0.0, 1.0, 1.0 }
};
void resetMaterial() {
GLfloat c[] = { 1, 1, 1, 1 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, c);
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
void drawSquare(int color) {
glPushMatrix(); {
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[color]);
glScalef(1, 0.5, 1);
glutSolidCube(1);
} glPopMatrix();
}
void drawBoard() {
for (int i = 0; i < 8; i++)
for (int j = 0; j < 8; j++) {
glPushMatrix(); {
glTranslatef(i + 0.5, 0, j + 0.5);
drawSquare((i + j) % 2);
} glPopMatrix();
}
}
void drawAxes() {
glBegin(GL_LINES); {
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[0]);
glVertex3f(-2, 0, 0); glVertex3f(5, 0, 0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[1]);
glVertex3f(0, -2, 0); glVertex3f(0, 5, 0);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, rgb[2]);
glVertex3f(0, 0, -2); glVertex3f(0, 0, 5);
} glEnd();
}
void letThereBeLight() {
/*Add ambient light*/
GLfloat ambientLight[] = { 0.2f, 0.2f, 0.2f, 1.0f };
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
/*Add positioned light*/
GLfloat lightColor1[] = { 0.2f, 0.2f, 0.1f, 1.0f };
GLfloat lightPosition1[] = { -8, 8, 5, 0.0f };
glLightfv(GL_LIGHT0, GL_SPECULAR, lightColor1);
glLightfv(GL_LIGHT0, GL_POSITION, lightPosition1);
/*Add directed light*/
GLfloat lightColor2[] = { 0.3, 0.3, 0.3, 1.0f };
GLfloat lightPosition2[] = { 8, 8, -5, 1.0f };
glLightfv(GL_LIGHT1, GL_AMBIENT, lightColor2);
glLightfv(GL_LIGHT1, GL_POSITION, lightPosition2);
}
void display(void) {
// Clear frame buffer and depth buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up viewing transformation, looking down -Z axis
glLoadIdentity();
gluLookAt(0, 5, -15, 0, 0, 3, 0, 1, 0);
letThereBeLight();
resetMaterial();
// Rotate view:
glPushMatrix(); {
glRotatef(rotate_y, 1, 0, 0);
glRotatef(rotate_x, 0, 1, 0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, colors[0]);
glPushMatrix(); {
glTranslatef(-4, 0, -4); // Move to center
drawBoard();
} glPopMatrix();
drawAxes(); // For debuging
} glPopMatrix();
/* End */
glFlush();
glutSwapBuffers();
}
void mouseFunc(int button, int state, int x, int y) {
if (GLUT_LEFT_BUTTON == button)
left_click = state;
xold = x;
yold = y;
}
void motionFunc(int x, int y) {
if (GLUT_DOWN == left_click) {
rotate_y = rotate_y + (y - yold) / 5.f;
rotate_x = rotate_x + (x - xold) / 5.f;
glutPostRedisplay();
}
xold = x;
yold = y;
}
void reshapeFunc(int new_width, int new_height) {
width = new_width;
height = new_height;
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(50, width / height, 1, 20);
glMatrixMode(GL_MODELVIEW);
glutPostRedisplay();
}
int main(int argc, char **argv) {
/* Creation of the window */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(900, 600);
glEnable(GL_DEPTH_TEST);
glutCreateWindow("Chess");
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
glEnable(GL_POLYGON_SMOOTH);
glEnable(GL_LINE_SMOOTH);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
glShadeModel(GL_SMOOTH);
glDisable(GL_COLOR_MATERIAL);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
/* Declaration of the callbacks */
glutDisplayFunc(&display);
glutReshapeFunc(&reshapeFunc);
glutMouseFunc(&mouseFunc);
glutMotionFunc(&motionFunc);
/* Loop */
glutMainLoop();
/* Never reached */
return 0;
}
gluPerspective(50, width / height, 0, 20);
^ wat
zNear needs to be greater than zero (emphasis mine):
Depth buffer precision is affected by the values specified for zNear
and zFar. The greater the ratio of zFar to zNear is, the less
effective the depth buffer will be at distinguishing between surfaces
that are near each other.
If r = zFar / zNear roughtly log2(r) bits of depth buffer
precision are lost. Because r approaches infinity as zNear approaches 0, zNear must never be set to 0.
EDIT: Given the newly-posted MCVE:
int main(int argc, char **argv) {
/* Creation of the window */
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(900, 600);
glEnable(GL_DEPTH_TEST); // too soon
glutCreateWindow("Chess");
...
}
It's just like datenwolf said: You're glEnable()ing before you have a current GL context (glutCreateWindow() creates the context and makes it current).
Don't call any gl*() functions until after glutCreateWindow().
public void onSurfaceChanged(int w, int h)
{
Matrix.frustumM(projectionMatrix,0,-(9f/18.5f),(9f/18.5f),-1f,1f,1.0f,1000f);
}
I had to change my near clipping form 0.01f to 1.0f.
I've a problem with my clock. In the first 10 minutes the hour hand doesn't show the correct hour and it changes when the minute hand's change but after 10 minutes it shows correctly. Could you please help me to fix it?
I think there is a problem in these lines but I am not sure:
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
Here is my whole code:
#include <GL/glut.h>
#include <gl/gl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
GLUquadricObj *Cylinder;
struct tm *newtime;
time_t l_time;
int M_TWOPI=0;
GLfloat rx, ry, rz, angle;
GLfloat LightAmbient[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f };
GLfloat LightPosition[]= { 5.0f, 25.0f, 15.0f, 1.0f };
GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
static int view_state = 1; // Ortho view = 1, Perspective = 0
void newLine(float Start, float End, float angle){
float c = cos(angle), s = sin(angle);
glVertex2f( -8.0f*Start*c, -8.0f*Start*s);
glVertex2f( -8.0f*End*c, -8.0f*End*s);
}
void Sprint( float x, float y, char *st)
{
int l,i;
l=strlen( st );
glRasterPos3f( x, y, -1);
for( i=0; i < l; i++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]);
}
}
static void TimeEvent(int te)
{
rx = 30 * cos( angle );
ry = 30 * sin( angle );
rz = 30 * cos( angle );
angle += 0.01;
if (angle > M_TWOPI) angle = 0;
glutPostRedisplay();
glutTimerFunc( 100, TimeEvent, 1);
}
void Draw_clock( GLfloat cx, GLfloat cy, GLfloat cz )
{
int hour_ticks , sec_ticks;
glPushMatrix();
glTranslatef(cx,cy,cz);
glRotatef(180, 1.0, 0.0, 0.0);
glPushMatrix();
glColor3f(1.0, 0.5, 0.0);
glTranslatef( 0, 0, 0.0);
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 2.0);
glPopMatrix();
glRotatef(90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.75, 0, 4, 16, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 1.0);
glTranslatef( 0, 0, 0.0);
glRotatef( (360/60) * newtime->tm_min, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 3.0);
glScalef(0.5, 0.5, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
glutSolidCone (0.5, 6, 6, 16);
glPopMatrix();
glPushMatrix();
glColor3f(1.0, 0.0, 0.0);
glTranslatef( 0, 0, -0.0);
glRotatef( (360/60) * newtime->tm_sec, 0.0, 0.0, 1.0);
glPushMatrix();
glTranslatef(0.0, 0.0, 4.0);
glScalef(0.25, 0.25, 1.0);
glPopMatrix();
glRotatef( 90, 1.0, 0.0, 0.0);
gluCylinder(Cylinder, 0.25, 0, 6, 16, 16);
glPopMatrix();
for(hour_ticks = 0; hour_ticks < 12; hour_ticks++)
{
glPushMatrix();// Draw next arm axis.
glColor3f(0.0, 1.0, 1.0); // give it a color
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/12) * hour_ticks, 0.0, 0.0, 1.0);
glTranslatef( 6.0, 0.0, 0.0);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINES);
newLine(0.08f, 0.2f, 0.0f);
glEnd();
glPopMatrix();
}
for(sec_ticks = 0; sec_ticks < 60; sec_ticks++)
{
glPushMatrix();
glTranslatef(0.0, 0.0, 0.0);
glRotatef( (360/60) * sec_ticks, 0.0, 0.0, 1.0);
glTranslatef(6.0, 0.0, 0.0);
glutSolidCone(1.0, 2.0, 3, 4);
glPopMatrix();
}
glPopMatrix();
}
void num()
{
glColor3f( 0.0, 0.0, 1.0);
Sprint(-6.2,-0.2,"9"); //counting from center
Sprint(-0.2,-6.2,"6");
Sprint(2.8,-5.5,"5");
Sprint(5.0,-3.2,"4");
Sprint(5.0,+2.8,"2");
Sprint(2.8,+5.0,"1");
Sprint(-3.33,+4.95,"11");
Sprint(-0.2,-6.2,"6");
Sprint(-3.2,-5.45,"7");
Sprint(-0.4,5.7,"12");
Sprint(-5.35,-3.25,"8");
Sprint(-5.55,+2.8,"10");
Sprint(5.8,-0.2,"3");
}
void display_clock()
{
time(&l_time); // Get time
newtime = localtime(&l_time); // Convert to local time
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// easy way to put text on the screen.
glMatrixMode (GL_PROJECTION);
glLoadIdentity();
glOrtho(-8.0, 8.0, -8.0, 8.0, 1.0, 60.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
glDisable(GL_COLOR_MATERIAL);
glColor3f( 1.0, 1.0, 1.0);
Sprint(-2, 3, " Clock");
Draw_clock( 0.0, 0.0, -14.0);
num();
glutSwapBuffers();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
display_clock();
glFlush();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (50, 50);
glutCreateWindow (argv[0]);
glutSetWindowTitle("Clock");
Cylinder = gluNewQuadric();
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutTimerFunc( 10, TimeEvent, 1);
glutMainLoop();
return 0;
}
You are doing integer arithmetic:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
should be:
glRotatef(30.0 * newtime->tm_hour + 6.0) * (60.0 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
and so on.
Though, as 360/12 and 360/60 are constants you might as well replace them with the calculated values.
I believe your problem is here:
glRotatef((360/12) * newtime->tm_hour + (360/60) * (60 / (newtime->tm_min+1)), 0.0, 0.0, 1.0);
It must be:
glRotatef((360/12) * newtime->tm_hour + (360.0/12 / 60 ) * (newtime->tm_min+1), 0.0, 0.0, 1.0);
I have knew that there is some problem in this code as if I run it nothing is drawn .
portion of program where wrong is located in this code
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glLoadIdentity ();
glPushMatrix();
glRotated((GLdouble) Vangle,1.0,0.0,0.0);
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPopMatrix();
glutWireCube (1.0);
glFlush ();
}
can you tell what is wrong ?
I think in matrix stack methods (push and pop) , I do not know how do I use it
Note : value of Vangle is zero .
As pst pointed out: Move the glutWireCube() call inside the glPush/PopMatrix() pair.
Give this a shot:
#include <GL/glut.h>
void display()
{
glEnable( GL_DEPTH_TEST );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// projection
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
double w = glutGet( GLUT_WINDOW_WIDTH );
double h = glutGet( GLUT_WINDOW_HEIGHT );
double ar = w / h;
gluPerspective( 60, ar, 0.1, 100 );
// "camera" transform
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
// draw cube
glPushMatrix();
float Vangle = 0.0f;
glRotated((GLdouble) Vangle,1.0,0.0,0.0);
glutWireCube (1.0);
glPopMatrix();
glutSwapBuffers();
}
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
glutInitWindowSize( 640, 480 );
glutCreateWindow( "GLUT" );
glutDisplayFunc( display );
glutMainLoop();
return 0;
}