Blank screen while using glGenBuffers in openGL - c

#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);

Related

How to setup gluLookat() and glOrtho() in openGL?

I am new to openGL and have a hard time to understand how to actually parametrize the function gluLookat. I what it does but don't know how to know which parameters I should pass to that function and what consequences it will have.
This is my current code and strangely enough I don't even see the axis I drew... I think it is due to the parameters I pass to the function gluLookAt();
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <GL/glut.h>
void init(void)
{
glClearColor(0.8, 0.8, 0.8, 0.0); /* window color white */
glEnable(GL_DEPTH_TEST);
}
void drawScene(void)
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(3.0,3.0,3.0, 0.0,0.0,0.0, 0.0,1.0,0.0);
//used some random parameters...
gluLookAt(0.0, 0.0, 0.0, 5.0, 5.0, 5.0, 0, 1, 0);
//axis
//x
glColor3f(1.0,0.0,0.0);
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex3d(0.0,0.0,0.0);
glVertex3d(1.0,0.0,0.0);
glEnd();
//y
glColor3f(0.0,1.0,0.0);
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex3d(0.0,0.0,0.0);
glVertex3d(1.0,0.0,0.0);
glEnd();
//z
glColor3f(0.0,0.0,1.0);
glLineWidth(1.0);
glBegin(GL_LINES);
glVertex3d(0.0,0.0,0.0);
glVertex3d(1.0,0.0,0.0);
glEnd();
glFlush();
}
void herschaal(){
glViewport(0,0,500,500);
glMatrixMode(GL_PROJECTION);
glOrtho(-10, 10, 10, -10, 10, -10);
glLoadIdentity();
}
int main( int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowPosition(50, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("mijn test");
glutReshapeFunc(herschaal);
init();
glutDisplayFunc(drawScene);
glutMainLoop();
return 0;
}

glGenBuffers is null in GLEW?

I wrote a simple program using OpenGL, SDL 2, and GLEW. It works properly on Linux, under Wine, and on one Windows 7 system I ran it on. However, on another Windows system, it is crashing as soon as it gets to glGenBuffers, even though it says OpenGL 2.1 is available. Here is the code:
#define GLEW_STATIC
#include <stdio.h>
#include <GL/glew.h>
#include <SDL2/SDL.h>
SDL_Window *window;
SDL_GLContext context;
GLuint vbo;
void Init() {
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_LoadLibrary(NULL);
int min, max;
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,&max);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,&min);
printf("Default OpenGL version %d.%d\n",max,min);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,1);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,&max);
SDL_GL_GetAttribute(SDL_GL_CONTEXT_MINOR_VERSION,&min);
printf("OpenGL version %d.%d\n",max,min);
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL,1);
window=SDL_CreateWindow("", 0, 0, 500, 500, SDL_WINDOW_OPENGL);
context = SDL_GL_CreateContext(window);
glewExperimental=1;
GLenum err=glewInit();
if(GLEW_OK!=err)
{
printf("Error: %s\n", glewGetErrorString(err));
return(1);
}
if(window==NULL) {
printf("Could not create window: %s\n", SDL_GetError());
return(1);
}
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glShadeModel(GL_FLAT);
glEnableClientState(GL_VERTEX_ARRAY);
float data[][2] = {{50,50},{100,50},{75,100}};
glGenBuffers(1,&vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW);
}
void Render() {
glViewport(0,0, (GLsizei) 500, (GLsizei) 500);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, (GLdouble) 500, 0.0f, (GLdouble) 500);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(2, GL_FLOAT, 2*sizeof(float), 0);
glDrawArrays(GL_TRIANGLES,0,3);
SDL_GL_SwapWindow(window);
}
int main(int argc, char **argv){
Init();
Render();
SDL_Delay(2000);
glDeleteBuffers(1,&vbo);
return(0);
}
Here is some more information I have added since initially posting this question:
Using %p to print the address of glBindBuffer, it is 0.
Both (GLEW_ARB_vertex_buffer_object == GL_TRUE) and (GLEW_ARB_vertex_array_object == GL_TRUE) return 0.
glGetString(GL_RENDERER) returns "GDI Generic".
I have also tried setting the depth size to 16, but that still doesn't work.
The graphics card is an Nvidia 750 TI.

OpenGL Pre-compiled Header Skipped

