I'm trying to get this room to have a lightbulb effect but im not sure whats wrong with it. as of right now the light just seems to be shining on the top of the sphere and the side of the cone and the walls are not getting any of the light. i tried fiddling around with the position and direction but nothing is working.
#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
glClearColor(0,0,0,1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,1,1,100);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL);
//glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
//floor
glBegin(GL_POLYGON);
glColor3f(1,1,1);
glVertex3f(0,0,0);
glVertex3f(0,10,0);
glVertex3f(10,10,0);
glVertex3f(10,0,0);
glEnd(
);
//wall
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex3f(0,10,0);
glVertex3f(0,10,10);
glVertex3f(10,10,10);
glVertex3f(10,10,0);
glEnd();
//wall2
glBegin(GL_POLYGON);
glColor3f(0,1,0);
glVertex3f(10,10,0);
glVertex3f(10,10,10);
glVertex3f(10,0,10);
glVertex3f(10,0,0);
glEnd();
}
void drawObjects(){
//draw cone
glShadeModel(GL_FLAT);
glColor3f(1,0,1);
glTranslatef(2,2,0);
glutSolidCone(cone,5,10,2);
//draw sphere
glShadeModel(GL_SMOOTH);
glTranslatef(5,5,0);
glColor3f(1,0,0);
glutSolidSphere(sphere,500,500);
}
void move(unsigned char key, int x, int y){
switch(key){
case 'y':
viewy++;
glutPostRedisplay();
break;
case 'x':
viewx++;
glutPostRedisplay();
break;
case 'z':
viewz++;
glutPostRedisplay();
break;
case 'Z':
viewz--;
glutPostRedisplay();
glutPostRedisplay();
break;
//moves head
case 'd':
headup--;
headright--;
glutPostRedisplay();
break;
case 'a':
headup++;
headright++;
glutPostRedisplay();
break;
}
}
void display(){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
drawRoom();
GLfloat light_position[] = {5.0,5.0,10.0,0.0};
GLfloat light_direction[] = {-5,-5,-5};
GLfloat amb[] = {0,0,0,1};
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION,light_direction);
glLightfv(GL_LIGHT0, GL_AMBIENT,amb);
drawObjects();
glutSwapBuffers();
glFlush();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Room");
myinit();
glutDisplayFunc(display);
glutKeyboardFunc(move);
//glutMotionFunc(moveHead);
glutMainLoop();
return 0;
}
1st: You must supply normals.
2nd: The OpenGL fixed function pipeline evaluated illumination only at the vertices and then does barycentric color interpolation. Corners of a box with a light source somewhere inside receive the least light. With a box consisting of only 6 quads you will not get a nice falloff, but only dimly lit walls. You either have to use a fragment shader to implement per fragment lighting (recommended) or tesselate your quads down into a reasonable number of smaller quads forming the wall, so that there are enough interpolation sampling points, where illumination is evaluated.
Related
I do not understand how this main function works. I have a display function, which uses glDrawArrays, but I do not see it being called. I only see it being used as a parameter for glutDisplayFunction.
Here is my main:
int main(int argc, char** argv){
// Set up the window
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
glutInitWindowSize(800, 600);
glutCreateWindow("Hello Triangle");
// Tell glut where the display function is
glutDisplayFunc(display);
// A call to glewInit() must be done after glut is initialized!
GLenum res = glewInit();
// Check for any errors
if (res != GLEW_OK) {
fprintf(stderr, "Error: '%s'\n", glewGetErrorString(res));
return 1;
}
// Set up your objects and shaders
init();
// Begin infinite event loop
glutMainLoop();
return 0;
}
The problem is, I need to create two different triangles, on the same window, using seperate VAOs and VBOs. I've created the seperate VAO and VBO for my second triangle. However, I do not see how I am meant to generate and link my buffers, draw my arrays, switch to my second buffer, and draw those arrays, when I do not even know when my display function is being called.
My display function looks like this:
void display(){
glClear(GL_COLOR_BUFFER_BIT);
// NB: Make the call to draw the geometry in the currently activated vertex buffer. This is where the GPU starts to work!
glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}
All operations could be done in separate function named asyouwant called from main
example:
#include <GL/glut.h>
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello world :D");
glutDisplayFunc(displayMe); // = > draw in displayme function
glutMainLoop();
return 0;
}
void displayMe(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.5, 0.0, 0.0);
glVertex3f(0.5, 0.5, 0.0);
glVertex3f(0.0, 0.5, 0.0);
glEnd();
// a second geoform
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(-0.5, 0.0, 0.0);
glVertex3f(-0.5, -0.5, 0.0);
glVertex3f(0.0, -0.5, 0.0);
glEnd();
glFlush();
}
As complement: for VAO and buffer
1- Init (declare VAO , declare buffer of vertices, ...)
GLuint VaoID;
glGenVertexArrays(1, &VaoID);
glBindVertexArray(VaoID);
// An array of 3 vectors which represents 3 vertices
static const GLfloat g_vertex_buffer_data[] = {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
0.0f, 1.0f, 0.0f,
};
Once time only
// This will identify our vertex buffer
GLuint vertexbuffer;
// Generate 1 buffer, put the resulting identifier in vertexbuffer
glGenBuffers(1, &vertexbuffer);
// The following commands will talk about our 'vertexbuffer' buffer
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
// Give our vertices to OpenGL.
glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
2- Use it (bind and draw in display fucntion)
// 1st attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexAttribPointer(
0, // attribute 0. No particular reason for 0, but must match the layout in the shader.
3, // size
GL_FLOAT, // type
GL_FALSE, // normalized?
0, // stride
(void*)0 // array buffer offset
);
// Draw the triangle !
glDrawArrays(GL_TRIANGLES, 0, 3); // Starting from vertex 0; 3 vertices total -> 1 triangle
glDisableVertexAttribArray(0);
I'm creating a 3D game with OpenGL and I would like to make a toolbar in the top of the window. For that, I tried to use SDL to draw the buttons and OpenGL to draw the actual game. Here is the relevant part of my code:
void openMainWindow(){
SDL_Surface *screen;
SDL_Event event;
SDL_Rect position;
SDL_Init(SDL_INIT_VIDEO);
putenv("SDL_VIDEO_CENTERED=center");
SDL_WM_SetCaption("Example",NULL);
SDL_WM_SetIcon(IMG_Load("icon.png"),NULL);
screen = SDL_SetVideoMode(832,487,32,SDL_HWSURFACE | SDL_OPENGL);
glLoadIdentity();
gluPerspective(70,(double)832/487,1,1000);
//Some things to initialize the window
int continue = 1;
while(continue){
SDL_PollEvent(&event);
switch(event.type){
//Events
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//Draw a square
glEnd();
//Draw more things the same way
glFlush();
SDL_GL_SwapBuffers();
SDL_Surface *button1 = SDL_CreateRGBSurface(SDL_HWSURFACE,50,50,32,0,0,0,0);
SDL_FillRect(button1,NULL,SDL_MapRGB(screen->format,50,50,50);
position.x = 8;
position.y = 8;
SDL_BlitSurface(button1,NULL,screen,&position);
SDL_Flip(screen);
}
SDL_Quit();
}
The problem with this is that when this function is called, the process ends and returns 3 (which means that there is an error). So I tried to draw the buttons with OpenGL like this:
void openMainWindow(){
//Everything before the while loop is the same as in the other code
int continue = 1;
while(continue){
SDL_PollEvent(&event);
switch(event.type){
//Events
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//Draw a square
glEnd();
//Draw more things the same way
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glBegin(GL_QUADS); //Draw the button
glColor3ub(50,50,50);
glVertex2d(-0.5,-0.5);
glVertex2d(-0.5,0.5);
glVertex2d(0.5,0.5);
glVertex2d(0.5,-0.5);
glEnd();
glEnable(GL_DEPTH_TEST);
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
}
I know that the second code should center the button in the window, but I use this code just to test if it works (and it doesn't, that's why I'm posting this question).
With the second code, the 3D things appear in the window as they should, but I can't see any button. How do I put 2D buttons in a 3D OpenGL window?
The second code works by adding the following right before drawing the 2D button:
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
and the following code right after drawing the 2D button:
glEnable(GL_DEPTH_TEST);
gluPerspective(70,(double)640/480,0.5,INFINITE); //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D
gluLookAt(0,0,3,1,0,3,0,0,0.01); //Where you want to position the camera and where you want to look at
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
Here is the complete code that works:
void openMainWindow(){
//Everything before the while loop is the same as in the other code
int continue = 1;
while(continue){
SDL_PollEvent(&event);
switch(event.type){
//Events
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_QUADS);
//Draw a square
glEnd();
//Draw more things the same way
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glDisable(GL_DEPTH_TEST);
glLoadIdentity();
glBegin(GL_QUADS); //Draw the button
glColor3ub(50,50,50);
glVertex2d(-0.5,-0.5);
glVertex2d(-0.5,0.5);
glVertex2d(0.5,0.5);
glVertex2d(0.5,-0.5);
glEnd();
glEnable(GL_DEPTH_TEST);
gluPerspective(70,(double)640/480,0.5,INFINITE); //These parameters have to be the same as the ones used for gluPerspective when initializing the 3D
gluLookAt(0,0,3,1,0,3,0,0,0.01); //Where you want to position the camera and where you want to look at
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glFlush();
SDL_GL_SwapBuffers();
}
SDL_Quit();
}
I've been looking everywhere on how to do this and looked in the redbook of opengl and that only talks about smooth shading a 2d polygon. I'm not sure how you would do smooth shading for a glutSolidSphere. I know you have to do glShadeModel. and how can i tell the difference if its flat or smooth when there is no light for the scene.
#include <GLUT/glut.h>
#include <stdio.h>
#include <stdlib.h>
//Global variables
double sphere = 1, cone = 1, viewy = 2, viewx = -10, viewz = 5,headup = 5,headright = 5;
void myinit(){
glClearColor(0,0,0,1);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60,1,1,100);
//glOrtho(-2,20,-2,20,-10,10);
}
void drawRoom(){
//floor
glBegin(GL_POLYGON);
glColor3f(1,1,1);
glVertex3f(0,0,0);
glVertex3f(0,10,0);
glVertex3f(10,10,0);
glVertex3f(10,0,0);
glEnd(
);
//wall
glBegin(GL_POLYGON);
glColor3f(0,0,1);
glVertex3f(0,10,0);
glVertex3f(0,10,10);
glVertex3f(10,10,10);
glVertex3f(10,10,0);
glEnd();
//wall2
glBegin(GL_POLYGON);
glColor3f(0,1,0);
glVertex3f(10,10,0);
glVertex3f(10,10,10);
glVertex3f(10,0,10);
glVertex3f(10,0,0);
glEnd();
}
void drawObjects(){
//draw cone
glColor3f(1,0,1);
glTranslatef(2,2,0);
glutSolidCone(cone,5,10,2);
//draw sphere
glTranslatef(5,5,0);
glColor3f(1,0,0);
glutSolidSphere(sphere,500,500);
}
void move(unsigned char key, int x, int y){
switch(key){
case 'y':
viewy++;
glutPostRedisplay();
break;
case 'x':
viewx++;
glutPostRedisplay();
break;
case 'z':
viewz++;
glutPostRedisplay();
break;
//moves head
case 'd':
headup--;
headright--;
glutPostRedisplay();
break;
case 'a':
headup++;
headright++;
glutPostRedisplay();
break;
}
}
void display(){
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(viewx,viewy,viewz,headup,headright,5,0,0,1);
glClear(GL_COLOR_BUFFER_BIT);
drawRoom();
drawObjects();
glutSwapBuffers();
glFlush();
}
int main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("Room");
myinit();
glutDisplayFunc(display);
glutKeyboardFunc(move);
glutMotionFunc(moveHead);
glutMainLoop();
return 0;
}
You need to enable lighting, enable at least one light, and supply differing per-vertex normals for your geometry to see the effects of GL_SMOOTH.
...how can i tell the difference if its flat or smooth when there is no light for the scene.
You can retrieve the current shading model via glGetIntegerv():
GLenum shadeModel;
glGetIntegerv( GL_SHADE_MODEL, &shadeModel );
Here is a simple opengl program by me. I'm trying to clear the screen before I draw a triangle. I've called glClear() in my init() function, however, it seemed that it failed to clear the screen.
#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
void myIdleFunc()
{
glBegin(GL_TRIANGLES);
{
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
}
glEnd();
glFlush();
usleep(1000000);
}
void init()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutCreateWindow("Hello, World!");
init();
glutIdleFunc(myIdleFunc);
glutMainLoop();
return 1;
}
Here is a screen-shot, the text is from the gnome terminal in the back ground.
Where's your display callback? You shouldn't use the idle function for drawing.
All drawing needs to take place in the appropriate callbacks, the GL context might not be active until glutMainLoop starts running, and with no active context, your commands simply get ignored (without a context, there might not even be a place to store errors for retrieval with glGetError).
NOTE: Usually you want to clear the buffer at the beginning of every frame. You might get away with clearing just once with single-buffering, but double-buffering is better and requires you to somehow render the entire area between each swap.
Your problem is, that you do clear the screen in your initialization code. But you need to clear it every frame, so right at the start of your display (or in your case idle) function.
I don't use GLUT but a really simple way to display something like this is:
while( !done ) { /* Loop until done. Do drawing. */
glClear( GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_TRIANGLES);
{
glColor3f(1.0f, 1.0f, 1.0f);
glVertex2f(0.0f, 1.0f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
}
glEnd();
glFlush();
SwapBuffers( );
etc... event handling....
}
Run this code and probably you will get the solution.
#include<GL/gl.h>
#include<GL/glut.h>
#include<stdio.h>
double x_0 = -100;
double y_0 = -25;
double x_1 = 100;
double y_1 = -25;
double x_2 = 100;
double y_2 = 25;
double x_3 = -100;
double y_3 = 25;
void
init(void)
{
/*initialize the x-y co-ordinate*/
glClearColor(0,0,0,0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-320, 319,-240, 239);
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
void
drawRectangle()
{
glBegin(GL_LINES);
glVertex2d(x_0, y_0);
glVertex2d(x_1, y_1);
glVertex2d(x_1, y_1);
glVertex2d(x_2, y_2);
glVertex2d(x_2, y_2);
glVertex2d(x_3, y_3);
glVertex2d(x_3, y_3);
glVertex2d(x_0, y_0);
glEnd();
glFlush();
}
int
main(int argc, char *argv[])
{
double x_0, y_0, x_1, y_1;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(640, 480);
glutInitWindowPosition(400, 400);
glutCreateWindow("Clear Screen");
init();
drawRectangle();
/* clear the screen. You can uncomment following three lines to view the effect of clearing */
// glClearColor(0, 0, 0, 0);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// /* don't forget to flush */
// glFlush();
/***/
glutMainLoop();
}
Compile and run -
gcc file.c -lglut -lGLU -lGL
./a.out
I'm having trouble getting a texture loaded with SOIL to show up properly on this quad. In case it's not clear, I'm just writing up a little 2D sprite engine, and this is the rendering portion (needs a bit of optimization without a doubt). I haven't done any OpenGL in a couple months, and I'm admittedly quite rusty.
#include <OpenGL/OpenGL.h>
#include <GLUT/GLUT.h>
#include "SOIL.h"
#include <stdio.h>
GLuint linktex;
void drawSprite(GLint left, GLint right, GLint bottom, GLint top, GLuint texture){
//Draw clockwise
glColor3f(1.0, 1.0, 1.0);
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS);
glTexCoord2i(1,1); glVertex2i(right , top);
glTexCoord2i(1,0); glVertex2i(right , bottom);
glTexCoord2i(0,0); glVertex2i(left , bottom);
glTexCoord2i(0,1); glVertex2i(left , top);
glEnd();
}
void display(void){
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glOrtho(0.0, 240.0, 0.0, 160.0, -1.0, 1.0);
drawSprite(50, 82, 50, 82, linktex);
glFlush();
}
void reshape(int w, int h){
glViewport(0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
}
void init(){
glEnable(GL_TEXTURE_2D);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glFrontFace(GL_CW);
GLuint linktex = SOIL_load_OGL_texture(
"link.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
if( 0 == linktex )
{
printf( "SOIL loading error: '%s'\n", SOIL_last_result());
}
}
int main (int argc, char **argv) {
glutInit (&argc, argv);
glutInitDisplayMode (GLUT_SINGLE);
glutInitWindowSize (240, 160);
glutInitWindowPosition (100, 100);
glutCreateWindow ("Test");
glutDisplayFunc (display);
glutReshapeFunc (reshape);
glutMainLoop();
init();
return 0;
}
It looks like when you are loading the texture, you are assigning the id to a local variable linktex instead of the global you declared at the beginning of the file.
When you reference linktex in the void display(void); method, the texture is un-initialized.
Try changing your call to load the texture to :
// comment out the type declaration, to assign to the global instead of a local
/*GLuint*/ linktex = SOIL_load_OGL_texture(
"link.png",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
);