Archived

This topic is now archived and is closed to further replies.

exa_einstein

OpenGL multi window with opengl

Recommended Posts

I have created two windows, but my (simple... i hope) problem is to draw into them. Do we have some function to tell OpenGL to render INTO a particular window? (switch between more wnds) possible send HWND or HDC? how? please help, i have no idea...

Share this post


Link to post
Share on other sites
As far as I know OpenGL will always render into the window which is in the same thread. Don''t know of any parameters to pass... Never tried what happens if you have two windows in the same thread, but you could just create two threads, give each it''s own window and gl-commands?

Share this post


Link to post
Share on other sites
no, I think that OpenGL library (=dll) is loaded only once for INSTANCE, threads are only using imported funcs.
idea (maybe helpful) what exactly does wglMakeCurrent() and wglCreateContext() and that third wgl_something_()? I never thought about them.

Share this post


Link to post
Share on other sites
quote:
from MSDN The wglMakeCurrent function makes a specified OpenGL rendering context the calling thread''s current rendering context. All subsequent OpenGL calls made by the thread are drawn on the device identified by hdc. You can also use wglMakeCurrent to change the calling thread''s current rendering context so it''s no longer current.

So, if you have two threads, each OpenGL call inside the thread will be made to the opengl rendering context of that thread. The other stuff is for creating a rendering context and binding it to a window.

Share this post


Link to post
Share on other sites
This should help, it renders 4 opengl screens each in an edit control in a dialogbox. Plus this is in Win32 API
What you can basically do with this is create windows within your window, get their DCs and Wiggles then render.

Main Source File

// windowopengl.cpp : Defines the entry point for the application.

//


#include "stdafx.h"
#include "resource.h"
#include <gl\glut.h>
#include <gl\gl.h>
#include <gl\glaux.h>

#define MAX_LOADSTRING 100

// Global Variables:

HINSTANCE hInst; // current instance

TCHAR szTitle[MAX_LOADSTRING]; // The title bar text

TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text

int TheEnd; // Flag used by thread to end

int xrot; // rotation


// Foward declarations of functions included in this code module:

ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK OpenGL(HWND, UINT, WPARAM, LPARAM);
DWORD WINAPI InitGL(HWND);
int MySetPixelFormat(HDC hdc);
void RenderScene(int);

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.

MSG msg;
HACCEL hAccelTable;

// Initialize global strings

LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_WINDOWOPENGL, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);

// Perform application initialization:

if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_WINDOWOPENGL);

// Main message loop:

while (GetMessage(&msg, NULL, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}

return msg.wParam;
}



//

// FUNCTION: MyRegisterClass()

//

// PURPOSE: Registers the window class.

//

// COMMENTS:

//

// This function and its usage is only necessary if you want this code

// to be compatible with Win32 systems prior to the 'RegisterClassEx'

// function that was added to Windows 95. It is important to call this function

// so that the application will get 'well formed' small icons associated

// with it.

//

ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_WINDOWOPENGL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_WINDOWOPENGL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//

// FUNCTION: InitInstance(HANDLE, int)

//

// PURPOSE: Saves instance handle and creates main window

//

// COMMENTS:

//

// In this function, we save the instance handle in a global variable and

// create and display the main program window.

//

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable


hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//

// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)

//

// PURPOSE: Processes messages for the main window.

//

// WM_COMMAND - process the application menu

// WM_PAINT - Paint the main window

// WM_DESTROY - post a quit message and return

//

//

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:

switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case IDM_OPENGL:
DialogBox(hInst, (LPCTSTR)IDD_OPENGL, hWnd, (DLGPROC)OpenGL);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...

RECT rt;
GetClientRect(hWnd, &rt);
DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.

LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

// Mesage handler for OpenGL box.

LRESULT CALLBACK OpenGL(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
static HANDLE hOpenGL;
static DWORD lpThreadId;

switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK)
{
TheEnd=1;
Sleep(500);
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
if (LOWORD(wParam) == IDRENDER)
{

EnableWindow(GetDlgItem(hDlg,IDRENDER),FALSE);
hOpenGL=CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)InitGL,hDlg,0,&lpThreadId);

}
break;
}
return FALSE;
}


// Here, we do the regular initialization of OpenGL

