OpenGL having trouble with Z-Buffer and Depth Test [duplicate] - c++

This question already has answers here:
Can't get depth testing to work in OpenGL
(2 answers)
Closed 3 years ago.
I've been struggling with OpenGL's Z-Buffer, i can't get it to work.
This is the code (i narrowed it down to the minimum necessary to show the problem):
#include <stdio.h>
#include <stdlib.h>
#include <glad\glad.h>
#include <SFML\Graphics.hpp>
#define WIDTH 800
#define HEIGHT 600
int main(void) {
sf::ContextSettings settings;
settings.antialiasingLevel = 16;
settings.majorVersion = 4;
settings.minorVersion = 6;
sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), "Test", sf::Style::Close, settings);
if (!gladLoadGL()) {
printf("COULD NOT INITALIZE OPENGL CONTEXT\n");
}
window.setActive(true);
window.setVerticalSyncEnabled(true);
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); // red
glVertex3f(-1.0f, -1.0f, 0.25f); // first triangle
glVertex3f( 1.0f, -1.0f, 0.25f);
glVertex3f( 0.0f, 1.0f, 0.25f);
glColor3f(0.0f, 0.0f, 1.0f); // blue
glVertex3f(-1.0f, 1.0f, 0.5f); // second triangle
glVertex3f( 1.0f, 1.0f, 0.5f);
glVertex3f( 0.0f, -1.0f, 0.5f);
glEnd();
window.display();
}
return(0);
}
Output:
The blue triangle is on top of the red one cause i drawed it for second, even though it should stay behind.
I first noticed this behaviour on my main OpenGL project where i would import 3D models and i would have triangles being rendered in front of others, suggesting that the Z-Buffer was not doing its thing.
I tried many solutions, such as enabling GL_CULL_FACE, playing around with the near plane from the projection matrix (on the 3d project), but nothing seems to work. Every fix i found online that works for almost everyone doesn't seem to work for me, so i'm kind of desperate...
If anyone knows the issue let me know!

As commented by #Quimby, the solution can be found here
The issue was that SFML needs you to set a depth buffer through the window settings.

It's been a while, but I think it's not the Z buffer issues. OpenGL uses right handed coordinate system and positive Z is away from the screen. So your blue triangle having larger z values would means it should be in front. Smaller z values should be farther away right? So this is correct behavior I believe.

This seems like correct output to me. OpenGL has flipped Z axis compared to DirectX. So +1 is front, -1 is back so the "camera" looks into the negative Z direction.

Related

OpenGL far clipping when using frustum

I'm using GLFW + OpenGL to try to make the "rotating cube". Although most of it is working, I have clipping in the far plane. I've tried changing values for frustum to very large numbers but it seems to have no effect.
int main(void) {
if (!glfwInit()) exit(EXIT_FAILURE);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
glfwWindowHint(GLFW_SAMPLES, 4); // 4x antialiasing
GLFWwindow* window = glfwCreateWindow(640, 360, "3dTest", NULL, NULL);
if (!window) {
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glClearColor(0.5f, 0.5f, 0.5f, 1.0f); // Grey Background
float rotqube = 0;
while (!glfwWindowShouldClose(window)) {
// clear color and depth buffer for new frame
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// set up camera
glViewport(0, 0, 640, 360);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-100.0, 100.0, -100.0, 100.0, 100.0, -100.0);
// position camera
glTranslatef(0.0f, 0.0f, -2.0f); // Translate Into The Screen 2.0 Units
glRotatef(rotqube, 0.0f, 1.0f, 0.0f); // Rotate The cube around the Y axis
glRotatef(rotqube, 1.0f, 1.0f, 1.0f);
glBegin(GL_QUADS); // Draw The Cube Using quads
glColor3f(0.0f, 1.0f, 0.0f); // Color Blue
glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)
...
glColor3f(1.0f, 0.0f, 1.0f); // Color Violet
glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
glEnd(); // End Drawing The Cube
rotqube += 0.3f;
//Swap buffer and check for events
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwDestroyWindow(window);
glfwTerminate;
exit(EXIT_SUCCESS);
return 0;
}
This is what it looks like:
You are not using a perspective projection at all. Your call
glFrustum(-100.0, 100.0, -100.0, 100.0, 100.0, -100.0);
has no effect whatsever, besides setting the GL_INVALID_VALUE error state.
As stated in the OpenGL 2.0 specification, section 2.11 "Coordinate Transformations":
For
void Frustum( double l, double r, double b, double t, double n, double f );
the coordinates (l, b, −n)^T and (r, t, −n)^T
specify the points on the near clipping
plane that are mapped to the lower left and upper right corners of the window,
respectively (assuming that the eye is located at (0, 0, 0)^T). f gives the distance
from the eye to the far clipping plane. If either n or f is less than or equal to zero,
l is equal to r, b is equal to t, or n is equal to f, the error INVALID_VALUE results.
Trying to set a projection where one of the near or far planes lies behind the camera does not make the slightest sense, and would result in a lot of mathematical oddities during rendering (i.e division by zero for vertices lying on the camera plane), hence it is not allowed.
Since this function fails with an error, you are using the identity matrix as the projection matrix, and do end up with a orthographic projection.
Now having written all that, I must make you aware that all of this is completely outdated. The fixed function pipeline and the GL matrix stack, including functions like glFrustum, glLoadIdendity, glRotate, and immediate mode rendering using glBegin/glEnd are deprecated and have been removed form core profiles of OpenGL almost a decade ago. It is a really bad idea to try to learn this stuff in 2018, and I strongly advice you to learn modern OpenGL instead.
glFrustum(-100.0, 100.0, -100.0, 100.0, 100.0, -100.0);
^ wat
glFrustum(GLdouble left, GLdouble right, GLdouble bottom, GLdouble top, GLdouble nearVal, GLdouble farVal):
Parameters:
left, right:
Specify the coordinates for the left and right vertical clipping planes.
bottom, top:
Specify the coordinates for the bottom and top horizontal clipping planes.
nearVal, farVal:
Specify the distances to the near and far depth clipping planes.
Both distances must be positive.
Try something like 0.1 to 100.0:
glFrustum(-100.0, 100.0, -100.0, 100.0, 0.1, 100.0);

