Reputation: 45
I am following a tutorial on youtube but the problem is the maker of the tutorial is using SDL 1.2 and Im using 2.00 so i had to change the code up a little bit following the SDL migration guide. However my code isn't doing what its supposed to which is to draw a triangle. Simple as that. All I get is a white screen.
======main============
#include "stdafx.h"
void init()
{
glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 2.0, -5.0);
glVertex3f(-2.0, -2.0, -5.0);
glVertex3f(2.0, -2.0, -5.0);
glEnd();
}
int main(int argc, char* argv[]){
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window * window;
window = SDL_CreateWindow("OpenGLTest", 300, 300, 640, 480, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
init();
SDL_Event * mainEvent;
mainEvent = new SDL_Event;
while (true){
SDL_PollEvent(mainEvent);
display();
SDL_GL_SwapWindow(window);
if (mainEvent->type == SDL_QUIT){
break;
}
}
return 0;
}
=====stdafx=========
#pragma once
#include <iostream>
#include <SDL.h>
#include <Windows.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#define PI 3.14159265
using namespace std;
Any ideas what I'm doing wrong?
Upvotes: 2
Views: 957
Reputation: 52165
SDL_WINDOW_OPENGL
alone is necessary but not sufficient.
Create a GL context with SDL_GL_CreateContext()
and make it current via SDL_GL_MakeCurrent()
:
#include <GL/glew.h>
#include <SDL/SDL.h>
int main( int argc, char *argv[] )
{
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window * window = SDL_CreateWindow
(
"OpenGLTest",
300, 300,
640, 480,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
);
SDL_GLContext context = SDL_GL_CreateContext( window );
SDL_GL_MakeCurrent( window, context );
glewInit();
bool running = true;
while( running )
{
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if ( ev.type == SDL_QUIT)
running = false;
}
glClearColor(0.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45, 640.0 / 480.0, 1.0, 500.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_TRIANGLES);
glVertex3f(0.0, 2.0, -5.0);
glVertex3f(-2.0, -2.0, -5.0);
glVertex3f(2.0, -2.0, -5.0);
glEnd();
SDL_GL_SwapWindow(window);
}
SDL_GL_DeleteContext( context );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
Upvotes: 3