Hey guys I'm having a problem with my coding at the moment.
The Problem is that my #Include <glut.h> File is being skipped when looking for Precompiled Header Use and cannot find a way to solve it.
Here is my code:
#include <D:/GL/glut.h>
#include <stdafx.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
using namespace System;
void drawScene(void)
{
int i, j;
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 0.0, 0.0);
glLoadIdentity();
glTranslatef(0.0, 0.0, -25.0);
glutWireCube(5.0); // Box.
glColor3f(1.0, 0.0, 0.0);
for(i=5; i<5; i++)
{
for (j = -5; j < 5; j++)
{
glPushMatrix();
glTranslatef(i*5, j*5, -35.0);
glColor3f(1.0, 1.0, 0);
glutSolidCube(5.0);
glColor3f(0.0, 0.0, 1.0);
glutWireCube(5.0);
glPopMatrix();
}
}
glFlush();
}
void setup(void)
{
glClearColor(1.0, 1.0, 1.0, 0.0);
}
void resize (int w, int h)
{
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-10.0, 10.0, -10.0, 10.0, 10.0, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void KeyInput(unsigned char key, int x, int y)
{
switch(key)
{
case 27:
exit(0);
break;
default:
break;
}
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500); /* Size of the Program Window */
glutInitWindowPosition(100,100);
glutCreateWindow("Voxel Assignment");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(KeyInput);
glutMainLoop();
return 0;
}
It's probably because of the very strange absolute path usage, with drive specifier.
Don't do that, include paths are not supposed to include stuff at that level.
Just say #include <GL/glut.h> and adjust your compiler's settings to add the required directory to the include path.

Changing the color of a teapot sometimes not working

This program should allow the user to select the material of a teapot, that will change dynamically.So I made a menu.
I use setRGB (declared in a utility file) to set the color of an array of 3 GLfloat's.
createMenu just creates the menu reading the voices from a va_list.
setMaterial sets the material, according to the value passed with an enumeration:
typedef enum
{
BlackPlastic= 0,
Brass,
Bronze,
Chrome,
Copper,
Gold,
Peweter,
Silver,
PolishedSilver
}MaterialType;
I will omit the body of the functions that I have tested since they work:
int createMenu(void (*callback) (int),int key, const char* const first, ...)
{
// creates a menu, the number of entries depends on the list length,
// the value starts from zero
}
void setRGB( GLfloat* color, GLfloat red, GLfloat green, GLfloat blue)
{
// Sets the color
}
void setMaterial (GLfloat** material, MaterialType type)
{
// Sets the material color (ambient, diffuse, specular).
}
That's the whole program.My fear here is that I am doing something wrong so that the teapot isn't drawn because OpenGL enters in an invalid state.
The problem is that sometimes I don't see the teapot drawn in the window, I just get a black window.Incredibly sometimes work and I see the teapot, and I am able to change the material colors.
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "utility.h"
#include <stdlib.h>
GLfloat width=500, height=500;
GLfloat** material;
GLfloat light[3][3]= { {1,1,0}, {1,0.5,0}, {1,0,0} };
void menuCallback (int choice)
{
setMaterial((GLfloat**)material, choice);
glutPostRedisplay();
}
void init()
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width/2, width/2, -height/2, height/2, 1, 1000);
material= malloc(4*sizeof(GLfloat*));
for(GLuint i=0; i<4; i++)
{
material[i]=malloc(3*sizeof(GLfloat));
}
setRGB(material[3], 1, 1, 0);
setMaterial(material, BlackPlastic);
}
void display()
{
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glShadeModel(GL_FLAT);
glMaterialfv(GL_FRONT, GL_AMBIENT, material[0] );
glMaterialfv(GL_FRONT, GL_DIFFUSE, material[1]);
glMaterialfv(GL_FRONT, GL_SPECULAR, material[2]);
glMaterialfv(GL_FRONT, GL_SHININESS, material[3] );
glLightfv(GL_LIGHT0, GL_AMBIENT, light[0]);
glLightfv(GL_LIGHT0, GL_DIFFUSE, light[1]);
glLightfv(GL_LIGHT0, GL_SPECULAR, light[2]);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glutSolidTeapot(100);
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
}
int main(int argc,char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE );
glutInitWindowPosition(100, 100);
glutInitWindowSize(width,height);
glutCreateWindow(*argv);
createMenu(menuCallback, GLUT_LEFT_BUTTON, "Black Plastic", "Brass", "Bronze", "Chrome", "Copper", "Gold", "Peweter", "Silver", "Polished Silver", NULL);
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
init();
glutMainLoop();
return 0;
}
You're drawing a solid teapot? I presume you enabled depth testing (though your glutInitDisplayMode lacks the depth buffer bit). Anyway, you should probably also clear the depth buffer. Right now you're clearing only the color buffer
glClear(GL_COLOR_BUFFER_BIT);
Change it into
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

Problem with GLUT

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

Resources