gluLookAt not working properly after drawing shapes

I am really confused by how gluLookAt and glOrtho or gluPersective work together. Here is the problem.
I draw a 2D triangle and a 2D pentagon in the z-axis of -5.
//pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
And Then I define my camera position (0,0,-10) and perspective
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
gluLookAt(0, 0, -10, 0, 0, -200, 0, 1, 0);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
5.2); //The far z clipping coordinate
Based on my understanding, I can see nothing in the scene. Because objects are defined in the -5 z-axis, however, the camera is at -10 z-axis, and it looks into negative z-axis. Thus, the object should be behind the camera. But why I can still see the objects in the scene?
Similarly, I can still see the object when I define my camera at postive 5 at look towards positive z-axis. Why?
Another question is why I can see the objects after I set the far z clipping coordinate to 5?
Anyone can explain this?
My full code:
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <stdlib.h>
#include <GL/glut.h>
#include <math.h>
#include <iostream>
#include <stdlib.h> //Needed for "exit" function
//Include OpenGL header files, so that we can use OpenGL
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
gluLookAt(0, 0, -10, 0, 0, -200, 0, 1, 0);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
5.2); //The far z clipping coordinate
}
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex3f(-0.7f, -1.5f, -5.0f);
glVertex3f(0.7f, -1.5f, -5.0f);
glVertex3f(0.4f, -0.5f, -5.0f);
glVertex3f(-0.4f, -0.5f, -5.0f);
glEnd(); //End quadrilateral coordinates
glBegin(GL_TRIANGLES); //Begin triangle coordinates
//Pentagon
glVertex3f(0.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 0.5f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(0.5f, 1.0f, -5.0f);
glVertex3f(1.5f, 1.0f, -5.0f);
glVertex3f(1.0f, 1.5f, -5.0f);
//Triangle
glVertex3f(-0.5f, 0.5f, -5.0f);
glVertex3f(-1.0f, 1.5f, -5.0f);
glVertex3f(-1.5f, 0.5f, -5.0f);
glEnd(); //End triangle coordinates
glutSwapBuffers(); //Send the 3D scene to the screen
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("Basic Shapes - videotutorialsrock.com");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene);
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
You're saying:
I draw a 2D triangle and a 2D pentagon in the z-axis of -5
...
And Then I define my camera position (0,0,-10) and perspective
If that is really the order you're doing the operations in, then that's the culprit. OpenGL is a command-based API, not a scene graph. Calling glVertex(x, y, z) does not mean "there is a vertex with coordinates x, y, z in the scene." It means "now go and draw a verex on coordinates x, y, z." This means the vertex is transformed by the modelview and projection matrix active at the time of the glVertex() call.
In other words, you issue the vertices with the default modelview and projection matrices, so they get drawn on the screen normally. Then you change the modelview and projection; if you then went on to issue any more vertices, they would be transformed by these new modelview and projection values. But the ones issued previously are already on the screen and unaffected.
In other words, remove these two lines from your draw function:
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
The comments seem to indicate the issue rather well.
The most common setup is to define the projection matrix in the resize hook (as it depends on the aspect ratio), and the view matrix (=camera position and orientation) in the draw function, before issuing any render commands.
As #datenwolf correctly pointed out in comments, this is the common setup for single view rendering. If you have more than one viewport, each of them will probably need a different projection matrix, in which case you would set up the projection matrix along with the view matrix in rendering code, before issuing primitives.

