Converting OpenCV CV-to-CV2-Code - c++

Im trying to convert an OpenCV-codesample, which i found on the internet, from the old IplImage-Format to the currently used Mat-Format, but im inexperienced regarding the correct use of pointers/classes and hope someone can help me convert the few codelines. The code mainly initializes a webcam, grabs the frames und displays them via OpenGL.
The mentioned difficult code:
CvCapture *cvCapture = 0;
cvCapture = cvCreateCameraCapture(0);
IplImage* newImage = cvQueryFrame( cvCapture );
cvReleaseCapture( &cvCapture );
I tried:
cv::VideoCapture *cvCapture = 0;
cvCapture.open(0);
cvCapture.read(cv:Mat& newImage);
cvCapture.release;
Original Code:
#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <gl/glew.h>
#include <gl/glut.h>
#include <opencv/highgui.h>
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// GLUT callbacks and functions
void initGlut(int argc, char **argv);
void displayFunc(void);
void idleFunc(void);
void reshapeFunc(int width, int height);
void mouseFunc(int button, int state, int x, int y);
void mouseMotionFunc(int x, int y);
void keyboardFunc(unsigned char key, int x, int y);
void specialFunc(int key, int x, int y);
//-----------------------------------------------------------------------------
// other [OpenGL] functions
void countFrames(void);
void renderBitmapString(float x, float y, float z, void *font, char *string);
//-----------------------------------------------------------------------------
bool bFullsreen = false;
int nWindowID;
//-----------------------------------------------------------------------------
// parameters for the framecounter
char pixelstring[30];
int cframe = 0;
int time = 0;
int timebase = 0;
//-----------------------------------------------------------------------------
// OpenCV variables
CvCapture *cvCapture = 0;
GLuint cameraImageTextureID;
//-----------------------------------------------------------------------------
bool bInit = false;
//-----------------------------------------------------------------------------
void displayFunc(void) {
if(!bInit) {
// initialize 1st camera on the bus
cvCapture = cvCreateCameraCapture(0);
// initialze OpenGL texture
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &cameraImageTextureID);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, cameraImageTextureID);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
bInit = true;
}
IplImage* newImage = cvQueryFrame( cvCapture );
if( (newImage->width > 0) && (newImage->height > 0)) {
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)newImage->width,0.0,(GLdouble)newImage->height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, cameraImageTextureID);
if(newImage->nChannels == 3)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, newImage->width, newImage->height, 0,
GL_BGR, GL_UNSIGNED_BYTE, newImage->imageData);
else if(newImage->nChannels == 4)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, newImage->width, newImage->height,
0, GL_BGRA, GL_UNSIGNED_BYTE, newImage->imageData);
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex2i(0,0);
glTexCoord2i(newImage->width,0);
glVertex2i(newImage->width,0);
glTexCoord2i(newImage->width,newImage->height);
glVertex2i(newImage->width,newImage->height);
glTexCoord2i(0,newImage->height);
glVertex2i(0,newImage->height);
glEnd();
}
glDisable(GL_TEXTURE_RECTANGLE_ARB);
countFrames();
glutSwapBuffers();
}
//-----------------------------------------------------------------------------
void initGlut(int argc, char **argv) {
// GLUT Window Initialization:
glutInit (&argc, argv);
glutInitWindowSize (640, 480);
glutInitWindowPosition(300, 100);
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
nWindowID = glutCreateWindow ("simpleGLUT - CvCamera");
// Register callbacks:
glutDisplayFunc (displayFunc);
glutReshapeFunc (reshapeFunc);
glutKeyboardFunc (keyboardFunc);
glutSpecialFunc (specialFunc);
glutMouseFunc (mouseFunc);
glutMotionFunc (mouseMotionFunc);
glutIdleFunc (idleFunc);
}
//-----------------------------------------------------------------------------
void idleFunc(void) {
glutPostRedisplay();
}
//-----------------------------------------------------------------------------
void reshapeFunc(int width, int height) {
glViewport(0, 0, width, height);
}
//-----------------------------------------------------------------------------
// mouse callback
void mouseFunc(int button, int state, int x, int y) {
}
//-----------------------------------------------------------------------------
void mouseMotionFunc(int x, int y) {
}
//-----------------------------------------------------------------------------
void keyboardFunc(unsigned char key, int x, int y) {
switch(key) {
// -----------------------------------------
#ifdef WIN32
// exit on escape
case '\033':
if(bInit) {
glDeleteTextures(1, &cameraImageTextureID);
cvReleaseCapture( &cvCapture );
}
exit(0);
break;
#endif
// -----------------------------------------
// switch to fullscreen
case 'f':
bFullsreen = !bFullsreen;
if(bFullsreen)
glutFullScreen();
else {
glutSetWindow(nWindowID);
glutPositionWindow(100, 100);
glutReshapeWindow(640, 480);
}
break;
// -----------------------------------------
}
}
//-----------------------------------------------------------------------------
void specialFunc(int key, int x, int y) {
//printf("key pressed: %d\n", key);
}
//-----------------------------------------------------------------------------
void countFrames(void) {
time=glutGet(GLUT_ELAPSED_TIME);
cframe++;
if (time - timebase > 50) {
sprintf(pixelstring, "fps: %4.2f", cframe*1000.0/(time-timebase));
timebase = time;
cframe = 0;
// Draw status text and uni-logo:
}
glDisable(GL_LIGHTING);
glColor4f(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 200, 0, 200);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// render the string
renderBitmapString(5,5,0.0,GLUT_BITMAP_HELVETICA_10,pixelstring);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
//-----------------------------------------------------------------------------
void renderBitmapString(float x, float y, float z, void *font, char *string) {
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
//-----------------------------------------------------------------------------
void main(int argc, char **argv) {
initGlut(argc, argv);
glutMainLoop();
}

Solved my own problem. Also had to change old code like: newImage->width and newImage->nChannels. Dont know how stable the code is, but maybe someone can use it:
#ifdef WIN32
#include <windows.h>
#endif
#include <stdio.h>
#include <gl/glew.h>
#include <gl/glut.h>
#include <opencv/highgui.h>
#include <opencv2\highgui.hpp>
#include <opencv2\core.hpp>
//-----------------------------------------------------------------------------
// GLUT callbacks and functions
void initGlut(int argc, char **argv);
void displayFunc(void);
void idleFunc(void);
void reshapeFunc(int width, int height);
void mouseFunc(int button, int state, int x, int y);
void mouseMotionFunc(int x, int y);
void keyboardFunc(unsigned char key, int x, int y);
void specialFunc(int key, int x, int y);
//-----------------------------------------------------------------------------
// other [OpenGL] functions
void countFrames(void);
void renderBitmapString(float x, float y, float z, void *font, char *string);
//-----------------------------------------------------------------------------
bool bFullsreen = false;
int nWindowID;
//-----------------------------------------------------------------------------
// parameters for the framecounter
char pixelstring[30];
int cframe = 0;
int time = 0;
int timebase = 0;
//-----------------------------------------------------------------------------
// OpenCV variables
cv::VideoCapture cap(0);
GLuint cameraImageTextureID;
//-----------------------------------------------------------------------------
bool bInit = false;
//-----------------------------------------------------------------------------
void displayFunc(void) {
if(!bInit) {
// initialize 1st camera on the bus
// initialze OpenGL texture
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glGenTextures(1, &cameraImageTextureID);
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, cameraImageTextureID);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_RECTANGLE_ARB, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
bInit = true;
}
cv::Mat newImage;
cap.read(newImage);
int rows = newImage.rows;
int cols = newImage.cols;
cv::Size s = newImage.size();
rows = s.height;
cols = s.width;
if( (s.width > 0) && (s.height > 0)) {
// clear the buffers
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_RECTANGLE_ARB);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,(GLdouble)s.width,0.0,(GLdouble)s.height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBindTexture(GL_TEXTURE_RECTANGLE_ARB, cameraImageTextureID);
if(newImage.channels() == 3)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGB, s.width, s.height, 0, GL_BGR,
GL_UNSIGNED_BYTE, newImage.data);
else if(newImage.channels() == 4)
glTexImage2D(GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, s.width, s.height, 0, GL_BGRA,
GL_UNSIGNED_BYTE, newImage.data);
glBegin(GL_QUADS);
glTexCoord2i(0,0);
glVertex2i(0,0);
glTexCoord2i(s.width,0);
glVertex2i(s.width,0);
glTexCoord2i(s.width,s.height);
glVertex2i(s.width,s.height);
glTexCoord2i(0,s.height);
glVertex2i(0,s.height);
glEnd();
}
glDisable(GL_TEXTURE_RECTANGLE_ARB);
countFrames();
glutSwapBuffers();
}
void idleFunc(void) {
glutPostRedisplay();
}
void reshapeFunc(int width, int height) {
glViewport(0, 0, width, height);
}
void mouseFunc(int button, int state, int x, int y) {
}
void mouseMotionFunc(int x, int y) {
}
void keyboardFunc(unsigned char key, int x, int y) {
switch(key) {
// exit on escape
case '\033':
if(bInit) {
glDeleteTextures(1, &cameraImageTextureID);
cap.release();
}
exit(0);
break;
// switch to fullscreen
case 'f':
bFullsreen = !bFullsreen;
if(bFullsreen)
glutFullScreen();
else {
glutSetWindow(nWindowID);
glutPositionWindow(100, 100);
glutReshapeWindow(640, 480);
}
break;
}
}
void specialFunc(int key, int x, int y) {
//printf("key pressed: %d\n", key);
}
void countFrames(void) {
time=glutGet(GLUT_ELAPSED_TIME);
cframe++;
if (time - timebase > 50) {
sprintf(pixelstring, "fps: %4.2f", cframe*1000.0/(time-timebase));
timebase = time;
cframe = 0;
// Draw status text and uni-logo:
}
glDisable(GL_LIGHTING);
glColor4f(1.0,1.0,1.0,1.0);
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluOrtho2D(0, 200, 0, 200);
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glLoadIdentity();
// render the string
renderBitmapString(5,5,0.0,GLUT_BITMAP_HELVETICA_10,pixelstring);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
}
void renderBitmapString(float x, float y, float z, void *font, char *string) {
char *c;
glRasterPos3f(x, y,z);
for (c=string; *c != '\0'; c++) {
glutBitmapCharacter(font, *c);
}
}
void main(int argc, char **argv) {
glutInit (&argc, argv);
glutInitWindowSize (640, 480);
glutInitWindowPosition(300, 100);
glutInitDisplayMode ( GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
nWindowID = glutCreateWindow ("simpleGLUT - CvCamera");
// Register callbacks:
glutDisplayFunc (displayFunc);
glutReshapeFunc (reshapeFunc);
glutKeyboardFunc (keyboardFunc);
glutSpecialFunc (specialFunc);
glutMouseFunc (mouseFunc);
glutMotionFunc (mouseMotionFunc);
glutIdleFunc (idleFunc);
glutMainLoop();
}

Related

I am trying to use structure in c++ but its not working

in this method
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
that uses the structure i made
struct GLintPoint {
GLint x,y;
};
it says that there is an error and that "no operator "[]" matches these operands, operator types are GLintPoint[int]" where i try to assign a value from points to point. Well i did use the right brackets and it is an int in there so what is the problem? FYI this code is to draw the Sierpinski gasket with the user making the initial 3 points by clicking the screen with the mouse. Just in case you would like to see it here is the whole program.
#include <windows.h>
#include <gl/Gl.h>
#include "glut.h"
#include <iostream>
using namespace std;
const int screenWidth = 640;
const int screenHeight = 480;
struct GLintPoint {
GLint x,y;
};
void display (void){
glClear(GL_COLOR_BUFFER_BIT);
//glColor3f(1.0,1.0,1.0);
glFlush();
}
void drawDot( GLint x, GLint y)
{
glBegin( GL_POINTS );
glVertex2i( x, y );
glEnd();
}
void Sierpinski(GLintPoint points) {
glClear(GL_COLOR_BUFFER_BIT);
GLintPoint T[3] = {{10,10},{600,10},{300,600}};
int index=rand()%3;
GLintPoint point=points[index];
drawDot(point.x, point.y);
for (int i = 0;i<55000;i++) {
index=rand()%3;
point.x=(point.x+points[index].x)/2;
point.y=(point.y+points[index].y)/2;
drawDot(point.x,point.y);
}
glFlush();
}
void myMouse(int button, int state, int x, int y){
static GLintPoint corner[2];
static int numCorners = 0;
if(state == GLUT_DOWN){
if(button == GLUT_LEFT_BUTTON){
corner[numCorners].x = x;
corner[numCorners].y = screenHeight - y;
if(++numCorners ==2 ){
glRecti(corner[0].x, corner[0].y, corner[1].x, corner[1].y);
numCorners = 0;
glFlush();
}
}
else if(button == GLUT_RIGHT_BUTTON){
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
}
}
void myInit() {
glClearColor(1.0,1.0,1.0,0.0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.0f,0.0f,0.0f);
glPointSize(2.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, (GLdouble)screenWidth, 0.0, (GLdouble)screenHeight);
}
void main (int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(screenWidth, screenHeight);
glutInitWindowPosition(100,150);
glutCreateWindow("mouse dots");
glutDisplayFunc(display);
glutPostRedisplay();
glutMouseFunc(myMouse);
myInit();
glutMainLoop();
}
I'm inferring from the name of the function argument, points, that you intend the function to accept an array of points. But it's actually just written to take one.
So when you write points[index] the compiler is looking for operator[] in your GLintPoint struct (and there isn't one).
If you change the function prototype to take an array of GLintPoints I think you'll have better luck.

openGL: Why isn't text appearing on screen?

I'm moving my OpenGL project over from GLUT to GLFW. Currently I am trying to get text to appear on screen using the library FreeType. I am following the tutorials from their site, but I am stuck. I believe I have it all written out, but for some reason it isn't working. What am I missing?
Here is my code:
#include <iostream>
#include <stdlib.h>
// Include GLEW
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include "ft2build.h"
#include FT_FREETYPE_H
FT_Library library;
FT_Face face;
static void error_callback(int error, const char* description)
{
fputs(description, stderr);
}
//should only be used for key press or key release
static void key_press(GLFWwindow* window, int key, int scancode, int action, int mods)
{
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GL_TRUE);
}
void render_text(const std::string &str, FT_Face face, float x, float y, float sx, float sy) {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
const FT_GlyphSlot glyph = face->glyph;
for (auto c : str) {
if (FT_Load_Char(face, c, FT_LOAD_RENDER) != 0)
continue;
glTexImage2D(GL_TEXTURE_2D, 0, GL_R8,
glyph->bitmap.width, glyph->bitmap.rows,
0, GL_RED, GL_UNSIGNED_BYTE, glyph->bitmap.buffer);
const float vx = x + glyph->bitmap_left * sx;
const float vy = y + glyph->bitmap_top * sy;
const float w = glyph->bitmap.width * sx;
const float h = glyph->bitmap.rows * sy;
struct {
float x, y, s, t;
} data[6] = {
{ vx, vy, 0, 0 },
{ vx, vy - h, 0, 1 },
{ vx + w, vy, 1, 0 },
{ vx + w, vy, 1, 0 },
{ vx, vy - h, 0, 1 },
{ vx + w, vy - h, 1, 1 }
};
glBufferData(GL_ARRAY_BUFFER, 24 * sizeof(float), data, GL_DYNAMIC_DRAW);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glDrawArrays(GL_TRIANGLES, 0, 6);
x += (glyph->advance.x >> 6) * sx;
y += (glyph->advance.y >> 6) * sy;
}
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
}
void Resize(GLFWwindow* window)
{
int width, height;
glfwGetFramebufferSize(window, &width, &height);
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-5.f, 5.f, -5.f, 5.f, 5.f, -5.f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void Setup()
{
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST); // Depth Testing
glDepthFunc(GL_LEQUAL);
glDisable(GL_CULL_FACE);
glCullFace(GL_BACK);
//used for font
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void Display(GLFWwindow* window)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
render_text("TEST", face, 0, 0, 100, 100);
glfwSwapBuffers(window);
glfwPollEvents();
}
void Update(GLFWwindow* window)
{
Display(window);
}
int main(void)
{
GLFWwindow* window;
glfwSetErrorCallback(error_callback);
if (!glfwInit())
exit(EXIT_FAILURE);
window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
if (!window)
{
glfwTerminate();
exit(EXIT_FAILURE);
}
glfwMakeContextCurrent(window);
glfwSwapInterval(1);
glewExperimental = true;
if (glewInit() != GLEW_OK)
{
//Problem: glewInit failed, something is seriously wrong.
std::cout << "glewInit failed, aborting." << std::endl;
exit(EXIT_FAILURE);
}
FT_Error error = FT_Init_FreeType(&library);
if (error) {
fprintf(stderr, "Could not init freetype library\n");
return 1;
}
error = FT_New_Face(library, "Stoke-Regular.ttf", 0, &face);
if (error == FT_Err_Unknown_File_Format)
{
std::cout << "font is unsupported" << std::endl;
return 1;
}
else if (error)
{
std::cout << "font could not be read or opened" << std::endl;
return 1;
}
error = FT_Set_Pixel_Sizes(face, 0, 48);
if (error)
{
std::cout << "Problem adjusting pixel size" << std::endl;
return 1;
}
//keyboard controls
glfwSetKeyCallback(window, key_press);
Resize(window);
Setup();
while (!glfwWindowShouldClose(window))
{
Update(window);
}
glfwDestroyWindow(window);
glfwTerminate();
exit(EXIT_SUCCESS);
}

SDL window not respondig

My SDL window crashes when clicking or minimizes it, the background is white and the OpenGL does not update the screen more. Follows the code:
#include <stdio.h>
#include <SDL.h>
#include <SDL_opengl.h>
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const char * SCREEN_NAME = "Tetris SDL";
SDL_Window *window;
SDL_GLContext glcontext;
int init();
void main_loop();
void quit();
void events();
void update();
void draw();
int main(int argc, char* args[])
{
if(!init())
quit();
main_loop();
quit();
}
int init()
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
return 0;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 0);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
window = SDL_CreateWindow(SCREEN_NAME, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
if(!window)
return 0;
glcontext = SDL_GL_CreateContext(window);
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
return 1;
}
void main_loop()
{
while(true)
{
events();
update();
draw();
//only for test
SDL_Delay(16);
}
}
void events(){}
void update(){}
void draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.0, 0.0, 0.0);
glBegin(GL_TRIANGLES);
glColor3f(1,0,0); glVertex2f(0.0f,0.5f);
glColor3f(0,1,0); glVertex2f(-0.5f, -0.5f);
glColor3f(0,0,1); glVertex2f(0.5f, -0.5f);
glEnd();
SDL_GL_SwapWindow(window);
}
void quit()
{
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
SDL_Quit();
}
I'm using SDL 2.0 and GNU GCC compiler and Windows XP. Does anyone know what could be the problem?
You don't handle any events while in the loop.
If you don't do that then the event of minimizing will not be handled (usually means the window buffer is freed to be use by someone else).

