opengl: unable to draw left wall - c

I am trying to start building a very basic room using opengl. For some reason in the function drawWalls() the left wall doesn't get drawn or i'm misunderstanding the camera positioning. Does anyone have any idea why I can't see the left wall when running this code?
my code:
#include <gl/gl.h>
#include <gl/glut.h>
/*defining colors used in the room*/
GLfloat black[] = {0.0, 0.0, 0.0, 1.0};
GLfloat white[] = {1.0, 1.0, 1.0, 1.0};
GLfloat brown[] = {165.0/255.0, 42.0/255.0, 42.0/255.0, 1.0};
void drawFloor()
{
glBegin(GL_QUAD_STRIP);
glColor4fv(black);
glVertex3f(0.0, 0.0, -3.0);
glVertex3f(1.0, 0.0, -3.0);
glVertex3f(2.0, 0.0, -3.0);
glEnd();
}
void drawWalls()
{
glBegin(GL_QUAD_STRIP);
glColor4fv(white);
glPolygonMode(GL_FRONT, GL_FILL);
//draw left wall
glVertex3f(0.0, 0.0, 2.0);
glVertex3f(0.0, 1.0, 2.0);
glVertex3f(0.0, 0.0, 1.0);
glVertex3f(0.0, 1.0, 1.0);
glVertex3f(0.0, 0.0, 0.0);
glVertex3f(0.0, 1.0, 0.0);
//draw front wall
glVertex3f(0.0, 0.0, 3.0);
glVertex3f(0.0, 1.0, 3.0);
glVertex3f(1.0, 0.0, 3.0);
glVertex3f(1.0, 1.0, 3.0);
glVertex3f(2.0, 0.0, 3.0);
glVertex3f(2.0, 1.0, 3.0);
glEnd();
}
void display()
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0, 0.0, 0.0, 0.8, 0.0, 1.0, 0.0, 1.0, 0.0);
drawFloor();
drawWalls();
glFlush();
glutSwapBuffers();
}
void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60, (GLfloat) w / (GLfloat) h, 1, 500);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void idleFunc()
{
glutPostRedisplay();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500, 500);
glutInitWindowPosition(0, 0);
glutCreateWindow("My room");
glEnable(GL_DEPTH_TEST);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idleFunc);
glutMainLoop();
}

Why do you expect to see the left wall? It is off-screen (for any reasonable aspect ratios of your window). If we imagine some "world space", you'll have the folliwing situation: Your camera is placed at the origin and looking at almost diagonal vector. The lfet wall is placed at x=0 too, so you have almost 45 degrees between the view direction and the wall. Your frustum starts at distance 1 in front of the camera, and has a vertical FOV of 60 degrees, and the horizontal FOV is defined by the aspect ratio of the window. So if you make the window much wider than tall, you have a chance of seeing that wall.

Related