OpenGL c++ - unwanted black triangle appears

I started working on a simple GUI application using OpenGL. Drawing a simple background quad makes a lot of difficulties and I just can't spot what I am doing wrong or what's 'broken'. Here is the part of the code which is responsible for drawing my background 'quad':
static void Draw(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(0.5f, 0.5f, 0.5f);
glBegin(GL_QUADS);
glVertex2f(-1.0f, -1.0f);
glVertex2f(1.0f, -1.0f);
glVertex2f(-1.0f, 1.0f);
glVertex2f(1.0f, 1.0f);
glEnd();
glutSwapBuffers();
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Pro Sound");
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glutDisplayFunc(Draw);
glutKeyboardFunc(keyPressed);
glutKeyboardUpFunc(keyUp);
glutMainLoop();
}
The output looks like a gray quad, but on top of it (right side) there is a black triangle with vertices on the centre, top right corner and bottom right corner. So my background looks like bitten by a triangle-teeth pixel monster. The thing is, it recently worked properly, I even could add some textures, position it well etc.
It looks like you are drawing the quad vertices in the wrong order. Vertices should be ordered, by default, in a counter-clockwise fashion along the perimeter of the polygon.
So what you have is:
(-1.0f, -1.0f) Bottom-Left
( 1.0f, -1.0f) Bottom-Right
(-1.0f, 1.0f) Top-Left
( 1.0f, 1.0f) Top-Right
which is creating:
When you want:
(-1.0f, -1.0f) Bottom-Left
( 1.0f, -1.0f) Bottom-Right
( 1.0f, 1.0f) Top-Right
(-1.0f, 1.0f) Top-Left
which creates:
See also Face Culling and Primitive - Quads
Instead of using GL_QUADS and changing your coordinates, keep your coordinates and use GL_TRIANGLE_STRIP.
I'm fairly certain GL_QUADS is deprecated, and it seems to be good practice to use GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN whether you are drawing quads or not.
The only time you should use GL_QUADS is when you are in immediate mode and sketching up a small program in which performance does not matter. (This may be one of those times; but for reinforcement and good measure, I always use GL_TRIANGLE_STRIP or GL_TRIANGLE_FAN.)

Drawing tetrahedron in openGL + SDL 2.0

So I'm pretty new to openGL programming and am just going over the basics for now. I know I should be using VBOs and stuff but wanted to get a little foundation first. I wont present you with all the code just the stuff that draws and sets the scene.
Heres a little code for setting up my camera:
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(70, width / height, 1, 1000);
glEnable(GL_DEPTH_TEST);
// Move the camera back to view the scene
glTranslatef(0.0f, 0.0f, -5.0f);
I tried to create it around the origin like so (also I never draw the bottom face) :
void drawtetrahedron(GLfloat angle)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(angle, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); //FRONT
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 1.0f, 0.0f); //RIGHT
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glColor3f(0.0f, 0.0f, 1.0f); //LEFT
glVertex3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(0.0f, -1.0f, -1.0f);
glEnd();
}
When my window first comes up the red triangle looks fine, but as I rotate it the shape looks a little distorted. If I rotate all the way around (where I cant see the red face at all) it looks normal... What am I missing here?
Heres where it starts to look weird
Also any pointers on openGL stuff I'm doing incorrectly (or in general) are greatly appreciated! :D
I don't know if this is what you consider a wierd looking shape, but your shape doesn't seem to be a regular Tetrahedron:
The 3 Corners of the base don't have the same distance to the top corner (the two front corners have a distance of sqrt(6) to the top corner, while the back corner has a distance of sqrt(5)).
the distance on the base is off too: the front corners have a distance of sqrt(2) while the distance between any front corner and the back corner is sqrt(3).
An example for a regular tetrahedron would be:
(Please note that these coordinates don't have a base parallel to the xz plane)
(1,1,1)(1,-1,-1)(-1,1,-1)(-1,-1,1)
Your code itself looks to be ok. (Except for the translating the projection matrix) I, myself prefer to create code blocks after push/popmatrix and glbegin/end (these things { ... }), but that's just to keep my code easy to read.
Also, as a general rule of thumb, in opengl you don't move the camera: you move everything else. (That's why translating negative z moves objects away from you, translating positive x makes them move right and so on...)

OpenGL on Fedora 15