DWORD WINAPI InitGL(HWND hDlg)
{
HDC hDC1,hDC2,hDC3,hDC4;
HWND hEdit1,hEdit2,hEdit3,hEdit4;
HGLRC hglrc1,hglrc2,hglrc3,hglrc4;

// First, let's get the handle to all the window we created in the Dialog

// with the resource editor

hEdit1=GetDlgItem(hDlg,IDC_EDIT1);
hEdit2=GetDlgItem(hDlg,IDC_EDIT2);
hEdit3=GetDlgItem(hDlg,IDC_EDIT3);
hEdit4=GetDlgItem(hDlg,IDC_EDIT4);

// For every window, we get the HDC

hDC1=GetDC(hEdit1);
hDC2=GetDC(hEdit2);
hDC3=GetDC(hEdit3);
hDC4=GetDC(hEdit4);

// OpenGL Initialization

glShadeModel(GL_SMOOTH); // Enable Smooth Shading

glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background

glClearDepth(1.0f); // Depth Buffer Setup

glEnable(GL_DEPTH_TEST); // Enables Depth Testing

glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations


MySetPixelFormat(hDC1);
MySetPixelFormat(hDC2);
MySetPixelFormat(hDC3);
MySetPixelFormat(hDC4);

hglrc1 = wglCreateContext(hDC1);
hglrc2 = wglCreateContext(hDC2);
hglrc3 = wglCreateContext(hDC3);
hglrc4 = wglCreateContext(hDC4);

//render here

while(TheEnd !=1 )
{

// Select first Window as the rendering context and draw to it

wglMakeCurrent(hDC1, hglrc1);
RenderScene(1);
SwapBuffers(hDC1);

// Select second Window as the rendering context and draw to it

wglMakeCurrent(hDC2, hglrc2);
RenderScene(2);
SwapBuffers(hDC2);

// Select third Window as the rendering context and draw to it

wglMakeCurrent(hDC3, hglrc3);
RenderScene(2);
SwapBuffers(hDC3);

// Select fourth Window as the rendering context and draw to it

wglMakeCurrent(hDC4, hglrc4);
RenderScene(1);
SwapBuffers(hDC4);

// Throttle the loop. If we don't put a sleep here, the thread will use 100% cpu

Sleep(1);
}

wglMakeCurrent(NULL, NULL);

// Cleanup

ReleaseDC (hDlg, hDC1) ;
ReleaseDC (hDlg, hDC2) ;
ReleaseDC (hDlg, hDC3) ;
ReleaseDC (hDlg, hDC4) ;

wglDeleteContext(hglrc1);
wglDeleteContext(hglrc2);
wglDeleteContext(hglrc3);
wglDeleteContext(hglrc4);

return TRUE;
}

// Set up the pixel format

int MySetPixelFormat(HDC hdc)
{


PIXELFORMATDESCRIPTOR pfd = {
sizeof(PIXELFORMATDESCRIPTOR), // size of this pfd

1, // version number

PFD_DRAW_TO_WINDOW | // support window

PFD_SUPPORT_OPENGL | // support OpenGL

PFD_DOUBLEBUFFER, // double buffered

PFD_TYPE_RGBA, // RGBA type

24, // 24-bit color depth

0, 0, 0, 0, 0, 0, // color bits ignored

0, // no alpha buffer

0, // shift bit ignored

0, // no accumulation buffer

0, 0, 0, 0, // accum bits ignored

32, // 32-bit z-buffer

0, // no stencil buffer

0, // no auxiliary buffer

PFD_MAIN_PLANE, // main layer

0, // reserved

0, 0, 0 // layer masks ignored

};

int iPixelFormat;

// get the device context's best, available pixel format match

if((iPixelFormat = ChoosePixelFormat(hdc, &pfd)) == 0)
{
MessageBox(NULL, "ChoosePixelFormat Failed", NULL, MB_OK);
return 0;
}

// make that match the device context's current pixel format

if(SetPixelFormat(hdc, iPixelFormat, &pfd) == FALSE)
{
MessageBox(NULL, "SetPixelFormat Failed", NULL, MB_OK);
return 0;
}

return 1;
}


// This is where we render, in this case, we will draw a simple 3D-Triangle

void RenderScene(int axis)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

if(axis==1) glRotatef(xrot,1.0f,0.0f,0.0f);
if(axis==2) glRotatef(xrot,0.0f,1.0f,0.0f);

glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f(0, 0.8f, 0); //top

glColor3f(0, 1, 0);
glVertex3f(-0.5, -0.5, 0.5); //left

