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.
Related
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;
}
#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);
I wrote some lines of code to draw a spinning rectangle:
#include "glut.h"
static GLfloat spin = 0.0; /* current angle */
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRotatef(spin, 0.0, 0.0, 1.0); /* rotate Oxyz (OZ) */
glColor3f(1.0, 1.0, 1.0); /* bgr-color: white; */
glRectf(-25.0, -25.0, 25.0, 25.0); /* Rectangle */
glPopMatrix();
glutSwapBuffers(); /* Swap 2 buffer */
}
void spinDisplay(void)
{
spin = spin + 2.0; /* Add 2 degrees for every loop action */
if (spin > 360.0)
spin = spin - 360.0;
glutPostRedisplay(); /* Alert: Redraw */
}
/* Change window mode */
void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h); /* change viewport */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-50.0, 50.0, -50.0, 50.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
/* Mouse interaction */
void mouse(int button, int state, int x, int y)
{
switch (button) {
case GLUT_LEFT_BUTTON: /* left mouse */
if (state == GLUT_DOWN)
glutIdleFunc(spinDisplay); /* idle mode switch to spinDisplay */
break;
//case GLUT_MIDDLE_BUTTON: /* middle mouse button */
//if (state == GLUT_DOWN)
//glutIdleFunc(NULL);
//break;
default:
break;
}
}
/* main */
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); /* double buffer */
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow ("spinning rectangle");
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape); /* reshape func (redraw) when window size changed */
glutMouseFunc(mouse); /* mouse func */
glutMainLoop();
return 0;
}
And I wanna save framebuffer to image (JPEG, BMP, TIFF) or file. Could you tell me how? Do I need to use some lib like jpeglib, libtiff?
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 );
Consider this code:
#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
double width=600;
double height=600;
void processMouse(int button, int state, int x, int y)
{
glColor4f(1.0,0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
static void render()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glutMouseFunc(processMouse);
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Board");
glutDisplayFunc(render);
glutMainLoop();
}
The render function is executed, and every time a click is performed, it should start the function processMouse.
So if the mouse is clicked, all the window should become red with the instructions:
glColor4f(1.0,0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
But when I click the mouse I notice a strange behaviour: only a part of the window gets colored, the part at bottom left (instead of all the screen).
The window remains in this state, until I open a google chrome window.If I open a google chrome (or another graphical application), all the window become colored of red.
Why this? I also have problems with more complex programs, it seems like sometime the glVertex instructions are ignored.If I try debugging the program with fprintf it appears that everything is ok, and everything seems to go like expected (for example I tried to print mouse coordinates in the processMouse function, they were ok), except for the fact that what I draw is ignored.
Edit:
I have modified this code but it still has the same problem:
#include <stdlib.h>
#include <stdarg.h>
#include <GLUT/GLUT.h>
#include <OpenGL/OpenGL.h>
double width=600;
double height=600;
bool down=false;;
// http://elleestcrimi.me/2010/10/06/mouseevents-opengl/
static void render()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
if(down)
{
glColor4f(1.0,0.0,0.0,0.0);
glBegin(GL_POLYGON);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(1.0, 0.0, 0.0);
glVertex3f(1.0, 1.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
glEnd();
glFlush();
}
}
void processMouse(int button, int state, int x, int y)
{
if(state==GLUT_DOWN)
{
down=true;
glutPostRedisplay();
}
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(width, height);
glutCreateWindow("Board");
glutMouseFunc(processMouse);
glutDisplayFunc(render);
glutMainLoop();
}
Still getting only a part of the screen red.
PS: Solved using glutSwapBuffers(), thanks.
When you are using double buffering with GLUT, you need to call glutSwapBuffers() to see the result of the draw.
Add this to the end of your render() function and it will work fine.