Extended Rotating Cube Example with Texturing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am currently working on an extension for a working rotation cube example.
But since there were much changes, because index_buffers are not a good idea for texture, I
have now a bug. I can't figure out why my cube consists only of some triangles now.
So I hope that someone else can find the problem.
For better understaning I put the whole code on this page.
https://www.dropbox.com/s/0mj5wb56rfzvpsx/Rotating_Cube.zip
GLfloat vertex_buffer_data[] = { /* 8 cube vertices */
-1.0, -1.0, 1.0,
1.0, -1.0, 1.0,
1.0, 1.0, 1.0,
-1.0, 1.0, 1.0,
-1.0, -1.0, -1.0,
1.0, -1.0, -1.0,
1.0, 1.0, -1.0,
-1.0, 1.0, -1.0,
};
GLushort index_buffer_data[] = { // Indices of 6*2 triangles
0, 1, 2,
2, 3, 0,
1, 5, 6,
6, 2, 1,
7, 6, 5,
5, 4, 7,
4, 0, 3,
3, 7, 4,
4, 5, 1,
1, 0, 4,
3, 2, 6,
6, 7, 3,
};
GLfloat UV[] = {
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0,
0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0,
1.0, 1.0
};
typedef struct {
GLfloat Position[3];
GLfloat UV[2];
} VertexData;
VertexData Vertices[index_size];
/*----------------------------------------------------------------*/
/******************************************************************
*
* Display
*
* This function is called when the content of the window needs to be
* drawn/redrawn. It has been specified through 'glutDisplayFunc()';
* Enable vertex attributes, create binding between C program and
* attribute name in shader
*
*******************************************************************/
void Display()
{
/* Clear window; color specified in 'Initialize()' */
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnableVertexAttribArray(vPosition);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vUV);
glBindBuffer(GL_ARRAY_BUFFER, TextureID);
glVertexAttribPointer(vUV, 2, GL_FLOAT, GL_FALSE, 0, 0);
/*glEnableVertexAttribArray(vColor);
glBindBuffer(GL_ARRAY_BUFFER, CBO);
glVertexAttribPointer(vColor, 3, GL_FLOAT,GL_FALSE, 0, 0);
*/
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, IBO);
//GLint size;
//glGetBufferParameteriv(GL_ELEMENT_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
/* Activate first (and only) texture unit */
glActiveTexture(GL_TEXTURE0);
/* Bind current texture */
glBindTexture(GL_TEXTURE_2D, TextureID);
/* Get texture uniform handle from fragment shader */
TextureUniform = glGetUniformLocation(ShaderProgram, "myTextureSampler");
/* Set location of uniform sampler variable */
glUniform1i(TextureUniform, 0);
/* Enable position and UV attribute */
glEnableVertexAttribArray(vPosition);
glEnableVertexAttribArray(vUV);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, sizeof(vertex_buffer_data), 0);
glVertexAttribPointer(vUV, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const GLvoid*) sizeof(Vertices[0].Position));
/* Associate program with shader matrices */
GLint projectionUniform = glGetUniformLocation(ShaderProgram, "ProjectionMatrix");
if (projectionUniform == -1)
{
fprintf(stderr, "Could not bind uniform ProjectionMatrix\n");
exit(-1);
}
glUniformMatrix4fv(projectionUniform, 1, GL_TRUE, ProjectionMatrix);
GLint ViewUniform = glGetUniformLocation(ShaderProgram, "ViewMatrix");
if (ViewUniform == -1)
{
fprintf(stderr, "Could not bind uniform ViewMatrix\n");
exit(-1);
}
glUniformMatrix4fv(ViewUniform, 1, GL_TRUE, ViewMatrix);
GLint RotationUniform = glGetUniformLocation(ShaderProgram, "ModelMatrix");
if (RotationUniform == -1)
{
fprintf(stderr, "Could not bind uniform ModelMatrix\n");
exit(-1);
}
glUniformMatrix4fv(RotationUniform, 1, GL_TRUE, ModelMatrix);
glDrawArrays(GL_TRIANGLES, 0, numVertices);
/* Disable attributes */
glDisableVertexAttribArray(vPosition);
glDisableVertexAttribArray(vUV);
/* Swap between front and back buffer */
glutSwapBuffers();
}
void SetupDataBuffers()
{
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(Vertices), Vertices, GL_STATIC_DRAW);
}
void getNewVertices() {
int i;
for(i=0; i < index_size; i++){
Vertices[i].Position[0] = vertex_buffer_data[3*index_buffer_data[i]+0];
Vertices[i].Position[1] = vertex_buffer_data[3*index_buffer_data[i]+1];
Vertices[i].Position[2] = vertex_buffer_data[3*index_buffer_data[i]+2];
Vertices[i].UV[0] = UV[2*i];
Vertices[i].UV[1] = UV[2*i+1];
}
}
First of all, your vertex pointers are wrong.
You are not using (a VBO named) TextureID anywhere, so remove all of this code:
glEnableVertexAttribArray(vPosition);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glVertexAttribPointer(vPosition, 3, GL_FLOAT, GL_FALSE, 0, 0);
glEnableVertexAttribArray(vUV);
glBindBuffer(GL_ARRAY_BUFFER, TextureID);
glVertexAttribPointer(vUV, 2, GL_FLOAT, GL_FALSE, 0, 0);
Replace it with:
glBindBuffer (GL_ARRAY_BUFFER, VBO);
glEnableVertexAttribArray (vPosition);
glEnableVertexAttribArray (vUV);
Finally, since you pack position and uv into an array of structs, this is what you want:
glVertexAttribPointer (vPosition, 3, GL_FLOAT, GL_FALSE, sizeof(VertexData), 0);
glVertexAttribPointer (vUV, 2, GL_FLOAT, GL_FALSE, sizeof(VertexData), (const GLubyte*)0 + offsetof(struct VertexData,UV));
That will fix everything I immediately see wrong with your code, there may still be other issues that I missed.

