OpenGL blank screen? - c++

I am learning OpenGL from book, I did exactly what is in the book, but when I run it (Eclipse C++ IDE) I get just blank screen.
Book is "OpenGL guide for programmers". I think the error is in Reshape(), but code is from book.
#include <iostream>
#include <GL/glew.h>
#include <GL/glut.h>
typedef const char* String;
String TITLE = "OpenGL";
int HEIGHT = 480;
int WIDTH = HEIGHT / 9 * 12;
void Update(int time){
glutPostRedisplay();
glutTimerFunc( 10, Update, 1);
}
void Init(){
glClearColor(1.0, 1.0, 1.0, 0.0);
glShadeModel(GL_FLAT);
}
void Render(){
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0);
glLoadIdentity();
gluLookAt(0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glScalef(1.0, 2.0, 1.0); // Modeling transformation
glutWireCube(1.0);
glutSwapBuffers();
}
void Reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutCreateWindow(TITLE);
glewExperimental = true;
GLenum err = glewInit();
if(err!=GLEW_OK){
exit(1);
}
Init();
Update(1);
glutDisplayFunc(Render);
glutReshapeFunc(Reshape);
glutMainLoop();
return 0;
}

Yes reshape function is definitely wrong
use that
void Reshape(GLsizei w,GLsizei h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (h == 0)
h = 1;
GLfloat aspectRatio;
aspectRatio = static_cast<GLfloat>(w) / static_cast<GLfloat>(h);
gluPerspective(45.0,aspectRatio,1.0,4000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
instead

Related

Performance drop with GL_LINES?

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

OpenGL show object on the screen

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 ();
}

Making spotlight stationary

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.

Drawing multiple shapes in OpenGL

#include <Windows.h>
#include <GL\glut.h>
#pragma comment(lib, "glut32.lib")
#pragma comment(lib, "glu32.lib")
#pragma comment(lib, "opengl32.lib")
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
bool init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glMatrixMode(GL_PROJECTION);
// Set grid to be from 0 to 1
gluOrtho2D(0.0, 3.0, 0.0, 3.0);
return true;
}
void drawline(float from_x, float from_y, float to_x, float to_y)
{
// From coordinate position
glVertex2f(from_x, from_y);
// To coordinate position
glVertex2f(to_x, to_y);
}
void render(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
glLineWidth(2.0); // Set line width to 2.0
glLoadIdentity();
// Draw line
glBegin(GL_LINES);
drawline(0.25, 0.5, 0.4, 0.5);
drawline(0.4, 0.6, 0.4, 0.5);
drawline(0.4, 0.4, 0.4, 0.5);
drawline(0.6, 0.5, 0.75, 0.5);
glEnd();
// Draw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.4, 0.5);
glVertex2f(0.6, 0.6);
glVertex2f(0.6, 0.4);
glEnd();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("My OpenGL program");
init();
// Draw shape one
glPushMatrix();
glTranslatef(1.5, 1.5, 0.0);
glutDisplayFunc(render);
glPopMatrix();
// Draw shape two
glPushMatrix();
glTranslatef(2.5, 2.5, 0.0);
glutDisplayFunc(render);
glPopMatrix();
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
I'm working to draw something like this: http://i.imgur.com/JJpbJ7M.png
I don't need the whole thing, I just want to be able to draw two shapes where I want them to be. However, this isn't working, can anyone point me into the right direction?
You almost had it.
Draw your shape(s) in render(), not main():
#include <GL/glut.h>
void drawline(float from_x, float from_y, float to_x, float to_y)
{
// From coordinate position
glVertex2f(from_x, from_y);
// To coordinate position
glVertex2f(to_x, to_y);
}
void drawShape()
{
glColor3f(1.0, 1.0, 0.0); // Color (RGB): Yellow
glLineWidth(2.0); // Set line width to 2.0
// Draw line
glBegin(GL_LINES);
drawline(0.25, 0.5, 0.4, 0.5);
drawline(0.4, 0.6, 0.4, 0.5);
drawline(0.4, 0.4, 0.4, 0.5);
drawline(0.6, 0.5, 0.75, 0.5);
glEnd();
// Draw triangle
glBegin(GL_TRIANGLES);
glVertex2f(0.4, 0.5);
glVertex2f(0.6, 0.6);
glVertex2f(0.6, 0.4);
glEnd();
}
void render(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( 0.0, 4.0, 0.0, 4.0, -1, 1 );
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Draw shape one
glPushMatrix();
glTranslatef(1.5, 1.5, 0.0);
drawShape();
glPopMatrix();
// Draw shape two
glPushMatrix();
glTranslatef(2.5, 2.5, 0.0);
drawShape();
glPopMatrix();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(640, 480);
glutCreateWindow("My OpenGL program");
glutDisplayFunc(render);
glutMainLoop();
return 0;
}
I removed reshape() and init() since the default glutReshapeFunc() already calls glViewport() and you should be resetting your projection and modelview matrices each frame anyway.

How to keep the same size of the object (let's say a square) after resizing the window [duplicate]

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Changing projection in OpenGL
I want to find a way to keep the same size of the object. I have this square which moves from upper left to right bottom of screen. When I resize the output window the size of the square increases as well.
Below is my display and reshape function.
void Changesize(int w,int h)
{
ww=w;
wh=h;
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity() ;
glMatrixMode(GL_MODELVIEW);
}
void display(void)
{
printf("i=%f & j=%f \n",i,j);
glClear (GL_COLOR_BUFFER_BIT);
glColor3i (rand(), rand(), rand());
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glBegin(GL_POLYGON);
glColor3i (rand(), rand(), rand());
glVertex3f (i, j-0.125, 0.0);
glVertex3f (i+0.125, j-0.125, 0.0);
glVertex3f (i+0.125, j , 0.0);
glVertex3f (i, j, 0.0);
glEnd();
glFlush ();
usleep(100000);
i=i+0.0125;
j=j-0.0125;
if (i>0.999999)
{
i=0;
j=1;
}
glutSwapBuffers();
glutPostRedisplay();
}
Give this a shot:
#include <GL/glut.h>
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glColor3ub(255,0,0);
glPushMatrix();
glScalef(50,50,50);
glBegin(GL_QUADS);
glVertex2f(-1,-1);
glVertex2f(1,-1);
glVertex2f(1,1);
glVertex2f(-1,1);
glEnd();
glPopMatrix();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho( -w/2.0, w/2.0, -h/2.0, h/2.0, -1, 1);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(800,600);
glutCreateWindow("Scale");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}