Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - pythoneer

Pages: [1]
1
Graphics / pbo pixel drawing
« on: October 29, 2010, 12:04:45 am »
hi im new to sfml (and opengl) and have a little question about sfml and ogl pbo. i hope i'm not on the wrong forum.

im trying to convert my actual raytracer from java to c/c++ to make it interactive/realtime.

in my case (and rt in general) i need a "fast" way to draw pixels. i tried several methods and hear about ogl's pbo. i wanna give it a try but i'm to **** to get it to work.

i tried several ways and end up in abnormal programm termination. i'm using win7 and mingw and i'm pretty sure that sfml (tutorial works) , opengl (tutorial works) and hopefully glew[for pbo stuff] (an example with glTexImage2D works .. hopefully it needs glew) works and is linked and running.

but i cant get this pbo stuff to work.

is it possible to provide me an really simple excample or give me some hints on my actual code?

what im trying to do is; set pixels in a buffer (pbo) and map it to a quad.

i hope someone can give me a hint ... or a better excample
Code: [Select]


#include <GL/glew.h>

#include <SFML/Window.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/Graphics.hpp>
#include <string>
#include <sstream>
#include <stdio.h>


#define WIDTH 512
#define HEIGHT 512

int main(int argc, char** argv/*, sf::RenderWindow *App*/)
{
    sf::RenderWindow App(sf::VideoMode(512, 512, 32), "SFML OpenGL");

//    glClearColor(0.0, 0.0, 0.0, 0.0);
//    glShadeModel(GL_FLAT);
//    glEnable(GL_TEXTURE_2D);
//    glClearDepth(1.f);
// what to use ?    
    glClearColor(0.5f, 0.3f, 0.2f, 1.f);
    glEnable (GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glLoadIdentity();

   
   
    GLuint buf_size = WIDTH * HEIGHT * 3;
    GLuint id;

    glGenBuffers(1, &id);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, WIDTH, HEIGHT, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, id);
    glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, buf_size, 0, GL_STREAM_DRAW);

    int sweep = 0;
    GLubyte *mem = 0;
    //bool done = false;

    //while (!done) {
    while (App.IsOpened())
    {

        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();

            // Escape key : exit
            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();

            // Resize event : adjust viewport
            if (Event.Type == sf::Event::Resized)
                glViewport(0, 0, Event.Size.Width, Event.Size.Height);
        }


        ++sweep;
        glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, id);
        mem = (GLubyte *) glMapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY);
        for (unsigned int y = 0; y < HEIGHT; ++y)
            for (unsigned int x = 0; x < WIDTH; ++x) {
                mem[y * WIDTH * 3 + x * 3] = y % 255;
                mem[y * WIDTH * 3 + x * 3 + 1] = sweep % 255;
                mem[y * WIDTH * 3 + x * 3 + 2] = x % 255;
            }
        glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER_ARB);
        glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, WIDTH, HEIGHT, GL_RGB, GL_UNSIGNED_BYTE, 0);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glBegin(GL_QUADS);
            glTexCoord2f(0.0, 0.0);
            glVertex3f(-1.0, -1.0, 0.0);
            glTexCoord2f(0.0, 1.0);
            glVertex3f(-1.0, 1.0, 0.0);
            glTexCoord2f(1.0, 1.0);
            glVertex3f(1.0, 1.0, 0.0);
            glTexCoord2f(1.0, 0.0);
            glVertex3f(1.0, -1.0, 0.0);
        glEnd();
        //glFlush();  // needed?

        App.Display();
    }
}

Pages: [1]
anything