Here is the code, there aren't any errors, what is wrong with it?
I compile it, the command prompt opens, the window opens, the window is all white, I recolored it to grey.., and it also does not draw my shape, so what is the problem?
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
void ProcessSpecialKeys(int key, int x, int y){
switch(key){
case GLUT_KEY_RIGHT:
exit(0);
case GLUT_KEY_LEFT:
exit(0);
case GLUT_KEY_UP:
exit(0);
case GLUT_KEY_DOWN:
exit(0);
default:
exit(0);
}
}
void renderScene(void) {
glClearColor(0.3f, 0.3f, 0.3f, 0.3f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glutSwapBuffers();
}
void renderPrimitive(void){
glBegin(GL_QUADS);
glVertex3f(-1.0f,-1.0f,0.0f);
glVertex3f(-1.0f,1.0f,0.0f);
glVertex3f(1.0f,1.0f,0.0f);
glVertex3f(1.0f,-1.0f,0.0f);
glEnd();
}
void display(void){
glTranslatef(0.0f,0.0f,-0.5f);
renderPrimitive();
glFlush();
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear(GL_COLOR_BUFFER_BIT);
}
int main(int argc, char **argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("Dimension");
glutDisplayFunc(display);
glLoadIdentity();
glutSpecialFunc(ProcessSpecialKeys);
glutMainLoop();
return 1;
}
The problem is that you are clearing the display after you draw your scene in the display function. Also, there is a useless glFlush() call. Remove these lines:
glFlush();
glClearColor(0.3f,0.3f,0.3f,0.3f);
glClear(GL_COLOR_BUFFER_BIT);
Add a glutSwapBuffers() instead of them.
glFlush() is used in single buffered mode.
Remember, you are using double buffered mode, so there are two buffers. Your scene is drawn to the back buffer, and to display it, you have to copy it to the front buffer by calling glutSwapBuffers() at the end of the display function.
Related
I am trying to make a triangle that will change its color if I right click on it, but my code isn't working. I am using Codeblocks OpenGL GLUT Project. If i change the GLUT) GB value to 0 it won't show the output
#include<glut.h>
#include<gl/gl.h>
#include<gl/glu.h>
#define RED 1
#define GREEN 2
#define BLUE 3
#define GLUT_RGBA 1
// i have made changed in RGB and RGBA by changing it's value 0 or 1 but no //output
#define GLUT_DOUBLE 2
#define GLUT_DEPTH 16
// also changed these two values but still no
float angle =0.0;
float red = 1.0, blue =1.0, green=1.0;
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT/GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(angle,0.0,1.0,0.0);
glColor3f(red,green,blue);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0);
glEnd();
angle++;
glutSwapBuffers();
}
void ProcessMenuEvents(int options){
switch (options){
case RED:
red =1.0; green=0.0 ; blue=0.0;
break;
case GREEN:
red =0.0; green=1.0 ; blue=0.0;
break;
case BLUE:
red =0.0; green=0.0 ; blue=1.0;
break;
}
}
int main(int argc, char **argv){
glutInit(&argc,argv);
//glutInitDisplayMode(GLUT_DEPTH/GLUT_DOUBLE/1.0);
glutInitDisplayMode(GLUT_DEPTH/GLUT_DOUBLE/GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(320,320);
glutCreateWindow("GHJYUGH");
glutDisplayFunc(renderScene);
glutIdleFunc(renderScene);
glutCreateMenu(ProcessMenuEvents);
glutAddMenuEntry("red",RED);
glutAddMenuEntry("green",GREEN);
glutAddMenuEntry("blue",BLUE);
glutAttachMenu(GLUT_RIGHT_BUTTON);
glutMainLoop();
}
The error was due to this problem because of the vertex angle, the program gave some vertex errors
void renderScene(void) {
glClear(GL_COLOR_BUFFER_BIT/GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(angle,0.0,1.0,0.0);
glColor3f(red,green,blue);
glBegin(GL_TRIANGLES);
glVertex3f(-0.5,-0.5,0.0);
glVertex3f(0.5,0.0,0.0);
glVertex3f(0.0,0.5,0);
glEnd();
angle++;
glutSwapBuffers();
}
I'm a complete novice in OpenGL and have started following a book on this topic. The first program that they demonstrate is the Sierpinski Gasket program. Here's what the code looks like in the book:
#include<GL/glut.h>
#include<stdlib.h>
void myinit()
{
//attributes
glClearColor(1.0,1.0,1.0,1.0);
glColor3f(1.0,0.0,0.0); //draw in red
//set up viewing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
}
void display()
{
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; //triangle
GLfloat p[2]={7.5,5.0}; //arbitrary point
glClear(GL_COLOR_BUFFER_BIT); //clear the window
glBegin(GL_POINTS);
//Gasket Program
for(int i=0;i<5000;i++){
int j=rand()%3;
//compute points halfway between random vertex and arbitrary point
p[0]=(vertices[j][0]+p[0])/2;
p[1]=(vertices[j][1]+p[1])/2;
//plot the point
glVertex2fv(p);
}
glEnd();
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(0,0);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
myinit();
glutMainLoop();
return 0;
}
However, when I compile the program it only displays a window completely filled with white and nothing else. Why isn't the above code working the way it should?
Swap glFlush() for glutSwapBuffers():
#include<GL/glut.h>
void display()
{
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT); //clear the window
//set up viewing
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,50.0,0.0,50.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
GLfloat vertices[3][2]={{0.0,0.0},{25.0,50.0},{50.0,0.0}}; //triangle
GLfloat p[2]={7.5,5.0}; //arbitrary point
glColor3f(1.0,0.0,0.0); //draw in red
glBegin(GL_POINTS);
//Gasket Program
for(int i=0;i<5000;i++)
{
int j=rand()%3;
//compute points halfway between random vertex and arbitrary point
p[0]=(vertices[j][0]+p[0])/2;
p[1]=(vertices[j][1]+p[1])/2;
//plot the point
glVertex2fv(p);
}
glEnd();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutCreateWindow("Sierpinski Gasket");
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
glFlush() won't actually swap the back/front buffers requested by GLUT_DOUBLE. You need glutSwapBuffers() for that.
I want to make a space invader game in opengl. So I thought of creating the enemies using triangles. Before making the game, I want to try out my hand in animation. I have triangle. i want to translate it left upto some point with animation(i.e, triangle is translated after some interval of time. It should look as if it is moving).After reaching some point in the left, I want to translate it back to the same position or some distances right. The process should go on till the screen is open. I used sleep function. But it is not working. No animation is shown. Only the translated triangle is drawn at different translated position. Help me.
Here is my code-
#include "windows.h"
#include <gl/glut.h>
#include<stdio.h>
void init( void )
{
printf( "OpenGL version: %s\n", (char*)glGetString(GL_VERSION));
printf( "OpenGL renderer: %s\n", (char*)glGetString(GL_RENDERER));
//Configure basic OpenGL settings
glClearColor(1.0, 1.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(0.0,640.0,0.0,480.0);
glColor3f(1.0,0.0,0.0);
glPointSize(3);
}
void house(int x, int y,int z)
{
glPushMatrix();
glTranslatef(x, y,z);
glBegin (GL_LINES);
glVertex2i (0,30);
glVertex2i (15,60);
glVertex2i (15,60);
glVertex2i (30,30);
glVertex2i (30,30);
glVertex2i (0,30);
glEnd();
//Sleep(200);
glPopMatrix();
//glutSwapBuffers();
}
// Main drawing routine. Called repeatedly by GLUT's main loop
void display( void )
{
//Clear the screen and set our initial view matrix
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
int i;
for(i = 10; i < 350; i = i + 50)
{
house(i,20,0);
Sleep(200);
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}
// Entry point - GLUT setup and initialization
int main( int argc, char** argv )
{
glutInit( &argc, argv );
glutInitDisplayMode (GLUT_DEPTH | GLUT_SINGLE| GLUT_RGB);
glutInitWindowSize (800, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow( "OpenGL Test" );
glutDisplayFunc( display );
init();
glutMainLoop();
return 0;
}
In main() you have declared your display() as display callback function. GLUT will call this function either when it determines that the window need to be redrawn or when it is told to redraw it for example by the function glutPostRedisplay().
The display function is expected to call redraw the windows at a specific point in time. The glFlush() will force the execution of the GL commands.
The problem is that your animation loop is inside the redraw function and glFlush() is called at the end, showing the result at once. And you don't tell GLUT to redraw the windows. This is why you don't seee the animation.
For the purpose of the tutorial, I propose you to define a global variable for the initial position of the house drawing. Of course, you'll have to improve this as soon as you understood how all this works.
static int pos = 10; // temporary work around, just for the demo
Then define a timer function, that gets called after a time interval. This will be the core of your animation, organizing the moving, and the redrawing of the window by calling glutPostRedisplay() :
void timerfunc(int value) // handle animation
{
pos += 50; // update the postion
if (pos<350) // as in your originial loop
glutTimerFunc(200, timerfunc, 0); // plan next occurence
glutPostRedisplay(); // redraw the window
}
Activate your timer function, in main() just before launching glutMainLoop():
glutTimerFunc(200, timerfunc, 0); // call a timer function
glutMainLoop(); // this call is already in your code
Your display function can then be changed into:
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
house(pos, 20, 0); // draw the house at its last calculated position
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
}
Then it works in animated modus !
#include <stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<GL/glut.h>
void display (void)
{
glClearColor(1.f, 0.f, 0.f, 1.f);
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Colorcube Viewer");
glutDisplayFunc(display);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
i am not able to figure out whats the problem with this code ?
it does not give me a red window.
You need to call glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); after setting the clear color (because you have depth test enabled, make sure that you're clearing both the color buffer AND the depth buffer
I'm trying to use GLUT and OpenGL to draw a triangle that will change its z-coordinates based off a user input. I can't get it working though; no compiler errors, but the triangle doesn't change when I press those keys. Does anyone know why?
/////////////////////////////
/////OPENGL ASSIGNMENT 1/////
/////////////////////////////
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
#include <GL/glut.h>
using namespace std;
/////GLOBAL VARIABLES/////
float zCoord = -10.0;
////Function Declarations/////
void initRendering();
void windowResize(int width, int height);
void keyBoardEvents(unsigned char key, int x, int y); ///this is the function that will use user input
void drawing(); /// Note: this is the function we will use to draw shapes using opengl functions
////Function Definitions/////
void initRendering()
{
glEnable(GL_DEPTH_TEST);
}
void windowResize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (double) width/ (double) height, 1.0, 200.0); ////Note: 45.0 is the camera angle, w/h is the aspect ratio///
}
void keyBoardEvents(unsigned char key, int x, int y)
{
switch (key)
{
case 27: exit(0);
break;
case 'w': zCoord = zCoord + 5.0f;
break;
case 's': zCoord = zCoord - 5.0f;
break;
case 'r': zCoord = -5.0f;
break;
}
if (zCoord < -30.0) ////// here we ensure -5.0 > zCoord > -30.0
{
zCoord = 30.0;
}
else if (zCoord > -5.0)
{
zCoord = -5.0;
}
}
void drawing()
{
//Standard OpenGL at the beginning of each drawing() function:
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
///Specifically to this one, drawing the rectangle:
glBegin(GL_TRIANGLES); ////here we say start grouping the next 3 statements as 3 vertices of a triangle
glVertex3f(-0.5f, 0.5f, (float)zCoord);
glVertex3f(-1.0f, 1.5f, (float)zCoord);
glVertex3f(-1.5f, 0.5f, (float)zCoord);
glEnd;
glutSwapBuffers();
}
int main(int argc, char** argv) ////here we are passing some initial arguments to main, that the user does not have to input
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DEPTH | GLUT_RGB | GLUT_DOUBLE ); ///initiates things like depth (DEPTH) and color (RGB)/////
glutInitWindowSize(400, 400); /////the initial window size
glutCreateWindow("MentorAssignment_1"); /////here we using an OpenGL function to make a window of title MentorAssignment_1
initRendering();
//////HANDLER FUNCTIONS///// ////These are functions that call other functions, like keyBoardEvents and drawing, every time glutMainLoop() runs
glutKeyboardFunc(keyBoardEvents);
glutDisplayFunc(drawing);
glutReshapeFunc(windowResize);
glutMainLoop(); ///The openGL function that ensures main runs infinitely! (until escape is pressed)
return 0;
}
You're missing parentheses after glEnd, which means you're not calling the function, just taking its address. Issuing a buffer swap after a glBegin() can likely put OpenGL in error state and cause it to ignore all further rendering commands. The net effect would be the triangle seems not to move, simply because it's no longer redrawn.
You need to call glutPostRedisplay() at the end of your keyBoardEvents function to indicate to GLUT that the frame should be redrawn.