glColor3f(0, 0,1);
glVertex3f(0.5, -0.5, 0.5); //right



glColor3f(1.0f,0.0f,0.0f); // Red

glVertex3f( 0.0f, 0.8f, 0.0f); // Top Of Triangle (Right)

glColor3f(0.0f,0.0f,1.0f); // Blue

glVertex3f( .5f,-.5f, .5f); // Left Of Triangle (Right)

glColor3f(0.0f,0.5f,0.0f); // Green

glVertex3f( 0.5f,-0.5f, -0.5f); // Right Of Triangle (Right)


glColor3f(1.0f,0.0f,0.0f); // Red

glVertex3f( 0.0f, 0.8f, 0.0f); // Top Of Triangle (Back)

glColor3f(0.0f,1.0f,0.0f); // Green

glVertex3f( 0.5f,-0.5f, -0.5f); // Left Of Triangle (Back)

glColor3f(0.0f,0.0f,1.0f); // Blue

glVertex3f(-0.5f,-0.5f, -0.5f); // Right Of Triangle (Back)


glColor3f(1.0f,0.0f,0.0f); // Red

glVertex3f( 0.0f, 0.8f, 0.0f); // Top Of Triangle (Left)

glColor3f(0.0f,0.0f,1.0f); // Blue

glVertex3f(-0.5f,-0.5f,-0.5f); // Left Of Triangle (Left)

glColor3f(0.0f,1.0f,0.0f); // Green

glVertex3f(-0.5f,-0.5f, 0.5f); // Right Of Triangle (Left)


glEnd();

xrot+=1.5f;

}


Resource ID file

//{{NO_DEPENDENCIES}}

// Microsoft Developer Studio generated include file.

// Used by windowopengl.rc

//

#define IDC_MYICON 2
#define IDD_WINDOWOPENGL_DIALOG 102
#define IDD_ABOUTBOX 103
#define IDS_APP_TITLE 103
#define IDM_ABOUT 104
#define IDM_EXIT 105
#define IDS_HELLO 106
#define IDI_WINDOWOPENGL 107
#define IDI_SMALL 108
#define IDC_WINDOWOPENGL 109
#define IDR_MAINFRAME 128
#define IDD_OPENGL 129
#define IDC_EDIT1 1000
#define IDRENDER 1001
#define IDC_EDIT2 1002
#define IDC_EDIT3 1003
#define IDC_EDIT4 1004
#define IDM_OPENGL 32771
#define IDC_STATIC -1

// Next default values for new objects

//

#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 130
#define _APS_NEXT_COMMAND_VALUE 32772
#define _APS_NEXT_CONTROL_VALUE 1002
#define _APS_NEXT_SYMED_VALUE 110
#endif
#endif


Resource File

//Microsoft Developer Studio generated resource script.

//

#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////

//

// Generated from the TEXTINCLUDE 2 resource.

//

#define APSTUDIO_HIDDEN_SYMBOLS
#include "windows.h"
#undef APSTUDIO_HIDDEN_SYMBOLS
#include "resource.h"

/////////////////////////////////////////////////////////////////////////////

#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////

// English (U.S.) resources


#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
#ifdef _WIN32
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)
#endif //_WIN32


/////////////////////////////////////////////////////////////////////////////

//

// Icon

//


// Icon with lowest ID value placed first to ensure application icon

// remains consistent on all systems.

IDI_WINDOWOPENGL ICON DISCARDABLE "windowopengl.ICO"
IDI_SMALL ICON DISCARDABLE "SMALL.ICO"

/////////////////////////////////////////////////////////////////////////////

//

// Menu

//


IDC_WINDOWOPENGL MENU DISCARDABLE
BEGIN
POPUP "&File"
BEGIN
MENUITEM "E&xit", IDM_EXIT
END
MENUITEM "&OpenGL", IDM_OPENGL
POPUP "&Help"
BEGIN
MENUITEM "&About ...", IDM_ABOUT
END
END


/////////////////////////////////////////////////////////////////////////////

//

// Accelerator

//


IDC_WINDOWOPENGL ACCELERATORS MOVEABLE PURE
BEGIN
"?", IDM_ABOUT, ASCII, ALT
"/", IDM_ABOUT, ASCII, ALT
END


/////////////////////////////////////////////////////////////////////////////

//

// Dialog

//