What is the use of reshape() function in glut?

How is the reshape() function working in this code and how is it getting its parameter from glutReshapeFunc(reshape) without any parameter in reshape of glutReshapeFunc(reshape)?
What is the value of int x, int y in void keyboard (unsigned char key, int x, int y) function?
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
static int year = 0, day = 0;
void init(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glShadeModel (GL_FLAT);
}
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glPushMatrix();
glutWireSphere(1.0, 20, 16); /* draw sun */
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutWireSphere(0.2, 10, 8); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
}
void reshape (int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
void keyboard (unsigned char key, int x, int y)
{
switch (key) {
case `d':
day = (day + 10) % 360;
glutPostRedisplay();
break;
case `D':
day = (day - 10) % 360;
glutPostRedisplay();
break;
case `y':
year = (year + 5) % 360;
glutPostRedisplay();
break;
case `Y':
year = (year - 5) % 360;
glutPostRedisplay();
break;
default:
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
init ();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
It appears that the glutReshapeFunc() function takes a pointer to a function; presumably, in fact, it is declared as something somewhat similar to:
void glutReshapeFunc(void (*function)(int x, int y));
Similarly, glutDisplayFunc() takes another pointer to a function, and glutKeyboardFunc() also takes a pointer to a function. When a function is specified by name without the function call parentheses after it, it reduces to 'pointer to a function' (or you can think of a bare function name as a pointer to the function body, like a bare array name is a pointer to the start of the array).
You'd have to read the manual to discover the purpose of the x and y parameters to the keyboard() function. They aren't used by the code shown. They are likely the position of something, but which something is less than clear without reading the manual.
reshape and keyboard functions are used as so called callbacks. You're giving GLUT pointers to those functions, GLUT keeps those pointers and calls those function, with parameters, at the times as specified in the GLUT documentation.
About like that:
void (*display_callback)(void);
void (*reshape_callback)(int, int);
void (*keyboard_callback(unsigned char, int, int);
/* ... */
void eventloop(...)
{
while(...) {
if( keyboard_event )
keyboard_callback(keyboard_event->key, mouse_x, mouse_y);
if( window_reshaped )
reshape_callback(window->width, window->height);
if( needs_redraw )
display_callback();
}
}
Now regarding what's done in the reshape callback: Everything that's placed there in beginner tutorials is actually much better done in the display function. Setting the viewport, setting the projection I mean. Later you'll probably want to draw a HUD, some text or a minimap, or a split view. And once you've reached that point, a reshape function doing viewport and projection setup becomes a liability. So get rid of it now.
void display(void)
{
int const w = glutGet(GLUT_WINDOW_WIDTH);
int const h = glutGet(GLUT_WINDOW_HEIGHT);
glClear (GL_COLOR_BUFFER_BIT);
glColor3f (1.0, 1.0, 1.0);
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode (GL_PROJECTION);
glLoadIdentity ();
gluPerspective(60.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glPushMatrix();
glutWireSphere(1.0, 20, 16); /* draw sun */
glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);
glTranslatef (2.0, 0.0, 0.0);
glRotatef ((GLfloat) day, 0.0, 1.0, 0.0);
glutWireSphere(0.2, 10, 8); /* draw smaller planet */
glPopMatrix();
glutSwapBuffers();
}
I hope this will be a direct answer to this question.
The reshape function is a call back function which is called whenever size or shape of the application window changes. Reshape function takes 2 arguments,they are width and height of reshaped window. Mainly these parameters are used to set a new viewport.

Enlarging a cube and putting it into a 3D space

I've created a program to display a lined cube on a white canvas but I am unsure how to multiple that cube into lets say? 10 x 10.
Another question is how would I go about creating the same cube in a 3D space?
Here's my code:
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("Box.cpp");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(KeyInput);
glutMainLoop();
return 0;
}
The glu library has a lot of useful tidbits like gluLookAt( xfrom, yfrom, zfrom, xto, yto, zto, xup, yup, zup );
You can scale things using glScaled( factor ) / glScalef( factor )
You should poke around Google for some GL 1.X documentation.

gluCylinder() how works OpenGL

I wanted to build a cricket ground with OpenGL. I made several polygons to indicate field, pitch and bowling lines. But the problem is when I am trying to use gluCylinder to make stumps I made depth glEnable(GL_DEPTH_TEST), but my polygons are then not working. I just want to know how Can I use gluCylinder to make stumps with those polygons I have made.
I have following code, but want to add stumps here but I cant,
#include <GL/gl.h>
#include <GL/glut.h>
static double deg=0.0;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT);
glRotatef(deg, 0.0, 0.0, 1.0); // Rotate by deg
// field
glColor3f (0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex3f (0, 0, 0.0);
glVertex3f (1, 0, 0.0);
glVertex3f (1, 0.75, 0.0);
glVertex3f (0.8, 0.82, 0.0);
glVertex3f (0.6, 0.85, 0.0);
glVertex3f (0.4, 0.85, 0.0);
glVertex3f (0.2, 0.82, 0.0);
glVertex3f (0.0, 0.75, 0.0);
glEnd();
// pitch
glColor3f (0.25, 0.30, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.5,0.65,0.0);
glVertex3f(0.47,0.35,0.0);
glVertex3f(0.60,0.35,0.0);
glVertex3f(0.57,0.65,0.0);
glEnd();
//ump line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.49,0.63,0.0);
glVertex3f(0.49,0.6315,0.0);
glVertex3f(0.58,0.6315,0.0);
glVertex3f(0.58,0.63,0.0);
glEnd();
//bat line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.46,0.40,0.0);
glVertex3f(0.46,0.4025,0.0);
glVertex3f(0.61,0.4025,0.0);
glVertex3f(0.61,0.40,0.0);
glEnd();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // "esc" on keyboard
exit(0);
break;
case 97: // "a" on keyboard
deg = deg+5.0;
glutPostRedisplay();
break;
case 100:
deg = deg-5.0;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
glEnable(GL_DEPTH_TEST) requires a depth buffer. Make sure to allocate one via oring in GLUT_DEPTH in your glutInitDisplayMode() call.
Make sure to clear your new depth buffer via oring in GL_DEPTH_BUFFER_BIT in your glClear() call.
#include <GL/glut.h>
static double deg=0.0;
void display(void)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glRotatef(deg, 0.0, 0.0, 1.0); // Rotate by deg
// field
glColor3f (0.0, 0.5, 0.0);
glBegin(GL_POLYGON);
glVertex3f (0, 0, 0.0);
glVertex3f (1, 0, 0.0);
glVertex3f (1, 0.75, 0.0);
glVertex3f (0.8, 0.82, 0.0);
glVertex3f (0.6, 0.85, 0.0);
glVertex3f (0.4, 0.85, 0.0);
glVertex3f (0.2, 0.82, 0.0);
glVertex3f (0.0, 0.75, 0.0);
glEnd();
// pitch
glColor3f (0.25, 0.30, 0.0);
glBegin(GL_POLYGON);
glVertex3f(0.5,0.65,0.0);
glVertex3f(0.47,0.35,0.0);
glVertex3f(0.60,0.35,0.0);
glVertex3f(0.57,0.65,0.0);
glEnd();
//ump line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.49,0.63,0.0);
glVertex3f(0.49,0.6315,0.0);
glVertex3f(0.58,0.6315,0.0);
glVertex3f(0.58,0.63,0.0);
glEnd();
//bat line
glColor3f (1.0, 1.0, 1.0);
glBegin(GL_POLYGON);
glVertex3f(0.46,0.40,0.0);
glVertex3f(0.46,0.4025,0.0);
glVertex3f(0.61,0.4025,0.0);
glVertex3f(0.61,0.40,0.0);
glEnd();
glPopMatrix();
glFlush ();
}
void init (void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyboard(unsigned char key, int x, int y)
{
switch (key) {
case 27: // "esc" on keyboard
exit(0);
break;
case 'a': // "a" on keyboard
deg = deg+5.0;
glutPostRedisplay();
break;
case 'z':
deg = deg-5.0;
glutPostRedisplay();
break;
}
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize (600, 600);
glutInitWindowPosition (100, 100);
glutCreateWindow ("hello");
init ();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0;
}
Be aware that GL_POLYGON only supports convex polygons.

fatal error C1083: Cannot open include file: 'aux.h': Permission denied

I copied the following code from OpenGL's Redbook (Chapter 4: Display Lists) into my editor (Visual Studio Express Edition 2008), named it list.c and got compilation error.
#include "aux.h"
#include <GL/gl.h>
#include <GL/glu.h>
GLuint listName = 1;
void myinit (void)
{
glNewList (listName, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glBegin (GL_TRIANGLES);
glVertex2f (0.0, 0.0);
glVertex2f (1.0, 0.0);
glVertex2f (0.0, 1.0);
glEnd ();
glTranslatef (1.5, 0.0, 0.0);
glEndList ();
glShadeModel (GL_FLAT);
}
void drawLine (void)
{
glBegin (GL_LINES);
glVertex2f (0.0, 0.5);
glVertex2f (15.0, 0.5);
glEnd ();
}
void display(void)
{
GLuint i;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
for (i = 0; i < 10; i++)
glCallList (listName);
drawLine ();
glFlush ();
}
void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat) w,
1.5 * (GLfloat) h/(GLfloat) w);
else
gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5,
1.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
auxInitDisplayMode (AUX_SINGLE | AUX_RGBA);
auxInitPosition (0, 0, 400, 50);
auxInitWindow (argv[0]);
myinit ();
auxReshapeFunc (myReshape);
auxMainLoop(display);
}
Don't use not aux, nor glaux. They're too old. GLUT is much more easier, while many of people use SDL or SFML (they're more flexible and feature-full than GLUT). You should try SFML - it has lots of features inside, it's lightweight and portable. You should be satisfied using this one =)
Still if you wanna use GLUT, here are your problems:
glutInit() should be called before
any other operations
glutInitDisplayMode() and other
window functions must be called
before any drawing
operations
Here's some sample code:
#include <GL/glut.h >
// rendering function
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0,1.0,1.0);
glBegin (GL_LINES);
glVertex2f (0.0, 0.5);
glVertex2f (15.0, 0.5);
glEnd ();
glFlush();
}
// some initializations
void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}
int main(int argc, char **argv)
{
// initialize GLUT library
glutInit(&argc, argv);
// set display mode
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// set initial window size
glutInitWindowSize(250,250);
// set initial window position
glutInitWindowPosition(100,100);
// create the window
glutCreateWindow("moofoo");
// do our initialization routines
init();
// set function which will be called each frame
glutDisplayFunc(display);
// enter main rendering loop
glutMainLoop();
return 0;
}
You need to include the Windows header file. Fix:
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
// etc...
I have changed the code placing glutXxx functions instead of auxXxx ones. It compiles but doesn't run, I'd appreciate feedback here:
#include "GL/glut.h"
#include <stdio.h>
//#include <windows.h>
GLuint listName = 1;
void myinit (void)
{
glNewList (listName, GL_COMPILE);
glColor3f(1.0, 0.0, 0.0);
glBegin (GL_TRIANGLES);
glVertex2f (0.0, 0.0);
glVertex2f (1.0, 0.0);
glVertex2f (0.0, 1.0);
glEnd ();
glTranslatef (1.5, 0.0, 0.0);
glEndList ();
glShadeModel (GL_FLAT);
}
void drawLine (void)
{
glBegin (GL_LINES);
glVertex2f (0.0, 0.5);
glVertex2f (15.0, 0.5);
glEnd ();
}
void display(void)
{
GLuint i;
glClear (GL_COLOR_BUFFER_BIT);
glColor3f(0.0, 1.0, 0.0);
for (i = 0; i < 10; i++)
glCallList (listName);
drawLine ();
glFlush ();
}
void myReshape(GLsizei w, GLsizei h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
gluOrtho2D (0.0, 2.0, -0.5 * (GLfloat) h/(GLfloat) w,
1.5 * (GLfloat) h/(GLfloat) w);
else
gluOrtho2D (0.0, 2.0 * (GLfloat) w/(GLfloat) h, -0.5,
1.5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
int main(int argc, char** argv)
{
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGBA);
glutInitWindowPosition (100, 100);
glutInitWindowPosition (100,100);
glutInit (&argc, argv);
myinit ();
glutDisplayFunc (display);
glutReshapeFunc (myReshape);
glutMainLoop();
}

Resources