I'm trying to implement a pure stationary spotlight into my simple OpenGL scene, which doesn't move with the camera. It should keep highlighting the center of the scene (coords of 0,0,0), where I put the teapot.
Here is a simplified code:
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <math.h>
#include <iostream>
const int windowWidth = 640;
const int windowHeight = 480;
float alpha=0.0, alphaDelta=0.0, ratio, beta=0.0, betaDelta=0.0;
float x=0.0, y=1.75, z=5.0;
float lx=0.0, ly=0.0, lz=-1.0;
int moveDelta = 0;
float lastMouseX=windowWidth*0.5, lastMouseY=windowHeight*0.5;
void reshape(int w, int h){
//...
}
// handles angle changes
void orientMe(float horizontalAngle, float verticalAngle) {
lx = sin(horizontalAngle);
if(beta > -1.5 && beta < 1.5)
ly = sin(verticalAngle);
lz = -cos(horizontalAngle);
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0.0, 1.0, 0.0);
}
// handles x,y,z coords changes
void flatMovement(int i) {
x = x + i*(lx)*0.1;
z = z + i*(lz)*0.1;
glLoadIdentity();
gluLookAt(x, y, z,
x + lx, y + ly, z + lz,
0.0, 1.0, 0.0);
}
void display() {
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta) {
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
glutSwapBuffers();
}
void pressSpecialKey(int key, int x, int y) {
//...
}
void releaseSpecialKey(int key, int x, int y) {
//...
}
static void idle(void)
{
glutPostRedisplay();
}
void mouseMove(int x, int y) {
//...
}
void setupLights() {
GLfloat spotDirection[] = {0.0f, -1.0f, 0.0f, 1.0f};
GLfloat spotPosition[] = {0.0f, 4.0f, 0.0f, 1.0f};
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
glLightfv(GL_LIGHT0, GL_POSITION, spotPosition);
glEnable(GL_LIGHT0);
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Spotlight");
glutSpecialFunc(pressSpecialKey);
glutSpecialUpFunc(releaseSpecialKey);
glutPassiveMotionFunc(mouseMove);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
setupLights();
glutMainLoop();
return EXIT_SUCCESS;
}
Unfortunately, the spotlight seems to follow camera changes and I have no idea what's wrong with the code above.
EDIT
Thanks to what #genpfault posted, I've alreadysorted things out.
The answer was to leave only the glEnable(GL_LIGHT0) in the main() function, and move the rest of the code responsible for lighting to the very end of the display() function (right before calling glutSwapBuffers().
So the final, simplified code looks as follows:
#include <windows.h>
#define GLUT_DISABLE_ATEXIT_HACK
#include <gl/glut.h>
#include <gl/glu.h>
#include <gl/gl.h>
#include <math.h>
#include <iostream>
const int windowWidth = 640;
const int windowHeight = 480;
float alpha=0.0, alphaDelta=0.0, ratio, beta=0.0, betaDelta=0.0;
float x=0.0, y=1.75, z=5.0;
float lx=0.0, ly=0.0, lz=-1.0;
int moveDelta = 0;
float lastMouseX=windowWidth*0.5, lastMouseY=windowHeight*0.5;
void reshape(int w, int h){
//...
}
// handles angle changes
void orientMe(float horizontalAngle, float verticalAngle) {
//...
}
// handles x,y,z coords changes
void flatMovement(int i) {
//...
}
void setupLights() {
//THE CONTENTS SLIGHTLY CHANGED HERE
GLfloat spotPosition[] = {2.0f, 4.0f, 0.0f, 1.0f};
GLfloat spotDirection[] = {0.0f, -1.0f, 0.0f, 1.0f};
glLightfv(GL_LIGHT0, GL_POSITION, spotPos
glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 40);ition);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, spotDirection);
}
void display() {
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta) {
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
setupLights(); // PUT THE LIGHTING SETUP AT THE END OF DISPLAY()
glutSwapBuffers();
}
void pressSpecialKey(int key, int x, int y) {
//...
}
void releaseSpecialKey(int key, int x, int y) {
//...
}
static void idle(void)
{
glutPostRedisplay();
}
void mouseMove(int x, int y) {
//...
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(windowWidth,windowHeight);
glutInitWindowPosition(10,10);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("Spotlight");
glutSpecialFunc(pressSpecialKey);
glutSpecialUpFunc(releaseSpecialKey);
glutPassiveMotionFunc(mouseMove);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHT0); // IN THE MAIN, LEAVE ONLY THE ENABLING CALL
glutMainLoop();
return EXIT_SUCCESS;
}
Set your light position after you set your camera transform:
void display()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.0, 5.0, -5.0, 5.0, -5.0, 5.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// orient observer
if(moveDelta)
flatMovement(moveDelta);
if(alphaDelta || betaDelta)
{
alpha += alphaDelta;
alphaDelta = 0;
beta += betaDelta;
betaDelta = 0;
orientMe(alpha, beta);
}
setupLights();
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0,1,0);
glBegin(GL_QUADS);
glNormal3d(0,1,0);
glVertex3f(-100.0, 0.0, -100.0);
glVertex3f(-100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, 100.0);
glVertex3f(100.0, 0.0, -100.0);
glEnd();
glColor3f(1,0,0);
glutSolidTeapot(1);
glutSwapBuffers();
}
See here. Your existing code follows the "Moving the Light Source Together with Your Viewpoint" call sequence.
Related
I tried to draw a simple line in OpenGL:
#include "stdafx.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <freeglut.h>
float startX = -180.0f;
float startZ = -100.0f;
float qtyX = 36;
float qtyZ = 20;
float red = 0.0f, green = 0.0f, blue = 0.0f;
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0.0, 50.0f, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0);
glBegin(GL_LINES);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(50.0f, 50.0f, 50.0f);
glEnd();
//glutWireSphere(20.0f, 20.0f, 20.0f);
glutSwapBuffers();
glFlush();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-64.0, 64.0, -36.0, 36.0, 10, 10000.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1280, 720);
glutInitWindowPosition(100, 100);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
However, when I run the program, the performance drop significantly then after a few seconds, it stops responding (I have to run task manager and force quit the program). However, when I disable the line and draw the glutWireSphere (or a polygon), the performance is normal.
Is there a problem with my code? Or perhaps GL_LINES is deprecated?
Windows 10, 1070 MaxQ, Visual Studio
I have an openGL program that drawn 3 houses and another things but, when I compile with gcc, just appears a black screen - and this is happening with all my openGL programs
What can I do?
PS: If I resize my screen, the drawn appears.
OS: macOS Mojave
PC: MacBook Air 2017
Development in Visual Studio Code
gcc version: 10.0.0
#include <stdlib.h>
#include <stdio.h>
#include <glut/glut.h>
#include <math.h>
#define PI 3.14159265358979324
//teto
bool teto = true;
bool base = true;
bool janela = true;
bool porta = true;
void Teto(void)
{
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(20.0, 10.0, 0.0);
glVertex3f(40.0, 0.0, 0.0);
glEnd();
}
void Base(void)
{
glBegin(GL_QUADS);
glColor3f(0, 1, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 30.0, 0.0);
glVertex3f(35.0, 30.0, 0.0);
glVertex3f(35.0, 0.0, 0.0);
glEnd();
}
void Porta(void)
{
glBegin(GL_QUADS);
glColor3f(0, 0, 1);
glVertex3f(0, 0.0, 0.0);
glVertex3f(0, 10.0, 0.0);
glVertex3f(5.0, 10.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Janela()
{
glBegin(GL_QUADS);
glColor3f(0, 0, 0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 5.0, 0.0);
glVertex3f(5.0, 5.0, 0.0);
glVertex3f(5.0, 0.0, 0.0);
glEnd();
}
void Sun()
{
static float R = 10.0; // Radius of circle.
static float X = 20.0; // X-coordinate of center of circle.
static float Y = 70.0; // Y-coordinate of center of circle.
static int numVertices = 30; // Number of vertices on circle.
float t = 0; // Angle parameter.
int i;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
// Draw a line loop with vertices at equal angles apart on a circle
// with center at (X, Y) and radius R, The vertices are colored randomly.
glBegin(GL_LINE_LOOP);
for(i = 0; i < numVertices; ++i)
{
glColor3f(1.0, 1.0, 0.0);
glVertex3f(X + R * cos(t), Y + R * sin(t), 0.0);
t += 2 * PI / numVertices;
}
glEnd();
}
// Drawing routine.
void drawHouse(void)
{
glPopMatrix();
glPushMatrix();
//glPopMatrix();
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
//Sun();
glTranslatef(30.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(3.0f,30.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glPopMatrix();
glScalef(1.75f, 1.75f, 0.0f);
glScalef(1.25f, 1.25f, 0.0f);
//glScalef(0.75f, 0.75f, 0.0f);
glTranslatef(25.0f,20.0f,0.0f);
if(teto)
Teto();
glTranslatef(3.0f,-30.0f,0.0f);
if(base)
Base();
glTranslatef(15.0f,0.0f,0.0f);
if(porta)
Porta();
glTranslatef(-10.0f,20.0f,0.0f);
if(janela)
Janela();
glTranslatef(20.0f,0.0f,0.0f);
if(janela)
Janela();
glFlush();
}
// Initialization routine.
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
// OpenGL window reshape routine.
void resize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 130.0, 0.0, 100.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
// Keyboard input processing routine.
void keyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
case 'c':
base = !base;
break;
case 't':
teto = !teto;
break;
case 'j':
janela = !janela;
break;
case 'p':
porta = !porta;
break;
case 's':
Sun();
//glRotatef(10.0, 0.0f, 0.0f, 1.0f);
break;
default:
break;
}
glutPostRedisplay();
}
// Main routine.
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
glutInitWindowSize(900, 700);
glutInitWindowPosition(100, 100);
glutCreateWindow("House.cpp");
glutDisplayFunc(drawHouse);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
setup();
glutMainLoop();
}
My code looks like this Modification in this code will be appreciated. I tried, only rectangle appears not the text.
#include <GL/glut.h>
#include<bits/stdc++.h>
using namespace std;
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void RenderToDisplay()
{
int l,lenghOfQuote, i;
char str[80];
strcpy(str,"Have courage and be kind");
cout<<str;
lenghOfQuote = (int)strlen(str);
for (i = 0; i < lenghOfQuote; i++)
{
glColor3f(1.0, 0.0, 0.0);
glutStrokeCharacter(GLUT_STROKE_ROMAN, str[i]);
}
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex3f(150.0f, 200.0f, 0.0f);
glColor3f(0.940, 0.37, 0.47);
glVertex3f(450.0f, 200.0f, 0.0f);
glColor3f(0.940, 0.37, 0.47);
glVertex3f(450.0f, 400.0f, 0.0f);
glColor3f(0.69, 0.27, 0.57);
glVertex3f(150.0f, 400.0f, 0.0f);
glColor3f(0.69, 0.27, 0.57);
glEnd();
RenderToDisplay();
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(600, 600);
glutInitWindowPosition(0,0);
glLineWidth(3);
glutCreateWindow("Assignment Q2");
init2D(0.0, 0.0, 0.0);
glutDisplayFunc(display);
glutMainLoop();
}
You could tell me the comments if you need to ask anything else. I used codeblocks to test this program.
Okay after a day I got it, I'm so dumb :')
#ifdef __APPLE_CC__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
void init2D(float r, float g, float b)
{
glClearColor(r, g, b, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0, 600.0, 0.0, 600.0);
}
void rectangle()
{
glBegin(GL_POLYGON);
glColor3f(0.4,0,0.8);
glVertex3f(150.0f, 200.0f, 0.0f);
glColor3f(0.4,0,0.8);
glVertex3f(450.0f, 200.0f, 0.0f);
glColor3f(0.6,0,0.6);
glVertex3f(450.0f, 400.0f, 0.0f);
glColor3f(0.6,0,0.6);
glVertex3f(150.0f, 400.0f, 0.0f);
glEnd();
}
void text()
{
char menu[80];
strcpy(menu,"Have courage and be kind");
int len;
len = strlen(menu);
glColor3f(1,1,1);
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
gluOrtho2D( 0, 600, 0, 600 );
glMatrixMode( GL_MODELVIEW );
glPushMatrix();
glLoadIdentity();
glRasterPos2i(190, 300);
for ( int i = 0; i < len; ++i )
{
glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, menu[i]);
}
glPopMatrix();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
rectangle();
text();
glFlush();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowPosition(0, 0);
glutInitWindowSize(600, 600);
glutCreateWindow("Assignment 1 Question 2");
init2D(0.0f, 0.0f, 0.0f);
glutDisplayFunc(display);
glutMainLoop();
}
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 just started learning OpenGL so I have problem with showing the object on the screen.
Display() contains this object and I want to show this one by glutDisplayFunc(Display); but it's not working, I have only grey screen after compiling. (Yeah, this object is wheel)
#include "stdafx.h"
#include <math.h>
#include <GL/freeglut.h>
GLint Width = 1024, Height = 640;
const int CubeSize = 500;
void Display (void) {
glClearColor (0.7, 0.7, 0.7, 1);
glClear (GL_COLOR_BUFFER_BIT);
glColor3ub (255, 200, 200);
int iTimeElapsed = glutGet(GLUT_ELAPSED_TIME);
float fRevolveScale1 = 0.2f;
float fRevolveScale2 = 0.4f;
long i;
// clear all pixels
glClear(GL_COLOR_BUFFER_BIT);
// push temp state
glPushMatrix();
// translate to center
glTranslatef(0.5f, 0.5f, 0.0);
// rotate around pivot
glRotatef(iTimeElapsed * fRevolveScale1, 0.0, 0.0, 1.0);
// translate to planet location
glTranslatef(0.25f, 0.25f, 0.0);
glRotatef(iTimeElapsed * fRevolveScale2, 0.0, 0.0, 1.0);
glColor3f(0.129412f, 0.129412f, 0.129412f);
glutSolidSphere(0.1f, 16, 16);
// five bolts, step 72 degree (72*5=360 degree)
glColor3f(1.0, 1.0, 1.0);
for (i = 0; i<5; ++i)
{
glPushMatrix();
glRotatef(72.0f*i, 0.0, 0.0, 1.0); // rotate coordinate 72 degree
glTranslatef(0.04f, 0.0, 0.0);// translate on the rotated coordinate
glutSolidSphere(0.01f, 16, 16);
glPopMatrix();
}
glTranslatef(0.0f, 0.0f, 0.0f);// translate on the rotated coordinate
glutSolidSphere(0.01, 16, 16);
// pop temp state
glPopMatrix();
glFlush();
glutPostRedisplay();
glFinish ();
}
void Reshape(GLint w, GLint h) {
Width = w, Height = h;
glViewport (0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity ();
glOrtho (0, w, 0, h, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity ();
}
void Keyboard(unsigned char key, int x, int y) {
const char ESCAPE = '\033 ';
if (key == ESCAPE) exit (0);
}
void main(int argc, char *argv []) {
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGB);
glutInitWindowSize (Width, Height);
glutCreateWindow("E X A M P L E");
glutDisplayFunc(Display);
glutReshapeFunc(Reshape);
glutKeyboardFunc(Keyboard);
glutMainLoop ();
}