IDD_ABOUTBOX DIALOG DISCARDABLE 22, 17, 230, 75
STYLE DS_MODALFRAME | WS_CAPTION | WS_SYSMENU
CAPTION "About"
FONT 8, "System"
BEGIN
ICON IDI_WINDOWOPENGL,IDC_MYICON,14,9,16,16
LTEXT "windowopengl Version 1.0",IDC_STATIC,49,10,119,8,
SS_NOPREFIX
LTEXT "Copyright (C) 2001",IDC_STATIC,49,20,119,8
DEFPUSHBUTTON "OK",IDOK,195,6,30,11,WS_GROUP
END

IDD_OPENGL DIALOG DISCARDABLE 0, 0, 304, 170
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Window OpenGL example by Chong Hin Ooi, chongooi@mediaone.net"
FONT 8, "MS Sans Serif"
BEGIN
DEFPUSHBUTTON "Render",IDRENDER,190,149,50,14
PUSHBUTTON "OK",IDOK,247,149,50,14
EDITTEXT IDC_EDIT1,7,7,138,58,ES_AUTOHSCROLL
EDITTEXT IDC_EDIT2,159,7,138,58,ES_AUTOHSCROLL
EDITTEXT IDC_EDIT3,7,78,138,58,ES_AUTOHSCROLL
EDITTEXT IDC_EDIT4,159,78,138,58,ES_AUTOHSCROLL
END


#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////

//

// TEXTINCLUDE

//


2 TEXTINCLUDE DISCARDABLE
BEGIN
"#define APSTUDIO_HIDDEN_SYMBOLS\r\n"
"#include ""windows.h""\r\n"
"#undef APSTUDIO_HIDDEN_SYMBOLS\r\n"
"#include ""resource.h""\r\n"
"\0"
END

3 TEXTINCLUDE DISCARDABLE
BEGIN
"\r\n"
"\0"
END

1 TEXTINCLUDE DISCARDABLE
BEGIN
"resource.h\0"
END

#endif // APSTUDIO_INVOKED



/////////////////////////////////////////////////////////////////////////////

//

// DESIGNINFO

//


#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO DISCARDABLE
BEGIN
IDD_OPENGL, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 297
TOPMARGIN, 7
BOTTOMMARGIN, 163
END
END
#endif // APSTUDIO_INVOKED



/////////////////////////////////////////////////////////////////////////////

//

// String Table

//


STRINGTABLE DISCARDABLE
BEGIN
IDS_APP_TITLE "windowopengl"
IDS_HELLO "Window OpenGL example by Chong Hin Ooi"
IDC_WINDOWOPENGL "WINDOWOPENGL"
END

#endif // English (U.S.) resources

/////////////////////////////////////////////////////////////////////////////




#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////

//

// Generated from the TEXTINCLUDE 3 resource.

//



/////////////////////////////////////////////////////////////////////////////

#endif // not APSTUDIO_INVOKED




Hope that helps.
[edit] Misspelled a tag.

[edited by - Xiachunyi on August 11, 2003 11:59:02 AM]

Share this post


Link to post
Share on other sites
the above two are right. my last renderer used 9 windows (VCL)


OpenGL renders in the selected context in the current thread.

If you have two windows and only need to rendere in one of them at a time, create a context for both and then use wglmakecurrent to select which you wish to render in.

if you want to have both windows being rendered into at the same time, you can either swap the current context between the two (a bit dodgey, but hey, it works) or you can do it the better way and create a two threads. In each thread you have a seperate opengl thread that renders to one of the windows.

Either way, under win32 OpenGL always renders into the THREADS current device context.

I have no idea about macos or os2 or solaris or linux or dos though. I never played with opengl that much under them.

Share this post