opengl not drawing anything

Just trying to draw a point using glut and glew for opengl version 4.3
my code
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
is not rendering anything, can someone tell me what did i miss? here is full code:
#include <iostream>
using namespace std;
#include "vgl.h"
#include "LoadShaders.h"
enum VAO_IDs { Triangles, NumVAOS };
#define WIDTH 1024
#define HEIGHT 768
#define REFRESH_DELAY 10 //ms
//general
long frameCount = 0;
// mouse controls
int mouse_old_x, mouse_old_y;
int mouse_buttons = 0;
float rotate_x = 0.0, rotate_y = 0.0;
float translate_z = -3.0;
/////////////////////////////////////////////////////////////////////
//! Prototypes
/////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int x, int y);
void mouse(int button, int state, int x, int y);
void motion(int x, int y);
void timerEvent(int value)
{
glutPostRedisplay();
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount++);
}
void init (void)
{
// default initialization
glClearColor(0.0, 0.0, 0.0, 1.0);
glDisable(GL_DEPTH_TEST);
// viewport
glViewport(0, 0, WIDTH, HEIGHT);
// projection
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (GLfloat)WIDTH / (GLfloat)HEIGHT, 1, 10000.0);
}
void Renderer(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POINTS);
glPointSize(100);
glColor3f(255, 0, 0);
glVertex3d(10, 10, 0);
glEnd();
glFlush();
glutSwapBuffers();
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA);
glutInitWindowSize(WIDTH, HEIGHT);
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow("The Abyss");
glutKeyboardFunc(keyboard);
glutMotionFunc(motion);
glutMouseFunc(mouse);
glutTimerFunc(REFRESH_DELAY, timerEvent, frameCount);
if (glewInit()) //i guess this is true on failure
{
cerr << "Error initializing glew, Program aborted." << endl;
exit(EXIT_FAILURE);
}
//Init First
init();
//Init callback for Rendering
glutDisplayFunc(Renderer);
//Main Loop
glutMainLoop();
exit(EXIT_SUCCESS);
}
////////////////////////////////////////////////////////////////////////
//!Mouse and keyboard functionality
////////////////////////////////////////////////////////////////////////
void keyboard(unsigned char key, int /*x*/, int /*y*/)
{
switch (key)
{
case (27) :
exit(EXIT_SUCCESS);
break;
}
}
void mouse(int button, int state, int x, int y)
{
if (state == GLUT_DOWN)
{
mouse_buttons |= 1<<button;
}
else if (state == GLUT_UP)
{
mouse_buttons = 0;
}
mouse_old_x = x;
mouse_old_y = y;
}
void motion(int x, int y)
{
float dx, dy;
dx = (float)(x - mouse_old_x);
dy = (float)(y - mouse_old_y);
if (mouse_buttons & 1)
{
rotate_x += dy * 0.2f;
rotate_y += dx * 0.2f;
}
else if (mouse_buttons & 4)
{
translate_z += dy * 0.01f;
}
mouse_old_x = x;
mouse_old_y = y;
}
glutInitContextVersion(4, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
You have requested a Core context. You need to:
Specify a vertex and fragment shader. There are no freebies in Core.
Stop using deprecated functionality like glBegin() and glMatrixMode().
Start using VBOs to submit your geometry.
Start using glDrawArrays() and friends to draw your geometry.

how to do zoom in my code (mandelbrot)

i have the following code and i wanted to know how may i insert zoom into my code.(i read some similar subjects but i can't figure).
GLsizei width = 600;
GLsizei height = 600;
int max = 500;
double xpos=0,ypos=0;
double xmax = 2.0;
double xmin = -2.0;
double ymax = 2.0;
double ymin = -2.0;
using namespace std;
void display()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(-2, width, -2, height);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT );
mandelbrot();
glutSwapBuffers();
}
void reshape(GLsizei w, GLsizei h) {
width=w; height=h;
glViewport(0,0,width,height);
glutPostRedisplay();
}
void setXYpos(int px, int py)
{
xpos=xmin+(xmax-xmin)*px/width;
ypos=ymax-(ymax-ymin)*py/height;
}
void mandelbrot()
{
...}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitWindowSize(600, 600);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutCreateWindow("Mandelbrot");
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
return 0;
}
It's not enough to zoom in this way; you have to map the corners of the selected region to points in the complex plane and re-generate the Mandelbrot mapping for the new coordinates.