I am trying to program in OpenGL.
so wrote a test program call t_gl1.cpp
I built it successfully with
$ g++ t_gl1.cpp -lglut -lGL -lGLU -o t_gl1
No any error.
However, if I try to run it, I got
freeglut (./t_gl1): ERROR: Internal error in function fgOpenWindow
X Error of failed request: BadWindow (invalid Window parameter)
Major opcode of failed request: 4 (X_DestroyWindow)
Resource id in failed request: 0x0
Serial number of failed request: 26
Current serial number in output stream: 29
Does any one know what is going on?
Here is the code, tested on Windows and Mac, no problem. But can't get it run on Fedora nor Ubuntu
#include <iostream>
#include <stdlib.h>
#ifdef __APPLE__
#include <OpenGL/OpenGL.h>
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
using namespace std;
//Called when a key is pressed
void handleKeypress(unsigned char key, //The key that was pressed
int x, int y) { //The current mouse coordinates
switch (key) {
case 27: //Escape key
exit(0); //Exit the program
}
}
//Initializes 3D rendering
void initRendering() {
//Makes 3D drawing work when something is in front of something else
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //NEW OF THIS
glClearColor(0.7f,0.9f,1.0f,1.0f); //background, last number to be 1.0f
}
//Called when the window is resized
void handleResize(int w, int h) {
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity(); //Reset the camera
gluPerspective(45.0, //The camera angle
(double)w / (double)h, //The width-to-height ratio
1.0, //The near z clipping coordinate
200.0); //The far z clipping coordinate
}
float _angle=30.0f; //kinda global variable
//Draws the 3D scene
void drawScene() {
//Clear information from last draw
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
glLoadIdentity(); //Reset the drawing perspective
glTranslatef(0.0f,0.0f,-5.0f);
glPushMatrix();
glRotatef(_angle, 0.0f, 1.0f , 0.0f);
glColor3f(0.5f,0.0f,0.8f);
glBegin(GL_QUADS); //Begin quadrilateral coordinates
//Trapezoid
glVertex3f(-0.7f, -1.5f, 0.0f);
glVertex3f(0.7f, -1.5f, 0.0f);
glVertex3f(0.4f, -0.5f, 0.0f);
glVertex3f(-0.4f, -0.5f, 0.0f);
glEnd(); //End quadrilateral coordinates
glPopMatrix();
glPushMatrix(); //push
glRotatef(_angle, 1.0f, 0.0f, 0.0f);
glBegin(GL_TRIANGLES); //Begin triangle coordinates Begin Pentagon
glColor3f(1.0f,0.0f,0.0f);
//Pentagon
glVertex3f(0.5f, 0.5f, -0.0f);
glVertex3f(1.5f, 0.5f, -0.0f);
glVertex3f(0.5f, 1.0f, -0.0f);
glVertex3f(0.5f, 1.0f, -0.0f);
glVertex3f(1.5f, 0.5f, -0.0f);
glVertex3f(1.5f, 1.0f, -0.0f);
glVertex3f(0.5f, 1.0f, -0.0f);
glVertex3f(1.5f, 1.0f, -0.0f);
glVertex3f(1.0f, 1.5f, -0.0f);
glEnd(); //end Pentagon
glPopMatrix(); //pop
glPushMatrix();
glRotatef(_angle, -1.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
//Triangle
glVertex3f(-0.5f, 0.5f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex3f(-1.0f, 1.5f, -0.0f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex3f(-1.5f, 0.5f, -0.0f);
glEnd(); //End triangle coordinates
glPopMatrix();
glutSwapBuffers(); //Send the 3D scene to the screen
}
void update(int value)
{
_angle+=2.0f;
if(_angle>360)
_angle-=360;
glutPostRedisplay();
glutTimerFunc(25,update,0);
}
int main(int argc, char** argv) {
//Initialize GLUT
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(400, 400); //Set the window size
//Create the window
glutCreateWindow("window");
initRendering(); //Initialize rendering
//Set handler functions for drawing, keypresses, and window resizes
glutDisplayFunc(drawScene); //display the "drwwScene" most important part, others are settings
glutKeyboardFunc(handleKeypress);
glutReshapeFunc(handleResize);
glutTimerFunc(25,update,0); //add the timer function to make animation
glutMainLoop(); //Start the main loop. glutMainLoop doesn't return.
return 0; //This line is never reached
}
I aggreed with talonmies.
It was because of the Nvidia problem. I could't find a official way to install Nvidia's developer's driver on Fedora 15. So I used rpmfusion. The driver runs and I can run CUDA. Only GLX related stuff are messed up. However, it also messed up Gnome 3.
So I switched to Arch Linux. With a much clean start, install Nvida and install Xorg according to the ArchWiki. And then install Gnome. Amazingly, OpenGL works. I guess the driver in Arch repo is not the developer's driver. Maybe just for display driver. However, CUDA works.
I am glad with that. And hopefully this will be helpful to some one else who want to run CUDA and OpenGL on Linux.
Thanks,
Alfred
There is nothing wrong with the code. I can successfully compile and run it on a properly configured 64 bit linux system with NVIDIA release 280.13 drivers. You have a driver or X11 driver installation or configuration problem. There is no programming help required for this question.
Since you tagged this question cuda I'm going to assume you're running NVidia hardware.
Try installing some drivers.