Link to post
Share on other sites

  • Announcements

  • Forum Statistics

    • Total Topics
      628294
    • Total Posts
      2981882
  • Similar Content

    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
    • By Abecederia
      So I've recently started learning some GLSL and now I'm toying with a POM shader. I'm trying to optimize it and notice that it starts having issues at high texture sizes, especially with self-shadowing.
      Now I know POM is expensive either way, but would pulling the heightmap out of the normalmap alpha channel and in it's own 8bit texture make doing all those dozens of texture fetches more cheap? Or is everything in the cache aligned to 32bit anyway? I haven't implemented texture compression yet, I think that would help? But regardless, should there be a performance boost from decoupling the heightmap? I could also keep it in a lower resolution than the normalmap if that would improve performance.
      Any help is much appreciated, please keep in mind I'm somewhat of a newbie. Thanks!
    • By test opty
      Hi,
      I'm trying to learn OpenGL through a website and have proceeded until this page of it. The output is a simple triangle. The problem is the complexity.
      I have read that page several times and tried to analyse the code but I haven't understood the code properly and completely yet. This is the code:
       
      #include <glad/glad.h> #include <GLFW/glfw3.h> #include <C:\Users\Abbasi\Desktop\std_lib_facilities_4.h> using namespace std; //****************************************************************************** void framebuffer_size_callback(GLFWwindow* window, int width, int height); void processInput(GLFWwindow *window); // settings const unsigned int SCR_WIDTH = 800; const unsigned int SCR_HEIGHT = 600; const char *vertexShaderSource = "#version 330 core\n" "layout (location = 0) in vec3 aPos;\n" "void main()\n" "{\n" " gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n" "}\0"; const char *fragmentShaderSource = "#version 330 core\n" "out vec4 FragColor;\n" "void main()\n" "{\n" " FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n" "}\n\0"; //******************************* int main() { // glfw: initialize and configure // ------------------------------ glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); // glfw window creation GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "My First Triangle", nullptr, nullptr); if (window == nullptr) { cout << "Failed to create GLFW window" << endl; glfwTerminate(); return -1; } glfwMakeContextCurrent(window); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); // glad: load all OpenGL function pointers if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cout << "Failed to initialize GLAD" << endl; return -1; } // build and compile our shader program // vertex shader int vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, nullptr); glCompileShader(vertexShader); // check for shader compile errors int success; char infoLog[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(vertexShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << endl; } // fragment shader int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, nullptr); glCompileShader(fragmentShader); // check for shader compile errors glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if (!success) { glGetShaderInfoLog(fragmentShader, 512, nullptr, infoLog); cout << "ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n" << infoLog << endl; } // link shaders int shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); // check for linking errors glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if (!success) { glGetProgramInfoLog(shaderProgram, 512, nullptr, infoLog); cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << endl; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); // set up vertex data (and buffer(s)) and configure vertex attributes float vertices[] = { -0.5f, -0.5f, 0.0f, // left 0.5f, -0.5f, 0.0f, // right 0.0f, 0.5f, 0.0f // top }; unsigned int VBO, VAO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); // bind the Vertex Array Object first, then bind and set vertex buffer(s), //and then configure vertex attributes(s). glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); // note that this is allowed, the call to glVertexAttribPointer registered VBO // as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind glBindBuffer(GL_ARRAY_BUFFER, 0); // You can unbind the VAO afterwards so other VAO calls won't accidentally // modify this VAO, but this rarely happens. Modifying other // VAOs requires a call to glBindVertexArray anyways so we generally don't unbind // VAOs (nor VBOs) when it's not directly necessary. glBindVertexArray(0); // uncomment this call to draw in wireframe polygons. //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // render loop while (!glfwWindowShouldClose(window)) { // input // ----- processInput(window); // render // ------ glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // draw our first triangle glUseProgram(shaderProgram); glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to // bind it every time, but we'll do so to keep things a bit more organized glDrawArrays(GL_TRIANGLES, 0, 3); // glBindVertexArray(0); // no need to unbind it every time // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) glfwSwapBuffers(window); glfwPollEvents(); } // optional: de-allocate all resources once they've outlived their purpose: glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); // glfw: terminate, clearing all previously allocated GLFW resources. glfwTerminate(); return 0; } //************************************************** // process all input: query GLFW whether relevant keys are pressed/released // this frame and react accordingly void processInput(GLFWwindow *window) { if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) glfwSetWindowShouldClose(window, true); } //******************************************************************** // glfw: whenever the window size changed (by OS or user resize) this callback function executes void framebuffer_size_callback(GLFWwindow* window, int width, int height) { // make sure the viewport matches the new window dimensions; note that width and // height will be significantly larger than specified on retina displays. glViewport(0, 0, width, height); } As you see, about 200 lines of complicated code only for a simple triangle. 
      I don't know what parts are necessary for that output. And also, what the correct order of instructions for such an output or programs is, generally. That start point is too complex for a beginner of OpenGL like me and I don't know how to make the issue solved. What are your ideas please? What is the way to figure both the code and the whole program out correctly please?
      I wish I'd read a reference that would teach me OpenGL through a step-by-step method. 
  • Popular Now