I'm trying out a simple implementation of trying to render a bitmap font, as given in the Red book. The problem is that my viewport is blank throughout and nothing comes on the screen.
Here's the code:
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
GLubyte A[14] = {
0xc0,0x00,
0x60,0xc0,
0x3f,0x80,
0x11,0x00,
0x0a,0x00,
0x0a,0x00,
0x04,0x00,
};
void init(void)
{
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glClearColor(0,0,0,0);
}
void display(void)
{
float c[4];
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1,1,1);
glGetFloatv(GL_CURRENT_RASTER_POSITION, c);
printf("%f %f %f %f\n", c[0],c[1],c[2],c[3]);
printf("%x\n",A[0]);
glRasterPos2f(20,20);
glGetFloatv(GL_CURRENT_RASTER_POSITION_VALID, c);
printf("%f\n", c[0]);
glBitmap(12,7,0,0,11,0,A);
//glBitmap(12,7,0,0,11,0,A);
//glBitmap(12,7,0,0,11,0,A);
glGetFloatv(GL_CURRENT_RASTER_POSITION, c);
printf("%f %f %f %f\n", c[0],c[1],c[2],c[3]);
glFlush();
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0, h, -1.0, 1.0);
//gluOrtho2D(0,1,0,1);
glMatrixMode(GL_MODELVIEW);
//glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("FOnt");
init();
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
Works for me:
FreeGLUT on Vista64 (compiled as 32-bit), Radeon HD 6570. VS 2008.
EDIT: On Linux too:
Ubuntu 10.04 amd64, Intel HD Graphics 3000, Mesa DRI Intel(R) Sandybridge Mobile GEM 20100330 DEVELOPMENT
EDIT2:
Try using double-buffering via glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) or glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA) and adding glutSwapBuffers() to the end of your display() function.
And/or try disabling your compositor (compiz, kwin, unity, GNOME Shell, etc) if you have one enabled.
Might be a driver bug.
Replace GL_SINGLE with GLUT_DOUBLE
Replace glFlush() with glutSwapBuffers()
Add glLoadIdentity() after glMatrixMode(GL_MODELVIEW)
Then it should work
Related
I'm creating OpenGL texture using default function glGenTextures. When OpenGL version set to 3.0 everything works fine, but when I override it with 4.2 glGenTextures starts to throw error #1282 (invalid operation). What i'm doing wrong?
Here's code segment I've tested:
#include "GL/freeglut.h"
#include "GL/gl.h"
#define MAJOR_GL_VERSION 3
#define MINOR_GL_VERSION 0
int w = 200;
int h = 200;
const char* title = "title";
int main(int argc, char const *argv[])
{
puts("Overriding default OpenGL version...");
glutInitContextVersion(MAJOR_GL_VERSION, MINOR_GL_VERSION);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_ALPHA);
glutInitWindowSize(w, h);
glutCreateWindow(title);
printf("Using OpenGL Version: %s\n=========\n", (char*)glGetString(GL_VERSION));
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glEnable( GL_ALPHA_TEST );
glEnable( GL_BLEND );
GLenum error;
GLuint id = 0;
glGenTextures(1, &id);
if((error = glGetError()) != GL_NO_ERROR || id == 0)
{
printf("Gl error: %s (errno %i)\n", gluErrorString(error), error);
return 0;
}
while (1) { }
return 0;
}
The error does probably not happen in the line you expect it to happen. Chances are high that some of the methods before glGenTextures is the problem. Neither of this lines
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, h, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glShadeModel(GL_SMOOTH);
are allowed in a OpenGL Core Profile. Profiles were introduces in OpenGL 3.2, thus the Core Profile request does not have any effect when requesting a 3.0 context. But with 3.2+, you'll get a core profile which removed a lot of stuff.
You can either remove the lines mentioned above and replace them with a Core-Profile compatible code. Or you could explicitly request a compatibility profile (glutInitContextProfile(GLUT_COMPATIBILITY_PROFILE) when you want to stick to the fixed function pipeline.
I keep calling glutMainLoopEvent to process the graphics. However, after someone closed the window, I would like to exit the loop and show Code reached here. . it seems when a window is closed, an exit function is called and the entire application stops. While I need the application to continue. How should I fix the code?
#include <stdio.h>
#include <GL/freeglut.h>
//display function - draws a triangle rotating about the origin
void cback_render()
{
//keeps track of rotations
static float rotations = 0;
//OpenGL stuff for triangle
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glRotatef(rotations, 0, 0, 1);
glBegin(GL_TRIANGLES);
glVertex3f(0,0,0);
glVertex3f(1,0,0);
glVertex3f(0,1,0);
glEnd();
//display on screen
glutSwapBuffers();
//rotate triangle a little bit, wrapping around at 360°
if (++rotations > 360) rotations -= 360;
}
void timer(int value )
{
glutPostRedisplay();
glutMainLoopEvent();
glutTimerFunc(30, timer, 1);
}
int main(int argc, char **argv)
{
//initialisations
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(100, 100);
glutInitWindowSize(512, 512);
//create window and register display callback
glutCreateWindow("freegluttest");
glutDisplayFunc (cback_render);
glutTimerFunc(30, timer, 1);
//loop forever
long i=0;
while(1)
{
printf("[%ld]\n",i);
i++;
glutMainLoopEvent();
}
printf("Code reached here.");
return 0;
}
Use GLUT_ACTION_ON_WINDOW_CLOSE to allow your program to continue when a window is closed.
glutSetOption(GLUT_ACTION_ON_WINDOW_CLOSE,
GLUT_ACTION_GLUTMAINLOOP_RETURNS);
Sources:
http://www.lighthouse3d.com/cg-topics/glut-and-freeglut/
http://freeglut.sourceforge.net/docs/api.php
hi guys recently 'm learning the basics of opengl..
so understanded them i tried to write my first program. At the beginning i tried with the functions that allow you to create buffers for the drawing call. but the linker tell to me that these function are undeclared. so i tried with the old one ( i think maybe i'm wrong) and it works.
so my code is the following
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <stdio.h>
void reshape(int, int);
void display(void);
void keyboard(unsigned char, int, int);
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition (100, 100);
glutCreateWindow(argv[0]);
glClearColor(0.0,0.0,0.0,0.0);
glShadeModel(GL_FLAT);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
void reshape(int w, int h)
{
glViewport(0,0,(GLsizei) w, (GLsizei) h);
}
void display(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
GLuint* i;
glGenBuffers(1,i);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.00f,1.0f);
glVertex3f(0.5f, 0.0f, 0.0f);
glVertex3f(-0.5f,0.5f, 0.0f);
glVertex3f( 0.0f,-0.5f, 0.0f);
glEnd();
glBegin(GL_TRIANGLES);
glColor3f(1.0f,1.00f,1.0f);
glVertex3f(-0.5f, 0.0f, 0.0f);
glVertex3f(0.5f,0.5f, 0.0f);
glVertex3f(0.0f,-0.5f, 0.0f);
glEnd();
//glutWireCube(10);
glFlush();
}
void keyboard(unsigned char key, int x, int y)
{
switch(key) {
case 'l':
break;
}
}
the problem born with the glGenBuffers and all the gl* functions.
here there is the gcc's commande used
gcc -o cube main.c -lGL -lGLU -lglut
and this is the error
main.c: In function ‘display’:
main.c:40:3: warning: implicit declaration of function ‘glGenBuffers’ [-Wimplicit-function-declaration]
glGenBuffers(1,i);
sorry for my English and thank to all.
You are missing the following line at the beginning of your code
#define GL_GLEXT_PROTOTYPES
#include <GL/glut.h>
#include <GL/glext.h>
#include <GL/gl.h>
#include <stdlib.h>
#include <stdio.h>
It compiles without any warnings like this on my Ubuntu computer, see glGenBuffers not defined?
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GL/glut.h>
void changeSize(int w, int h)
{
if(h == 0)
h = 1;
float ratio = w / h;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, w, h);
gluPerspective(40,ratio,1.5,20);
glMatrixMode(GL_MODELVIEW);
}
void renderScene(void)
{
glClear(GL_COLOR_BUFFER_BIT );
glLoadIdentity();
glTranslatef(0.0,0.0,-5.0);
glDrawArrays(GL_TRIANGLES,0,3);
glutSwapBuffers();
}
void init()
{
GLfloat verts[] = {
0.0, 1.0,
-1.0, -1.0,
1.0, -1.0
};
GLuint bufferid;
glGenBuffers(1,&bufferid);
glBindBuffer(GL_ARRAY_BUFFER,bufferid);
glBufferData(GL_ARRAY_BUFFER,sizeof(verts),verts,GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
if(glGetError()==GL_NO_ERROR)
printf("no error");
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowPosition(100,100);
glutInitWindowSize(500,500);
glutCreateWindow("MM 2004-05");
glewInit();
init();
glutDisplayFunc(renderScene);
glutReshapeFunc(changeSize);
if (GLEW_ARB_vertex_program && GLEW_ARB_fragment_program)
printf("Ready for GLSL\n");
else {
printf("No GLSL support\n");
//exit(1);
}
glutMainLoop();
return 0;
}
When using glGenBuffers my screen turns out black and shows no error. If i draw some other shape without using buffers they are displayed but not with buffer objects.
openGL version:3.0
operating system:ubuntu
IDE:eclipse
When using glGenBuffers you're using the OpenGL-3.0 specification. To draw anything in OpenGL-3.0+ you need to use shaders, hence why the screen is black; your triangle isn't being shaded.
You are using calls for generic vertex attributes here:
glEnableVertexAttribArray(0);
glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);
Generic vertex attributes can only be used in combination with shaders. As long as you're using the fixed function pipeline, you also have to use fixed function vertex attributes.
The corresponding calls using fixed function attributes are:
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(2, GL_FLOAT, 0, 0);
When i try to run this code after compiling it, i got only a window border without content, so where is the error?
Note: i am using ubuntu and gcc for compiling
gcc -lglut Simple.c -o Simple
The output
#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL Library
#include <GL/glu.h> // Header File For The GLu Library
void SetupRC(void);
void RenderScene(void);
void ChangeSize(GLsizei w, GLsizei h);
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClear(GL_COLOR_BUFFER_BIT);
// set the color to these values
// R G B
glColor3f(56.0f, 19.0f,68.0f);
// Draw a filled rectangle with current color
glRectf(-25.0f, 50.0f, 50.0f, -25.0f);
// Flush drawing commands
glFlush();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition(400,200);
glutInitWindowSize(640,468);
glutCreateWindow("Simple");
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
return 0;
}
// Setup the rendering state
void SetupRC(void)
{
glClearColor(0.0f, 2.0f, 1.0f, 0.0f);
}
// Handling window resizing
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;
// Prevent divide by zero
if (h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat) w / (GLfloat) h;
if (w <= h) {
glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
}
else
glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Try adding a glutSwapBuffers() to the end of RenderScene().
One thing. You have a depth buffer so you should also add GL_DEPTH_BUFFER_BIT to the clear call.
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);