I am learning OpenGL these days, and I tried to compile the example code on the book(OpenGL SuperBible)
The code likes this: first use glEnable(GL_LINE_STIPPLE) to open the GL_LINE_STIPPLE, and then glLineStipple(2, (GLushort)0x00ff), last I draw some lines, but when executed, it just displayed the normal lines. (in Ubuntu)
However, I compiled the same code in windows, it worked!!
Why? Are there any different details between Windows and Linux?
#include <QtGui>
#include "GLWidget.h"
GLWidget::GLWidget(QWidget *parent)
: QGLWidget(parent)
{
setFormat(QGLFormat(QGL::DoubleBuffer));
}
GLWidget::~GLWidget()
{
}
void GLWidget::initializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glEnable(GL_LINE_STIPPLE);
}
void GLWidget::resizeGL(int w, int h)
{
if(h == 0)
h = 1;
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100, 100, -100, 100, -1, +1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
drawLine();
}
void GLWidget::drawLine()
{
GLint factor = 1;
GLushort pattern = 0x00ff;
glColor3f(1.0f, 1.0f, 1.0f);
for(GLfloat i = -90.0f; i < 90.0f; i += 20.0f)
{
glLineStipple(factor, pattern);
glLineWidth(5.0);
glBegin(GL_LINES);
glVertex2f(-80.0f, i);
glVertex2f(+80.0f, i);
glEnd();
factor++;
}
}
I've seen from your comment answer to #Lefteris that you're using the Mesa3D/DRI based "radeon" driver. I suggest you download AMD's propritary driver (fglrx) fron their website, install that and try again.
You would have to tell us what OpenGL context you are using to see why it does not work. If in Linux it is an openGL 4 context for example glEnable(GL_LINE_STIPPLE) is not a part of openGL 4.
On the other hand in Windows if you don't bother to create a specific openGL context you get the default which is a version that definitely supports glEnable(GL_LINE_STIPPLE).
So please tell us what OpenGL context you are using in Linux.
Stippled primitives are TODO for anything above a R200.
glLineStipple is a deprecated API so perhaps it was removed from the Linux driver.
Related
This question already has answers here:
OpenGL texture mapping stubbornly refuses to work
(4 answers)
Closed 6 years ago.
I am trying to create an application that all it does is display an image full screen and then flash through a sequence of images quickly (144hz) repeatedly. I have just started looking at OpenGL, have done a few tutorials and cannot figure out what I'm doing wrong here. The part that I'm getting stuck on is actually rendering the image to to the display as it only shows up as a white square. I have gone through other stack overflow questions for this but none of the suggestions have worked for me.
I am doing this in Visual Studio 2015, using a win32 application and have installed the NupenGL package. For testing purposes, I am using a 256x256 bitmap image and am loading it through the SOIL library which I have built and statically linked.
I was originally thinking that I did not building/linking the SOIL library properly so something funky was going on trying to load the image. I created a custom BMP loader which didn't work and I also tried other peoples BMP loaders on stack overflow to no avail. I now believe that it is not the loading of the texture but that I am messing up something when actually trying to render it. Also in my code below I output if the texture is invalid but it always comes back good.
Output (FULLSCREEN):
Output (WINDOWED):
My Code:
#include <gl/freeglut.h>
#include <stdio.h>
#include <iostream>
#include "SOIL.h"
void init();
void display(void);
void keyPressed(unsigned char key, int x, int y);
void resize(int heightY, int widthX);
// define the window position on screen
int window_x;
int window_y;
// variables representing the window size
int window_width = 480;
int window_height = 480;
// variable representing the window title
char *window_title = "Resolution Enhancement via Display Vibrations";
bool fullscreen = false;
//-------------------------------------------------------------------------
// Program Main method.
//-------------------------------------------------------------------------
void main(int argc, char **argv)
{
// Connect to the windowing system + create a window
// with the specified dimensions and position
// + set the display mode + specify the window title.
glutInit(&argc, argv);
glutInitWindowSize(window_width, window_height);
glutInitWindowPosition(window_x, window_y);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow(window_title);
glutFullScreen();
// Setup keyPressed
glutKeyboardFunc(keyPressed);
// Handler for when the screen resizes
glutReshapeFunc(resize);
// Set OpenGL program initial state.
init();
// Set the callback functions
glutDisplayFunc(display);
// Start GLUT event processing loop
glutMainLoop();
}
//-------------------------------------------------------------------------
// Set OpenGL program initial state.
//-------------------------------------------------------------------------
void init()
{
// Set the frame buffer clear color to black.
glClearColor(0.0, 0.0, 0.0, 0.0);
}
//-------------------------------------------------------------------------
// This function is passed to glutDisplayFunc in order to display
// OpenGL contents on the window.
//-------------------------------------------------------------------------
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -5.0f);
GLuint texture = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
(
"C:/Users/joeja/Desktop/Grass.bmp",
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
);
if (texture == 0) {
std::cout << "Texture not found!\n" << std::endl;
}
else
{
std::cout << "Texture is good\n" << std::endl;
}
glBindTexture(GL_TEXTURE_2D, texture);
glBegin(GL_QUADS); // front face
glTexCoord2f(0.0f, 0.0f); glVertex3f(0.5f, -0.5f, 0.5f);
glTexCoord2f(1.0f, 0.0f); glVertex3f(0.5f, 0.5f, 0.5f);
glTexCoord2f(1.0f, 1.0f); glVertex3f(-0.5f, 0.5f, 0.5f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-0.5f, -0.5f, 0.5f);
glEnd();
glutSwapBuffers();
}
void resize(int heightY,int widthX) {
const float ar = (float)widthX / (float)heightY;
glViewport(0, 20, widthX, heightY);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-ar + 1, ar - 1, -1.0, 1.0, 2.0, 90.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void keyPressed(unsigned char key, int x, int y) {
switch (key) {
case 27:
case 70:
case 102: /* Fullscreen mode (Additional) : f/F */
fullscreen = !fullscreen;
if (fullscreen)
{
glutFullScreen(); /* Go to full screen */
}
else
{
glutReshapeWindow(800, 600); /* Restore us */
glutPositionWindow(0, 0);
}
break;
}
}
Do not load the image from file every frame.
In your init you should:
load the image with SOIL like you already are, storing the ID as texture
In display you should,
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D)
draw stuff
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
You may notice that you could skip some enables/disables by just enabling texture 2D and leaving it on. I gave pseudocode that tries to always work, skipping redundant state changes is an optimization not relevant to the problem.
My question is how to speed up drawing on OpenGL on windows.
The test code is below. I copied it from some cairo example on the web.
the fps drop to 30 to 40 per second, even slower than a web browser.
just draw line every frame, I tried write javascript on html5. The same function just draws a line, and it runs much faster.
why cairo draw line on opengl so slow? Did I do something wrong?
and how can I speed it up?
I think c++ should be much faster than javascript
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
#define _USE_MATH_DEFINES
#include <math.h>
#include <iostream>
#include <chrono>
#include <random>
#include <gl/glut.h>
#include <gl/glext.h>
#include <cairo.h>
using namespace std;
double win_width = 800;
double win_height = 600;
double hw = win_width / 2;
double hh = win_height / 2;
double line_width = 1;
//double line_width = 1 / win_width;
cairo_surface_t * surf = NULL;
cairo_t * cr = NULL;
unsigned char * surf_data = NULL;
GLuint texture_id;
// Interface //
void opengl_init(void)
{
printf("OpenGL version: %s\n", glGetString(GL_VERSION));
printf("OpenGL vendor: %s\n", glGetString(GL_VENDOR));
printf("OpenGL renderer: %s\n", glGetString(GL_RENDERER));
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
}
void opengl_cleanup(void)
{
glDeleteTextures(1, &texture_id);
}
void opengl_draw(int width, int height, unsigned char * surf_data)
{
if (!surf_data)
{
printf("draw_func() - No valid pointer to surface-data passed\n");
return;
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,
0,
GL_RGBA,
width,
height,
0,
GL_BGRA,
GL_UNSIGNED_BYTE,
surf_data);
glColor3f(0.25f, 0.5f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f((GLfloat)width, 0.0f);
glVertex2f(1.0f, 0.0f);
glTexCoord2f((GLfloat)width, (GLfloat)height);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, (GLfloat)height);
glVertex2f(0.0f, 1.0f);
glEnd();
glPopMatrix();
}
void opengl_resize(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glDeleteTextures(1, &texture_id);
glGenTextures(1, &texture_id);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id);
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,
0,
GL_RGBA,
width,
height,
0,
GL_BGRA,
GL_UNSIGNED_BYTE,
NULL);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
}
void drawShape()
{
//save current brush
cairo_save(cr);
// clear background
cairo_set_operator(cr, CAIRO_OPERATOR_OVER);
//cairo_scale(cr, (double)win_height / 1.0f, (double)win_height / 1.0f);
cairo_set_source_rgba(cr, 1, 1, 1, 1);
cairo_paint(cr);
//set line color and style
cairo_set_line_cap(cr, CAIRO_LINE_CAP_ROUND);
cairo_set_line_width(cr, line_width);
static double angle = 0;
angle += 0.01f;
//draw rect
cairo_set_source_rgba(cr, 1, 0, 0, 1);
//cairo_rectangle(cr, 0.5f + sinf(angle) * 0.1f, 0.5f, 0.1f, 0.1f);
cairo_rectangle(cr, hw + sin(angle) * 100, hh, 100, 100);
cairo_fill(cr);
cairo_stroke(cr);
//draw circle
cairo_set_source_rgba(cr, 0, 0, 1, 1);
cairo_arc(cr, 300, hh, 100, 0, 2 * M_PI);
//cairo_fill(cr);
cairo_stroke(cr);
//draw line
static double r = 100;
static double posx = 500;
static double posy = 500;
static double x = 0;
static double y = 0;
x = r * cosf(angle);
y = r * sinf(angle);
cairo_set_source_rgba(cr, 0, 1, 0, 1);
cairo_move_to(cr, x + posx, y + posy);
cairo_line_to(cr, -x + posx, -y + posy);
cairo_stroke(cr);
int minx = 5;
int maxx = win_width - 5;
int miny = 5;
int maxy = win_height - 5;
int n = 50 * 2;
std::default_random_engine randomEngine;
randomEngine.seed(std::chrono::steady_clock::now().time_since_epoch().count());
std::uniform_real_distribution<float> rangeX(minx, maxx);
std::uniform_real_distribution<float> rangeY(miny, maxy);
cairo_set_source_rgba(cr, 0, 0, 0, 1);
for (int i = 0; i < n * 2; i += 4)
{
float x1 = rangeX(randomEngine);
float y1 = rangeY(randomEngine);
float x2 = rangeX(randomEngine);
float y2 = rangeY(randomEngine);
cairo_move_to(cr, x1, y1);
cairo_line_to(cr, x2, y2);
}
cairo_stroke(cr);
//restore previous brush
cairo_restore(cr);
}
void display(void)
{
static int fps = 0;
static int frame = 0;
static long long startTime = chrono::system_clock::now().time_since_epoch().count();
static long long lastTime = 2;
long long now = chrono::system_clock::now().time_since_epoch().count();
++frame;
//update per second
if (now - lastTime > 10000000)
{
lastTime = now;
fps = frame;
frame = 0;
cout << fps << endl;
}
drawShape();
opengl_draw(win_width, win_height, surf_data);
glutSwapBuffers();
}
cairo_t*
create_cairo_context(int width,
int height,
int channels,
cairo_surface_t** surf,
unsigned char** buffer)
{
cairo_t* cr;
// create cairo-surface/context to act as OpenGL-texture source
*buffer = (unsigned char*)calloc(channels * width * height, sizeof(unsigned char));
if (!*buffer)
{
printf("create_cairo_context() - Couldn't allocate buffer\n");
return NULL;
}
*surf = cairo_image_surface_create_for_data(*buffer,
CAIRO_FORMAT_ARGB32,
width,
height,
channels * width);
if (cairo_surface_status(*surf) != CAIRO_STATUS_SUCCESS)
{
free(*buffer);
printf("create_cairo_context() - Couldn't create surface\n");
return NULL;
}
cr = cairo_create(*surf);
if (cairo_status(cr) != CAIRO_STATUS_SUCCESS)
{
free(*buffer);
printf("create_cairo_context() - Couldn't create context\n");
return NULL;
}
return cr;
}
void cleanup(void)
{
opengl_cleanup();
free(surf_data);
cairo_destroy(cr);
exit(0);
}
void keyboard(unsigned char key, int x, int y)
{
switch (key)
{
//27 is ESC key
case 27:
case 'q':
cleanup();
break;
case 'd':
cairo_surface_write_to_png(surf, "frame.png");
break;
case '+':
if (line_width < 10)
line_width += 1;
break;
case '-':
if (line_width > 1)
line_width -= 1;
break;
}
}
void idle(void)
{
glutPostRedisplay();
}
int main(int argc, char ** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA);
glutInitWindowSize(win_width, win_height);
if (glutCreateWindow("Opengl Test") == 0)
exit(-2);
// create cairo-surface/context to act as OpenGL-texture source
cr = create_cairo_context(win_width, win_height, 4, &surf, &surf_data);
// setup "GL-context"
opengl_init();
glutDisplayFunc(display);
glutKeyboardFunc(keyboard);
glutIdleFunc(idle);
opengl_resize(win_width, win_height);
glutMainLoop();
return 0;
}
and here is the html and js i use
index.html
<!DOCTYPE html>
<html>
<head>
<title></title>
<script type="text/javascript" src="main.js"></script>
<style type="text/css">
html, body {
margin: 0px;
}
canvas {
display: block;
}
</style>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
main.js
window.onload = function() {
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"),
width = canvas.width = window.innerWidth,
height = canvas.height = window.innerHeight;
render();
function render() {
context.clearRect(0, 0, width, height);
for(var i = 0; i < 100; i += 1){
context.beginPath();
context.moveTo(Math.random() * width, Math.random() * height);
context.lineTo(Math.random() * width, Math.random() * height);
context.stroke();
}
requestAnimationFrame(render);
}
};
Your bottleneck is actually not OpenGL but Cairo. You're using Cairo with its standard software rasterizer backend; so the CPU is doing all the heavy lifting and OpenGL is just used as a glorified surface blitter. Admittedly the method for loading the finished image into OpenGL is not optimal (glTexSubImage2D should be used instead of glTexImage2D), but this is hardly your bottleneck there.
So what should you do: Ideally you'd be using a OpenGL accelerated backend for Cairo as described in http://cairographics.org/OpenGL/
Another option is ditching Cairo and use a vector rendering library directly targeted at OpenGL; I'm thinking of NanoVG here (I have no affiliations to this project). The main advantage of NanoVG is, that its whole internal architecture has been designed with OpenGL as backend in mind.
If you want to profile the influence on the improperly chosen method for texture upload here's a fixed variant of the code (remove opengl_cleanup, it does nothing good for this very example and also get rid of opengl_resize it's very bad practice to do projection setup in the resize handler).
void opengl_draw(int width, int height, void const * surf_data)
{
static GLuint texture_id = 0;
static int tex_width = 0;
static int tex_height = 0;
if (!surf_data)
{
printf("draw_func() - No valid pointer to surface-data passed\n");
return;
}
if( !texture_id ) {
glGenTextures(1, &texture_id);
}
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id);
if( width != tex_width || height != tex_height ) {
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB,
0,
GL_RGBA,
tex_width = width,
tex_height = height,
0,
GL_BGRA,
GL_UNSIGNED_BYTE,
surf_data);
} else {
glTexSubImage2D(GL_TEXTURE_RECTANGLE_ARB,
0, 0, 0,
tex_width, tex_height,
GL_BGRA,
GL_UNSIGNED_BYTE,
surf_data);
}
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
glOrtho(0.0f, 1.0f, 0.0f, 1.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glColor3f(0.25f, 0.5f, 1.0f);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(0.0f, 0.0f);
glTexCoord2f((GLfloat)width, 0.0f);
glVertex2f(1.0f, 0.0f);
glTexCoord2f((GLfloat)width, (GLfloat)height);
glVertex2f(1.0f, 1.0f);
glTexCoord2f(0.0f, (GLfloat)height);
glVertex2f(0.0f, 1.0f);
glEnd();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
}
just draw line every frame, I tried write javascript on html5. The same function just draws a line, and it runs much faster.
And what do you think does this tell you? HTML Canvas may me implemented in any way that satisfies the specification. The Browser may use Cairo, its own rendering engine, may or may not use the GPU. This is not a useful comparison, because you don't know what you're actually comparing there.
It is very painful to compile cairo with OpenGL backend on Windows. But here's how I did it. Works on cygwin and msys2 with x86_64-w64-mingw32 toolchain.
Cygwin provides a precompiled cairo package for Win64. It doesn't support OpenGL. However if you install that package it will automatically pull in all the dependencies of cairo, including pixman. Also install the GLEW package (OpenGL Extension Wrangler).
Download the cairo source code, and extract it to your workspace.
Open the configure file. Search for -lGL and replace all instances with -lglew32 -lopengl32. This will make cairo link to the Windows OpenGL library instead of the Linux one. The library name here is case-sensitive, and must match the opengl32.dll in C:\Windows\system32. If not then the build system will complain about not able to find opengl32.dll.
Open src/cairo-gl.h. Under the line
#if CAIRO_HAS_GL_SURFACE || CAIRO_HAS_GLESV2_SURFACE
Add
#include <GL/glew.h>
Under the line
#if CAIRO_HAS_WGL_FUNCTIONS
Add
#include <GL/wglew.h>
This file must be included before windows.h, otherwise GLEW will complain that gl.h is alreaded included.
There is a bug in cairo wgl device creation process. Basically cairo will create an invisible window, and attach the GL context you provide to that window. However if your context does not have the same pixel format as the dummy window, then the binding will fail. Here's my quick fix. Open src/cairo-wgl-context.c, find static cairo_status_t _wgl_dummy_ctx (cairo_wgl_context_t *ctx).
Before the line
wglMakeCurrent(ctx->dummy_dc, ctx->rc);
Add
ctx->rc = wglCreateContextAttribsARB(ctx->dummy_dc,ctx->rc,NULL);
Now execute
./configure --host=x86_64-w64-mingw32 --enable-gl --enable-wgl
make
If configure complains it cannot find certain programs, first make sure the binutils for Win64 toolchain is installed. Then use ln to make a hard link to the program with x86_64-w64-mingw32 prefix.
The make process will inevitably fail in the test folder, because it attempts to compile tests that require the Linux GL library. Ignore the failure, and go to /src/.libs. You should see libcairo-2.dll and other files needed for linking.
The most common issue is that only libcairo.a is produced, but no libcairo-2.dll. Make sure you modified the configure file exactly as I said.
Copy the libcairo-2.dll to wherever you want. If your program links dynamically to cairo, it should now work fine. However if want to link statically, there is another complication. Some parts of cairo use Windows critical section to implement mutex. The critical sections are initialized during DllMain. However if you link statically then you must manually initialize them. Just call the void _cairo_mutex_initialize (void) function within cairo-mutex.c.
This is the setting I have to work with (I cannot change any of these values)
#include <stdlib.h>
#include <GL/glut.h>
const GLdouble FRUSTDIM = 100.0f;
void reshape(int w, int h) // Resize the GL Window. w=width, h=height
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-FRUSTDIM, FRUSTDIM, -FRUSTDIM, FRUSTDIM, 320., 640.);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
I want to build a wall, but something is wrong and I dont quite understand. If I'm not mistaken, the current space is (-100 - 100)x(-100 - 100)x(320 - 640) and the camera is currently at 0,0,320
I want to make a room, but I can't even set up a wall :(....
I tried using QUADS and QUAD_STRIP, but it still wont show up when I run it D:
My code:
void display(void)
{
glBegin(GL_QUADS);
glColor3f(1,1,1);
glVertex3f(50,50,420);
glVertex3f(50,-50,420);
glVertex3f(-50,-50,420);
glVertex3f(-50,50,420);
glEnd();
glutSwapBuffers();
glFlush();
}
I just need to draw a wall to get myself going. If there is any code u think is required to solve my problem, comment and I will edit my question. (FYI other codes are working fine because the skeleton was give to me to start myself going).
It needs to include this:
glClear(GL_DEPTH_BUFFER_BIT|GL_COLOR_BUFFER_BIT); // clearing window
glDisable(GL_LIGHTING);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
to make the window clear for you to see the object. Additionally:
glTranslatef(-100,-100,-630.0);
glBegin(GL_QUADS);
/* Back Wall */
glColor3f(1.0f, 0.0f, 0.0f);
glNormal3f(0,0,1);
glVertex3f(0,0,0);
glVertex3f(200,0,0);
glVertex3f(200,200,0);
glVertex3f(0,200,0);
glEnd();
this would make the wall (pretty much the same, but just added translate before building it).
When i try to run this program it starts, shows the title bar but there is nothing - it only docks for the bottom task line and when i click it - nothing shows. There is no window with the program. Any idea ? Here's the code that uses OpenGL stuff :
#include <GL/glut.h> // Header File For The GLUT Library
#include <GL/gl.h> // Header File For The OpenGL32 Library
#include <GL/glu.h> // Header File For The GLu32 Library
void SetupRC(void);
void RenderScene(void);
void ChangeSize(GLsizei w, GLsizei h);
// Called to draw scene
void RenderScene(void)
{
// Clear the window with current clearing color
glClearColor (0.f,0.f,0.f,0.f);
glClear(GL_COLOR_BUFFER_BIT);
// set the color to these values
// R G B
glColor3f(1.0f, 1.0f, 1.0f);
// Draw a filled rectangle with current color
glRectf(-25.0f, 25.0f, 25.0f, -25.0f);
// Flush drawing commands
glFlush();
glutSwapBuffers();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH); // color, buffer
glutCreateWindow("Simple");
glutInitWindowPosition(400,200);
glutInitWindowSize(640,468);
glutDisplayFunc(RenderScene);
glutReshapeFunc(ChangeSize);
SetupRC();
glutMainLoop();
return 0;
}
// Setup the rendering state
void SetupRC(void)
{
glClearColor(0.0f, 1.0f, 1.0f, 0.0f);
}
// Handling window resizing
void ChangeSize(GLsizei w, GLsizei h)
{
GLfloat aspectRatio;
// Prevent divide by zero
if (h == 0)
h = 1;
// Set Viewport to window dimensions
glViewport(0, 0, w, h);
// Reset coordinate system
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// Establish clipping volume (left, right, bottom, top, near, far)
aspectRatio = (GLfloat) w / (GLfloat) h;
if (w <= h) {
glOrtho(-100.0, 100.0, -100 / aspectRatio, 100.0 / aspectRatio, 1.0, -1.0);
}
else
glOrtho(-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 1.0, -1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
Run glxinfo from the terminal and see if any issues show up.
You might have to install mesa-utils to use glxinfo
sudo apt-get install mesa-utils
From what you've shared, it seems like a graphics card issue. You might be able to get better help over at the Ubuntu Forums. Good luck.
Just change single buffering with double buffering and it will work (instead of GLUT_SINGLE put GLUT_DOUBLE).
Hey all, I'm very new to OpenGL (just started seriously programming with it today) and I'm trying to use it to give my SDL games a 3D boost. I've setup a small test program below:
#include <SDL/SDL.h>
#include <gl/gl.h>
int main(int argc, char *argv[])
{
SDL_Event event;
float theta = 0.0f;
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(800, 600, 32, SDL_OPENGL | SDL_HWSURFACE | SDL_RESIZABLE | SDL_FULLSCREEN);
glViewport(0, 0, 800, 600);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glMatrixMode(GL_MODELVIEW);
int done;
for(done = 0; !done;)
{
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 255, 0, 0));
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0.0f,0.0f,0.0f);
glRotatef(theta, 0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES);
glColor3f(0.83f, 0.83f, 0.0f);
glVertex2f(0.0f, 1.0f);
glColor3f(0.83f, 0.83f, 0.0f);
glVertex2f(0.87f, -0.5f);
glColor3f(0.83f, 0.83f, 0.0f);
glVertex2f(-0.87f, -0.5f);
glEnd();
theta += 10.0f;
SDL_Flip(screen);
SDL_GL_SwapBuffers();
SDL_PollEvent(&event);
if(event.key.keysym.sym == SDLK_ESCAPE)
done = 1;
}
}
My problem is that the red background I'm trying to rendered is never rendered, only the OpenGL Triangle is rendered.
Thanks in advance to anyone who can help me. It's much appreciated.
There's one simple rule about OpenGL: It doesn't play well with others. What happens in your case is, that the double buffer swap (initiated by SDL_GL_SwapBuffers) will in some way replace everything in the window, not being rendered by OpenGL.
Just draw everything using OpenGL.
You fill the back buffer on one line with SDL_FillRect then you clear it on the next with glClear. Have you tried swapping the order of the operations?
Not that I disagree with the accepted answer; in general trying to mix software rendering methods with OpenGL is a recipe for confusion at best, but you might get lucky in this case.
As for rending textured quads, you should be able to work it out from NeHe lesson 6. People complain about NeHe but it's a reasonable guide for getting started. Just don't use it as an example of good coding or of efficient modern OpenGL usage. Start here and move to more complex stuff later.
If you're using C++, SFML library might be a better option (it has C bindings though, but haven't tried those). It plays nicely with OpenGL and has functions to cooperatively work alongside GL. As far as I understood it, SFML functions themselves use GL to render. Although, I do suggest that you do rendering only with GL calls as noted above.
your SDL_FillRect isn't show as red, because you call glClear with GL_COLOR_BUFFER_BIT set afterwards