You requested a double buffered window, so you have to do a doublebuffer swap when you're done drawing. Replace that glFinish()
in your display function with glutSwapBuffers()
.
Issue displaying OpenGL terrain mesh in C
Question
I am having issues making my openGL terrain display in the window. I just get a see-through window, and I'm not sure what I'm missing.
Here is my code:
#ifdef __APPLE__
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
#include <stdlib.h>
#define MAP_SCALE 20.0f
#define MAP_X 5
#define MAP_Z 5
int height[5][5] ={
{ 0, 0, 1, 1, 2 },
{ 0, 1, 2, 2, 3 },
{ 0, 1, 3, 2, 3 },
{ 0, 1, 2, 1, 2 },
{ 0, 0, 1, 1, 1 } };
float terrain[5][5][3];
int x, z;
float aspect = 1.0f;
void initializeTerrain()
{
for (z = 0; z < MAP_Z; z++)
{
for (x = 0; x < MAP_X; x++)
{
terrain[x][z][0] = (float)(x);
terrain[x][z][1] = (float)height[x][z];
terrain[x][z][2] = -(float)(z);
}
}
}
void display(void){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glColor3f(1.0, 0.0, 0.0);
for (z = 0; z < MAP_Z-1; z++)
{
glBegin(GL_TRIANGLE_STRIP);
for (x = 0; x < MAP_X-1; x++)
{
glVertex3f(terrain[x][z][0], terrain[x][z][1], terrain[x][z][2]);
glVertex3f(terrain[x+1][z][0], terrain[x+1][z][1], terrain[x+1][z][2]);
glVertex3f(terrain[x][z+1][0], terrain[x][z+1][1], terrain[x][z+1][2]);
glVertex3f(terrain[x+1][z+1][0], terrain[x+1][z+1][1], terrain[x+1][z+1][2]);
}
glEnd();
}
glFlush();
}
void myInit(){
initializeTerrain();
glMatrixMode(GL_PROJECTION);
glViewport(0, 0, 500, 500);
gluLookAt(1,1,0, 0,0,0, 0.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glClearColor(0.0, 0.0, 1.0, 1.0);
}
void reshape(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
if (w <= h)
glOrtho(-10.0, 10.0, -5.0 * (GLfloat) h / (GLfloat) w,
15.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);
else
glOrtho(-10.0 * (GLfloat) w / (GLfloat) h,
10.0 * (GLfloat) w / (GLfloat) h, -5.0, 15.0, -10.0, 10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void main(int argc, char** argv){
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(500, 500);
glutCreateWindow("A3");
myInit();
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutMainLoop();
}
When I resize, it fills with white, but I don't get the mesh. Can anyone help?
Thanks!
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow